Armin Rigo avatar Armin Rigo committed cf812c6

Partly untested: support for callbacks with different calling
convensions on Windows.

Comments (0)

Files changed (4)

c/_cffi_backend.c

     return NULL;
 }
 
+static PyObject *b_get_function_type_args(PyObject *self, PyObject *arg)
+{
+    CTypeDescrObject *fct = (CTypeDescrObject *)arg;
+    PyObject *x, *args, *res, *ellipsis, *conv;
+    Py_ssize_t end;
+
+    if (!CTypeDescr_Check(arg) || !(fct->ct_flags & CT_FUNCTIONPTR)) {
+        PyErr_SetString(PyExc_TypeError, "expected a 'ctype' funcptr object");
+        return NULL;
+    }
+
+    args = NULL;
+    ellipsis = NULL;
+    conv = NULL;
+    x = NULL;
+
+    end = PyTuple_GET_SIZE(fct->ct_stuff);
+    args = PyTuple_GetSlice(fct->ct_stuff, 2, end);
+    if (args == NULL)
+        goto error;
+    res = PyTuple_GET_ITEM(fct->ct_stuff, 1);
+    ellipsis = PyInt_FromLong(fct->ct_extra == NULL);
+    if (ellipsis == NULL)
+        goto error;
+    conv = PyTuple_GET_ITEM(fct->ct_stuff, 0);
+    x = PyTuple_Pack(4, args, res, ellipsis, conv);
+    /* fall-through */
+ error:
+    Py_XDECREF(args);
+    Py_XDECREF(ellipsis);
+    Py_XDECREF(conv);
+    return x;
+}
+
 static void invoke_callback(ffi_cif *cif, void *result, void **args,
                             void *userdata)
 {
     cif_description_t *cif_descr;
     ffi_closure *closure;
     Py_ssize_t size;
-
-    if (!PyArg_ParseTuple(args, "O!O|O:callback", &CTypeDescr_Type, &ct, &ob,
-                          &error_ob))
+    long fabi = FFI_DEFAULT_ABI;
+
+    if (!PyArg_ParseTuple(args, "O!O|Ol:callback", &CTypeDescr_Type, &ct, &ob,
+                          &error_ob, &fabi))
         return NULL;
 
     if (!(ct->ct_flags & CT_FUNCTIONPTR)) {
     {"new_union_type", b_new_union_type, METH_VARARGS},
     {"complete_struct_or_union", b_complete_struct_or_union, METH_VARARGS},
     {"new_function_type", b_new_function_type, METH_VARARGS},
+    {"get_function_type_args", b_get_function_type_args, METH_O},
     {"new_enum_type", b_new_enum_type, METH_VARARGS},
     {"_getfields", b__getfields, METH_O},
     {"newp", b_newp, METH_VARARGS},
     v = PyInt_FromLong(FFI_DEFAULT_ABI);
     if (v == NULL || PyModule_AddObject(m, "FFI_DEFAULT_ABI", v) < 0)
         return;
+    Py_INCREF(v);
+    if (PyModule_AddObject(m, "FFI_CDECL", v) < 0)  /*win32 name*/
+        return;
 
     init_errno();
 }
     BFunc = new_function_type((BStruct,), BShort, False)
     assert repr(BFunc) == "<ctype 'short(*)(struct foo)'>"
 
+def test_get_function_type_args():
+    BChar = new_primitive_type("char")
+    BShort = new_primitive_type("short")
+    BStruct = new_struct_type("foo")
+    complete_struct_or_union(BStruct, [('a1', BChar, -1),
+                                       ('a2', BShort, -1)])
+    BFunc = new_function_type((BStruct,), BShort, False)
+    a, b, c, d = get_function_type_args(BFunc)
+    assert a == (BStruct,)
+    assert b == BShort
+    assert c == False
+    assert d == FFI_DEFAULT_ABI
+
 def test_function_void_result():
     BVoid = new_void_type()
     BInt = new_primitive_type("int")
         """
         return self._backend.buffer(cdata, size)
 
-    def callback(self, cdecl, python_callable, error=None):
+    def callback(self, cdecl, python_callable, error=None, conv=None):
         """Return a callback object.  'cdecl' must name a C function pointer
         type.  The callback invokes the specified 'python_callable'.
         Important: the callback object must be manually kept alive for as
         if not callable(python_callable):
             raise TypeError("the 'python_callable' argument is not callable")
         BFunc = self.typeof(cdecl, consider_function_as_funcptr=True)
+        if conv is not None:
+            BFunc = self._functype_with_conv(BFunc, conv)
         return self._backend.callback(BFunc, python_callable, error)
 
+    def _functype_with_conv(self, BFunc, conv):
+        abiname = '%s' % (conv.upper(),)
+        try:
+            abi = getattr(self._backend, 'FFI_' + abiname)
+        except AttributeError:
+            raise ValueError("the calling convention %r is unknown to "
+                             "the backend" % (abiname,))
+        if abi == self._backend.FFI_DEFAULT_ABI:
+            return BFunc
+        # xxx only for _cffi_backend.c so far
+        try:
+            bfunc_abi_cache = self._bfunc_abi_cache
+            return bfunc_abi_cache[BFunc, abi]
+        except AttributeError:
+            bfunc_abi_cache = self._bfunc_abi_cache = {}
+        except KeyError:
+            pass
+        args, res, ellipsis, _ = self._backend.get_function_type_args(BFunc)
+        result = self._backend.new_function_type(args, res, ellipsis, abi)
+        bfunc_abi_cache[BFunc, abi] = result
+        return result
+
     def getctype(self, cdecl, replace_with=''):
         """Return a string giving the C type 'cdecl', which may be itself
         a string or a <ctype> object.  If 'replace_with' is given, it gives

doc/source/index.rst

 
 Note that callbacks of a variadic function type are not supported.
 
-Windows: you can't yet specify the calling convention of callbacks.
-(For regular calls, the correct calling convention should be
-automatically inferred by the C backend.)
+Windows: for regular calls, the correct calling convention should be
+automatically inferred by the C backend, but that doesn't work for
+callbacks.  The default calling convention is "cdecl", like in C;
+if needed, you must force the calling convention with the keyword
+argument ``conv``::
+
+    ffi.callback("int(*)(int, int)", myfunc, conv="stdcall")
+    ffi.callback("int(*)(int, int)", myfunc, conv="cdecl")  # default
 
 Be careful when writing the Python callback function: if it returns an
 object of the wrong type, or more generally raises an exception, then
 
 ``ffi.errno``: the value of ``errno`` received from the most recent C call
 in this thread, and passed to the following C call, is available via
-reads and writes of the property ``ffi.errno``.
+reads and writes of the property ``ffi.errno``.  On Windows we also save
+and restore the ``GetLastError()`` value, but to access it you need to
+declare and call the ``GetLastError()`` function as usual.
 
 ``ffi.buffer(pointer, [size])``: return a read-write buffer object that
 references the raw C data pointed to by the given 'cdata', of 'size'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.