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

function with modified __name__ uses original name when there's an arg error #48572

Closed
erickt mannequin opened this issue Nov 14, 2008 · 10 comments
Closed

function with modified __name__ uses original name when there's an arg error #48572

erickt mannequin opened this issue Nov 14, 2008 · 10 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@erickt
Copy link
Mannequin

erickt mannequin commented Nov 14, 2008

BPO 4322
Nosy @jcea, @benjaminp, @bitdancer, @berkerpeksag, @vadmium, @iritkatriel
Dependencies
  • bpo-2786: Names in function call exception should have class names, if they're methods
  • Files
  • issue4322.patch
  • 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 = None
    created_at = <Date 2008-11-14.06:44:18.058>
    labels = ['type-bug', 'library', '3.11']
    title = "function with modified __name__ uses original name when there's an arg error"
    updated_at = <Date 2021-11-26.16:39:47.344>
    user = 'https://bugs.python.org/erickt'

    bugs.python.org fields:

    activity = <Date 2021-11-26.16:39:47.344>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2008-11-14.06:44:18.058>
    creator = 'erickt'
    dependencies = ['2786']
    files = ['34318']
    hgrepos = []
    issue_num = 4322
    keywords = ['patch']
    message_count = 7.0
    messages = ['75853', '75886', '194424', '212988', '213183', '249573', '407068']
    nosy_count = 8.0
    nosy_names = ['jcea', 'benjamin.peterson', 'erickt', 'r.david.murray', 'berker.peksag', 'martin.panter', 'mchelem', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4322'
    versions = ['Python 3.11']

    Linked PRs

    @erickt
    Copy link
    Mannequin Author

    erickt mannequin commented Nov 14, 2008

    I ran into a case where I modified the __name__ attribute of a function
    and then didn't specify the right number of arguments, and I got a
    TypeError that used the original function name, as demonstrated here:

    >>> def foo(): pass
    ... 
    >>> foo.__name__ = 'bar'
    >>> foo(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes no arguments (1 given)

    I would have expected it to say "TypeError: bar() ...". I'm guessing
    that the interpreter isn't using the __name__ attribute in this case.

    @erickt erickt mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 14, 2008
    @benjaminp
    Copy link
    Contributor

    This is because these errors use the code object's name attribute
    (f.func_code.co_name) for error messages.

    @benjaminp
    Copy link
    Contributor

    I think fixing this is a valid request.

    @benjaminp benjaminp reopened this Aug 4, 2013
    @benjaminp benjaminp removed the invalid label Aug 4, 2013
    @mchelem
    Copy link
    Mannequin

    mchelem mannequin commented Mar 9, 2014

    func_name was not available on the places where the error strings are set (PyErr_Format), so I added it and passed it around as needed.

    This is the simplest approach, but I am not sure it is the best (newbie here). Thus, I am waiting for your comments to improve the solution.

    pjenvey noted I cannot just change PyEval_EvalCodeEx, since it is part of the interface. I intend to write a new function and call it from the old interface, setting func_name to co_name.

    Before I do that, I want to check if this design is correct. Also, I am not sure what this new function would be called, I am not even sure what the "Ex" means in PyEval_EvalCodeEx.

    @bitdancer
    Copy link
    Member

    The 'Ex' generally means "this is a public API but we needed to change it, so we made a new function with an extended API". Which means yours would be PyEval_EvalCodeExEx, I suppose :)

    Seriously, though, I don't know what we actually do in a case like this. I don't touch the C code very often myself. Hopefully Benjamin will find time to take a look.

    @vadmium
    Copy link
    Member

    vadmium commented Sep 2, 2015

    bpo-2786 currently proposes to pass __qualname__; if accepted that might also satisfy this report.

    @iritkatriel
    Copy link
    Member

    Reproduced on 3.11:

    >>> def foo(): pass
    ... 
    >>> foo.__name__ = 'bar'
    >>> foo(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes 0 positional arguments but 1 was given

    @furkanonder
    Copy link
    Sponsor Contributor

    When too many arguments are passed to a function, a TypeError is generated using __qualname__. I created a PR that solves the problem by updating the __qualname__ attribute when the function's __name__ attribute is changed. I know it's not a very good solution so I'm waiting for your ideas and suggestions.

    @rhettinger
    Copy link
    Contributor

    I'm not sure I agree that there is a problem. ISTM that reporting the original __qualname__ is useful because it can be related back the original source code. Presumably, that is what you really want when tracking down a bug.

    @JelleZijlstra
    Copy link
    Member

    I also don't see a problem here. If you change __qualname__, the error message reflects that:

    >>> def foo(): pass
    ... 
    >>> foo.__qualname__ = "bar"
    >>> foo(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: bar() takes 0 positional arguments but 1 was given
    

    @iritkatriel iritkatriel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants