Skip to content

Commit 77c4b5d

Browse files
[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 ff2b5f4) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent 4b96a34 commit 77c4b5d

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_syntax.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,13 @@
382382
Traceback (most recent call last):
383383
SyntaxError: invalid syntax
384384
385+
# But prefixes of soft keywords should
386+
# still raise specialized errors
387+
388+
>>> (mat x)
389+
Traceback (most recent call last):
390+
SyntaxError: invalid syntax. Perhaps you forgot a comma?
391+
385392
From compiler_complex_args():
386393
387394
>>> def f(None=1):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Properly raise custom syntax errors when incorrect syntax containing names
2+
that are prefixes of soft keywords is encountered. Patch by Pablo Galindo.

Parser/pegen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
610610
Py_ssize_t size;
611611
PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
612612
for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
613-
if (strncmp(*keyword, the_token, (size_t)size) == 0) {
613+
if (strlen(*keyword) == (size_t)size &&
614+
strncmp(*keyword, the_token, (size_t)size) == 0) {
614615
return _PyPegen_name_from_token(p, t);
615616
}
616617
}

0 commit comments

Comments
 (0)