gh-149277: Fix error position for invalid numeric literals#149456
gh-149277: Fix error position for invalid numeric literals#149456anujbharambe wants to merge 3 commits intopython:mainfrom
Conversation
|
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 |
Documentation build overview
18 files changed ·
|
|
I have made the requested changes; please review again. Added |
|
Thanks for making the requested changes! @picnixz: please review the changes made to this pull request. |
| with self.subTest(source=source): | ||
| with self.assertRaises(SyntaxError) as cm: | ||
| compile(source, "<test>", "eval") | ||
| self.assertEqual(cm.exception.offset, expected_offset) |
There was a problem hiding this comment.
Please also test the rendered carret not just the carret offset. Use check_syntax_error for that.
| def test_end_of_numerical_literals_offset(self): | ||
| # gh-149277: verify the error caret points at the first invalid | ||
| # character, not the last valid digit. | ||
| cases = [ | ||
| ("0xfg", 4), | ||
| ("0x9g", 4), | ||
| ("0b1z", 4), | ||
| ("0o7q", 4), | ||
| ("9spam", 2), | ||
| ("0xfspam", 4), | ||
| ("1.0x", 4), | ||
| ("1e3w", 4), | ||
| ("1jz", 3), | ||
| ] | ||
| for source, expected_offset in cases: |
There was a problem hiding this comment.
Use @subTests decorator for cases instead of this loop and subTest().
|
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 |
Summary
Fix the
SyntaxErrorcaret position for invalid numeric literals. Previously,the caret pointed at the last valid digit instead of the first invalid character.
For example,
0x9gnow 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 inverify_end_of_number()in the error branch. Since
_PyTokenizer_syntaxerrorcomputes the column offsetfrom
tok->cur, backing up one character caused the caret to point at thepreceding (valid) character. Removing the backup keeps
tok->curat the correctposition.
Fixes #149277