Skip to content
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

Curiosity: f((a)=1) is not a syntax error -- why? #78822

Closed
gvanrossum opened this issue Sep 12, 2018 · 6 comments
Closed

Curiosity: f((a)=1) is not a syntax error -- why? #78822

gvanrossum opened this issue Sep 12, 2018 · 6 comments
Labels
3.8 only security fixes

Comments

@gvanrossum
Copy link
Member

BPO 34641
Nosy @gvanrossum, @benjaminp, @serhiy-storchaka, @emilyemorehouse, @tirkarthi
PRs
  • bpo-34641: Further restrict the LHS of keyword argument function call syntax. #9212
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-09-13.00:14:42.640>
    created_at = <Date 2018-09-12.00:31:54.088>
    labels = ['3.8']
    title = 'Curiosity: f((a)=1) is not a syntax error -- why?'
    updated_at = <Date 2018-09-13.00:14:42.638>
    user = 'https://github.com/gvanrossum'

    bugs.python.org fields:

    activity = <Date 2018-09-13.00:14:42.638>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-13.00:14:42.640>
    closer = 'benjamin.peterson'
    components = []
    creation = <Date 2018-09-12.00:31:54.088>
    creator = 'gvanrossum'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34641
    keywords = ['patch']
    message_count = 6.0
    messages = ['325107', '325111', '325123', '325125', '325126', '325219']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'benjamin.peterson', 'serhiy.storchaka', 'emilyemorehouse', 'xtreak']
    pr_nums = ['9212']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue34641'
    versions = ['Python 3.8']

    @gvanrossum
    Copy link
    Member Author

    Emily and I just discovered that f((a)=1) is accepted and compiled the same as f(a=1). This goes against the intention that keyword arguments have the syntax f(NAME=expr).

    I suspect that this behavior was introduced at the time we switched from generating from the (concrete) parse tree to first converting to an ast and then generating code from there. I.e. in the distant past (long before 2.7 even).

    But I still think it ought to be fixed. Thoughts? Anyone have an idea *where* to fix it?

    @gvanrossum gvanrossum added the 3.8 only security fixes label Sep 12, 2018
    @benjaminp
    Copy link
    Contributor

    I expect you'd have to make the check of test nodes in ast.c stricter. Here's a slightly gross implementation of that:

    diff --git a/Python/ast.c b/Python/ast.c
    index 94962e00c7..b7cebf4777 100644
    --- a/Python/ast.c
    +++ b/Python/ast.c
    @@ -2815,15 +2815,22 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
                     identifier key, tmp;
                     int k;
     
    -                /* chch is test, but must be an identifier? */
    -                e = ast_for_expr(c, chch);
    -                if (!e)
    +                static const int chain[] = {test, or_test, and_test, not_test, comparison, expr, xor_expr, and_expr, shift_expr, arith_expr, term, factor, power, atom_expr, atom, 0};
    +
    +                node *expr_node = chch;
    +                for (int i = 0; chain[i]; i++) {
    +                    if (TYPE(expr_node) != chain[i])
    +                        break;
    +                    if (NCH(expr_node) != 1)
    +                        break;
    +                    expr_node = CHILD(expr_node, 0);
    +                }
    +                if (TYPE(expr_node) != NAME) {
    +                    ast_error(c, chch,
    +                            "keyword can't be an expression");
                         return NULL;
    -                /* f(lambda x: x[0] = 3) ends up getting parsed with
    -                 * LHS test = lambda x: x[0], and RHS test = 3.
    -                 * SF bug 132313 points out that complaining about a keyword
    -                 * then is very confusing.
    -                 */
    +                }
    +                e = ast_for_expr(c, chch);
                     if (e->kind == Lambda_kind) {
                         ast_error(c, chch,
                                 "lambda cannot contain assignment");

    @tirkarthi
    Copy link
    Member

    There seems to be some of the similar cases that have tests added as part of c960f26 and the original error message added at 3e0055f.

    Existing tests :

    >>> f(x()=2)
    Traceback (most recent call last):
    SyntaxError: keyword can't be an expression
    >>> f(a or b=1)
    Traceback (most recent call last):
    SyntaxError: keyword can't be an expression
    >>> f(x.y=1)
    Traceback (most recent call last):
    SyntaxError: keyword can't be an expression

    Thanks

    @serhiy-storchaka
    Copy link
    Member

    Could you please create a PR Benjamin? And please keep the comment about special casing lambda.

    See also bpo-30858 about the wording of the error message.

    @tirkarthi
    Copy link
    Member

    Tested the patch.

    ➜  cpython git:(master) ✗ ./python.exe
    Python 3.8.0a0 (heads/master-dirty:731ff68eee, Sep 12 2018, 11:40:16)
    [Clang 7.0.2 (clang-700.1.81)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def foo(a): pass
    ...
    >>> foo((a)=1)
      File "<stdin>", line 1
    SyntaxError: keyword can't be an expression

    ➜ cpython git:(master) ✗ ./python.exe -m unittest -v test.test_syntax
    test_assign_call (test.test_syntax.SyntaxTestCase) ... ok
    test_assign_del (test.test_syntax.SyntaxTestCase) ... ok
    test_bad_outdent (test.test_syntax.SyntaxTestCase) ... ok
    test_break_outside_loop (test.test_syntax.SyntaxTestCase) ... ok
    test_global_param_err_first (test.test_syntax.SyntaxTestCase) ... ok
    test_kwargs_last (test.test_syntax.SyntaxTestCase) ... ok
    test_kwargs_last2 (test.test_syntax.SyntaxTestCase) ... ok
    test_kwargs_last3 (test.test_syntax.SyntaxTestCase) ... ok
    test_no_indent (test.test_syntax.SyntaxTestCase) ... ok
    test_nonlocal_param_err_first (test.test_syntax.SyntaxTestCase) ... ok
    test_unexpected_indent (test.test_syntax.SyntaxTestCase) ... ok

    ----------------------------------------------------------------------
    Ran 11 tests in 0.006s

    OK

    Thanks

    @benjaminp
    Copy link
    Contributor

    New changeset c9a71dd by Benjamin Peterson in branch 'master':
    closes bpo-34641: Further restrict the LHS of keyword argument function call syntax. (GH-9212)
    c9a71dd

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants