[Python-bugs-list] [ python-Bugs-409355 ] Meta-class inheritance problem

noreply@sourceforge.net noreply@sourceforge.net
Wed, 21 Mar 2001 11:18:06 -0800


Bugs item #409355, was updated on 2001-03-17 02:10
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=409355&group_id=5470

Category: Python Interpreter Core
>Group: Feature Request
>Status: Closed
Priority: 7
Submitted By: Lorien Dunn (loriend)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Meta-class inheritance problem

Initial Comment:
I've run into a bug in python 2.0. The real code that
the following pseudo code represents throws a
TypeError.

class MyDerivedClass(MyPurePythonClass,MyBoostClass):
        def __init__(self):
                MyPurePythonClass.__init__(self)
                MyBoostClass.__init__(self)

The problem occurs in MyPurePythonClass- Python says
"unbound method must be called with class instance 1st
argument". This makes sense: I'm passing a meta-class
instead of a class. David Abrahams (the main author of
boost::python) agrees that this is a Python problem,
and that it should affect Zope as well.

I think I've found the point in the python 2.0 sorce
where it occurs:


ceval.c, line 1816 (reformatted for clarity):

/*              BEGIN CODE SAMPLE               */

else
{
/* Unbound methods must be called with an instance of
the class (or a derived class) as first argument */

     if (na > 0 && (self = stack_pointer[-n]) != NULL
                && PyInstance_Check(self)
                && PyClass_IsSubclass((PyObject
*) 		 		   (((PyInstanceObject*)  self)->in_class),
class))
                  /* Handy-dandy */ ;
      else
      {
        PyErr_SetString(PyExc_TypeError, "unbound
method must be 		called with class instance 1st
argument");
        x = NULL;
        break;
      }
}

/*              END CODE SAMPLE         */


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-03-21 11:18

Message:
Logged In: YES 
user_id=6380

I *think* I've fixed this now -- please test with the
current CVS.

Please let me know if I goofed up, and I'll reopen the bug
report.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-03-18 14:22

Message:
Logged In: YES 
user_id=6380

Zope has several workarounds, so isn't affected.

But I agree it could be fixed.  The test is there because it wants to enforce a concept: 'self' should be an instance 
of the class that defines the method (where a subclass instance is fine too).

But the implementation of the test is based too much on the default implementation of classes.

Could someone please submit a patch that replaces the concrete isinstance() test with an isinstance() test similar 
to that in bltinmodule.c?  (Maybe we need to add a new C API, PyObject_IsInstance().)

If someone could come up with a check that conve

----------------------------------------------------------------------

Comment By: David Abrahams (david_abrahams)
Date: 2001-03-18 05:30

Message:
Logged In: YES 
user_id=52572

Sorry, that anonymous comment is mine. I'm still a bit 
clumsy with SourceForge.

-Dve

----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2001-03-18 05:03

Message:
Logged In: NO 

I was a bit surprised to learn that this check was still in 
the code after issubclass and isinstance had been fixed to 
work with extension classes. Suppose you checked for a 
__class__ attribute on the first argument instead (or 
additionally)?

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-03-17 21:29

Message:
Logged In: YES 
user_id=31435

Assigned to Guido, because I bet JimF would refuse to take 
blame for this <wink>.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=409355&group_id=5470