Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > traceback as string

Reply
Thread Tools

traceback as string

 
 
John Hunter
Guest
Posts: n/a
 
      12-16-2003

What is the best way to get the traceback as a string. I tried

def exception_to_str(s = None):

sh = StringIO.StringIO()
if s is not None: print >>sh, s
traceback.print_stack(sh)
return sh.getvalue()


but got

Traceback (most recent call last):
File "/home/jdhunter/seizure/python/eegdb/eegview/eegview.py", line 944, in on_menuFileOpen_activate
msg = exception_to_str('Could not read data:')
File "/hunter/jdhunter/python/projects/jdh/jdh/cbook.py", line 707, in exception_to_str
traceback.print_stack(sh)
File "/usr/local/lib/python2.3/traceback.py", line 235, in print_stack
print_list(extract_stack(f, limit), file)
File "/usr/local/lib/python2.3/traceback.py", line 266, in extract_stack
lineno = f.f_lineno

Thanks!
John Hunter

 
Reply With Quote
 
 
 
 
Colin Brown
Guest
Posts: n/a
 
      12-16-2003

"John Hunter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> What is the best way to get the traceback as a string.


I define the following module "Excepts.py" for logging exceptions in daemon
processes:

------------------------------------------
import sys,traceback

def error():
tb =
traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info(
)[2])
return tb[len(tb)-1].replace('\n','')

def errorstack():
return
''.join(traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.e
xc_info()[2]))
-------------------------------------------

Colin Brown
PyNZ


 
Reply With Quote
 
 
 
 
Stephen Ferg
Guest
Posts: n/a
 
      01-15-2004
I haven't been using this function for very long, but it seems to work.

You pass it the exception object, and it returns a string.

================================================== =

def exception_format(e):
"""Convert an exception object into a string,
complete with stack trace info, suitable for display.
"""
import traceback
info = "".join(traceback.format_tb(sys.exc_info()[2]))
return str(e) + "\n\n" + info

================================================== ==

Here's an example of how to use it.

==================================================

try:
main1()
except Exception, e:
print exception_format(e)

================================================== ==
 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      01-16-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Stephen Ferg) writes:
[...]
> try:
> main1()
> except Exception, e:
> print exception_format(e)

[...]

Just a semi-random warning for anybody as stupid as me: "finally:"
blocks do not handle exceptions, so don't expect things like using the
traceback module to work there:

try:
main1()
finally:
print exception_format(e) # WRONG


I spent a long time tracing through the Python source code trying to
track down a non-existent bug in a C extension, thanks to this
misconception.


John
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
Newbie: Capture traceback message to string? Sean Schertell Python 2 01-17-2007 03:54 PM
Re: Newbie: Capture traceback message to string? Gary Herron Python 0 01-17-2007 02:19 AM
Re: ICMP traceback Terry Baranski Cisco 0 06-20-2004 02:06 PM
AW: traceback as string Oliver Walczak Python 1 12-18-2003 09:06 PM



Advertisments