Skip to content

Only scan line 2 for encoding declaration after blank or comment lines - #1485

Closed
seragAhmed wants to merge 1 commit into
python-babel:masterfrom
seragAhmed:fix/1463-parse-encoding-second-line
Closed

seragAhmed wants to merge 1 commit into
python-babel:masterfrom
seragAhmed:fix/1463-parse-encoding-second-line

Conversation

@seragAhmed

Copy link
Copy Markdown

parse_encoding() previously used ast.parse(line1) to decide whether to inspect line 2 for a magic encoding comment, which caused it to scan line 2 even when line 1 contained executable statements or docstrings.

Per PEP 263 and CPython's tokenize.detect_encoding(), a second-line encoding declaration is only valid when line 1 is blank or a comment. This replaces the ast.parse check with the PEP 263 blank/comment line match (^[ \t\f]*(?:[#\r\n]|$)).

Fixes #1463.

@akx akx closed this Nov 24, 2026
@akx

akx commented Nov 24, 2026

Copy link
Copy Markdown
Member

Duplicate of #1465.

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.

Python extraction honors a second-line encoding declaration after executable code

3 participants