Exceptions from callbacks
Eric Dorland
dorland at lords.com
Tue Nov 2 22:39:27 EST 1999
Jonathan Giddy wrote:
>
> 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.
You're not understanding exactly. It is synchronous (ie one thread), but
since the library is calling the callback, and not the python
interpreter, I can't return NULL to the interpreter. Basically, is there
a way from inside the callback to say "Wake up python, an exception!",
without having to return NULL to the interpreter, which is impossible
from the callback.
--
Eric Dorland
dorland at lords.com
More information about the Python-list
mailing list