473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3603
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fowlertrainer | last post by:
Hello python-list, This is a thread's run method. def run(self): try: page=urllib.urlopen(base) src=page.read() page=None self.ResultCode=0
1
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self):...
0
by: Ehab Teima | last post by:
Hello, I have built a server application. It's multithreaded, and everything works fine so far. I have only one issue with catching exceptions after I read the documentation but I can't find any...
1
by: Michael P. Soulier | last post by:
Hello, For a GUI app I've tried resetting sys.excepthook to my own exceptionhandler bound method, which accepts a type, value and traceback object. Now, the traceback module has print_exc...
2
by: Philippe Martin | last post by:
Hi, Is there a way to catch traceback.print_exc() output into a string ? Philippe
4
by: billiejoex | last post by:
Hi there, I'm facing a case where I need to get the traceback outptut when occurring an exception. I solved such problem by using traceback module in conjunction with StringIO: import...
8
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python...
2
by: Sami | last post by:
Hello, In the Python book that I am using to learn the language it says that the traceback.print_exc() can be used to stop exception propagation and make the program keep running. Here is a...
0
by: Gabriel Genellina | last post by:
En Mon, 26 May 2008 05:31:27 -0300, <Dominique.Holzwarth@ch.delarue.comescribió: Don't inherit from Exception - you should be able to log *any* exception, not only this specific one, I presume?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.