returning traceback strings

Matthew Boedicker mboedick at mboedick.org
Fri Jun 14 00:11:09 CEST 2002


Hello,

I am looking for something exactly like traceback.print_exc(), but instead
of writing the traceback text to a filehandle, I want it to return it to
me as a string.  I was unable to find any way to do this using the traceback
module, since everything there seems to be in terms of file handles being
passed around.

The problem is I need to pass the entire multi-line traceback string to a
file-like object's write() method in one call, not line by line.

This is the best workaround I was able to come up with:

import cStringIO
import traceback

def print_exc_str():
    buf = cStringIO.StringIO()
    traceback.print_exc(file=buf)
    return buf.getvalue()

Any suggestions for a better way?  What about the traceback module using
strings internally, and wrapping another layer around that to write to files
and file-like objects?

-- 
Matthew Boedicker http://mboedick.org





More information about the Python-list mailing list