473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

May 30 '07 #1
5 1779
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
The 'raise' in line 1271 re-raises the last error instead of the exception
in whose block it is called.

This:
try:
raise KeyError
except:
try:
raise IndexError
except: pass
raise

raises IndexError, not KeyError.

--

Regards,
Tijs
May 30 '07 #2
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.
May 30 '07 #3
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,
Yes ... this would be some kind of criminal joke
>
Question... I don't know what contract.py is, but could it be doing
something that *bad*?
No. I've searched the source for "AttributeError" and it appears only
in except clauses.
contracty.py is a library that adds Eiffel-like "design-by-
contract" (DBC) to Python. Precisely, I can add preconditions (and
postconditions) about the arguments into the methods docstring. These
are checked at runtime (and appear in the epydoc docu.) This is a
great thing I never want to miss anymore (and it was working fine for
some months now.)
(See http://www.wayforward.net/pycontract/ )
When the problem appears, contract.py is doing a pre-condition check.
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.
:-)

@Tijs: I think when re-raising, the backtrace will always point to the
line where it was re-raised but not to line 1265. (Or can we re-raise
an exception so that it preserves the backtrace of the "original"
exception?)

---
I'm speculating about there's a misleading backtrace. Maybe another
exception happens, but somehow using the obsolete exc_info of the last
exception (the AttributeError). I remember about some way to clear the
exc_info, maybe this must be added into contract.py? I'll find it out,
or a debugger session this night will help (I'll post again)
Ruben
May 30 '07 #4
>
@Tijs: I think when re-raising, the backtrace will always point to the
line where it was re-raised but not to line 1265. (Or can we re-raise
an exception so that it preserves the backtrace of the "original"
exception?)
@Tijs: Because of distrusting my own text above, I've checked re-
raising ... and indeed, you've been right. "raise" does _not_ produce
a new backtrace but uses the old one. Learned something new about
Python now... I think that clearifies all the "magic" behaviour.
Thank you,
Ruben

May 30 '07 #5
En Wed, 30 May 2007 15:30:43 -0300, Nebur <no****************@gmx.de>
escribió:
@Tijs: I think when re-raising, the backtrace will always point to the
line where it was re-raised but not to line 1265. (Or can we re-raise
an exception so that it preserves the backtrace of the "original"
exception?)
I think Tijs is right. raise (with no arguments) will raise the *last*
exception, not the one that was active when you entered the except clause;
it preserves the traceback.
Either replace the inner try/except using getattr, or save the full
exception details (including traceback) and use the 3-argument form of the
raise statement.
See http://docs.python.org/ref/raise.html

--
Gabriel Genellina

May 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1711
by: Raymond Arthur St. Marie II of III | last post by:
Del's "except"ional PEP Rejection Ladieees and Gentilmen and Pyth-O-neers of all ages. Step right up. Don't be shy. Come one come all. Be the first on your block TO GROK *THE* one *THE* only...
3
2475
by: George Young | last post by:
I have a bunch of blanket "except:" clauses like: class DatabaseError(StandardError): pass class OperationalError(StandardError): pass try: stuff except _pg.error, msg: raise...
0
1135
by: Bob Alistar | last post by:
Hello! I have an IVR server controlling Dialogic hardware through a DLL exposing the C API to Python. The server farms out incoming calls to python scripts based on the telephone number that...
7
6370
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
3
2164
by: Ernesto | last post by:
Within the scope of one Python file (say myFile.py), I'd like to print a message on ANY exception that occurs in THAT file, dependent on a condition. Here's the pseudocode: if...
2
1385
by: AWasilenko | last post by:
I can't figure out this problem Im having, I just can't understand why it is ignoring the call I put in. First the code (This is a cherrypy website): import sys, cherrypy, html class Root:...
0
1375
by: egur | last post by:
Hi, I'm looking for reliable MySQL version (for Windows) that supports "EXCEPT" and "INTERSECT" execution. Would somebody recommend such version (and corresponding client), please?
3
1510
by: Adam W. | last post by:
I am trying to handle a Unicode error but its acting like the except clause is not even there. Here is the offending code: def characters(self, string): if self.initem: try:...
4
12198
by: GLSmyth | last post by:
I need to select cells from one table that do not appear in a second table. I know that this can be done in some flavors of SQL by using Except: Select Unit_PK From Table1 Except Select Unit_PK...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.