This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dunric
Recipients dunric
Date 2015-09-02.10:14:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441188888.02.0.100621474433.issue24983@psf.upfronthosting.co.za>
In-reply-to
Content
Hello,

it seems python interpreter improperly handles AttributeError exception raised in __getattr__ method, after called by unresolved attribute inside a property.

Bellow is a simple Python2 example of a class which defines __getattr__ method and a property, where is non-existing attribute accessed:

from __future__ import print_function

class MyClass(object):
    def __getattr__(self, name):
        print('__getattr__ <<', name)
        raise AttributeError(name)
        return 'need know the question'

    @property
    def myproperty(self):
        print(self.missing_attribute)
        return 42

my_inst = MyClass()
print(my_inst.myproperty)

# produces following output
__getattr__ << missing_attribute
__getattr__ << myproperty
Traceback (most recent call last):
  File "a.py", line 84, in <module>
    main()
  File "a.py", line 74, in main
    print('==', my_inst.myproperty)
  File "a.py", line 36, in __getattr__
    raise AttributeError(name)
AttributeError: myproperty


By the documentation https://docs.python.org/2/reference/datamodel.html#object.__getattr__ , if class defines __getattr__ method, it gets called at AttributeError exception and should return a computed value for name, or raise (new) AttributeError exception.

The misbehavior is in 2nd call of __getattr__, with 'myproperty' as an argument.

- self.myproperty does exist, no reason to call __getattr__ for it
- AttributeError exception raised in __getattr__ should be propagated to the outer stack, no recurrence in the same
History
Date User Action Args
2015-09-02 10:14:48dunricsetrecipients: + dunric
2015-09-02 10:14:48dunricsetmessageid: <1441188888.02.0.100621474433.issue24983@psf.upfronthosting.co.za>
2015-09-02 10:14:47dunriclinkissue24983 messages
2015-09-02 10:14:47dunriccreate