]> granicus.if.org Git - python/commitdiff
Remove the special casing of Py_None when converting the return value
authorThomas Heller <theller@ctypes.org>
Wed, 16 Aug 2006 15:10:12 +0000 (15:10 +0000)
committerThomas Heller <theller@ctypes.org>
Wed, 16 Aug 2006 15:10:12 +0000 (15:10 +0000)
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)

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

index 5716f16c7e94809a5b5e2df9a504296363c5767a..058105933565080ec5e22343396488be2b1cf7e7 100644 (file)
@@ -61,6 +61,7 @@ class BasicWrapTestCase(unittest.TestCase):
 
         def callback(v):
             args.append(v)
+            return v
 
         CallBack = CFUNCTYPE(c_int, c_int)
 
index bfa0cad911b8d8ae4bed13f0a4cc9d4471ccb7c5..759aea7eeeef545a34f2e7afcfed7c1173ce7e06 100644 (file)
@@ -222,6 +222,7 @@ class FunctionTestCase(unittest.TestCase):
 
         def callback(v):
             args.append(v)
+            return v
 
         CallBack = CFUNCTYPE(c_int, c_int)
 
index ab40d3defbd5ac1724042873166178314474b32f..dc99ff799a52ba28f2f0e6088afeee6d50e18273 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,11 @@ Core and builtins
 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.
 
index cbe3d03d7e5df6f4b3454ff25e085ae387e1b9a1..c8e669a86e67bc6e6f2855169361e4097b951a17 100644 (file)
@@ -205,7 +205,7 @@ if (x == NULL) _AddTraceback(what, __FILE__, __LINE__ - 1), PyErr_Print()
 
        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