Python 3.5+tracebackterm
traceback.StackSummary
frames and stack
A list of FrameSummary frames returned by extract_tb() and extract_stack(); its format() method turns it into traceback lines.
Syntax
traceback.StackSummary.extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)
It is a list subclass: slicing, len() and indexing work as usual. format() yields the same lines as a normal traceback and collapses repeats: deep recursion gives one "[Previous line repeated N more times]" line instead of a thousand identical frames. StackSummary.extract(walk_tb(tb), capture_locals=True) also records the repr of every frame's local variables — handy for crash reports, but an easy way to leak passwords and tokens into a log.
Examples
import traceback
def f(n):
return f(n - 1) if n else 1 / 0
try:
f(10)
except ZeroDivisionError as e:
stack = traceback.extract_tb(e.__traceback__)
print(isinstance(stack, list), len(stack)) # → True 12
print(stack.format()[-1].strip()) # → [Previous line repeated 8 more times]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy