Skip to content

Commit e5f13ce

Browse files
authored
bpo-43914: Correctly highlight SyntaxError exceptions for invalid generator expression in function calls (GH-28576)
1 parent a22be49 commit e5f13ce

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ invalid_arguments:
10571057
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, PyPegen_last_item(b, comprehension_ty)->target, "Generator expression must be parenthesized") }
10581058
| a=NAME b='=' expression for_if_clauses {
10591059
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?")}
1060-
| a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
1060+
| a=args b=for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a, b) }
10611061
| args ',' a=expression b=for_if_clauses {
10621062
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
10631063
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }

Lib/test/test_syntax.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,8 @@
12741274
class SyntaxTestCase(unittest.TestCase):
12751275

12761276
def _check_error(self, code, errtext,
1277-
filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
1277+
filename="<testcase>", mode="exec", subclass=None,
1278+
lineno=None, offset=None, end_lineno=None, end_offset=None):
12781279
"""Check that compiling code raises SyntaxError with errtext.
12791280
12801281
errtest is a regular expression that must be present in the
@@ -1294,6 +1295,11 @@ def _check_error(self, code, errtext,
12941295
self.assertEqual(err.lineno, lineno)
12951296
if offset is not None:
12961297
self.assertEqual(err.offset, offset)
1298+
if end_lineno is not None:
1299+
self.assertEqual(err.end_lineno, end_lineno)
1300+
if end_offset is not None:
1301+
self.assertEqual(err.end_offset, end_offset)
1302+
12971303
else:
12981304
self.fail("compile() did not raise SyntaxError")
12991305

@@ -1433,6 +1439,11 @@ def test_kwargs_last3(self):
14331439
self._check_error("int(**{'base': 10}, *['2'])",
14341440
"iterable argument unpacking follows "
14351441
"keyword argument unpacking")
1442+
1443+
def test_generator_in_function_call(self):
1444+
self._check_error("foo(x, y for y in range(3) for z in range(2) if z , p)",
1445+
"Generator expression must be parenthesized",
1446+
lineno=1, end_lineno=1, offset=11, end_offset=53)
14361447

14371448
def test_empty_line_after_linecont(self):
14381449
# See issue-40847

Parser/parser.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17880,15 +17880,15 @@ invalid_arguments_rule(Parser *p)
1788017880
}
1788117881
D(fprintf(stderr, "%*c> invalid_arguments[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "args for_if_clauses"));
1788217882
expr_ty a;
17883-
asdl_comprehension_seq* for_if_clauses_var;
17883+
asdl_comprehension_seq* b;
1788417884
if (
1788517885
(a = args_rule(p)) // args
1788617886
&&
17887-
(for_if_clauses_var = for_if_clauses_rule(p)) // for_if_clauses
17887+
(b = for_if_clauses_rule(p)) // for_if_clauses
1788817888
)
1788917889
{
1789017890
D(fprintf(stderr, "%*c+ invalid_arguments[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "args for_if_clauses"));
17891-
_res = _PyPegen_nonparen_genexp_in_call ( p , a );
17891+
_res = _PyPegen_nonparen_genexp_in_call ( p , a , b );
1789217892
if (_res == NULL && PyErr_Occurred()) {
1789317893
p->error_indicator = 1;
1789417894
D(p->level--);

Parser/pegen.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,8 +2551,17 @@ void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
25512551
return RAISE_SYNTAX_ERROR(msg);
25522552
}
25532553

2554+
2555+
static inline expr_ty
2556+
_PyPegen_get_last_comprehension_item(comprehension_ty comprehension) {
2557+
if (comprehension->ifs == NULL || asdl_seq_LEN(comprehension->ifs) == 0) {
2558+
return comprehension->iter;
2559+
}
2560+
return PyPegen_last_item(comprehension->ifs, expr_ty);
2561+
}
2562+
25542563
void *
2555-
_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args)
2564+
_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions)
25562565
{
25572566
/* The rule that calls this function is 'args for_if_clauses'.
25582567
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)
25662575
return NULL;
25672576
}
25682577

2569-
return RAISE_SYNTAX_ERROR_STARTING_FROM(
2578+
comprehension_ty last_comprehension = PyPegen_last_item(comprehensions, comprehension_ty);
2579+
2580+
return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
25702581
(expr_ty) asdl_seq_GET(args->v.Call.args, len - 1),
2582+
_PyPegen_get_last_comprehension_item(last_comprehension),
25712583
"Generator expression must be parenthesized"
25722584
);
25732585
}

Parser/pegen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ _RAISE_SYNTAX_ERROR_INVALID_TARGET(Parser *p, TARGETS_TYPE type, void *e)
327327
}
328328

329329
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
330-
void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args);
330+
void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
331331

332332

333333
// Generated function in parse.c - function definition in python.gram

0 commit comments

Comments
 (0)