[New-bugs-announce] [issue25070] Python 2.6 - Python 3.4 allows unparenthesized generator with *args, **kw, forbidden in 3.5
Antti Haapala
report at bugs.python.org
Fri Sep 11 15:03:44 CEST 2015
New submission from Antti Haapala:
User DeTeReR asked a question (http://stackoverflow.com/questions/32521140/generator-as-function-argument) on Stack Overflow about a special case of code that seemed to work in Python 3.4:
f(1 for x in [1], *[2])
and
f(*[2], 1 for x in [1])
I found out that when Python 2.6 introduced the "keyword arguments after *args", the checks in ast.c did not follow:
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)) == gen_for)
ngens++;
else
nkeywords++;
}
}
if (ngens > 1 || (ngens && (nargs || nkeywords))) {
ast_error(n, "Generator expression must be parenthesized "
"if not sole argument");
return NULL;
}
the *args, **kwargs were not considered to be of type "argument" by the Grammar, and thus the error was not generated in this case.
Further down, the error "non-keyword arg after keyword arg" was not triggered in the case of sole unparenthesized generator expression.
Now, the parsing changes in 3.5 have disallowed all of these constructs:
f(1 for i in [42], **kw)
f(1 for i in [42], *args)
f(*args, 1 for i in [42])
which were (erroneously) allowed in previous versions.
I believe at least 3.5 release notes should mention this change.
----------
components: Interpreter Core
messages: 250468
nosy: ztane
priority: normal
severity: normal
status: open
title: Python 2.6 - Python 3.4 allows unparenthesized generator with *args, **kw, forbidden in 3.5
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25070>
_______________________________________
More information about the New-bugs-announce
mailing list