Skip to content

Commit 25c82e9

Browse files
committed
gh-148306: Fix dis.distb() crash when traceback is None
1 parent 1a0edb1 commit 25c82e9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/dis.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets
142142
if tb is None:
143143
try:
144144
if hasattr(sys, 'last_exc'):
145-
tb = sys.last_exc.__traceback__
145+
exc = sys.last_exc
146+
tb = exc.__traceback__
147+
if isinstance(exc, SyntaxError):
148+
raise RuntimeError("can't disassemble a SyntaxError")
146149
else:
147150
tb = sys.last_traceback
148151
except AttributeError:
149152
raise RuntimeError("no last traceback to disassemble") from None
153+
154+
150155
while tb.tb_next: tb = tb.tb_next
151156
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)
152157

Lib/test/test_dis.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,16 @@ def test_distb_empty(self):
24782478
with self.assertRaises(RuntimeError):
24792479
dis.distb()
24802480

2481+
def test_distb_syntax_error(self):
2482+
try:
2483+
compile("???", "", "exec")
2484+
except SyntaxError as e:
2485+
sys.last_exc = e
2486+
sys.last_exc.__traceback__ = None
2487+
2488+
with self.assertRaises(RuntimeError):
2489+
dis.distb()
2490+
24812491
def test_distb_last_traceback(self):
24822492
self.maxDiff = None
24832493
# We need to have an existing last traceback in `sys`:

0 commit comments

Comments
 (0)