[Pyrex] Reraise not inside except clause

Phillip J. Eby pje at telecommunity.com
Wed Oct 17 16:11:07 CEST 2007


At 02:39 AM 10/16/2007, Robert Kern wrote:
>Greg Ewing wrote:
> > Robert Kern wrote:
> >> It is valid in Python to have an empty "raise" statement 
> anywhere, and it used
> >> to be valid in Pyrex before 0.9.6.
> >>
> >> Can this restriction be reverted?
> >
> > Not without bringing back the behaviour whereby the exception
> > you want to reraise can get clobbered by another exception
> > before you get a chance to reraise it.
> >
> > Python's way of dealing with this is to save the current
> > exception on entry to every function and restore it afterwards.
> > That would be an unacceptable amount of overhead for Pyrex,
> > given that the overwhelming majority of function calls do not
> > need this to be done.
> >
> > My suggestion is to think of another way to achieve whatever
> > it is that PyProtocols is trying to do. If you can provide
> > some more details about that, I may be able to help.
>
>The code is Phillip Eby's, not mine, but here it is:
>
>http://svn.eby-sarna.com/PyProtocols/src/protocols/_speedups.pyx?rev=205&view=markup
>
>It appears to be using the "raise" statement after checking for an error when
>calling PyObject_GetAttr. Pyrex doesn't catch the error itself because Phillip
>is declaring PyObject_GetAttr to return "void*" rather than "object". I'm not
>entirely sure why; he does so for other functions, but he comments 
>that this is
>because they return borrowed references and Pyrex's increffing gets 
>in the way.
>
>It looks like every time he "raise"s, he's already grabbing the current
>exception object. Most likely, that can be changed to "raise err".

No, it can't -- the error is coming from PyObject_GetAttr, not from 
the places where 'err' is being trapped.

The bare "raise" statements are passing through the error raised by 
PyObject_GetAttr, but not caught by Pyrex.  This was an intentional 
act to improve performance over Pyrex's error handling.

Unless there are tests that raise a non-AttributeError in a 
__getattr__, this code won't be exercised, so if you are patching to 
use 'raise err', you're creating a lurking core dump for the first 
time it comes up.





More information about the Pyrex mailing list