gh-148306: Fix dis.distb() crash when traceback is None#148317
gh-148306: Fix dis.distb() crash when traceback is None#148317Ignyra wants to merge 1 commit intopython:mainfrom
Conversation
|
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 |
a6f5978 to
fe1e2f3
Compare
|
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 |
fe1e2f3 to
fe4fb9a
Compare
|
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 |
fe4fb9a to
25c82e9
Compare
|
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 |
Lib/dis.py
Outdated
| tb = sys.last_exc.__traceback__ | ||
| exc = sys.last_exc | ||
| tb = exc.__traceback__ | ||
| if isinstance(exc, SyntaxError): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Ohh got it, that makes sense, thanks! I updated the PR :)
25c82e9 to
fe6ae6b
Compare
|
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 |
Summary
Prevent
dis.distb()from failing withAttributeErrorwhen the last exception has no traceback.Bug
In the REPL, a
SyntaxErrorraised does not always have a traceback attached. As a result,dis.distb()may attempt to accesstb.tb_nextwheretbisNone, leading to:Fix
Wrap the traceback traversal (
while tb.tb_next) in thetryblock so that cases where the traceback isNoneare handled gracefully.Also add a regression test simulating REPL state for a
SyntaxErrorthat has no traceback.This is my first contribution to CPython. I’d really appreciate any feedback. Thanks!
dis.distb()raise AttributeError when the previous input fails to compile #148306