-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Edge case in compiler when error displaying with non-utf8 lines #88515
Comments
The AST currently stores column offsets for characters as byte-offsets. However, when displaying errors, these byte-offsets must be turned into character-offsets so that the characters line up properly with the characters on the line when printed. This is done with the function Lines 142 to 161 in fdc7e52
However, consider a file like this: '┬ó┬ó┬ó┬ó┬ó┬ó' + f(4, 'Hi' for x in range(1)) # This line has a SyntaxError This prints File "test-normal.py", line 1 as expected. However if we use a custom source encoding line: # -- coding: cp437 -- it ends up printing out File "C:\Users\ammar\junk\test-utf16.py", line 2 where the carets/offsets are misaligned with the actual characters. This is because the string "┬ó" has the display width of 2 characters and encodes to 2 bytes in cp437 but when interpreted as utf-8 is the single character "¢" with a display width of 1. Note that this edge case is relatively hard to trigger because ordinarily what will happen here is that the call to PyErr_ProgramTextObject will fail because it tries to decode the line as utf-8: Lines 1693 to 1696 in ae3c66a
So this bug requires the input to be valid as both utf-8 and the source encoding. (Discovered while implementing PEP-657 colnotab#10) |
Lysandros, could you take a look? |
This affects also older versions: python3.8 lel.py
SyntaxError: Generator expression must be parenthesized |
I think the simplest solution is PR 26611. Ammar, can you check if that works for you? |
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: