Commits

tlynn committed f6fcecf Draft

Fix void* callback return types in ctypes backend.

  • Participants
  • Parent commits 1981c27

Comments (0)

Files changed (2)

File cffi/backend_ctypes.py

                     for arg, BArg in zip(args, BArgs):
                         args2.append(BArg._from_ctypes(arg))
                     res2 = init(*args2)
-                    return BResult._to_ctypes(res2)
+                    res2 = BResult._to_ctypes(res2)
+                    if isinstance(res2, ctypes.c_void_p):
+                        # workaround for http://bugs.python.org/issue1574593
+                        res2 = res2.value
+                    return res2
                 self._as_ctype_ptr = CTypesFunction._ctype(callback)
                 self._address = ctypes.cast(self._as_ctype_ptr,
                                             ctypes.c_void_p).value

File testing/backend_tests.py

         t = ffi.typeof("int(*(*)(int))(int)")
         assert repr(t) == self.TypeRepr % "int(*(*)(int))(int)"
 
+    def test_functionptr_voidptr_return(self):
+        ffi = FFI(backend=self.Backend())
+        def cb():
+            return None
+        p = ffi.callback("void*(*)()", cb)
+        res = p()
+        assert res is None
+        int_ptr = ffi.new('int')
+        void_ptr = ffi.cast('void*', int_ptr)
+        def cb():
+            return void_ptr
+        p = ffi.callback("void*(*)()", cb)
+        res = p()
+        assert repr(res) == "<cdata 'void *'>"
+        assert ffi.cast("long", res) != 0
+
     def test_char_cast(self):
         ffi = FFI(backend=self.Backend())
         p = ffi.cast("int", '\x01')