How can an Exception pass over an "except" clause ?

Gary Herron gherron at islandtraining.com
Wed May 30 12:24:59 EDT 2007


Nebur wrote:
> I'm using the contract.py library, running Python 2.4.4.
>
> Now I'm confronted with the following exception backtrace:
>  (...)
>   File "/usr/lib/python2.4/site-packages/contract.py", line 1265, in
> _check_preconditions
>     p = f.__assert_pre
> AttributeError: 'function' object has no attribute '__assert_pre'
>
> For my surprise, I found that the code of contract.py around line 1265
> looks like:
>
> 1264: try:
> 1265:     p = f.__assert_pre
> 1266: except AttributeError:
> 1267:     pass
>
> I'd expect line 1267 to "swallow" the AttributeError siliently. But
> the application stops with the above backtrace.
> Someone familiar enough with the Python innards ? How can one manage
> that an "except" seems to be ignored ?
>
>  Ruben
>
>   
Under any normal circumstances, that code can't produce that error.  
The attribute error will be swallowed by the except clause. 

However by being *VERY* perverse, I was able to reproduce the above
error by overwriting AttributeError with some other exception class (say
SyntaxError):
    AttributeError = SyntaxError
Then your code will be will produce a real AttributeError, but miss it
because (despite the spelling) it checks for a SyntaxError,

Question...  I don't know what contract.py is, but could it be doing
something that *bad*?
You could check py printing AttributeError and see what it really is. 
In my case it's:
    >>> print AttributeError
    exceptions.SyntaxError


Gary Herron

P.S.:  Anyone who does this kind of thing is a danger to society.  May
their finger fall off before they get a chance to distribute such a program.





More information about the Python-list mailing list