1

Is there a way to teach Python (3.x) to show the whole stack trace of an exception? Example:

def foo(n):
    try:
        1/n
    except Exception as x:
        return x
    else:
        return foo(n-1)

def bar(x):
    raise x

bar(foo(3))

emits

Traceback (most recent call last):
  File "/tmp/test.py", line 17, in <module>
    bar(foo(3))
  File "/tmp/test.py", line 15, in bar
    raise x
  File "/tmp/test.py", line 7, in foo
    1/n
ZeroDivisionError: division by zero

Unfortunately the call context of foo() is suppressed. Adding traceback.print_stack(x.__traceback__.tb_frame) to bar() prints

  File "/tmp/test.py", line 18, in <module>
    bar(foo(3))
  File "/tmp/test.py", line 13, in foo
    return foo(n-1)
  File "/tmp/test.py", line 13, in foo
    return foo(n-1)
  File "/tmp/test.py", line 13, in foo
    return foo(n-1)
  File "/tmp/test.py", line 5, in foo
    return x

which demonstrates that the information isn't lost. I'd like to convince Python to include these lines when it prints the error.

Matthias Urlichs
  • 2,301
  • 19
  • 29

0 Answers0