Skip to content

Fix crash when reporting a lib2to3 parse error - #1317

Open
afonsojanu wants to merge 1 commit into
google:mainfrom
afonsojanu:fix/parse-error-indexerror-crash
Open

Fix crash when reporting a lib2to3 parse error#1317
afonsojanu wants to merge 1 commit into
google:mainfrom
afonsojanu:fix/parse-error-indexerror-crash

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #1303.

The crash isn't really about the f-string itself, it's in how yapf reports the parse failure. FormatErrorMsg assumes any exception that isn't a SyntaxError or tokenize.TokenError carries a (filename, lineno, column) tuple as e.args[1], but parse.ParseError builds its message through a plain Exception.__init__(msg) call, so e.args ends up being a one-element tuple with just that string in it. Indexing e.args[1] on it raises IndexError before 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 a ParseError internally. 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) ParseError happens.

ParseError already stores where it failed as e.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:

yapf: test.py:1:8: bad input

instead of crashing.

Added a regression test in the existing BadInputTest class, 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).

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.
@google-cla

google-cla Bot commented Sep 3, 2026

Copy link
Copy Markdown

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Crash][Reproducible] IndexError: tuple index out of range

1 participant