getattr() woes

Thomas Rast foo.bar at freesurf.ch.invalid
Tue Dec 28 21:02:34 EST 2004


Hello

I've found out about a fundamental problem of attribute lookup, the
hard way.

asyncore.py uses the following code:

class dispatcher:
    # ...
    def __getattr__(self, attr):
        return getattr(self.socket, attr)

Now suppose that I'm asking for some attribute not provided by
dispatcher: The lookup mechanism will apparently try to find it
directly and fail, generating an AttributeError; next it will call
__getattr__ to find the attribute.  So far, no problems.

But I used a property much like this:

>>> import asyncore
>>> class Peer(asyncore.dispatcher):
...     def _get_foo(self):
...         # caused by a bug, several stack levels deeper
...         raise AttributeError('hidden!')
...     foo = property(_get_foo)
...

and as the error message suggests, the original AttributeError is
hidden by the lookup mechanism:

>>> Peer().foo
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/asyncore.py", line 366, in __getattr__
    return getattr(self.socket, attr)
AttributeError: 'NoneType' object has no attribute 'foo'

Is there anything that can be done about this?  If there are no better
solutions, perhaps the documentation for property() could point out
this pitfall?

- Thomas

-- 
If you want to reply by mail, substitute my first and last name for
'foo' and 'bar', respectively, and remove '.invalid'.



More information about the Python-list mailing list