traceback as string

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Dec 16 11:46:11 EST 2003


>>>>> "John" == John Hunter <jdhunter at ace.bsd.uchicago.edu> writes:

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

    John> def exception_to_str(s = None):

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

Two problems, I forget to use the file kwarg and called print_stack
instead of print_exc.

This seems to do what I want

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






More information about the Python-list mailing list