Undocumented Python 2.6 change: Py_None vs NULL when C implementation raises exception

Fredrik Lundh fredrik at pythonware.com
Wed Jul 23 21:45:52 CEST 2008

Heikki Toivonen wrote:

> I was debugging M2Crypto function written in C which changed behavior
> between Python 2.6 and earlier Python versions. In an error condition
> the function was supposed to raise exception type A, but with 2.6 it
> raised type B, and further, there was no string value for the exception.
> I tracked this down to the C code incorrectly returning Py_None when it
> should have returned NULL. Changing the C code to return NULL made it
> behave correctly in 2.6.
> I don't know how common a mistake it is to return Py_None when NULL
> should have been returned, but it might be worth a note in the list of
> changes for 2.6 that this behavior changed, don't you think?

the behaviour when setting the exception status without returning NULL 
has never been well-defined; it pretty much depends on what kind of 
error checking the code that runs later happens to use.

(this has been an entertaining source of obscure errors over the years; 
code that uses PyErr_Clear may mask errors, and code that uses 
PyErr_Occurred to check if something went wrong might raise the wrong 
error, often at an unexpected location).


More information about the Python-list mailing list