]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-130077: Properly match full soft keywords in the parser (GH-135317) (#135400)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Tue, 8 Jul 2025 23:40:55 +0000 (00:40 +0100)
committerGitHub <noreply@github.com>
Tue, 8 Jul 2025 23:40:55 +0000 (00:40 +0100)
(cherry picked from commit ff2b5f40c2bf5c71255caac8a743c09ba0758c02)

Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2025-06-09-23-57-37.gh-issue-130077.MHknDB.rst [new file with mode: 0644]
Parser/pegen.c

index 36a7f5a873dc8f8639df1ef2d6e3b65894c0a623..f2a2bc1d289861d5c4fc1ea60a269de5ddd741f7 100644 (file)
@@ -322,6 +322,13 @@ SyntaxError: invalid syntax
 Traceback (most recent call last):
 SyntaxError: invalid syntax
 
+# But prefixes of soft keywords should
+# still raise specialized errors
+
+>>> (mat x)
+Traceback (most recent call last):
+SyntaxError: invalid syntax. Perhaps you forgot a comma?
+
 From compiler_complex_args():
 
 >>> def f(None=1):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-06-09-23-57-37.gh-issue-130077.MHknDB.rst b/Misc/NEWS.d/next/Core and Builtins/2025-06-09-23-57-37.gh-issue-130077.MHknDB.rst
new file mode 100644 (file)
index 0000000..a7d0242
--- /dev/null
@@ -0,0 +1,2 @@
+Properly raise custom syntax errors when incorrect syntax containing names
+that are prefixes of soft keywords is encountered.  Patch by Pablo Galindo.
index 5b92133d9f905b52c072610a48b07ecd62becccf..fa1a4280019bbe0315c91f5bc4d245bd0f745c51 100644 (file)
@@ -637,7 +637,8 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
     Py_ssize_t size;
     PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
     for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
-        if (strncmp(*keyword, the_token, size) == 0) {
+        if (strlen(*keyword) == (size_t)size &&
+            strncmp(*keyword, the_token, (size_t)size) == 0) {
             return _PyPegen_name_from_token(p, t);
         }
     }