Skip to content

Commit fe1e2f3

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

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Lib/dis.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,16 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets
143143
try:
144144
if hasattr(sys, 'last_exc'):
145145
tb = sys.last_exc.__traceback__
146+
exc = sys.last_exc
146147
else:
147148
tb = sys.last_traceback
149+
exc = sys.last_value
148150
except AttributeError:
149151
raise RuntimeError("no last traceback to disassemble") from None
152+
153+
if isinstance(exc, SyntaxError):
154+
raise RuntimeError("cannot disassemble a SyntaxError")
155+
150156
while tb.tb_next: tb = tb.tb_next
151157
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)
152158

Lib/test/test_dis.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,15 @@ def test_dis_none(self):
12601260
pass
12611261
self.assertRaises(RuntimeError, dis.dis, None)
12621262

1263+
def test_distb_syntax_error(self):
1264+
try:
1265+
compile("???", "", "exec")
1266+
except SyntaxError as e:
1267+
sys.last_exc = e
1268+
sys.last_exc.__traceback__ = None
1269+
1270+
self.assertRaises(RuntimeError, dis.distb, None)
1271+
12631272
def test_dis_traceback(self):
12641273
self.maxDiff = None
12651274
try:

0 commit comments

Comments
 (0)