property() bug

Chuck Esterbrook ChuckEsterbrook at StockAlerts.com
Mon Oct 14 02:52:35 EDT 2002


I found a bug in both 2.2.2b1 and 2.2.1. Looking through the 
SourceForge Bugs Summaries, I didn't see it listed. Anyone have any 
comments on this before I add it to the list?

------------------------------------------------------------------------
class Good:
    def foo(self):
        # raises IOError as expected
        return open('lsadflkdsfkjsdf').read()
try:
    print Good().foo()
except IOError, e:
    pass  # as expected


class Bad:
    def getfoo(self):
        # should raise IOError, but lies and says AttributeError instead
        return open('lsadflkdsfkjsdf').read()
    foo = property(getfoo)
try:
    print Bad().foo
except IOError, e:
    pass  # expected, but doesn't happen
------------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 17, in ?
    print Bad().foo
AttributeError: Bad instance has no attribute 'foo'
------------------------------------------------------------------------

In summary, if you use properties instead of methods, then you lose 
your original exceptions. They all get converted to AttributeError.

This ruins any code that is expected to raise specific exceptions under 
certain circumstances: the invoking code will never get them or be able 
to distinguish them. It also makes debugging and development 
frustrating, as there is no information about what line of code in the 
property, or even deeper into the call stack, caused the exception and 
why.

I hope this can be fixed for 2.2.2 final.


-Chuck




More information about the Python-list mailing list