[pypy-dev] Bringing Cython and PyPy closer together

Stefan Behnel stefan_ml at behnel.de
Sat Feb 18 15:10:49 CET 2012

Amaury Forgeot d'Arc, 18.02.2012 14:52:
> 2012/2/18 Stefan Behnel
>> The exception handling code that you deleted in __Pyx_GetException(), that
>> which accesses exc_type and friends, is actually needed for correct
>> semantics of Cython code and Python code. Basically, it implements the part
>> of the except clause that moves the hot exception into sys.exc_info().
>> This equally applies to __Pyx_ExceptionSave() and __Pyx_ExceptionReset(),
>> which form something like an exception backup frame around code sections
>> that may raise exceptions themselves but must otherwise not touch the
>> current exception. Specifically, as part of the finally clause.
>> In order to fix this, is there a way to store away and restore the current
>> sys.exc_info() in PyPy?
> I certainly was a bit fast to remove code there, and these
> exc_value and curexc_value have always been a delicate
> part of the CPython interpreter.
> One thing I don't understand for example, is why Cython needs to deal with
> sys.exc_info, when no other extension uses it for exception management.

Here's an example.

Python code:

  def print_excinfo():

Cython code:

  from stuff import print_excinfo

      raise TypeError
  except TypeError:

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.

> The only way to know for sure is to have unit tests with different use
> cases.

Cython has loads of those in its test suite, as you can imagine. These
things are so tricky to get right that it's futile to even try without
growing a substantial test base of weird corner cases.


More information about the pypy-dev mailing list