Ray.Allen <ysj....@gmail.com> added the comment:

In fact, there are only three types of tp_getattro functions:
    1.For type objects, it is type_getattro(), in case of AttributeError, this 
function give the message format: 
        type object %(type)s has no attribute %(attr)s
    2.For super objects, it is super_getattro(), in case of no attribute found 
in one of its base class, it calls the 3'th getattr function below.
    3.For the base type 'object' and other new style class, it is 
PyObject_GenericGetAttr(), and in case of AttributeError, this function give 
the message format:
        %(type)s object has no attribute %(attr)s

So, there are only tow formats of AttributeError's exception messages:
    1.type object %(type)s has no attribute %(attr)s
    2.%(type)s object has no attribute %(attr)s
The first one is for type objects, the second one is for all the instances 
objects.

In most cases, these tow formats it is enough for program to display, bu t it 
is not well enough. Take the module objects for example, in case of 
AttributeError, we will always hope to know exactly which module has no 
attribute, not only the message: 'module object has attribute xxx'.

Also for the super() call, take super(A, b).xxx() for example, if the attribute 
is not found in the class next to the A in b's type's mro list, 
PyObject_GenericGetAttr() will be called with the two arguments, the super 
object its self and 'xxx'. But there are only few valid attributes of a super 
object, like '__thisclass__', '__self__', '__self_class__'. In most cases, we 
don't need these attributes of a super object, what we need is the attribute of 
one of b's type's base types. So I think once the AttributeError is raised in 
PyObject_GenericGetAttr() call in the end of super_getattro(), the exception 
message should tell us in which base class python can not found the attribte 
'xxx', but not the super object itself, although the exception is raised in the 
PyObject_GenericGetAttr(<super obj>, 'xxx').

For the solution, I think the type_getattro and super_getattro can just return 
NULL to indicate the attribute is not found, but for a wrapper function of this 
tow which is the tp_getattro for each type to raise the attribute error, with 
the reasonable  exception message. For example, mudule type can tell which 
module has no attribute, super type can tell which base class has no attribute, 
and so on.

What about others' opinion?

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue8297>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to