-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caret positioned wrong for SyntaxError reported by ast.c #78864
Comments
Input file with a subtle error: a number where an assignment target is required: for 1 in []: pass Run it, it gives a SyntaxError. Note how the caret pointing to the incorrect token is position one to the left of where you'd expect it: File "s.py", line 1 For every syntax error I've seen that's produced by ast.c this seems to be the case -- the caret is always positioned 1 too far left. I tried to understand how this is happening but my AST-fu is lacking. It seems this has been happening since ast.c started added column numbers -- in Python 2.7 there's no caret at all, but in 3.4 and later there's a caret and it has the same problem. (Also in 3.3; I don't have 3.2 or older 3.x lying around to test.) |
I'm doing some fairly major surgery on line and column numbers for fixing f-string errors. I'll see if I can include this case, too. |
I think ast.c is in the right here and there are two complementary bugs in caret printing and the parser. print_error_text does this: while (--offset > 0)
PyFile_WriteString(" ", f); which is off-by-one if offset is zero-indexed (which it should be IMO). The parser has an off-by-one error in the other direction. When it reports an error, it reports the offset of the tokenizer's "cur" buffer. This is generally advanced past the location that actually resulted in a parser error. I believe the parser should be fixed to report the error offset as the offset of the start of the token that caused the error. |
I think Benjamin's diagnosis is correct. In which case, my f-string error reporting changes (see bpo-34364) won't intersect with a fix to this issue. |
Added a PR that is an implementation of Benjamin's suggestion. The column offset returned has been made 0-indexed and errors now point to the start of the parsed token. This makes errors like def class instead of def class Also added tests for the original example and some more errors from ast.c |
Fixed by #53584. Thanks Ammar! |
Hm, should this be backported? I think it's safest not to. |
Wait, why did you make offsets 1-indexed? My suggestion was to make everything zero indexed... |
There's some context on the github PR but essentially it boiled down to:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: