summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-09-27 14:05:20 (GMT)
committerPablo Galindo <pablogsal@gmail.com>2021-09-27 21:01:00 (GMT)
commit3397e3192ccc95ff7076f735930d2f4c60fbc278 (patch)
treeca404ce008643c3f1568f48fc99548a86c3a56a0 /Parser
parent0eb57c3be47e45b10124428ab45827467cd2d58e (diff)
downloadcpython-3397e3192ccc95ff7076f735930d2f4c60fbc278.zip
cpython-3397e3192ccc95ff7076f735930d2f4c60fbc278.tar.gz
cpython-3397e3192ccc95ff7076f735930d2f4c60fbc278.tar.bz2
bpo-43914: Correctly highlight SyntaxError exceptions for invalid generator expression in function calls (GH-28576)
(cherry picked from commit e5f13ce5b48b551c09fdd0faeafa6ecf860de51c) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Diffstat (limited to 'Parser')
-rw-r--r--Parser/parser.c6
-rw-r--r--Parser/pegen.c16
-rw-r--r--Parser/pegen.h2
3 files changed, 18 insertions, 6 deletions
diff --git a/Parser/parser.c b/Parser/parser.c
index e57c603..2a437d5 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -17902,15 +17902,15 @@ invalid_arguments_rule(Parser *p)
}
D(fprintf(stderr, "%*c> invalid_arguments[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "args for_if_clauses"));
expr_ty a;
- asdl_comprehension_seq* for_if_clauses_var;
+ asdl_comprehension_seq* b;
if (
(a = args_rule(p)) // args
&&
- (for_if_clauses_var = for_if_clauses_rule(p)) // for_if_clauses
+ (b = for_if_clauses_rule(p)) // for_if_clauses
)
{
D(fprintf(stderr, "%*c+ invalid_arguments[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "args for_if_clauses"));
- _res = _PyPegen_nonparen_genexp_in_call ( p , a );
+ _res = _PyPegen_nonparen_genexp_in_call ( p , a , b );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 31ebccb..e20e926 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -2530,8 +2530,17 @@ void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
return RAISE_SYNTAX_ERROR(msg);
}
+
+static inline expr_ty
+_PyPegen_get_last_comprehension_item(comprehension_ty comprehension) {
+ if (comprehension->ifs == NULL || asdl_seq_LEN(comprehension->ifs) == 0) {
+ return comprehension->iter;
+ }
+ return PyPegen_last_item(comprehension->ifs, expr_ty);
+}
+
void *
-_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args)
+_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions)
{
/* The rule that calls this function is 'args for_if_clauses'.
For the input f(L, x for x in y), L and x are in args and
@@ -2545,8 +2554,11 @@ _PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args)
return NULL;
}
- return RAISE_SYNTAX_ERROR_STARTING_FROM(
+ comprehension_ty last_comprehension = PyPegen_last_item(comprehensions, comprehension_ty);
+
+ return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
(expr_ty) asdl_seq_GET(args->v.Call.args, len - 1),
+ _PyPegen_get_last_comprehension_item(last_comprehension),
"Generator expression must be parenthesized"
);
}
diff --git a/Parser/pegen.h b/Parser/pegen.h
index 4db7923..40222d6 100644
--- a/Parser/pegen.h
+++ b/Parser/pegen.h
@@ -325,7 +325,7 @@ _RAISE_SYNTAX_ERROR_INVALID_TARGET(Parser *p, TARGETS_TYPE type, void *e)
}
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
-void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args);
+void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
// Generated function in parse.c - function definition in python.gram