Exception not raised

M

Michele Petrazzo

Hi list, I have a strange error on my software on win 2k/xp and debian
3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython:

python, on a piece of code doesn't raise a KeyError on a dict (that
don't have that key), but the strange thing is that the try/except code
see that exception. Other strange thing is that other exceptions are raised!

Simple example extract from my code:

#code
def test():
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"

print "ok"
test()
print "ok"
#end code

Output:

ok
<type 'dict'> False

Here I see only one "ok" and not the "I'm here". The interpreter stop to
run here, but the application continue to work!

Other try:

#code
def test():
try:
print type(t_fields), 11 in t_fields
print t_fields[11]
except KeyError, ex:
print "Error", ex
#end code

Output:

ok
<type 'dict'> False
Error 11
ok

Here the output is ok, so python see that exception inside the
try/except and print it.

Last try:

#code
def test()
print type(t_fields), 11 in t_fields
print dont_exist
print t_fields[11]
#end code

Output:

ok
<type 'dict'> False
File "conn.py", line 231, in test
print dont_exist
NameError: global name 'dont_exist' is not defined


So all the exception are raised except the KeyError outside the try/except!

I don't know what can be.

Thanks to all that can help me.
Michele
 
D

Diez B. Roggisch

Michele said:
Simple example extract from my code:

#code
def test():
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"

print "ok"
test()
print "ok"
#end code

Output:

ok
<type 'dict'> False

Here I see only one "ok" and not the "I'm here". The interpreter stop to
run here, but the application continue to work!


Not here:

t_fields = {}
#code
def test():
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"

print "ok"
test()
print "ok"
#end code

Gives me

python2.4 /tmp/test.py
ok
<type 'dict'> False
Traceback (most recent call last):
File "/tmp/test.py", line 9, in ?
test()
File "/tmp/test.py", line 5, in test
print t_fields[11]
KeyError: 11


So - whatever you do, there must be some other code capturing that
exception. Maybe you don't use dict, but a subclass of it?

Diez
 
L

Larry Bates

Michele said:
Hi list, I have a strange error on my software on win 2k/xp and debian
3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython:

python, on a piece of code doesn't raise a KeyError on a dict (that
don't have that key), but the strange thing is that the try/except code
see that exception. Other strange thing is that other exceptions are
raised!

Simple example extract from my code:

#code
def test():
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"

print "ok"
test()
print "ok"
#end code

Output:

ok
<type 'dict'> False

Here I see only one "ok" and not the "I'm here". The interpreter stop to
run here, but the application continue to work!

Other try:

#code
def test():
try:
print type(t_fields), 11 in t_fields
print t_fields[11]
except KeyError, ex:
print "Error", ex
#end code

Output:

ok
<type 'dict'> False
Error 11
ok

Here the output is ok, so python see that exception inside the
try/except and print it.

Last try:

#code
def test()
print type(t_fields), 11 in t_fields
print dont_exist
print t_fields[11]
#end code

Output:

ok
<type 'dict'> False
File "conn.py", line 231, in test
print dont_exist
NameError: global name 'dont_exist' is not defined


So all the exception are raised except the KeyError outside the try/except!

I don't know what can be.

Thanks to all that can help me.
Michele

When I run your first example I get:
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\larry\My Documents\junk.py", line 14, in ?
test()
File "C:\Documents and Settings\larry\My Documents\junk.py", line 8, in test
print t_fields[11]
KeyError: 11
I don't know why you see anything different than a traceback when you
try to access t_fields[11] (which doesn't exist).

-Larry Bates
 
M

Michele Petrazzo

Diez said:
Not here:

t_fields = {}
#code
def test():
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"

print "ok"
test()
print "ok"
#end code

Gives me

KeyError: 11

Also on my environ when I try this 4 line code. My project, where that
strangeness happen has 500/600 + K of code.
So - whatever you do, there must be some other code capturing that
exception.

This code are inside a method into class that have no try/except. And
called from a method inside a wx.Frame derivate. The other strange thing
is that if I try the same code just before the "caller" to that method,
it raise an exception:

#code extract alway from the big project:

class errorHappen(object):
def methodError(self, t_fields):
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"


def myF(wx.Frame):
.... init ....
self._e = errorHappen()

def caller(self):
d = dict(dictionary with 50/100 keys)
#if here I try d[11], I'll KeyError
self._e.methodError(d) #here I don't see the exeception

Maybe you don't use dict, but a subclass of it?

All my dict are created as
d = dict()

and populate by: d.update(other_dict) or for k, val in iter: d[k] = val


Thanks,
Michele
 
M

Michele Petrazzo

Michele said:
Hi list, I have a strange error on my software on win 2k/xp and
debian 3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython:

Just for give evidence to my _failed_ tests, my a debugger (eric3), it
see the exception, so it break with a KeyError!
And the same code, no!

Thanks,
Michele
 
M

Michele Petrazzo

Opss, I forgot some words :)
Just for give evidence to my _failed_ tests, my a debugger (eric3), it

with a debugger (not my a debugger)
see the exception, so it break with a KeyError!
And the same code, no!

And the same code, exceute inside a terminal, no...
 
D

Diez B. Roggisch

This code are inside a method into class that have no try/except. And
called from a method inside a wx.Frame derivate. The other strange thing
is that if I try the same code just before the "caller" to that method,
it raise an exception:

So maybe the C-layer of wx in-between doesn't propagate the exception for whatever reason? Fact is: pythons
exception-handling is core part of the language and an error in there _extremely_ improbable . So I suggest you cut down
your example until it is self-contained with only a few dozen lines and still produces your observed behavior. Otherwise
no one will be able to help you.

Diez
 
M

Michele Petrazzo

Diez said:
So maybe the C-layer of wx in-between doesn't propagate the exception
for whatever reason?

Can be, but only that exception aren't raised, all the other, in the
same point of code, yes.
Fact is: pythons exception-handling is core part of the language and
an error in there _extremely_ improbable .

I think the same, and this is one of the reason that push me to use python.
So I suggest you cut down your example until it is self-contained
with only a few dozen lines and still produces your observed
behavior. Otherwise no one will be able to help you.

Into my 100 line code, that exception (and all the others) are raised! I
don't able, into all my tries, to reproduce that error on a small code...

I can publish my code, if someone has one hour of spare time and a
postgresql where test the code. I'm make some instruction to reproduce it.

Thanks,
Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top