[Python-checkins] bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords (GH-26630)

pablogsal webhook-mailer at python.org
Wed Jun 9 17:20:11 EDT 2021


https://github.com/python/cpython/commit/457ce60fc70f1c9290023f46fb82b6a490dff32e
commit: 457ce60fc70f1c9290023f46fb82b6a490dff32e
branch: main
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-06-09T22:20:01+01:00
summary:

bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords (GH-26630)

files:
M Lib/test/test_exceptions.py
M Lib/test/test_syntax.py
M Parser/pegen.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index b242c082f8568..9cb5466a674d1 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -215,6 +215,7 @@ def testSyntaxErrorOffset(self):
         check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
         check('[file for\n str(file) in []]', 2, 2)
         check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
+        check('match ...:\n    case {**rest, "key": value}:\n        ...', 2, 19)
 
         # Errors thrown by compile.c
         check('class foo:return 1', 1, 11)
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 5d3ce4cd9f7ad..72e4ab15c8724 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -267,7 +267,7 @@
 SyntaxError: invalid syntax. Perhaps you forgot a comma?
 
 # Make sure soft keywords constructs don't raise specialized
-# errors regarding missing commas
+# errors regarding missing commas or other spezialiced errors
 
 >>> match x:
 ...     y = 3
@@ -280,6 +280,24 @@
 Traceback (most recent call last):
 SyntaxError: invalid syntax
 
+>>> match x:
+...     case $:
+...        ...
+Traceback (most recent call last):
+SyntaxError: invalid syntax
+
+>>> match ...:
+...     case {**rest, "key": value}:
+...        ...
+Traceback (most recent call last):
+SyntaxError: invalid syntax
+
+>>> match ...:
+...     case {**_}:
+...        ...
+Traceback (most recent call last):
+SyntaxError: invalid syntax
+
 From compiler_complex_args():
 
 >>> def f(None=1):
diff --git a/Parser/pegen.c b/Parser/pegen.c
index e6518198eca07..82f840c605073 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -936,10 +936,9 @@ _PyPegen_get_last_nonnwhitespace_token(Parser *p)
     return token;
 }
 
-expr_ty
-_PyPegen_name_token(Parser *p)
+static expr_ty
+_PyPegen_name_from_token(Parser *p, Token* t)
 {
-    Token *t = _PyPegen_expect_token(p, NAME);
     if (t == NULL) {
         return NULL;
     }
@@ -957,6 +956,14 @@ _PyPegen_name_token(Parser *p)
                        t->end_col_offset, p->arena);
 }
 
+
+expr_ty
+_PyPegen_name_token(Parser *p)
+{
+    Token *t = _PyPegen_expect_token(p, NAME);
+    return _PyPegen_name_from_token(p, t);
+}
+
 void *
 _PyPegen_string_token(Parser *p)
 {
@@ -974,7 +981,7 @@ expr_ty _PyPegen_soft_keyword_token(Parser *p) {
     PyBytes_AsStringAndSize(t->bytes, &the_token, &size);
     for (char **keyword = p->soft_keywords; *keyword != NULL; keyword++) {
         if (strncmp(*keyword, the_token, size) == 0) {
-            return _PyPegen_name_token(p);
+            return _PyPegen_name_from_token(p, t);
         }
     }
     return NULL;



More information about the Python-checkins mailing list