Commits

Thomas Heller committed ffe7d3a

Remove the special casing of Py_None when converting the return value
of the Python part of a callback function to C. If it cannot be
converted, call PyErr_WriteUnraisable with the exception we got.
Before, arbitrary data has been passed to the calling C code in this
case.

(I'm not really sure the NEWS entry is understandable, but I cannot
find better words)

  • Participants
  • Parent commits 1358404
  • Branches legacy-trunk

Comments (0)

Files changed (4)

Lib/ctypes/test/test_as_parameter.py

 
         def callback(v):
             args.append(v)
+            return v
 
         CallBack = CFUNCTYPE(c_int, c_int)
 

Lib/ctypes/test/test_functions.py

 
         def callback(v):
             args.append(v)
+            return v
 
         CallBack = CFUNCTYPE(c_int, c_int)
 
 Library
 -------
 
+- If a the Python part of a ctypes callback function returns None,
+  and this cannot be converted to the required C type, an exception is
+  printed with PyErr_WriteUnraisable.  Before this change, the C
+  callback did return arbitrary values to the calling code.
+
 - The __repr__ method of a NULL ctypes.py_object() no longer raises
   an exception.
 

Modules/_ctypes/callbacks.c

 
 	result = PyObject_CallObject(callable, arglist);
 	CHECK("'calling callback function'", result);
-	if ((restype != &ffi_type_void) && result && result != Py_None) {
+	if ((restype != &ffi_type_void) && result) {
 		PyObject *keep;
 		assert(setfunc);
 #ifdef WORDS_BIGENDIAN