[issue24983] Wrong AttributeError propagation
David Unric added the comment: Oops, this was the strict version. The lazy method call version behaves exactly like property getter. So there is probably no implementation bug, but not too well thought out decision design, making debugging AttributeError exceptions in properties difficult and quite misleading. -- ___ Python tracker <http://bugs.python.org/issue24983> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24983] Wrong AttributeError propagation
David Unric added the comment: This looks a bit inconsistent. See following version with "manual" getter definition: class MyClass(object): def __init__(self, *args, **kwargs): # setting access to getter by attribute # without use of property decorator self.__dict__['myproperty'] = self._myproperty() def __getattr__(self, name): print('__getattr__ <<', name) raise AttributeError(name) return 'need know the question' def _myproperty(self): print(self.missing_attribute) return 42 my_inst = MyClass() print(my_inst.myproperty) # Produces (expected) output __getattr__ << missing_attribute Traceback (most recent call last): File "a.py", line 55, in my_inst = MyClass() File "a.py", line 33, in __init__ self.__dict__['myproperty'] = self._myproperty() File "a.py", line 48, in _myproperty print(self.missing_attribute) File "a.py", line 37, in __getattr__ raise AttributeError(name) AttributeError: missing_attribute I'd expect the original version using property decorator would behave the same way. Possible issue in property class implementation ? -- ___ Python tracker <http://bugs.python.org/issue24983> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24983] Wrong AttributeError propagation
David Unric added the comment: Thanks for the comprehensive response. I did suspected the 2nd call is caused how it has been described in paragraph 4. And you are probably right. Only think exception instance raised in __getattr__ should not lead to its another call but propagated to outer level, ie. should not get continue in getter in this example. -- ___ Python tracker <http://bugs.python.org/issue24983> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24983] Wrong AttributeError propagation
New submission from David Unric: 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 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 -- components: Interpreter Core messages: 249533 nosy: dunric priority: normal severity: normal status: open title: Wrong AttributeError propagation type: behavior versions: Python 2.7, Python 3.4 ___ Python tracker <http://bugs.python.org/issue24983> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com