How to get a complete exception stack trace in Python
Asked Answered
M

4

44

The following snippet:

import traceback

def a():
    b()

def b():
    try:
        c()
    except:
        traceback.print_exc()

def c():
    assert False

a()

Produces this output:

Traceback (most recent call last):
  File "test.py", line 8, in b
    c()
  File "test.py", line 13, in c
    assert False
AssertionError

What should I use if I want the complete stack trace including the call to a?

If it matters I have Python 2.6.6

edit: What I'd like to get is the same information I'd get if I left the try except out and let the exception propagate to the top level. This snippet for example:

def a():
    b()

def b():
    c()

def c():
    assert False

a()

Produces this output:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    a()
  File "test.py", line 2, in a
    b()
  File "test.py", line 5, in b
    c()
  File "test.py", line 8, in c
    assert False
AssertionError
Martelli answered 22/5, 2011 at 9:5 Comment(1)
I've now noticed this question. I answered a very similar one yesterday, and the answer seems pertinent here. It has the additional advantage that it produces a real traceback which you can not only print with the traceback functions, but also pass to the logging module or elsewhere.Medic
C
26

I don't know if there is a better way, but here's what I did:

import traceback
import sys

def format_exception(e):
    exception_list = traceback.format_stack()
    exception_list = exception_list[:-2]
    exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
    exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))

    exception_str = "Traceback (most recent call last):\n"
    exception_str += "".join(exception_list)
    # Removing the last \n
    exception_str = exception_str[:-1]

    return exception_str

def main1():
    main2()

def main2():
    try:
        main3()
    except Exception as e:
        print "Printing only the traceback above the current stack frame"
        print "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
        print
        print "Printing the full traceback as if we had not caught it here..."
        print format_exception(e)

def main3():
    raise Exception()

if __name__ == '__main__':
    main1()

And here's the output I get:

Printing only the traceback above the current stack frame
Traceback (most recent call last):
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception


Printing the full traceback as if we had not caught it here...
Traceback (most recent call last):
  File "exc.py", line 34, in <module>
    main1()
  File "exc.py", line 18, in main1
    main2()
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception
Conlin answered 21/9, 2012 at 23:21 Comment(2)
I'm accepting this as it's the only answer that actually solves the problem. However I'm still sad there there is no library call for this.Martelli
+1 - I posted a similar answer which also works outside except blocksUte
U
38

Here's a function based on this answer. It will also work when no exception is present:

def full_stack():
    import traceback, sys
    exc = sys.exc_info()[0]
    stack = traceback.extract_stack()[:-1]  # last one would be full_stack()
    if exc is not None:  # i.e. an exception is present
        del stack[-1]       # remove call of full_stack, the printed exception
                            # will contain the caught exception caller instead
    trc = 'Traceback (most recent call last):\n'
    stackstr = trc + ''.join(traceback.format_list(stack))
    if exc is not None:
         stackstr += '  ' + traceback.format_exc().lstrip(trc)
    return stackstr

print full_stack() will print the full stack trace up to the top, including e.g. IPython's interactiveshell.py calls, since there is (to my knowledge) no way of knowing who would catch exceptions. It's probably not worth figuring out anyway...

If print full_stack() is called from within an except block, full_stack will include the stack trace down to the raise. In the standard Python interpreter, this will be identical to the message you receive when not catching the exception (Which is why that del stack[-1] is there, you don't care about the except block but about the try block).

Ute answered 16/5, 2013 at 14:4 Comment(4)
Ah, I realize this is very similar to the accepted answer except that it treats non-except blocks cleanerUte
I slightly prefer your approach. Slicing the well-structured call stack is cleaner than slicing an unstructured list of lines. Thanks for the Pythonic alternative!Ojeda
Elegant solution! However, I think you are misusing lstrip() as it will remove any char presents in trc, which may lead to unexpected truncated stack. Prefered way would be for example if stackstr.startswith(trc): stackstr = stackstr.replace(trc, '', 1).Giordano
@Giordano Thanks, and good point - though since exc is not None I guess one could even have just stackstr += ' ' + traceback.format_exc()[len(trc):] since it'll always start with trc.Ute
C
26

I don't know if there is a better way, but here's what I did:

import traceback
import sys

def format_exception(e):
    exception_list = traceback.format_stack()
    exception_list = exception_list[:-2]
    exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
    exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))

    exception_str = "Traceback (most recent call last):\n"
    exception_str += "".join(exception_list)
    # Removing the last \n
    exception_str = exception_str[:-1]

    return exception_str

def main1():
    main2()

def main2():
    try:
        main3()
    except Exception as e:
        print "Printing only the traceback above the current stack frame"
        print "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
        print
        print "Printing the full traceback as if we had not caught it here..."
        print format_exception(e)

def main3():
    raise Exception()

if __name__ == '__main__':
    main1()

And here's the output I get:

Printing only the traceback above the current stack frame
Traceback (most recent call last):
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception


Printing the full traceback as if we had not caught it here...
Traceback (most recent call last):
  File "exc.py", line 34, in <module>
    main1()
  File "exc.py", line 18, in main1
    main2()
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception
Conlin answered 21/9, 2012 at 23:21 Comment(2)
I'm accepting this as it's the only answer that actually solves the problem. However I'm still sad there there is no library call for this.Martelli
+1 - I posted a similar answer which also works outside except blocksUte
W
12

Use

 traceback.print_stack()

http://docs.python.org/library/traceback.html#traceback.print_stack

suxmac2 $ python out.py 
  File "out.py", line 16, in <module>
    a()
  File "out.py", line 5, in a
    b()
  File "out.py", line 11, in b
    traceback.print_stack()
Weatherbound answered 22/5, 2011 at 9:16 Comment(5)
so that's providing the other half, is there a way to get the whole thing as one piece?Martelli
Call your own code later...nobody hinders you to call print_stack() and print_exc() yourself - or?Weatherbound
traceback.print_exception(*sys.exc_info())Raffish
@Raffish that has the same effect as traceback.print_exc()Martelli
I could call them both and merge the output, but it seems unlikey that the standard library doesn't cover this already, in essence I just want the same information that I'd get if I let the exception propagate to the top levelMartelli
C
6


Here is a bit better variant of Tobias Kienzler answer. It works same, but can be called not right in except block, but somewhere deeper. In other words, this variant will print same stacks, when called like

try:
   ...
except Exception:
    print full_stack()

or

def print_full_stack():
    print full_stack()

try:
   ...
except Exception:
    print_full_stack()

Here is code:

def full_stack():
    import traceback, sys
    exc = sys.exc_info()[0]
    if exc is not None:
        f = sys.exc_info()[-1].tb_frame.f_back
        stack = traceback.extract_stack(f)
    else:
        stack = traceback.extract_stack()[:-1]  # last one would be full_stack()
    trc = 'Traceback (most recent call last):\n'
    stackstr = trc + ''.join(traceback.format_list(stack))
    if exc is not None:
        stackstr += '  ' + traceback.format_exc().lstrip(trc)
    return stackstr
Courcy answered 12/11, 2017 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.