This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyObject_IsInstance() doesn't find bases named in type(name, bases, dict)
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: scoder
Priority: normal Keywords:

Created on 2008-05-19 15:44 by scoder, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg67065 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2008-05-19 15:44
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
version.
msg67171 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2008-05-21 19:41
Sorry, the bug was in Cython, which didn't call InstanceMethod().

Please ignore.
History
Date User Action Args
2022-04-11 14:56:34adminsetgithub: 47164
2008-05-21 20:44:48benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2008-05-21 19:41:09scodersetmessages: + msg67171
2008-05-19 15:44:50scodercreate