@@ -213,9 +213,6 @@ static void _CallPythonObject(void *mem,
213213 pArgs ++ ;
214214 }
215215
216- #define CHECK (what , x ) \
217- if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print()
218-
219216 if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR )) {
220217 error_object = _ctypes_get_errobj (& space );
221218 if (error_object == NULL )
@@ -235,7 +232,10 @@ if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyEr
235232 }
236233
237234 result = PyObject_CallObject (callable , arglist );
238- CHECK ("'calling callback function'" , result );
235+ if (result == NULL ) {
236+ _PyErr_WriteUnraisableMsg ("on calling ctypes callback function" ,
237+ callable );
238+ }
239239
240240#ifdef MS_WIN32
241241 if (flags & FUNCFLAG_USE_LASTERROR ) {
@@ -251,16 +251,17 @@ if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyEr
251251 }
252252 Py_XDECREF (error_object );
253253
254- if ((restype != & ffi_type_void ) && result ) {
255- PyObject * keep ;
254+ if (restype != & ffi_type_void && result ) {
256255 assert (setfunc );
256+
257257#ifdef WORDS_BIGENDIAN
258- /* See the corresponding code in callproc.c, around line 961 */
259- if (restype -> type != FFI_TYPE_FLOAT && restype -> size < sizeof (ffi_arg ))
258+ /* See the corresponding code in _ctypes_callproc():
259+ in callproc.c, around line 1219. */
260+ if (restype -> type != FFI_TYPE_FLOAT && restype -> size < sizeof (ffi_arg )) {
260261 mem = (char * )mem + sizeof (ffi_arg ) - restype -> size ;
262+ }
261263#endif
262- keep = setfunc (mem , result , 0 );
263- CHECK ("'converting callback result'" , keep );
264+
264265 /* keep is an object we have to keep alive so that the result
265266 stays valid. If there is no such object, the setfunc will
266267 have returned Py_None.
@@ -270,18 +271,32 @@ if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyEr
270271 be the result. EXCEPT when restype is py_object - Python
271272 itself knows how to manage the refcount of these objects.
272273 */
273- if (keep == NULL ) /* Could not convert callback result. */
274- PyErr_WriteUnraisable (callable );
275- else if (keep == Py_None ) /* Nothing to keep */
274+ PyObject * keep = setfunc (mem , result , 0 );
275+
276+ if (keep == NULL ) {
277+ /* Could not convert callback result. */
278+ _PyErr_WriteUnraisableMsg ("on converting result "
279+ "of ctypes callback function" ,
280+ callable );
281+ }
282+ else if (keep == Py_None ) {
283+ /* Nothing to keep */
276284 Py_DECREF (keep );
285+ }
277286 else if (setfunc != _ctypes_get_fielddesc ("O" )-> setfunc ) {
278287 if (-1 == PyErr_WarnEx (PyExc_RuntimeWarning ,
279288 "memory leak in callback function." ,
280289 1 ))
281- PyErr_WriteUnraisable (callable );
290+ {
291+ _PyErr_WriteUnraisableMsg ("on converting result "
292+ "of ctypes callback function" ,
293+ callable );
294+ }
282295 }
283296 }
297+
284298 Py_XDECREF (result );
299+
285300 Done :
286301 Py_XDECREF (arglist );
287302 PyGILState_Release (state );
0 commit comments