]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
ctypes callback functions only support 'fundamental' result types.
authorThomas Heller <theller@ctypes.org>
Tue, 17 Oct 2006 19:30:48 +0000 (19:30 +0000)
committerThomas Heller <theller@ctypes.org>
Tue, 17 Oct 2006 19:30:48 +0000 (19:30 +0000)
Check this and raise an error when something else is used - before
this change ctypes would hang or crash when such a callback was
called.  This is a partial fix for #1574584.

Will backport to release25-maint.

Lib/ctypes/test/test_callbacks.py
Misc/NEWS
Modules/_ctypes/callbacks.c

index 9d96a5445e244c1b5a40b9ded358f558ab6e3da2..f47fc37b3f025089b30db505dc3034ad5a400a60 100644 (file)
@@ -101,6 +101,19 @@ class Callbacks(unittest.TestCase):
             after = grc(o)
             self.failUnlessEqual((after, o), (before, o))
 
+    def test_unsupported_restype_1(self):
+        # Only "fundamental" result types are supported for callback
+        # functions, the type must have a non-NULL stgdict->setfunc.
+        # POINTER(c_double), for example, is not supported.
+
+        prototype = self.functype.im_func(POINTER(c_double))
+        # The type is checked when the prototype is called
+        self.assertRaises(TypeError, prototype, lambda: None)
+
+    def test_unsupported_restype_2(self):
+        prototype = self.functype.im_func(object)
+        self.assertRaises(TypeError, prototype, lambda: None)
+
 try:
     WINFUNCTYPE
 except NameError:
index 9d22aedfb2c11b94fca550637a0f5a338e69e1a9..b0f8f55a5b797fd61e3b63490686f133e94f7b0b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,10 @@ Core and builtins
 Library
 -------
 
+- ctypes callback functions only support 'fundamental' data types as
+  result type.  Raise an error when something else is used.  This is a
+  partial fix for Bug #1574584.
+
 - Fix turtle so that time.sleep is imported for the entire library.  Allows
   the demo2 function to be executed on its own instead of only when the
   module is run as a script.
index 41ec0f50289bccf3baf14fd520d0bff4b4ad0823..0e5d6c0456f5b5c8ef9d312704482ff92427cc9a 100644 (file)
@@ -293,8 +293,11 @@ ffi_info *AllocFunctionCallback(PyObject *callable,
                p->restype = &ffi_type_void;
        } else {
                StgDictObject *dict = PyType_stgdict(restype);
-               if (dict == NULL)
-                       goto error;
+               if (dict == NULL || dict->setfunc == NULL) {
+                 PyErr_SetString(PyExc_TypeError,
+                                 "invalid result type for callback function");
+                 goto error;
+               }
                p->setfunc = dict->setfunc;
                p->restype = &dict->ffi_type_pointer;
        }