[Pyrex] [Cython-dev] Is there any problem with having "except *" as the default behavior for cdef funcs?

Robert Bradshaw robertwb at math.washington.edu
Wed Sep 12 22:57:14 CEST 2007


On Sep 12, 2007, at 10:06 AM, William Stein wrote:

> On 9/12/07, Robert Bradshaw <robertwb at math.washington.edu> wrote:
>> The beauty of the except clause is that PyErr_Occurred is not called,
>> instead just does a check to see if the returned value matches the
>> except value, which is much faster. I think, however, if the except
>> value matches PyErr_Occurred should still probably be called to
>> verify it is an error rather than a valid value. (Put that on the
>> todo list...)
>
> Hey, that might be a really good idea, though with Python often
> having an error occur is the normal flow of control, and adding
> extra overhead to that which is not needed might be bad.  Thoughts?

It would output

if (__pyx_result == except_value && PyErr_Occurred()) [goto error  
handling code]

rather than

if (__pyx_result == except_value) [goto error handling code].

Thus PyErr_Occurred() would only get called (due to short-circuiting  
of &&) if the exception value was returned, but if (perchance) the  
except_value is correct (rather than flagging an error) the flow  
would continue. A one-line fix.

This would allow one to write functions with the semantics of  
PyInt_AsLong ( http://docs.python.org/api/intObjects.html ) but would  
mean if the function (which may be from c library) returned an error  
value without setting a python error, that error would be ignored,  
which would be bad. What would be a good way to signal one needs to  
check PyErr_Occurred() as well as the except value for a specific  
function?

- Robert




More information about the Pyrex mailing list