Skip to content

Commit 235d380

Browse files
pablogsalencukou
andauthored
[3.13] pythongh-130077: Properly match full soft keywords in the parser (pythonGH-135317) (python#135399)
* [3.13] pythongh-130077: Properly match full soft keywords in the parser (pythonGH-135317) (cherry picked from commit ff2b5f4) Co-authored-by: Pablo Galindo Salgado <[email protected]> * Remove line the main-branch commit removed --------- Co-authored-by: Petr Viktorin <[email protected]>
1 parent 55f8fe5 commit 235d380

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
@@ -352,6 +352,13 @@
352352
Traceback (most recent call last):
353353
SyntaxError: invalid syntax
354354
355+
# But prefixes of soft keywords should
356+
# still raise specialized errors
357+
358+
>>> (mat x)
359+
Traceback (most recent call last):
360+
SyntaxError: invalid syntax. Perhaps you forgot a comma?
361+
355362
From compiler_complex_args():
356363
357364
>>> 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
@@ -609,7 +609,8 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
609609
Py_ssize_t size;
610610
PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
611611
for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
612-
if (strncmp(*keyword, the_token, size) == 0) {
612+
if (strlen(*keyword) == (size_t)size &&
613+
strncmp(*keyword, the_token, (size_t)size) == 0) {
613614
return _PyPegen_name_from_token(p, t);
614615
}
615616
}

0 commit comments

Comments
 (0)