Skip to content

Decorator with paren tokens in arguments breaks inspect.getsource #83035

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

Closed
crusaderky mannequin opened this issue Nov 19, 2019 · 4 comments
Closed

Decorator with paren tokens in arguments breaks inspect.getsource #83035

crusaderky mannequin opened this issue Nov 19, 2019 · 4 comments
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@crusaderky
Copy link
Mannequin

crusaderky mannequin commented Nov 19, 2019

BPO 38854
Nosy @PCManticore, @1st1, @tirkarthi, @crusaderky, @AlexWaygood, @ahopkins
PRs
  • bpo-38854: Adjust inspect.getsource to properly extract source for decorated functions #17374
  • bpo-46873: Fix inspect.getsource - Count parentheses open/close in decorators #31605
  • 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 2019-11-19.21:37:42.431>
    labels = ['type-bug', 'library', '3.9', '3.10']
    title = 'Decorator with paren tokens in arguments breaks inspect.getsource'
    updated_at = <Date 2022-02-28.11:08:51.171>
    user = 'https://github.com/crusaderky'

    bugs.python.org fields:

    activity = <Date 2022-02-28.11:08:51.171>
    actor = 'AlexWaygood'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-11-19.21:37:42.431>
    creator = 'crusaderky'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38854
    keywords = ['patch']
    message_count = 2.0
    messages = ['356993', '357432']
    nosy_count = 6.0
    nosy_names = ['Claudiu.Popa', 'yselivanov', 'xtreak', 'crusaderky', 'AlexWaygood', 'ahopkins']
    pr_nums = ['17374', '31605']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue38854'
    versions = ['Python 3.9', 'Python 3.10']

    Linked PRs

    @crusaderky
    Copy link
    Mannequin Author

    crusaderky mannequin commented Nov 19, 2019

    Python 3.7.5 and 3.8.0
    A decorator causes inspect.getsource() to return clipped output:

    from collections import defaultdict
    from functools import wraps
    import inspect
    
    
    def foo(*args):
        def decorator(func):
            @wraps(func)
            def wrapper():
                pass
            return wrapper
        return decorator
    
    @foo(dict(), defaultdict(lambda: 1))
    def f():
        pass
    
    print(inspect.getsource(f))

    Output:

    @foo(dict(), defaultdict(lambda: 1))
    

    Expected output:

    @foo(dict(), defaultdict(lambda: 1))
    def f():
        pass
    

    These changes to the decorator parameters cause the problem to disappear:

    • @foo({}, defaultdict(lambda: 1))
    • @foo(dict(), defaultdict(list))

    @crusaderky crusaderky mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life labels Nov 19, 2019
    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Nov 25, 2019

    Thanks for the report!

    Turns out this was a bug in inspect.BlockFinder which is responsible for extracting out the source code from a given block. This class was considering ")" inside a decorator call as closing the decorator itself, even when the decorator call was accepting additional arguments that needed parentheses, such as function calls or tuples.

    Just submitted a PR #17374 to address this issue.

    @blueyed blueyed mannequin added stdlib Python modules in the Lib dir 3.9 only security fixes 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Jul 9, 2020
    @blueyed blueyed mannequin changed the title Decorator breaks inspect.getsource Decorator with paren tokens in arguments breaks inspect.getsource Jul 9, 2020
    @AlexWaygood AlexWaygood removed 3.7 (EOL) end of life 3.8 (EOL) end of life labels Feb 28, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @carljm
    Copy link
    Member

    carljm commented Nov 21, 2022

    Looks like there have been three PRs here already, but all of them seem to be incomplete and/or abandoned. Opened a new one: #99654

    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 7, 2022
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 7, 2022
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 7, 2022
    miss-islington added a commit that referenced this issue Dec 7, 2022
    miss-islington added a commit that referenced this issue Dec 7, 2022
    @AlexWaygood
    Copy link
    Member

    AlexWaygood commented Dec 7, 2022

    Fixed and backported. Thanks @carljm! 🎉

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 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

    2 participants