Fix crash when reporting a lib2to3 parse error - #1317
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
FormatErrorMsg assumed every non-SyntaxError, non-TokenError exception
carried a (filename, lineno, column) tuple in e.args[1], but
parse.ParseError builds its message through a plain
Exception.__init__(msg) call, so e.args is just a one-element tuple
holding that string. Indexing e.args[1] on it raises IndexError before
the real error ever gets reported.
This surfaces most easily on Python 3.12+, where PEP 701 relaxed the
rules around quote characters inside f-strings. yapf's own lib2to3
based grammar doesn't parse that construct yet, so something like
f"{tab["key"]}" is valid to the real interpreter but makes yapf's
parser raise ParseError, and formatting that error is what used to
crash with the unrelated IndexError instead of telling you what
actually went wrong.
ParseError already tracks the position it failed at as e.context
(a (prefix, (lineno, column)) pair), so use that directly instead of
assuming a shape the exception was never built with.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1303.
The crash isn't really about the f-string itself, it's in how yapf reports the parse failure.
FormatErrorMsgassumes any exception that isn't aSyntaxErrorortokenize.TokenErrorcarries a(filename, lineno, column)tuple ase.args[1], butparse.ParseErrorbuilds its message through a plainException.__init__(msg)call, soe.argsends up being a one-element tuple with just that string in it. Indexinge.args[1]on it raisesIndexErrorbefore the actual parse error message ever gets shown.The repro from the issue triggers this because of PEP 701 (Python 3.12):
f"{tab["SOME_STRING"]}"reuses the outer quote character inside the f-string expression, which the real interpreter accepts but yapf's own lib2to3-based grammar doesn't parse yet, so it raises aParseErrorinternally. That part is a separate, bigger piece of work (teaching the grammar about PEP 701), this PR is just about not crashing with a confusing, unrelated error when that (or any other)ParseErrorhappens.ParseErroralready stores where it failed ase.context, a(prefix, (lineno, column))pair, so I used that instead of guessing at a tuple shape the exception was never actually constructed with. After the fix, the example from the issue reports cleanly:instead of crashing.
Added a regression test in the existing
BadInputTestclass, skipped on Python < 3.12 since the repro needs PEP 701 to even be valid syntax. Ran the full test suite locally (612 tests, all green).