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 <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
----------
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 <[email protected]>
<http://bugs.python.org/issue24983>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com