how to enable error message in exec??

Michael Hudson mwh21 at cam.ac.uk
Sun Apr 8 17:38:39 EDT 2001


ed_tsang at yahoo.com writes:

> Hi 
> 
> My current script, say script A, need to execute some python code in
> other file, say script B, by using exec() function. But if there is
> any syntax error in script B, the exec() returns, but I would not
> know what kind of syntax error is that. How can I enable this kind
> of execution gives the same level of error reporting capability as
> if the python code is run thorugh in the same file?

Does traceback.print_exc() do what you want?

Eg:

/>> try:
|..  exec "a()=1"
|.. except SyntaxError:
|..  print "hello"
|..  traceback.print_exc()
|..  print "bye"
\__ 
hello
Traceback (most recent call last):
  File "<input>", line 2, in ?
SyntaxError: can't assign to function call
bye

HTH,
M.

-- 
  ARTHUR:  Why should he want to know where his towel is?
    FORD:  Everybody should know where his towel is.
  ARTHUR:  I think your head's come undone.
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 7



More information about the Python-list mailing list