[issue8297] AttributeError message text should include module name

Ray.Allen report at bugs.python.org
Sat Apr 3 11:17:20 CEST 2010


Ray.Allen <ysj.ray at 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 <report at bugs.python.org>
<http://bugs.python.org/issue8297>
_______________________________________


More information about the Python-bugs-list mailing list