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

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 38e98beeb94c5e08f83872e1d1bd5c80bfd38ef9..f1f9b5c00751001494948f7f7db5054f60abacbf 100644 (file)
@@ -238,6 +238,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 warnings.catch_warnings():
                 warnings.filterwarnings('ignore', '"is" with a literal',
                                         SyntaxWarning)
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 b552b4171888f5a7d3896f30a0d256bd669420a6..7fc8a585621d3a0e37ce7caba71438d2539fe4ea 100644 (file)
@@ -1303,7 +1303,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;