"print 0.1==0.1" -> TypeError; what did I do?

Pete Shinners pete at shinners.org
Tue Dec 4 11:49:52 EST 2001


Maciej Kalisiak wrote:

>>    hasattr(None, "spam")
>>
> 
> Yes!  After following your suggestions about PyErr_Occurred() I
> quickly found the problem, thanks!  I'm curious though, how exactly
> did the above statement get rid of the exception??


         v = PyObject_GetAttr(v, name);
         if (v == NULL) {
                 PyErr_Clear();
                 Py_INCREF(Py_False);
                 return Py_False;
         }

if you peek inside the "hasattr" source, you see this block.
if the object does not have the given attribute, an AttributeError will 
replace your current one when calling "PyObject_GetAttr". hasattr then 
clears this python error and returns.

therefore, the exception being raised by your function is being 
overwritten by another exception, then all the exceptions are cleared.

(and your exception magically goes away)

of course, in the correct python world, the code would have never gotten 
this far, because your extension function _must_ return NULL if anything 
raised an exception inside it.




More information about the Python-list mailing list