[issue2915] PyObject_IsInstance() doesn't find bases named in type(name, bases, dict)

Stefan Behnel report at bugs.python.org
Mon May 19 17:44:52 CEST 2008

New submission from Stefan Behnel <scoder at users.sourceforge.net>:

While porting the code that Cython generates to Py3a5 (almost completed,
BTW), I noticed a problem with class creation. We are currently using
this call to create a new class in Py3:

    PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type,
                                 name, bases, dict, NULL);

As an example, I subtype the built-in "list" type like this (Cython code!):

    class B(list):
        def append(self, *args):
            for arg in args:
                list.append(self, arg)

which calls type() as shown above with name="B" and bases=(PyList_Type,).

Surprisingly to me, the call to .append() then fails in the method
descriptor code with a type error on "self". I tried calling super(...)
instead, and it gives a similar error. I read through the descriptor
code and the thing that fails here is

	PyObject_IsInstance(self, (PyObject *)(descr->d_type))

in line 229 of descrobject.c, which internally calls

        PyObject_TypeCheck(inst, (PyTypeObject *)cls)

in line 2543 of abstract.c. The problem here is that this checks the
ob_type, which holds a "type" and not a "B", so it doesn't find the base
type "list" of the "B" type and instead looks through the base types of
"type". The result is that PyObject_IsInstance() does not consider the
result of the above call to type(name, bases, dict) an instance of the
types that were named in "bases".

As this works in Python 2.5.1 and also for equivalent Python code in the
interpreter of Python 3.0a5, I assume that this is a bug in the alpha

components: Interpreter Core
messages: 67065
nosy: scoder
severity: normal
status: open
title: PyObject_IsInstance() doesn't find bases named in type(name, bases, dict)
type: behavior
versions: Python 3.0

Tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list