all Python 3disfunction
dis.distb()
disassembly
Disassembles the frame where an exception was raised and marks the failing instruction with a --> arrow.
Syntax
dis.distb(tb=None, *, file=None)
Without an argument it uses sys.last_traceback, which is set only in an interactive session after an unhandled error — in a script distb() raises RuntimeError "no last traceback to disassemble". Inside except pass the traceback explicitly: sys.exc_info()[2] or e.__traceback__. The arrow tells you which operation of a long line failed — say, which of several subscripts went out of range.
Examples
import dis, io
try:
[1, 2][5]
except IndexError as e:
buf = io.StringIO()
dis.distb(e.__traceback__, file=buf)
print('-->' in buf.getvalue()) # → True
import dis
try:
dis.distb()
except RuntimeError as e:
print(e) # → no last traceback to disassemble
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy