[Python-Dev] bug: mro, subclassing and equality

Josh Hoyt josh@janrain.com
31 May 2002 14:26:39 -0700


Hello,

I found a bug in Python 2.2.1, that I think is related to method
resolution order and type initialization. I'm pretty new to Python, so
bear with me while I try to explain.

The following code:

class Nanite(type):
    key = "blargchoo"
    def __eq__(self, other):
        return self.key == other.key

def crashbang():
    """KILL!"""
    c1 = type("molly", (Nanite,), {})

    p1 = type('parent1', (), {})
    p2 = type('parent2', (), {})

    c1('child', (p1, p2), {})

causes the interpreter to seg fault.

GDB tells me that it's crashing in typeobject.c, at line 1217.

I made the following change, based on a bit of copy-coding, without
actually tracing the full execution path, and it seemed to fix the
problem, but hopefully someone here knows enough that they can tell
whether I just made the symptom go away or this actually fixes the
problem:

 ***************
 *** 1213,1224 ****
   
       /* Look in tp_dict of types in MRO */
       mro = type->tp_mro;
 !     if (mro == NULL) {
 !       n = 0;
 !     } else {
 !       assert(PyTuple_Check(mro));
 !       n = PyTuple_GET_SIZE(mro);
 !     }
       for (i = 0; i < n; i++) {
               base = PyTuple_GET_ITEM(mro, i);
               if (PyClass_Check(base))
 --- 1213,1220 ----
   
       /* Look in tp_dict of types in MRO */
       mro = type->tp_mro;
 !     assert(PyTuple_Check(mro));
 !     n = PyTuple_GET_SIZE(mro);
       for (i = 0; i < n; i++) {
               base = PyTuple_GET_ITEM(mro, i);
               if (PyClass_Check(base))

Thanks,

Josh Hoyt <josh@janrain.com>
Software Engineer
JanRain, Inc.