Skip to main content

traceback

The traceback module formats Python tracebacks for display. It walks __traceback__ frame chains, collects source context, applies PEP 657 column highlights, and renders the result CPython users expect from sys.excepthook.

Source-of-record: Lib/traceback.py, traceback docs.

Printing​

FunctionEffect
print_tb(tb, limit=None, file=None)Walk frames from oldest to newest.
print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, file=None, chain=True)Full exception report.
print_exc(limit=None, file=None, chain=True)The active exception.
print_last(limit=None, file=None, chain=True)sys.last_exc.
print_stack(f=None, limit=None, file=None)Stack of frame f (or caller).

Formatting​

FunctionReturns
format_tb(tb, limit=None)List of formatted strings.
format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, chain=True)List.
format_exception_only(exc, /, value=_sentinel, *, show_group=False)List, no traceback.
format_exc(limit=None, chain=True)Single string for the active exc.
format_list(extracted_list)Pretty FrameSummary list.
format_stack(f=None, limit=None)Pretty stack list.
extract_tb(tb, limit=None)StackSummary of FrameSummary.
extract_stack(f=None, limit=None)StackSummary from f.
clear_frames(tb)Drop local references from each frame.
walk_tb(tb) / walk_stack(f)Iterator of (frame, lineno).

Objects​

ClassRole
TracebackExceptionPre-extracted exception suitable for serialisation.
StackSummaryList of FrameSummary with formatting methods.
FrameSummaryfilename, lineno, name, line, locals, colno, end_colno.

TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False, max_group_width=15, max_group_depth=10).

MethodReturns
format(*, chain=True, _ctx=None)Generator of strings.
format_exception_only(*, show_group=False)List of strings.
print(*, file=None, chain=True)Print to file.

Cause and context​

A traceback rendering walks __cause__ first (PEP 3134 explicit raise X from Y), then __context__ (implicit), then the exception itself. The frames between are rendered. PEP 654 exception groups recursively render sub-groups.

Anchors and column highlights​

PEP 657 stores precise locations on the code object. The renderer underlines the offending span. TracebackException.format emits the anchor line beneath the source line.

Gopy status​

AreaState
Print and format APIsComplete.
TracebackException round-tripComplete.
Exception group renderingComplete.
PEP 657 anchorsComplete.
capture_localsComplete.

Reference​

  • CPython 3.14: traceback.
  • Lib/traceback.py.
  • module/traceback/. gopy port.