Skip to content

Commit 64b4b11

Browse files
committed
[3.12] pythongh-130077: Properly match full soft keywords in the parser (pythonGH-135317)
(cherry picked from commit ff2b5f4) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent 033aa5c commit 64b4b11

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
@@ -322,6 +322,13 @@
322322
Traceback (most recent call last):
323323
SyntaxError: invalid syntax
324324
325+
# But prefixes of soft keywords should
326+
# still raise specialized errors
327+
328+
>>> (mat x)
329+
Traceback (most recent call last):
330+
SyntaxError: invalid syntax. Perhaps you forgot a comma?
331+
325332
From compiler_complex_args():
326333
327334
>>> 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
@@ -637,7 +637,8 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
637637
Py_ssize_t size;
638638
PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
639639
for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
640-
if (strncmp(*keyword, the_token, size) == 0) {
640+
if (strlen(*keyword) == (size_t)size &&
641+
strncmp(*keyword, the_token, (size_t)size) == 0) {
641642
return _PyPegen_name_from_token(p, t);
642643
}
643644
}

0 commit comments

Comments
 (0)