gh-130077: Properly match full soft keywords in the parser (#135317) · python/cpython@ff2b5f4

GitHub

File tree

Lib/test

Misc/NEWS.d/next/Core_and_Builtins

Parser

Original file line numberDiff line numberDiff line change@@ -382,6 +382,13 @@

382382Traceback (most recent call last):

383383SyntaxError: invalid syntax

384384385+# 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+385392From compiler_complex_args():

386393387394>>> def f(None=1):

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.

Original file line numberDiff line numberDiff line change@@ -610,7 +610,8 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {

610610Py_ssize_tsize;

611611PyBytes_AsStringAndSize(t->bytes, &the_token, &size);

612612for (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) {

614615return_PyPegen_name_from_token(p, t);

615616 }

616617 }