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):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering out all SyntaxErrors here doesn’t seem quite right. It should still work if a SyntaxError is raised elsewhere. I think you could check whether tb is None, or just move the while loop below into the try block.

Copy link
Copy Markdown
Author

@Ignyra Ignyra Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be mistaken but from my understanding, a SyntaxError can not produce a traceback, since it occurs before any bytecode is executed, so my intention was to make the resulting error more informative in that specific case, to distinguish it from the other error that is raised where no exception has occurred at all.

That said, I can switch to the alternative approach if you think that would be more appropriate.

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