[Python-bugs-list] [ python-Bugs-493462 ] Attribute Search in MRO w/o tp_getattr

noreply@sourceforge.net noreply@sourceforge.net
Sun, 16 Dec 2001 17:24:39 -0800


Bugs item #493462, was opened at 2001-12-14 13:17
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=493462&group_id=5470

Category: Type/class unification
Group: Python 2.2
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Robert Stewart (rstewart)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Attribute Search in MRO w/o tp_getattr

Initial Comment:
In writing a metatype, I wanted to resolve attribute 
references via a tp_getattr-style function -- with 
either a char * or a Python string.  Unfortunately, 
PyObject_GenericGetAttr() first tries _PyType_Lookup() 
to find a descriptor via base class dictionaries.

Searching according to the MRO is fine.  The problem 
is that _PyType_Lookup() only searches the dictionary 
of each base class.  It never checks those bases to 
see it tp_getattr is set.

My workaround, which is not ideal, is to implement my 
own tp_getattro function which first calls 
PyObject_GenericGetAttr() to locate the attribute.  If 
that succeeds, that attribute is returned.  If it 
fails, then the tp_getattr-style function is called to 
locate the attribute.  This approach will return a 
base class attribute when the derived class overrides 
it, which is why I said it was not ideal.

The only way I can improve this process is to write my 
own replacement for PyObject_GenericGetAttr() and 
_PyType_Lookup().  That would make my code very 
brittle, so that's not a good solution.

What I'd like to see is for _PyType_Lookup() to try 
the dictionary, as it does now.  If that fails, I'd 
like it to check to see if tp_getattr is set.  If so, 
I'd like it to call the tp_getattr function to resolve 
the attribute.  Perhaps this change violates some 
ideal of Python that I can't foresee, but it would 
sure work great for me.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-12-14 13:41

Message:
Logged In: YES 
user_id=6380

When you talk about the bases and their tp_getattr, are you
talking about the tp_getattr of the base type, or of its
metatype? (Note: forget about tp_getattr -- you should leave
that NULL, and only implement tp_getatto.)

PyObject_GenericGetAttr() should only be used in situations
where the base type's tp_getattro is also
PyObject_GenericGetAttr().

Why do you think that writing your own replacement for 
PyObject_GenericGetAttr() and _PyType_Lookup() would be
brittle? That's what you are *supposed* to do if you're not
happy with them.

What do you want to do with your metatype? Why do you need
one? Have you considered prototyping it in Python?

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

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