Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r385:e1813a23b9c1
Date: 2012-06-16 10:25 +0200
http://bitbucket.org/cffi/cffi/changeset/e1813a23b9c1/

Log:    Compiles.

diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -4,6 +4,7 @@
 
 #ifdef MS_WIN32
 #include <windows.h>
+#include "misc_win32.h"
 #else
 #include <stddef.h>
 #include <stdint.h>
@@ -126,17 +127,11 @@
 
 /* whenever running Python code, the errno is saved in this thread-local
    variable */
-#if defined(__GNUC__) && !defined(MS_WIN32)
-#  define CFFI_USE_THREAD_LOCALS
-#endif
-
-#ifdef CFFI_USE_THREAD_LOCALS
+#ifndef MS_WIN32
 static __thread int cffi_saved_errno = 0;
 static void save_errno(void) { cffi_saved_errno = errno; }
 static void *restore_errno(void) { errno = cffi_saved_errno; return NULL; }
 static void init_errno(void) { }
-#else
-#include "non_gcc_errno.h"
 #endif
 
 /************************************************************/
@@ -1844,27 +1839,16 @@
 
 /************************************************************/
 
-#ifdef MS_WIN32
-typedef HMODULE dl_handle_t;
-#else
-typedef void *dl_handle_t;
-#endif
-
 typedef struct {
     PyObject_HEAD
-    dl_handle_t dl_handle;
+    void *dl_handle;
     char *dl_name;
 } DynLibObject;
 
 static void dl_dealloc(DynLibObject *dlobj)
 {
-#ifdef MS_WIN32
-    if (dlobj->dl_handle)
-        FreeLibrary(dlobj->dl_handle);
-#else
     if (dlobj->dl_handle != RTLD_DEFAULT)
         dlclose(dlobj->dl_handle);
-#endif
     free(dlobj->dl_name);
     PyObject_Del(dlobj);
 }
@@ -2526,7 +2510,7 @@
     char *bufferp;
     ffi_type **atypes;
     ffi_type *rtype;
-    unsigned int nargs;
+    Py_ssize_t nargs;
     CTypeDescrObject *fct;
 };
 
@@ -2870,7 +2854,7 @@
                             void *userdata)
 {
     save_errno();
-
+    {
     PyObject *cb_args = (PyObject *)userdata;
     CTypeDescrObject *ct = (CTypeDescrObject *)PyTuple_GET_ITEM(cb_args, 0);
     PyObject *signature = ct->ct_stuff;
@@ -2912,6 +2896,7 @@
     PyErr_WriteUnraisable(py_ob);
     memset(result, 0, SIGNATURE(0)->ct_size);
     goto done;
+    }
 
 #undef SIGNATURE
 }
@@ -3224,7 +3209,7 @@
 }
 static float _testfunc4(float a, double b)
 {
-    return a + b;
+    return (float)(a + b);
 }
 static void _testfunc5(void)
 {
diff --git a/c/misc_win32.h b/c/misc_win32.h
new file mode 100644
--- /dev/null
+++ b/c/misc_win32.h
@@ -0,0 +1,104 @@
+
+/************************************************************/
+/* errno and GetLastError support */
+
+struct cffi_errno_s {
+    int saved_errno;
+    int saved_lasterror;
+};
+
+static DWORD cffi_tls_index;
+
+static void init_errno(void)
+{
+    cffi_tls_index = TlsAlloc();
+    if (cffi_tls_index == TLS_OUT_OF_INDEXES)
+        PyErr_SetString(PyExc_WindowsError, "TlsAlloc() failed");
+}
+
+static struct cffi_errno_s *_geterrno_object(void)
+{
+    LPVOID p = TlsGetValue(cffi_tls_index);
+
+    if (p == NULL) {
+        p = PyMem_Malloc(sizeof(struct cffi_errno_s));
+        if (p == NULL)
+            return NULL;
+        memset(p, 0, sizeof(struct cffi_errno_s));
+        TlsSetValue(cffi_tls_index, p);
+    }
+    return (struct cffi_errno_s *)p;
+}
+
+static void save_errno(void)
+{
+    int current_err = errno;
+    int current_lasterr = GetLastError();
+    struct cffi_errno_s *p;
+
+    p = _geterrno_object();
+    if (p != NULL) {
+        p->saved_errno = current_err;
+        p->saved_lasterror = current_lasterr;
+    }
+    /* else: cannot report the error */
+}
+
+static void restore_errno(void)
+{
+    struct cffi_errno_s *p;
+
+    p = _geterrno_object();
+    if (p != NULL) {
+        SetLastError(p->saved_lasterror);
+        errno = p->saved_errno;
+    }
+    /* else: cannot report the error */
+}
+
+
+/************************************************************/
+/* Emulate dlopen()&co. from the Windows API */
+
+#define RTLD_DEFAULT   NULL
+#define RTLD_LAZY      0
+
+static void *dlopen(const char *filename, int flag)
+{
+    return (void *)LoadLibrary(filename);
+}
+
+static void *dlsym(void *handle, const char *symbol)
+{
+    if (handle == RTLD_DEFAULT) {
+        static const char *standard_dlls[] = {
+            "kernel32.dll",
+            "user32.dll",
+            "gdi32.dll",
+            NULL
+        };
+        const char **p;
+        void *result;
+
+        for (p = standard_dlls; *p != NULL; p++) {
+            result = GetProcAddress(GetModuleHandle(*p), symbol);
+            if (result)
+                return result;
+        }
+        return NULL;
+    }
+    else {
+        return GetProcAddress((HMODULE)handle, symbol);
+    }
+}
+
+static void dlclose(void *handle)
+{
+    FreeLibrary((HMODULE)handle);
+}
+
+
+/************************************************************/
+/* obscure */
+
+#define ffi_prep_closure(a,b,c,d)  ffi_prep_closure_loc(a,b,c,d,a)
diff --git a/c/non_gcc_errno.h b/c/non_gcc_errno.h
deleted file mode 100644
--- a/c/non_gcc_errno.h
+++ /dev/null
@@ -1,58 +0,0 @@
-
-#ifndef MS_WIN32
-#  error "only GCC or Win32 are supported so far"
-#endif
-
-struct cffi_errno_s {
-    int saved_errno;
-    int saved_lasterror;
-};
-
-static DWORD cffi_tls_index;
-
-static void init_errno(void)
-{
-    cffi_tls_index = TlsAlloc();
-    if (cffi_tls_index == TLS_OUT_OF_INDEXES)
-        PyErr_SetString(PyExc_WindowsError, "TlsAlloc() failed");
-}
-
-static struct cffi_errno_s *_geterrno_object(void)
-{
-    LPVOID p = TlsGetValue(cffi_tls_index);
-
-    if (p == NULL) {
-        p = PyMem_Malloc(sizeof(struct cffi_errno_s));
-        if (p == NULL)
-            return NULL;
-        memset(p, 0, sizeof(struct cffi_errno_s));
-        TlsSetValue(cffi_tls_index, p);
-    }
-    return (struct cffi_errno_s *)p;
-}
-
-static void save_errno(void)
-{
-    int current_err = errno;
-    int current_lasterr = GetLastError();
-    struct cffi_errno_s *p;
-
-    p = _geterrno_object();
-    if (p != NULL) {
-        p->saved_errno = current_err;
-        p->saved_lasterror = current_lasterr;
-    }
-    /* else: cannot report the error */
-}
-
-static void restore_errno(void)
-{
-    struct cffi_errno_s *p;
-
-    p = _geterrno_object();
-    if (p != NULL) {
-        SetLastError(p->saved_lasterror);
-        errno = p->saved_errno;
-    }
-    /* else: cannot report the error */
-}
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to