Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37c0844

Browse files
committed
Fix SF bug #1167751, Argument genexp corner case
Incorrect code was generated for: foo(a = i for i in range(10)) This should have generated a SyntaxError. Fix the Grammar so it raises a SyntaxError and test it. I'm uncertain whether this should be backported. It makes something that was Syntactically valid invalid. However, the code would either be completely broken or do the wrong thing.
1 parent c0d5faa commit 37c0844

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ dictmaker: test ':' test (',' test ':' test)* [',']
102102
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
103103

104104
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
105-
argument: [test '='] test [gen_for] # Really [keyword '='] test
105+
argument: test [gen_for] | test '=' test ['(' gen_for ')'] # Really [keyword '='] test
106106

107107
list_iter: list_for | list_if
108108
list_for: 'for' exprlist 'in' testlist_safe [list_iter]

Lib/test/test_genexps.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282
...
8383
SyntaxError: invalid syntax
8484
85+
Verify that parenthesis are required when used as a keyword argument value
86+
87+
>>> dict(a = i for i in xrange(10))
88+
Traceback (most recent call last):
89+
...
90+
SyntaxError: invalid syntax
91+
92+
Verify that parenthesis are required when used as a keyword argument value
93+
94+
>>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS
95+
{'a': <generator object at ...>}
96+
8597
Verify early binding for the outermost for-expression
8698
8799
>>> x=10
@@ -125,12 +137,12 @@
125137
>>> (y for y in (1,2)) = 10
126138
Traceback (most recent call last):
127139
...
128-
SyntaxError: assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[38]>, line 1)
140+
SyntaxError: assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[40]>, line 1)
129141
130142
>>> (y for y in (1,2)) += 10
131143
Traceback (most recent call last):
132144
...
133-
SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[39]>, line 1)
145+
SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_genexps.__test__.doctests[41]>, line 1)
134146
135147
136148
########### Tests borrowed from or inspired by test_generators.py ############

Python/graminit.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,26 +1496,34 @@ static arc arcs_69_0[1] = {
14961496
{26, 1},
14971497
};
14981498
static arc arcs_69_1[3] = {
1499-
{25, 2},
1500-
{147, 3},
1499+
{147, 2},
1500+
{25, 3},
15011501
{0, 1},
15021502
};
15031503
static arc arcs_69_2[1] = {
1504-
{26, 4},
1504+
{0, 2},
15051505
};
15061506
static arc arcs_69_3[1] = {
1507-
{0, 3},
1507+
{26, 4},
15081508
};
15091509
static arc arcs_69_4[2] = {
1510-
{147, 3},
1510+
{13, 5},
15111511
{0, 4},
15121512
};
1513-
static state states_69[5] = {
1513+
static arc arcs_69_5[1] = {
1514+
{147, 6},
1515+
};
1516+
static arc arcs_69_6[1] = {
1517+
{15, 2},
1518+
};
1519+
static state states_69[7] = {
15141520
{1, arcs_69_0},
15151521
{3, arcs_69_1},
15161522
{1, arcs_69_2},
15171523
{1, arcs_69_3},
15181524
{2, arcs_69_4},
1525+
{1, arcs_69_5},
1526+
{1, arcs_69_6},
15191527
};
15201528
static arc arcs_70_0[2] = {
15211529
{146, 1},
@@ -1806,7 +1814,7 @@ static dfa dfas[79] = {
18061814
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000"},
18071815
{324, "arglist", 0, 8, states_68,
18081816
"\000\040\010\060\000\000\000\000\000\000\000\000\000\002\000\140\010\111\023\000\000"},
1809-
{325, "argument", 0, 5, states_69,
1817+
{325, "argument", 0, 7, states_69,
18101818
"\000\040\010\000\000\000\000\000\000\000\000\000\000\002\000\140\010\111\023\000\000"},
18111819
{326, "list_iter", 0, 2, states_70,
18121820
"\000\000\000\000\000\000\000\000\000\000\000\042\000\000\000\000\000\000\000\000\000"},

0 commit comments

Comments
 (0)