New submission from ganges master:

it seems the code of PyObject_GenericGetAttr, which invokes the
descriptor protocol, silences any AttributeErrors raised by the
descriptor, for classes that also define __getattr__. it should
propagate up rather than being silently ignored.

the attached example is quite artificial, but it's a simplification of
real world code i had hard time debugging. turned out i misspelled an
attribute name inside the property getter function, which raised an
AttributeError as expected -- but the exception i got was quite
misleading, saying the instance has no attribute named so.

this bug only happens when the class defines a custom __getattr__. see
attached demo file for details.

----------
components: Interpreter Core
files: demo.txt
messages: 58581
nosy: gangesmaster
severity: normal
status: open
title: descriptor protocol bug
type: behavior
versions: Python 2.5, Python 2.6
Added file: http://bugs.python.org/file8943/demo.txt

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1615>
__________________________________
only happens when the class defines a custom __getattr__:

>>> class Foo(object):
...     def __getattr__(self, name):
...         if name == "spam":
...             return 17
...         else:
...             raise AttributeError("%s object has no attribute %r" %
...                 (self.__class__.__name__, name))
...     @property
...     def bacon(self):
...         return int.lalala
...
>>>
>>> f = Foo()
>>> f.spam
17
>>> f.bacon
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in __getattr__
AttributeError: Foo object has no attribute 'bacon'  <<-- expected 'int object 
has no attribute lalala'
>>>

----------------------------------------------------

without a custom __getattr__ works as expected

>>> class Bar(object):
...     def __init__(self):
...         self.x = 17
...
...     @property
...     def bacon(self):
...         return int.lalala
...
>>> b = Bar()
>>> b.x
17
>>> b.bacon
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in bacon
AttributeError: type object 'int' has no attribute 'lalala'
>>>
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to