469,602 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Overriding traceback print_exc()?

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
Oct 31 '06 #1
5 3285
Bob Greschke wrote:
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>

Oct 31 '06 #2
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.

Bob Greschke wrote:
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
Oct 31 '06 #3
Bob Greschke wrote:
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

Oct 31 '06 #4
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.

Bob Greschke wrote:
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
Oct 31 '06 #5
dr*******@gmail.com wrote:
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
--
--Scott David Daniels
sc***********@acm.org
Nov 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by fowlertrainer | last post: by
1 post views Thread by Oliver Walczak | last post: by
reply views Thread by Ehab Teima | last post: by
1 post views Thread by Michael P. Soulier | last post: by
2 posts views Thread by Philippe Martin | last post: by
4 posts views Thread by billiejoex | last post: by
8 posts views Thread by gregpinero | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.