Skip to content

gh-127927: Use the supplied source for parser syntax errors - #162853

Closed
saggl wants to merge 1 commit into
python:mainfrom
saggl:gh-127927-parser-error-source
Closed

saggl wants to merge 1 commit into
python:mainfrom
saggl:gh-127927-parser-error-source

Conversation

@saggl

@saggl saggl commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #127927.

An editor can pass an unsaved buffer and its filename to ast.parse(). If the file on disk contains different text, the parser can quote the saved line and compute the wrong column. For example, with pass saved in example.py:

import ast
ast.parse("résultat = 1 + * 2\n", filename="example.py")

The error points past the end of pass at column 6. With this change it points at the unexpected * in the supplied line, at column 16. This also corrects the diagnostic reported by Pyflakes when it checks this buffer.

Only look up the line on disk when the tokenizer has no retained source. The regression test checks the error text, filename, line number, and character offsets for both a Unicode string and Latin-1 bytes.

Both inputs reproduced the bug on unmodified CPython. All nine targeted test modules passed on rerun. The first run had one timeout in the REPL history test, which also passed in isolation.

./python -m test -j2 test_exceptions test_syntax test_compile test_ast test_traceback test_tokenize test_codeop test_cmd_line_script test_pyrepl

OpenAI Codex helped reproduce the issue, write the fix and regression test, and verify the behavior with Pyflakes.

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 30, 2026
@bedevere-app

bedevere-app Bot commented Nov 30, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

@picnixz

picnixz commented Nov 30, 2026

Copy link
Copy Markdown
Member

Why is there a comment that was changed?

@saggl

saggl commented Nov 30, 2026

Copy link
Copy Markdown
Author

the old comment only describes skipping the file lookup when the start rule is not Py_file_input. with this change we also skip it when the tokenizer already has the source, even if the start rule is Py_file_input.

but you are right, updating only that sentence would have been sufficient.

@saggl

saggl commented Nov 30, 2026

Copy link
Copy Markdown
Author

the problem is that the parser reads the source from disk even if the source was already provided directly to compile(). therefore the error can point to the wrong text and column.

the fix is to skip the file lookup when the tokenizer already has the source.

to verify this, codex ran a regression test with pass on disk and café = 1 + * 2 as the supplied source. with the fix, the error correctly points to * at column 12 for both a python string and latin-1 bytes.

@picnixz

picnixz commented Nov 30, 2026

Copy link
Copy Markdown
Member

The issue is that I don't know whether this is really the appropriate fix. On the issue I asked to know what happened between Python versions to understand why I get different offsets but no one investigated it. I would really prefer that we first solve this quetsion before just fixing this like that.

If I reread the docs, it says:

The filename argument should give the file from which the code was read

And thus, I would still expect that we should not necessarily read the input itself but the filename. So that's why I'd like to know why we have this issue in the first place and I'm going to close the PR. Please, avoid using agents to open PRs without reading the issue history first because not every issue has a settled conclusion or an actionable item.

@picnixz picnixz closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect SyntaxError attributes when raised by compile

2 participants