traceback as string

Discussion in 'Python' started by John Hunter, Dec 16, 2003.

  1. John Hunter

    John Hunter Guest

    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
    John Hunter, Dec 16, 2003
    #1
    1. Advertising

  2. John Hunter

    Colin Brown Guest

    "John Hunter" <> wrote in message
    news:...
    >
    > 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
    Colin Brown, Dec 16, 2003
    #2
    1. Advertising

  3. John Hunter

    Stephen Ferg Guest

    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)

    ====================================================
    Stephen Ferg, Jan 15, 2004
    #3
  4. John Hunter

    John J. Lee Guest

    (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
    John J. Lee, Jan 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Harris

    IDLE traceback on sys.exit()

    Dave Harris, Nov 2, 2003, in forum: Python
    Replies:
    0
    Views:
    373
    Dave Harris
    Nov 2, 2003
  2. Oliver Walczak

    AW: traceback as string

    Oliver Walczak, Dec 18, 2003, in forum: Python
    Replies:
    1
    Views:
    339
    Bengt Richter
    Dec 18, 2003
  3. Sean Schertell
    Replies:
    2
    Views:
    376
    Chris Mellon
    Jan 17, 2007
  4. Gary Herron
    Replies:
    0
    Views:
    422
    Gary Herron
    Jan 17, 2007
  5. Jack Bates
    Replies:
    0
    Views:
    276
    Jack Bates
    May 2, 2011
Loading...

Share This Page