]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40334: Reproduce error message for type comments on bare '*' in the new parser...
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Mon, 18 May 2020 19:14:47 +0000 (22:14 +0300)
committerGitHub <noreply@github.com>
Mon, 18 May 2020 19:14:47 +0000 (20:14 +0100)
Grammar/python.gram
Lib/test/test_syntax.py
Parser/pegen/parse.c
Parser/pegen/pegen.c
Parser/pegen/pegen.h

index cca920905462659ea92c9ab25d326ecaae3daa63..40e7818d4960227b908782ad53d14ed75cb2a207 100644 (file)
@@ -661,6 +661,7 @@ invalid_parameters:
         RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
 invalid_star_etc:
     | '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
+    | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
 invalid_lambda_star_etc:
     | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
 invalid_double_type_comments:
index 87ceced6c62a0a00b4bf9234b7e95018c1b55bfc..a82b444b67a27666f654bee89cad60de8e33d200 100644 (file)
@@ -178,6 +178,16 @@ SyntaxError: invalid syntax
 Traceback (most recent call last):
 SyntaxError: invalid syntax
 
+>>> import ast; ast.parse('''
+... def f(
+...     *, # type: int
+...     a, # type: int
+... ):
+...     pass
+... ''', type_comments=True)
+Traceback (most recent call last):
+SyntaxError: bare * has associated type comment
+
 
 From ast_for_funcdef():
 
index e9c20327c155af0e0b10d7f07f3660e8b9609097..fe95d274f37d2b2707736dce018a83c53832481c 100644 (file)
@@ -12041,7 +12041,7 @@ invalid_parameters_rule(Parser *p)
     return _res;
 }
 
-// invalid_star_etc: '*' (')' | ',' (')' | '**'))
+// invalid_star_etc: '*' (')' | ',' (')' | '**')) | '*' ',' TYPE_COMMENT
 static void *
 invalid_star_etc_rule(Parser *p)
 {
@@ -12071,6 +12071,27 @@ invalid_star_etc_rule(Parser *p)
         }
         p->mark = _mark;
     }
+    { // '*' ',' TYPE_COMMENT
+        Token * _literal;
+        Token * _literal_1;
+        Token * type_comment_var;
+        if (
+            (_literal = _PyPegen_expect_token(p, 16))  // token='*'
+            &&
+            (_literal_1 = _PyPegen_expect_token(p, 12))  // token=','
+            &&
+            (type_comment_var = _PyPegen_expect_token(p, TYPE_COMMENT))  // token='TYPE_COMMENT'
+        )
+        {
+            _res = RAISE_SYNTAX_ERROR ( "bare * has associated type comment" );
+            if (_res == NULL && PyErr_Occurred()) {
+                p->error_indicator = 1;
+                return NULL;
+            }
+            goto done;
+        }
+        p->mark = _mark;
+    }
     _res = NULL;
   done:
     return _res;
index 7f3e4561de60557a7b6171e224f3e76b2b327412..ca4ea824b3f28e3b69d9e9d04d44d5576c7f3681 100644 (file)
@@ -431,25 +431,6 @@ error:
     return NULL;
 }
 
-void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
-    int kwarg_unpacking = 0;
-    for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
-        keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
-        if (!keyword->arg) {
-            kwarg_unpacking = 1;
-        }
-    }
-
-    const char *msg = NULL;
-    if (kwarg_unpacking) {
-        msg = "positional argument follows keyword argument unpacking";
-    } else {
-        msg = "positional argument follows keyword argument";
-    }
-
-    return RAISE_SYNTAX_ERROR(msg);
-}
-
 #if 0
 static const char *
 token_name(int type)
@@ -2099,4 +2080,23 @@ _PyPegen_get_invalid_target(expr_ty e)
         default:
             return e;
     }
-}
\ No newline at end of file
+}
+
+void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
+    int kwarg_unpacking = 0;
+    for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
+        keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
+        if (!keyword->arg) {
+            kwarg_unpacking = 1;
+        }
+    }
+
+    const char *msg = NULL;
+    if (kwarg_unpacking) {
+        msg = "positional argument follows keyword argument unpacking";
+    } else {
+        msg = "positional argument follows keyword argument";
+    }
+
+    return RAISE_SYNTAX_ERROR(msg);
+}
index b9d4c048bb52b0252f8c3163d75f198a5e96ae17..146804a896fd1080bf333b6a13d656e5919aaf64 100644 (file)
@@ -256,13 +256,13 @@ asdl_seq *_PyPegen_seq_extract_starred_exprs(Parser *, asdl_seq *);
 asdl_seq *_PyPegen_seq_delete_starred_exprs(Parser *, asdl_seq *);
 expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_seq *);
 asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
-void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
 int _PyPegen_check_barry_as_flufl(Parser *);
 mod_ty _PyPegen_make_module(Parser *, asdl_seq *);
 
 // Error reporting helpers
-
 expr_ty _PyPegen_get_invalid_target(expr_ty e);
+void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
+
 
 void *_PyPegen_parse(Parser *);