Author ztane
Recipients ztane
Date 2015-09-11.13:03:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441976624.09.0.810536371871.issue25070@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-09-11 13:03:44ztanesetrecipients: + ztane
2015-09-11 13:03:44ztanesetmessageid: <1441976624.09.0.810536371871.issue25070@psf.upfronthosting.co.za>
2015-09-11 13:03:44ztanelinkissue25070 messages
2015-09-11 13:03:43ztanecreate