Author ysj.ray
Recipients chris.jerdonek, ysj.ray
Date 2010-04-03.09:17:18
SpamBayes Score 1.03245e-10
Marked as misclassified No
Message-id <1270286240.56.0.529198549222.issue8297@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2010-04-03 09:17:20ysj.raysetrecipients: + ysj.ray, chris.jerdonek
2010-04-03 09:17:20ysj.raysetmessageid: <1270286240.56.0.529198549222.issue8297@psf.upfronthosting.co.za>
2010-04-03 09:17:19ysj.raylinkissue8297 messages
2010-04-03 09:17:18ysj.raycreate