Overriding traceback print_exc()?

B

Bob Greschke

I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?

I kind of understand using the traceback module to alter the output of
exceptions that I am looking for, but I'm after those pesky ones that should
never happen. :)

Thanks!

Bob
 
F

Fredrik Lundh

Bob said:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine?

one way to do that is to put a big try/except around your main program,
and display the dialogue box in the except clause:

import traceback

def main():
raise RuntimeError("oops!")

try:
main()
except (KeyboardInterrupt, SystemExit):
raise
except:
print "ERROR", repr(traceback.format_exc())

another approach is to install an exit-handler that uses last_traceback
and friends to generate a traceback:

import atexit, traceback, sys

def postmortem():
if hasattr(sys, "last_traceback"):
print "ERROR", repr(traceback.format_exception(
sys.last_type, sys.last_value, sys.last_traceback
))

atexit.register(postmortem)

def main():
raise RuntimeError("oops!")

main()

the latter is less intrusive, and can be squirreled away in a support
module. also, the original exception is still reported to the console,
as usual.

</F>
 
D

dakman

You could always override sys.stderr with a instance of your own with a
write() method. Though you will still have to catch the exceptions.
 
Z

Ziga Seilnacht

Bob said:
I want to cause any traceback output from my applications to show up in one
of my dialog boxes, instead of in the command or terminal window (between
running on Solaris, Linux, OSX and Windows systems there might not be any
command window or terminal window to show the traceback messages in). Do I
want to do something like override the print_exc (or format_exc?) method of
traceback to get the text of the message and call my dialog box routine? If
that is right how do I do that (monkeying with classes is all still a grey
area to me)?

You can overwrite the sys.exepthook() with your own function:


import sys
from traceback import format_exception

def my_excepthook(exctype, value, traceback):
details = "".join(format_exception(exctype, value, traceback))
# now show the details in your dialog box

sys.excepthook = my_excepthook


See the documentation for details:
http://docs.python.org/lib/module-sys.html#l2h-5125

Hope this helps,
Ziga
 
D

draghuram

I usually have a function like this:

def get_excinfo_str():
"""return exception stack trace as a string"""

(exc_type, exc_value, exc_traceback) = sys.exc_info()
formatted_excinfo = traceback.format_exception(exc_type, exc_value,
exc_traceback)
excinfo_str = "".join(formatted_excinfo)

del exc_type
del exc_value
del exc_traceback

return(excinfo_str)


I can then call it from within "except" and print it to a log file.

Raghu.
 
S

Scott David Daniels

I usually have a function like this:

def get_excinfo_str():
"""return exception stack trace as a string"""
(exc_type, exc_value, exc_traceback) = sys.exc_info()
The parens here can be skipped:
exc_type, exc_value, exc_traceback = sys.exc_info()
formatted_excinfo = traceback.format_exception(exc_type, exc_value,
exc_traceback)
excinfo_str = "".join(formatted_excinfo)
del exc_type
del exc_value
del exc_traceback
The three del lines above don't do anything (the return decrefs the locals).
return(excinfo_str)
The parens here can be skipped as well:
return excinfo_str
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top