[Python-Dev] C-API functions for reading/writing tstate->exc_* ?

Stefan Behnel stefan_ml at behnel.de
Sun Feb 19 14:04:37 CET 2012


Hi,

the Cython and PyPy projects are currently working on getting Cython
implemented extensions to build and run in PyPy, interfacing at the C-API
level for now.

One problem we encountered was that there is currently no "abstract" way to
access tstate->exc_type and friends, i.e. the last exception that was
caught, aka. sys.exc_info(). Apparently, PyPy stores them at a frame level,
whereas CPython makes them available in thread local storage as bare struct
fields. Even if PyPy put them there on request, any changes to these fields
would pass unnoticed. And Cython needs to set them in order to properly
implement the semantics of a try-except statement (and in some other places
where exception state is scoped).

When compiling for PyPy, Cython therefore needs a way to tell PyPy about
any changes. For the tstate->curexc_* fields, there are the two functions
PyErr_Fetch() and PyErr_Restore(). Could we have two similar "official"
functions for the exc_* fields? Maybe PyErr_FetchLast() and
PyErr_RestoreLast()?

Note that Cython would not have a reason to actually use them in CPython,
and it should be uncommon for non-Cython extension modules to care about
the exc_* fields at all. So these functions won't be of much use if
actually implemented in CPython (although I wouldn't mind doing that). The
question is just if we could have two officially named functions that PyPy
(and maybe other Pythons) could implement in order to access the last
raised exception in a way that does not depend on implementation details.

Stefan



More information about the Python-Dev mailing list