From b4a0cf17c4f665162024f354ab0d3077d5c91a99 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Wed, 16 Aug 2006 15:10:12 +0000 Subject: 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) --- Lib/ctypes/test/test_as_parameter.py | 1 + Lib/ctypes/test/test_functions.py | 1 + Misc/NEWS | 5 +++++ Modules/_ctypes/callbacks.c | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py index 5716f16..0581059 100644 --- a/Lib/ctypes/test/test_as_parameter.py +++ b/Lib/ctypes/test/test_as_parameter.py @@ -61,6 +61,7 @@ class BasicWrapTestCase(unittest.TestCase): def callback(v): args.append(v) + return v CallBack = CFUNCTYPE(c_int, c_int) diff --git a/Lib/ctypes/test/test_functions.py b/Lib/ctypes/test/test_functions.py index bfa0cad..759aea7 100644 --- a/Lib/ctypes/test/test_functions.py +++ b/Lib/ctypes/test/test_functions.py @@ -222,6 +222,7 @@ class FunctionTestCase(unittest.TestCase): def callback(v): args.append(v) + return v CallBack = CFUNCTYPE(c_int, c_int) diff --git a/Misc/NEWS b/Misc/NEWS index ab40d3d..dc99ff7 100644 --- 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. diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index cbe3d03..c8e669a 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -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 -- cgit v0.12