returning traceback strings

Chris Liechti cliechti at gmx.net
Thu Jun 13 18:50:09 EDT 2002


Matthew Boedicker <mboedick at mboedick.org> wrote in
news:mailman.1024006339.11571.python-list at python.org: 
> 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?

try that:
  ''.join(traceback.format_exception(*sys.exc_info()))

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list