[Python-checkins] r52367 - in python/branches/release25-maint: Lib/ctypes/test/test_callbacks.py Misc/NEWS Modules/_ctypes/callbacks.c

thomas.heller python-checkins at python.org
Tue Oct 17 21:41:11 CEST 2006


Author: thomas.heller
Date: Tue Oct 17 21:41:10 2006
New Revision: 52367

Modified:
   python/branches/release25-maint/Lib/ctypes/test/test_callbacks.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_ctypes/callbacks.c
Log:
ctypes callback functions only support 'fundamental' result types.
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.

Backported from trunk.

Modified: python/branches/release25-maint/Lib/ctypes/test/test_callbacks.py
==============================================================================
--- python/branches/release25-maint/Lib/ctypes/test/test_callbacks.py	(original)
+++ python/branches/release25-maint/Lib/ctypes/test/test_callbacks.py	Tue Oct 17 21:41:10 2006
@@ -101,6 +101,19 @@
             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:

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Tue Oct 17 21:41:10 2006
@@ -75,6 +75,10 @@
 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.
+
 - Bug #813342: Start the IDLE subprocess with -Qnew if the parent
   is started with that option.
 

Modified: python/branches/release25-maint/Modules/_ctypes/callbacks.c
==============================================================================
--- python/branches/release25-maint/Modules/_ctypes/callbacks.c	(original)
+++ python/branches/release25-maint/Modules/_ctypes/callbacks.c	Tue Oct 17 21:41:10 2006
@@ -293,8 +293,11 @@
 		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;
 	}


More information about the Python-checkins mailing list