Is there a way of better tracback reporting??
ed_tsang at yahoo.com
ed_tsang at yahoo.com
Thu May 3 17:17:20 EDT 2001
hi I currentyl have the foloowing situation:
I am trying to execute a python function in file inc.py, from try.py.
I also want this function execution can be turned on or off at
try.py, without commenting any code or changing any code other than
the ENABLE_ME flag.
So I include a file pix.py that has a varable ENABLE_ME, which can be
enabled or diabled, from file pix.py.
Since the attached code is the simplified version, the opeation is
not continuous and can't show the user to enable or diable some
function execution at run time.
My problem is I want to print the traceback information just in case
something like: the function to be executed cannot be found in
inc.py. Although I am able to print some info, it does not specify me
the error is in inc.py instead it say from try.py during exec(cmd).
This may mislead user that the try.py is the one in question. How can
I point out the error is actually from inc.py???
Thanks
Here is the attached code:
file inc.py:
def a():
print "A is executed\n"
def b():
print "B is executed\n"
file pix.py:
from inc import *
ENABLE_ME = 1
file try.py:
try:
import traceback, sys
exec('import pix')
exec('reload(pix)')
if hasattr(pix,'ENABLE_ME') > 0:
if pix.ENABLE_ME == 1:
cmd = '%s.%s'%('pix','a()')
exec(cmd)
print "hihiht"
cmd = '%s.%s'%('pix','b()')
exec(cmd)
cmd = '%s.%s'%('pix','c()')
exec(cmd)
except SyntaxError:
exc = sys.exc_info()
traceback.print_exception(exc[0],exc[1],exc[2])
#traceback.print_exc()
Results:
A is executed
B is executed
Traceback (innermost last):
File "try.py", line 15, in ?
exec(cmd)
File "<string>", line 1, in ?
AttributeError: c
See the error is saying in exec(cmd) in try.py but the real problem
is there is no c function in inc.py .... :(
I want to generalise this to include any type of error hapeend when
executing functions in inc.py.
More information about the Python-list
mailing list