Skip to content

Commit fe6ae6b

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

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Lib/dis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,12 @@ def distb(tb=None, *, file=None, show_caches=False, adaptive=False, show_offsets
145145
tb = sys.last_exc.__traceback__
146146
else:
147147
tb = sys.last_traceback
148+
149+
while tb.tb_next: tb = tb.tb_next
148150
except AttributeError:
149151
raise RuntimeError("no last traceback to disassemble") from None
150-
while tb.tb_next: tb = tb.tb_next
152+
153+
151154
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)
152155

153156
# The inspect module interrogates this dictionary to build its

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)