Exceptions from callbacks

Jonathan Giddy jon at rdt.monash.edu.au
Mon Nov 1 02:49:24 CET 1999


Eric Dorland <dorland at lords.com> writes:

>I'm writing a Python wrapper for a library. The library stores some
>callbacks for some events, and with some glue code, get these to call
>python functions. That's the easy part. Now since its the library these
>callback and not Python per se, how do you pass an exception back to the
>interpreter, because you can't return NULL from the callback. Do you
>just set the exception, and Python will pick up on it?

Assuming these are synchronous callbacks from within the one thread (Python
code calls C interface calls library calls C callback calls Python callback), 
you can call PyErr_Clear() before calling the library routine, then return
if PyErr_Occurred() is true afterwards, return NULL from the C interface.  
See the Python/C API manual, section 1.3 "Exceptions".

If they happen to be asynchronous callbacks from threads started by the 
library, the simplest solution is to ignore them, maybe calling 
PyErr_PrintEx(0) to warn the user.

Jon.




More information about the Python-list mailing list