Skip to content

gh-148306: Fix dis.distb() crash when traceback is None#148317

Open
Ignyra wants to merge 1 commit intopython:mainfrom
Ignyra:fix-distb-syntaxerror
Open

gh-148306: Fix dis.distb() crash when traceback is None#148317
Ignyra wants to merge 1 commit intopython:mainfrom
Ignyra:fix-distb-syntaxerror

Conversation

@Ignyra
Copy link
Copy Markdown

@Ignyra Ignyra commented Apr 9, 2026

Summary

Prevent dis.distb() from failing with AttributeError when the last exception has no traceback.

Bug

In the REPL, a SyntaxError raised does not always have a traceback attached. As a result, dis.distb() may attempt to access tb.tb_next where tb is None, leading to:

AttributeError: 'NoneType' object has no attribute 'tb_next'

Fix

Wrap the traceback traversal (while tb.tb_next) in the try block so that cases where the traceback is None are handled gracefully.

Also add a regression test simulating REPL state for a SyntaxError that has no traceback.


This is my first contribution to CPython. I’d really appreciate any feedback. Thanks!

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot bot commented Apr 9, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 9, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 9, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Ignyra Ignyra force-pushed the fix-distb-syntaxerror branch from fe1e2f3 to fe4fb9a Compare April 9, 2026 23:22
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 9, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Ignyra Ignyra force-pushed the fix-distb-syntaxerror branch from fe4fb9a to 25c82e9 Compare April 9, 2026 23:32
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 9, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Lib/dis.py Outdated
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.

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.

Sorry, I didn't really explain earlier why direct filtering doesn't work well. Actually, SyntaxErrors can have tracebacks too. Like, if you run this code, you'll get a SyntaxError with a traceback.

def run_code(code):
    exec(code)

run_code("???")

After tracing the code, I found that this is actually due to the REPL's handling mechanism, which causes it to appear without a traceback. When there's a syntax error in the REPL input, it enters this section, and the traceback gets set to None here. Hope this helps.

: 3

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ohh got it, that makes sense, thanks! I updated the PR :)

@Ignyra Ignyra force-pushed the fix-distb-syntaxerror branch from 25c82e9 to fe6ae6b Compare April 11, 2026 04:04
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 11, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants