[Python-3000] exception-swallowing behaviour of hasattr

Christian Heimes lists at cheimes.de
Tue Jan 22 08:14:20 CET 2008


Dwayne C. Litzenberger wrote:
> I'm bringing this up again because the arguments I've seen in favour of 
> fixing hasattr have been fairly weak, and I'd like to raise some stronger 
> ones.  Also, I haven't seen this issue considered specifically in the 
> context of Python 3000.

The behavior of hasattr and (IIRC) isinstance and issubclass has been a
problem for several applications. For instance Zope requires code to
re-raise ConflictErrors. ZODB conflict errors must bubble through the
program flow until it hits the ZODB transaction system so it can re-roll
the transaction.

> I propose a few alternate behaviours for hasattr (and their consequences):
> 
>      1. Swallow only AttributeError.
> 
>          - KeyboardInterrupt, SystemExit, and AssertionError are all passed 
>            through, solving all three of the above problems.
> 
>          - The behaviour of hasattr is made consistent with getattr(x,y,z).

I prefer 1) for Python 3.0

>      2. Swallow only Exception.
> 
>          - KeyboardInterrupt and SystemExit are passed through, but 
>            AssertionError is still swallowed.
> 
>          - Breaks less code than the previous option.

I think it's a sensible solution for Python 2.6.

Christian



More information about the Python-3000 mailing list