Repository navigation
Conversation
This branch has not been deployed
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 #1463
Summary
babel.util.parse_encoding()previously ranast.parse()on line 1 to decide whether line 2 could contain a magic encoding comment. Single-line executable code (such asx = 1,"""docstring""", or;) succeeded underast.parse(), causing Babel to honor a second-line# coding:comment that Python itself ignores per PEP 263 (and to raise a spuriousSyntaxErrorif the file also began with a UTF-8 BOM).This change replaces the
ast.parse()check with_PYTHON_BLANK_OR_COMMENT_re(rb'[ \t\f]*(?:[#\r\n]|$)', matchingtokenize.blank_re), so line 2 is checked only when line 1 is blank or a comment.Changes
babel/util.py: Match line 1 against_PYTHON_BLANK_OR_COMMENT_rebefore reading line 2 forPYTHON_MAGIC_COMMENT_reinparse_encoding().tests/test_util.py: Add unit tests covering valid blank/shebang/comment first lines, ignored second-line declarations after executable code/docstrings/;/\x0b, UTF-8 BOM + code on line 1, and stream position preservation.tests/messages/test_extract_python.py: Add extraction tests verifying second-line# coding:behavior after code, blank/comment lines, and a UTF-8 BOM.Verification
python -X utf8 -m pytest tests/test_util.py tests/messages/test_extract.py tests/messages/test_extract_python.py:67 passedpython -X utf8 -m pytest(with imported CLDR data):8225 passed, 5 skipped, 2 xfailedruff check .: all checks passed