tkinter shell problem

P

Philippe C. Martin

Hi,

I have the following problem:

I wrote a tkinter shell which on a key return event first evals the input
buffer (in a try: except:) and then, in case of except, execs the input
buffer.


I have the problem if the exec fails:

If I trap it, I do not get the stack dump that I which I would show.

ex:

% import dontexist

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
import dontexist
ImportError: No module named dontexist


If I don"t trap it, then my clean up code which is after the exec obviously
does not get called (i.e, clear my input buffer, save the history, print a
prompt .......)

Yet Idle manages - any clue ?


Regards,

Philippe
 
E

Eric Brunel

Philippe said:
Hi,

I have the following problem:

I wrote a tkinter shell which on a key return event first evals the input
buffer (in a try: except:) and then, in case of except, execs the input
buffer.


I have the problem if the exec fails:

If I trap it, I do not get the stack dump that I which I would show.

Why? When catching an exception, you can get the exception type, the exception
itself and the traceback object via the sys.exc_info() function. You can then
use the traceback module to format the traceback just as Python would print it
if the exception went to the top-most level.

HTH
 
R

Russell E. Owen

"Philippe C. Martin said:
Hi,

I have the following problem:...
If I trap (an exception), I do not get the stack dump that I which I would show.
...
If I don"t trap it, then my clean up code...does not get called...

Yet Idle manages - any clue ?

Use the traceback module. For example:

try:
...code to protect...
except (SystemExit, KeyboardInterrupt):
raise
except Exception, e:
traceback.print_exc(file=sys.stderr)
....code to handle exception...

if you are sure none of your code can raise error exceptions that
inherit directly from Exception, you can simplify this to one except:
except StandardError, e:
Unfortunately, some common modules (including Tkinter) raise exceptions
that inherit from Exception, not StandardError.

-- Russell
 

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

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top