Programming Search⌘K

Featureadvanced

Tracebacks and exception objects

Also called traceback module, __traceback__, traceback object, format_exc, TracebackException, extract_tb, with_traceback.

A caught exception holds its stack trace in __traceback__, a chain of traceback objects. The traceback module extracts, formats and prints it, and TracebackException keeps it for later.

Language
Python

Synopsis

import traceback

def parse(text):
    return int(text)

def load(lines):
    return [parse(s) for s in lines]

try:
    load(['1', 'x'])
except ValueError as e:
    for frame in traceback.extract_tb(e.__traceback__):
        print(frame.name, frame.line)
Output
<module> load(['1', 'x'])
load return [parse(s) for s in lines]
parse return int(text)

The exception keeps the stack from the handler down to the place of the error. extract_tb gives one entry for each level, with the function name and the source line.

Semantics

r1. "Traceback objects represent the stack trace of an exception. A traceback object is implicitly created when an exception occurs".1 "when the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback."1

r2. "When an exception handler is entered, the stack trace is made available to the program."1 It is "the traceback attribute of the caught exception".1 __traceback__ is "A writable field that holds the traceback object associated with this exception."3

r3. A traceback object has tb_frame, which "Points to the execution frame of the current level", and tb_lineno, which "Gives the line number where the exception occurred".1 "The special writable attribute tb_next is the next level in the stack trace (towards the frame where the exception occurred), or None if there is no next level."1 Example: walk.

r4. The traceback module "provides a standard interface to extract, format and print stack traces of Python programs."2 format_exception returns "a list of strings, each ending in a newline and some containing internal newlines. When these lines are concatenated and printed, exactly the same text is printed as does print_exception()."2

r5. TracebackException can "Capture an exception for later rendering."2 The module has "a utility for capturing enough information about an exception to print it later, without the need to save a reference to the actual exception. Since exceptions can be the roots of large objects graph, this utility can significantly improve memory management."2 Example: later.

r6. with_traceback(tb) "sets tb as the new traceback for the exception and returns the exception object."3

Examples

def inner():
    return 1 / 0

def outer():
    return inner()

try:
    outer()
except ZeroDivisionError as e:
    tb = e.__traceback__
    while tb is not None:
        print(tb.tb_frame.f_code.co_name, tb.tb_lineno)
        tb = tb.tb_next
Output
<module> 8
outer 5
inner 2

The chain starts at the frame of the handler and ends at the frame where the error happened (rule r3).

import traceback

def risky(n):
    if n == 0:
        raise ValueError('n must not be 0')
    return 10 // n

saved = []
for n in [5, 0]:
    try:
        risky(n)
    except ValueError as e:
        saved.append(traceback.TracebackException.from_exception(e))

for te in saved:
    print([f.name for f in te.stack])
    print(''.join(te.format_exception_only()), end='')
Output
['<module>', 'risky']
ValueError: n must not be 0

The list keeps a summary of the error, not the exception and its frames (rule r5).

Pitfalls

A saved exception keeps the local variables of its frames alive:

This code runs but gives a wrong answer on purpose.

import weakref

class Buffer:
    pass

errors = []

def work():
    data = Buffer()
    ref = weakref.ref(data)
    try:
        raise ValueError('failed')
    except ValueError as e:
        errors.append(e)
    return ref

ref = work()
print('buffer alive:', ref() is not None)
Output
buffer alive: True

work has returned, but errors holds the exception, the exception holds the traceback, and the traceback holds the frame with data (rule r3). The reference says that with the traceback, exceptions "form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs."4

import traceback
import weakref

class Buffer:
    pass

errors = []

def work():
    data = Buffer()
    ref = weakref.ref(data)
    try:
        raise ValueError('failed')
    except ValueError as e:
        errors.append(e)
    return ref

ref = work()
for e in errors:
    traceback.clear_frames(e.__traceback__)
print('buffer alive:', ref() is not None)
Output
buffer alive: False

clear_frames "Clears the local variables of all the stack frames in a traceback".2 The code calls it after work has returned. Another way is to keep a TracebackException instead of the exception (rule r5).

Comparison

A Go error value has no stack trace of its own. A panic prints the stack when the program stops, and a deferred function can stop the panic: "A call to recover stops the unwinding and returns the argument passed to panic."5 A Python exception carries its traceback, so a handler can print or keep the stack (rule r2).

References

  1. The Python Language Reference, 3. Data model, 3.2.13.3. Traceback objects
  2. traceback, Print or retrieve a stack traceback, introduction; extract_tb; format_exception; clear_frames; TracebackException
  3. Built-in Exceptions, BaseException.with_traceback; BaseException.__traceback__
  4. The Python Language Reference, 8. Compound statements, 8.4.1. except clause
  5. Effective Go, Recover

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source