all Python 3tracebackfunction
traceback.extract_tb()
frames and stack
Turns an exception's traceback into a StackSummary — a list of frames with file, line and function that is easy to inspect from code.
Syntax
traceback.extract_tb(tb, limit=None)
It takes the traceback object e.__traceback__, not the exception: extract_tb(e) fails with AttributeError. Frames go from the outer one to the failure point: [-1] is the line that raised, [0] is the frame where it was caught. The traceback only covers the path from the try to the error, not the whole program stack: frames above the function holding the try are not in it — extract_stack() shows those.
Examples
import traceback
def parse(s):
return int(s)
try:
parse('x')
except ValueError as e:
frames = traceback.extract_tb(e.__traceback__)
print([(f.name, f.lineno) for f in frames]) # → [('<module>', 5), ('parse', 3)]
import traceback
try:
1 / 0
except ZeroDivisionError as e:
traceback.extract_tb(e) # → AttributeError: 'ZeroDivisionError' object has no attribute 'tb_frame'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy