]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44368: Ensure we don't raise incorrect custom syntax errors with soft keywords...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 9 Jun 2021 21:45:43 +0000 (14:45 -0700)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 21:45:43 +0000 (14:45 -0700)
(cherry picked from commit 457ce60fc70f1c9290023f46fb82b6a490dff32e)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Lib/test/test_exceptions.py
Lib/test/test_syntax.py
Parser/pegen.c

index b242c082f856820ac1b7a4a9d1759d47c6543eab..9cb5466a674d1364c279883a4503dbdb2cb0abe7 100644 (file)
@@ -215,6 +215,7 @@ class ExceptionTests(unittest.TestCase):
         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)
index 5d3ce4cd9f7adfd3477b1f66e3165d4f7307757c..72e4ab15c87249d111a53a2bef83e8440782fc73 100644 (file)
@@ -267,7 +267,7 @@ Traceback (most recent call last):
 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 @@ SyntaxError: invalid syntax
 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):
index e6518198eca0758ce20cfaf42a7b85afe2d71eb6..82f840c605073bd86ffebafa28b93e5c90eb22ba 100644 (file)
@@ -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;