Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,16 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets
if tb is None:
try:
if hasattr(sys, 'last_exc'):
tb = sys.last_exc.__traceback__
exc = sys.last_exc
tb = exc.__traceback__
if isinstance(exc, SyntaxError):
raise RuntimeError("can't disassemble a SyntaxError")
else:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError("no last traceback to disassemble") from None


while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti, file=file, show_caches=show_caches, adaptive=adaptive, show_offsets=show_offsets, show_positions=show_positions)

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,16 @@ def test_distb_empty(self):
with self.assertRaises(RuntimeError):
dis.distb()

def test_distb_syntax_error(self):
try:
compile("???", "", "exec")
except SyntaxError as e:
sys.last_exc = e
sys.last_exc.__traceback__ = None

with self.assertRaises(RuntimeError):
dis.distb()

def test_distb_last_traceback(self):
self.maxDiff = None
# We need to have an existing last traceback in `sys`:
Expand Down
Loading