get_traceback

half.italian at gmail.com half.italian at gmail.com
Mon Jun 4 22:03:04 EDT 2007


On Jun 4, 3:51 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Mon, 04 Jun 2007 14:23:00 -0300, <half.ital... at gmail.com> escribió:
>
> > Is there a function or idoim for returning an exception/traceback
> > rather than just printing it to stdout?  I'm running a deamon where
> > stdout is going to /dev/null, and I'm not even watching it..until
> > now.  All the functions I found in traceback and sys seemed only to
> > print the error rather than just returning it, so I resorted to this:
>
> Read again the docs for the traceback module.
> Maybe you are looking for traceback.format_exception(*sys.exc_info()) or  
> traceback.format_exc()
>
> >    f = tempfile.TemporaryFile(mode='w+')
> >    sys.stdout = f
> >    traceback.print_tb(sys.exc_info()[2])
>
> In this case you can use StringIO instead of a temporary file, and the  
> file argument to print_tb instead of swapping sys.stdout
>
> --
> Gabriel Genellina

Thanks Gabriel.  That was exactly what I was looking for.  Also, I'm
glad to make a connection to the StringIO class.  I'm sure I will
remember it the next time I need it.

~Sean




More information about the Python-list mailing list