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

wrong error messages when too many kwargs are received #75412

Closed
orenmn mannequin opened this issue Aug 17, 2017 · 6 comments
Closed

wrong error messages when too many kwargs are received #75412

orenmn mannequin opened this issue Aug 17, 2017 · 6 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@orenmn
Copy link
Mannequin

orenmn mannequin commented Aug 17, 2017

BPO 31229
Nosy @vstinner, @serhiy-storchaka, @orenmn
PRs
  • bpo-31229: fix wrong error messages when too many keyword arguments are received #3180
  • Files
  • issue31229_ver1.diff: diff file - ver1
  • 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 2017-08-23.18:19:03.308>
    created_at = <Date 2017-08-17.18:02:16.188>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'wrong error messages when too many kwargs are received'
    updated_at = <Date 2017-08-23.18:19:03.307>
    user = 'https://github.com/orenmn'

    bugs.python.org fields:

    activity = <Date 2017-08-23.18:19:03.307>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-08-23.18:19:03.308>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-08-17.18:02:16.188>
    creator = 'Oren Milman'
    dependencies = []
    files = ['47091']
    hgrepos = []
    issue_num = 31229
    keywords = ['patch']
    message_count = 6.0
    messages = ['300451', '300454', '300617', '300619', '300761', '300762']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['3180']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31229'
    versions = ['Python 3.7']

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Aug 17, 2017

    Some functions produce wrong error messages in case they receive too many
    keyword arguments:
    - in Objects/exceptions.c - ImportError_init:
        >>> ImportError(1, 2, 3, 4, a=5, b=6, c=7)
        TypeError: ImportError() takes at most 2 arguments (3 given)
    - in Python/bltinmodule.c - min_max:
        >>> min(1, 2, 3, 4, a=5, b=6, c=7)
        TypeError: function takes at most 2 arguments (3 given)
        >>> max(1, 2, 3, 4, a=5, b=6, c=7)
        TypeError: function takes at most 2 arguments (3 given)
    - in Modules/itertoolsmodule.c - product_new:
        >>> itertools.product(0, a=1, b=2, c=3, d=4, e=5, f=6)
        TypeError: product() takes at most 1 argument (6 given)
    - in Python/bltinmodule.c - builtin_print: 
        >>> print(0, a=1, b=2, c=3, d=4, e=5, f=6)
        TypeError: print() takes at most 4 arguments (6 given)

    ISTM that changing these error messages to refer to 'keyword arguments' instead
    of 'arguments' is a possible solution. (e.g. the last one would become
    'print() takes at most 4 keyword arguments (6 given)'

    To do that, I changed two 'takes at most' PyErr_Format calls in Python/getargs.c.
    I ran the test suite, and it seems that this patch doesn't break anything.
    The diff file is attached.
    (I didn't open a PR before hearing your opinion, as ISTM that changing code in
    getargs.c is a delicate thing.)

    what do you think?

    @orenmn orenmn mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Aug 17, 2017
    @serhiy-storchaka
    Copy link
    Member

    In all of these cases PyArg_ParseTupleAndKeywords() is used in uncommon way. These functions accept variable number of positional-only arguments, and PyArg_ParseTupleAndKeywords() is called with empty args tuple for parsing keyword arguments only. The patch uses a trick for detecting this case and generating more appropriate error message. Correct error message is generated in a normal case too.

    LGTM. But it may be worth to add a short comment about the necessary of this special case. And please add tests for error messages in all of these functions. test_call looks appropriate place for them.

    @serhiy-storchaka
    Copy link
    Member

    Could you please make a PR from your patch Oren?

    @orenmn
    Copy link
    Mannequin Author

    orenmn mannequin commented Aug 21, 2017

    I already wrote a patch, but I thought it would be better to wait until
    bpo-31236 is resolved.
    this is because bpo-31236 would change the error messages of min() and max(), and test_call tests exact error messages in
    CFunctionCallsErrorMessages, which is where I thought the tests of this
    issue should be added.

    @serhiy-storchaka
    Copy link
    Member

    New changeset bf9075a by Serhiy Storchaka (Oren Milman) in branch 'master':
    bpo-31229: Fixed wrong error messages when too many keyword arguments are received. (bpo-3180)
    bf9075a

    @serhiy-storchaka
    Copy link
    Member

    Thank you Oren for your contribution.

    @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.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant