Skip to content

gh-156894: Fix the position of syntax errors which cover a range - #156901

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:fix-156894-range-offsets
Sep 4, 2026
Merged

gh-156894: Fix the position of syntax errors which cover a range#156901
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:fix-156894-range-offsets

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

SyntaxError.offset and end_offset are columns in characters, but all callers of _PyTokenizer_syntaxerror_known_range() pass columns in bytes, so the reported position drifts by one column per preceding non-ASCII character:

>>> compile("abcd = 00010", "<t>", "exec")     # before and after
  File "<t>", line 1
    abcd = 00010
           ^^^
>>> compile("αβγδ = 00010", "<t>", "exec")     # before
  File "<t>", line 1
    αβγδ = 00010
               ^
>>> compile("αβγδ = 00010", "<t>", "exec")     # after
  File "<t>", line 1
    αβγδ = 00010
           ^^^

The conversion is done in _syntaxerror_range(), so all three call sites are fixed at once, and the -1 case is unaffected: it already counts characters.

test_tokenize.test_tolerant_incompatible_prefix_position_after_non_ascii asserted the column in bytes, so its expected value changes from 6 to 5.

The callers of _PyTokenizer_syntaxerror_known_range() pass columns in bytes,
but SyntaxError.offset and end_offset are columns in characters.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
# for the errors which cover a range
for source, offset, end_offset in [
('abcd = 00010', 8, 11),
('\u03b1\u03b2\u03b3\u03b4 = 00010', 8, 11),

@grayjk grayjk Sep 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include a test with a unicode combining character (e.g. \u0301) which has zero display width

Suggested change
('\u03b1\u03b2\u03b3\u03b4 = 00010', 8, 11),
('\u03b1\u03b2\u03b3\u03b4 = 00010', 8, 11),
('e\u0301'*4 = 00010', 8, 11),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It counts code points, not visible width.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 4, 2026
@serhiy-storchaka
serhiy-storchaka merged commit 59a6913 into python:main Sep 4, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@serhiy-storchaka
serhiy-storchaka deleted the fix-156894-range-offsets branch September 4, 2026 11:14
@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 59a691361cb7b1d4fde3b92f98a61490381c62c9 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 59a691361cb7b1d4fde3b92f98a61490381c62c9 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 59a691361cb7b1d4fde3b92f98a61490381c62c9 3.13

@bedevere-app

bedevere-app Bot commented Sep 4, 2026

Copy link
Copy Markdown

GH-156926 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 4, 2026
@bedevere-app

bedevere-app Bot commented Sep 4, 2026

Copy link
Copy Markdown

GH-156927 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 4, 2026
@bedevere-app

bedevere-app Bot commented Sep 4, 2026

Copy link
Copy Markdown

GH-156928 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Sep 4, 2026
serhiy-storchaka added a commit that referenced this pull request Sep 4, 2026
…ge (GH-156901) (GH-156928)

The callers of _PyTokenizer_syntaxerror_known_range() pass columns in bytes,
but SyntaxError.offset and end_offset are columns in characters.

(cherry picked from commit 59a6913)

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
serhiy-storchaka added a commit that referenced this pull request Sep 4, 2026
…ge (GH-156901) (GH-156927)

The callers of _PyTokenizer_syntaxerror_known_range() pass columns in bytes,
but SyntaxError.offset and end_offset are columns in characters.

(cherry picked from commit 59a6913)

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

2 participants