diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2021-09-27 13:37:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-27 13:37:43 (GMT) |
commit | e5f13ce5b48b551c09fdd0faeafa6ecf860de51c (patch) | |
tree | d53ec2b8a487a906024e139c106f2a1b21aa810a /Parser/pegen.c | |
parent | a22be4943c119fecf5433d999227ff78fc2e5741 (diff) | |
download | cpython-e5f13ce5b48b551c09fdd0faeafa6ecf860de51c.zip cpython-e5f13ce5b48b551c09fdd0faeafa6ecf860de51c.tar.gz cpython-e5f13ce5b48b551c09fdd0faeafa6ecf860de51c.tar.bz2 |
bpo-43914: Correctly highlight SyntaxError exceptions for invalid generator expression in function calls (GH-28576)
Diffstat (limited to 'Parser/pegen.c')
-rw-r--r-- | Parser/pegen.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c index c77c534..7e2d37c 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -2551,8 +2551,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 @@ -2566,8 +2575,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" ); } |