Skip to content

gh-149277: Fix error position for invalid numeric literals#149456

Open
anujbharambe wants to merge 3 commits intopython:mainfrom
anujbharambe:fix/invalid-literal-error-position
Open

gh-149277: Fix error position for invalid numeric literals#149456
anujbharambe wants to merge 3 commits intopython:mainfrom
anujbharambe:fix/invalid-literal-error-position

Conversation

@anujbharambe
Copy link
Copy Markdown
Contributor

@anujbharambe anujbharambe commented May 6, 2026

Summary

Fix the SyntaxError caret position for invalid numeric literals. Previously,
the caret pointed at the last valid digit instead of the first invalid character.

For example, 0x9g now correctly shows:
0x9g
^
SyntaxError: invalid hexadecimal literal

Instead of the previous incorrect output:
0x9g
^
SyntaxError: invalid hexadecimal literal

The issue was a spurious tok_backup(tok, c) call in verify_end_of_number()
in the error branch. Since _PyTokenizer_syntaxerror computes the column offset
from tok->cur, backing up one character caused the caret to point at the
preceding (valid) character. Removing the backup keeps tok->cur at the correct
position.

Fixes #149277

Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Please add tests.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 6, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@read-the-docs-community
Copy link
Copy Markdown

@anujbharambe
Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

Added test_end_of_numerical_literals_offset in Lib/test/test_grammar.py that verifies SyntaxError.offset points at the first invalid character for various invalid numeric literal cases (hex, binary, octal, decimal, float, scientific notation, and imaginary literals).

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 6, 2026

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@bedevere-app bedevere-app Bot requested a review from picnixz May 6, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error display for invalid literal is incorrect

3 participants