From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 11 Jun 2025 01:01:37 +0000 (+0200) Subject: [3.14] gh-130077: Properly match full soft keywords in the parser (GH-135317) (#135348) X-Git-Tag: v3.14.0b3~51 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=77c4b5d413aae2a219c99f6ef8f095eddfe664b8;p=thirdparty%2FPython%2Fcpython.git [3.14] gh-130077: Properly match full soft keywords in the parser (GH-135317) (#135348) gh-130077: Properly match full soft keywords in the parser (GH-135317) (cherry picked from commit ff2b5f40c2bf5c71255caac8a743c09ba0758c02) Co-authored-by: Pablo Galindo Salgado --- diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index fadafd9b3d3a..2624588a41b1 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -382,6 +382,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 index 000000000000..a7d02426b6fc --- /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 0919ff417f19..50641de27d37 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -610,7 +610,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_t)size) == 0) { + if (strlen(*keyword) == (size_t)size && + strncmp(*keyword, the_token, (size_t)size) == 0) { return _PyPegen_name_from_token(p, t); } }