]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-88943: Improve syntax error for non-ASCII character that follows a numerical liter...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 7 Sep 2023 14:00:13 +0000 (17:00 +0300)
committerGitHub <noreply@github.com>
Thu, 7 Sep 2023 14:00:13 +0000 (17:00 +0300)
It now points on the invalid non-ASCII character, not on the valid numerical literal.

Lib/test/test_grammar.py
Misc/NEWS.d/next/Core and Builtins/2023-09-07-16-05-36.gh-issue-88943.rH_X3W.rst [new file with mode: 0644]
Parser/tokenizer.c

index 7c15a23a69163bed302fe6a8252998f0073b3259..8501006b79926251e20379e4d4a05d49373e6ee8 100644 (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():
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-07-16-05-36.gh-issue-88943.rH_X3W.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-07-16-05-36.gh-issue-88943.rH_X3W.rst
new file mode 100644 (file)
index 0000000..a99830f
--- /dev/null
@@ -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.
index 6ec2489578556815b2523ff69ab3a484d7435766..46b7159ff0516b548e89ed0cd0e5e6e80221bc9f 100644 (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;