473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ 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_precondi tions
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 1816
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_precondi tions
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_precondi tions
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.Synt axError
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.Synt axError

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
1740
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 Exception you won't find
3
2506
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 DatabaseError, "error '%s' in '%s'" % ( msg, sql )
0
1156
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 was dialled, and these scripts are written by other people, sometimes in other companies, and quite often these people have no idea that a hangup event is actually quite hard to catch in the middle of a while1:pass. I had all sorts of alive/dead...
7
6395
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) == qaddrs+0x1000 and toks == "200": #producer write prod = int(toks, 16)
3
2187
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 anyExceptionOccurs(): if myCondition: print "Here's my global exception message"
2
1417
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: @cherrypy.expose def index(self, pageid = "Index"): selection = html.Page()
0
1407
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
1529
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: self.data.append(string.encode()) except: self.data.append('No habla la Unicode')
4
12348
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 From Table2 This would give me the Unit_PK cells in Table1 that do not exist in Table2. Microsoft Access apparently does not allow Except or Minus in the SQL statement, so I am wondering if this is possible some other way. Your help is...
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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 we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.