Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a complete exception stack trace in Python

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 
like image 424
Gordon Wrigley Avatar asked May 22 '11 09:05

Gordon Wrigley


People also ask

How do you capture a stack trace in Python?

Print Stack Trace in Python Using traceback Module The traceback. format_exc() method returns a string that contains the information about exception and stack trace entries from the traceback object. We can use the format_exc() method to print the stack trace with the try and except statements.

How do I get exception details in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

How do I print a stack trace error?

The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.


1 Answers

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).

like image 152
Tobias Kienzler Avatar answered Sep 20 '22 20:09

Tobias Kienzler