https://github.com/python/cpython/commit/235d380c4c0ec08cf4fb791541f0f53fd8de12e6
commit: 235d380c4c0ec08cf4fb791541f0f53fd8de12e6
branch: 3.13
author: Pablo Galindo Salgado <pablog...@gmail.com>
committer: pablogsal <pablog...@gmail.com>
date: 2025-08-04T15:18:05+01:00
summary:

[3.13] gh-130077: Properly match full soft keywords in the parser (GH-135317) 
(#135399)

* [3.13] gh-130077: Properly match full soft keywords in the parser (GH-135317)
(cherry picked from commit ff2b5f40c2bf5c71255caac8a743c09ba0758c02)

Co-authored-by: Pablo Galindo Salgado <pablog...@gmail.com>

* Remove line the main-branch commit removed

---------

Co-authored-by: Petr Viktorin <encu...@gmail.com>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-06-09-23-57-37.gh-issue-130077.MHknDB.rst
M Lib/test/test_syntax.py
M Parser/pegen.c

diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 68dceac97b53dc..b035883cf883b9 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -352,6 +352,13 @@
 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
index 00000000000000..a7d02426b6fc13
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-09-23-57-37.gh-issue-130077.MHknDB.rst
@@ -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.
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 656c42b348a96b..f01cc1ed03f287 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -609,7 +609,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);
         }
     }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to