Skip to content

gh-130077: Properly match full soft keywords in the parser #135317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,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);
}
}
Expand Down
Loading