fix(errors): handle parse.ParseError without IndexError in FormatErrorMsg - #1321
Open
jdymitarai wants to merge 1 commit into
Open
fix(errors): handle parse.ParseError without IndexError in FormatErrorMsg#1321jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
…rMsg
When code raises a parse.ParseError (such as unsupported syntax or grammar mismatches where ast.parse succeeds), FormatErrorMsg attempted to index e.args[1][0], e.args[1][1], e.args[1][2], assuming e.args[1] was a 3-tuple (filename, lineno, column). However, ParseError.args contains ('bad input: ...',) while line and column information are stored in e.context (('', (lineno, column))), resulting in an unhandled IndexError: tuple index out of range.
1. Add explicit support for parse.ParseError in FormatErrorMsg, extracting lineno and column from e.context and the message from e.msg.
2. Use getattr(e, 'filename', None) consistently across error formatters.
3. Add a safe fallback in FormatErrorMsg to handle any unexpected exception structures cleanly without crashing.
4. Add unit tests for ParseError formatting and FormatErrorMsg in yapftests/yapf_test.py.
Fixes google#1303, google#1250, google#1215, google#1256
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.
Summary
Fixes #1303, #1250, #1215, #1256 where YAPF crashed with an unhandled internal
IndexError: tuple index out of rangewhenever parsing raised aparse.ParseErrorinstead of producing a clean error diagnostic.Cause & Analysis
In
yapf/pytree/pytree_utils.py, when parsing fails,ast.parse(code)is executed to check if the snippet is invalid Python syntax (raisingSyntaxError). Ifast.parsesucceeds (for example on newer language constructs or syntax supported by Python's parser but not by thelib2to3grammar),ParseCodeToTreere-raisesparse.ParseError.When handling this exception in
yapf_api.py,FormatErrorMsg(e)attempted to format the error location usinge.args[1][0], e.args[1][1], e.args[1][2], assuminge.args[1]was a 3-element tuple(filename, lineno, column). However,_ylib2to3.pgen2.parse.ParseError.argsis a 1-tuple("bad input: ...",)where location metadata is stored ine.context((prefix, (lineno, column))). This resulted ine.args[1]raising an unhandledIndexError: tuple index out of rangeinstead of reporting the syntax error cleanly.Solution
parse.ParseErrorinFormatErrorMsg:linenoandcolumnfrome.context[1]if available.e.msg.getattr(e, 'filename', None)across formatters.FormatErrorMsgto handle unexpected exception argument shapes cleanly without raisingIndexError,TypeError, orAttributeError.ParseErrorformatting andFormatErrorMsginyapftests/yapf_test.py.