2012/2/18 Stefan Behnel <stefan_ml@behnel.de>
Here's an example.
Python code:
def print_excinfo(): print(sys.exc_info())
Cython code:
from stuff import print_excinfo
try: raise TypeError except TypeError: print_excinfo()
With the code removed, Cython will not store the TypeError in sys.exc_info(), so the Python code cannot see it. This may seem like an unimportant use case (who uses sys.exc_info() anyway, right?), but this becomes very visible when the code that uses sys.exc_info() is not user code but CPython itself, e.g. when raising another exception or when inspecting frames. Things grow really bad here, especially in Python 3.
I think I understand now, thanks for your example. Things are a bit simpler in PyPy because these exceptions are stored in the frame that is currently handling it. At least better than CPython which stores it in one place, and has to somehow save the state of the previous frames. Did you consider adding such a function to CPython? "PyErr_SetCurrentFrameExceptionInfo"? For the record, pypy could implement it as: space.getexecutioncontext().gettopframe_nohidden().last_exception = operationerr i.e. the thing returned by sys.exc_info(). -- Amaury Forgeot d'Arc