[Python-Dev] New/Old class exception pitfall
Alexander Belopolsky
alexander.belopolsky at gmail.com
Tue Mar 18 00:18:25 CET 2008
On Mon, Mar 17, 2008 at 6:49 PM, Guido van Rossum <guido at python.org> wrote:
..
> Really? Under which version exactly? On which platform? I cannot
> reproduce this with either 2.4, 2.5 or 2.6 on OS X.
Just retested in
Python 2.6a1+ (trunk:61449M, Mar 17 2008, 17:29:21)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
and
Python 2.5 (r25:51908, Nov 24 2006, 11:03:50)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
I don't have my PowerBook here, but I am sure I've seen in on Mac OS
too. Only new-style class behavior is problematic. The following
code prints 'b' for me:
__metaclass__ = type
class x:
pass
class y(x):
pass
try:
raise y
except y:
print "a"
except:
print "b"
> Which one would expect regardless of the metaclass, right?
Yes.
> I have no idea what you are talking about. Can you quote a file,
> revision and line number where this is done?
>
Sorry for the lack of details. The code in question is at
trunk/Python/errors.c:108 as of r61467:
"""
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
/* problems here!? not sure PyObject_IsSubclass expects to
be called with an exception pending... */
return PyObject_IsSubclass(err, exc);
}
return err == exc;
"""
(flushed left hoping it won't get garbled)
As you can see, subclass check is only performed if
PyExceptionClass_Check(err) passes, which includes a check for err
being derived from BaseException (see Include/pyerrors.h). This logic
allows returning err == exc when err is a string.
More information about the Python-Dev
mailing list