[Pyrex] Segfaults and leaked exceptions in 0.9.9

Stefan Behnel stefan_ml at behnel.de
Thu Aug 1 05:03:30 CEST 2013


Claudio Freire, 31.07.2013 20:41:
> On Wed, Jul 31, 2013 at 3:33 PM, Claudio Freire wrote:
>> This is interestingly new (I think this didn't happen in 0.9.8, but I
>> didn't check)
>>
>> test.pyx:
>>
> ...
>> . def bork():
>> .     cdef object rv
>> .     try:
>> .         rv = {}[3]
>> .     except:
>> .         rv = None
>> .     return rv
> ...
> 
>> >>> import lrucache
>> >>> lrucache.bork()
>> >>> 3
>> KeyError: 3

You should try Cython, I'm sure it doesn't suffer from the problem you
describe.


> I should mention that, checking the generated C source, the problem
> there lies in pyrex not calling PyErr_Clear at the end of the except
> block.

It has its own set of internal functions that handle exceptions. Calling
PyErr_Clear() wouldn't help at the end of the except block.

Also note that there are differences between Python 2 and Python 3 here.
The exception handling in Python 3 is much more strict. Python 2 sort-of
leaks exceptions.


> Not calling it is safe as long as the exception has been thrown by
> pyrex itself, but in this case, it's python that does, so python does
> set the thread's exception state, and pyrex fails to clean up.

It might be linked to the fact that Pyrex and Cython do not use frames.
Python uses frames for exception scoping and cleanup. It took me a while to
get the exception handling in Cython right (especially across Python
versions), but it definitely works now.

Stefan




More information about the Pyrex mailing list