gh-88943: Improve syntax error for non-ASCII character that follows a numerical literal (GH-109081)

It now points on the invalid non-ASCII character, not on the valid numerical literal.
This commit is contained in:
Serhiy Storchaka 2023-09-07 17:00:13 +03:00 committed by GitHub
parent ac31f714c3
commit b2729e93e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 1 deletions

View File

@ -236,6 +236,10 @@ class TokenTests(unittest.TestCase):
check(f"[{num}for x in ()]")
check(f"{num}spam", error=True)
# gh-88943: Invalid non-ASCII character following a numerical literal.
with self.assertRaisesRegex(SyntaxError, r"invalid character '' \(U\+2044\)"):
compile(f"{num}7", "<testcase>", "eval")
with self.assertWarnsRegex(SyntaxWarning, r'invalid \w+ literal'):
compile(f"{num}is x", "<testcase>", "eval")
with warnings.catch_warnings():

View File

@ -0,0 +1,3 @@
Improve syntax error for non-ASCII character that follows a numerical
literal. It now points on the invalid non-ASCII character, not on the valid
numerical literal.

View File

@ -1642,7 +1642,7 @@ verify_end_of_number(struct tok_state *tok, int c, const char *kind) {
tok_nextc(tok);
}
else /* In future releases, only error will remain. */
if (is_potential_identifier_char(c)) {
if (c < 128 && is_potential_identifier_char(c)) {
tok_backup(tok, c);
syntaxerror(tok, "invalid %s literal", kind);
return 0;