[pypy-dev] Bringing Cython and PyPy closer together

Stefan Behnel stefan_ml at behnel.de
Sun Feb 19 11:40:04 CET 2012


Amaury Forgeot d'Arc, 18.02.2012 15:41:
> 2012/2/18 Stefan Behnel
>> 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"?

We need read/write access and also swap the exception with another one in
some places (lacking a dedicated frame for generators, for example), that
makes it two functions at least. CPython and its hand-written extensions
won't have much use for this, so the only reason to add this would be to
make PyPy (and maybe others) happier when running Cython extensions.

I'll ask, although I wouldn't mind using a dedicated PyPy API for this.


> For the record, pypy could implement it as:
> space.getexecutioncontext().gettopframe_nohidden().last_exception =
> operationerr
> i.e. the thing returned by sys.exc_info().

I imagine that even if there is a way to do this from C code in PyPy, it
would be too inefficient for something as common as exception handling.

Stefan



More information about the pypy-dev mailing list