Skip to content

bpo-32012: Disallow trailing comma after genexpr without parenthesis. #4382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,20 @@ This section lists previously described changes and other bugfixes
that may require changes to your code.


Changes in Python behavior
--------------------------

* Due to an oversight, earlier Python versions erroneously accepted the
following syntax::

f(1 for x in [1],)

Python 3.7 now correctly raises a :exc:`SyntaxError`, as a generator
expression always needs to be directly inside a set of parentheses
and cannot have a comma on either side.
(Contributed by Serhiy Storchaka in :issue:`32012`.)


Changes in the Python API
-------------------------

Expand Down
21 changes: 18 additions & 3 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,32 @@

From ast_for_call():

>>> def f(it, *varargs):
>>> def f(it, *varargs, **kwargs):
... return list(it)
>>> L = range(10)
>>> f(x for x in L)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(x for x in L, 1)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
SyntaxError: Generator expression must be parenthesized
>>> f(x for x in L, y=1)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized
>>> f(x for x in L, *[])
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized
>>> f(x for x in L, **{})
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized
>>> f(L, x for x in L)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized
>>> f(x for x in L, y for y in L)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
SyntaxError: Generator expression must be parenthesized
>>> f(x for x in L,)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized
>>> f((x for x in L), 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SyntaxError is now correctly raised when a generator expression without
parenthesis is passed as an argument, but followed by a trailing comma.
A generator expression always needs to be directly inside a set of parentheses
and cannot have a comma on either side.
19 changes: 9 additions & 10 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
*/

int i, nargs, nkeywords, ngens;
int i, nargs, nkeywords;
int ndoublestars;
asdl_seq *args;
asdl_seq *keywords;
Expand All @@ -2721,28 +2721,27 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)

nargs = 0;
nkeywords = 0;
ngens = 0;
for (i = 0; i < NCH(n); i++) {
node *ch = CHILD(n, i);
if (TYPE(ch) == argument) {
if (NCH(ch) == 1)
nargs++;
else if (TYPE(CHILD(ch, 1)) == comp_for)
ngens++;
else if (TYPE(CHILD(ch, 1)) == comp_for) {
nargs++;
if (NCH(n) > 1) {
ast_error(c, ch, "Generator expression must be parenthesized");
return NULL;
}
}
else if (TYPE(CHILD(ch, 0)) == STAR)
nargs++;
else
/* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
nkeywords++;
}
}
if (ngens > 1 || (ngens && (nargs || nkeywords))) {
ast_error(c, n, "Generator expression must be parenthesized "
"if not sole argument");
return NULL;
}

args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
args = _Py_asdl_seq_new(nargs, c->c_arena);
if (!args)
return NULL;
keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Expand Down