This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Decorator with paren tokens in arguments breaks inspect.getsource
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, Claudiu.Popa, ahopkins, crusaderky, xtreak, yselivanov
Priority: normal Keywords: patch

Created on 2019-11-19 21:37 by crusaderky, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 17374 closed Claudiu.Popa, 2019-11-25 08:50
PR 31605 ahopkins, 2022-02-28 11:07
Messages (2)
msg356993 - (view) Author: Guido Imperiale (crusaderky) * Date: 2019-11-19 21:37
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))
msg357432 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2019-11-25 08:53
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 https://github.com/python/cpython/pull/17374 to address this issue.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 83035
2022-02-28 11:08:51AlexWaygoodsetnosy: + AlexWaygood
2022-02-28 11:08:40AlexWaygoodsetnosy: + yselivanov

versions: - Python 3.7, Python 3.8
2022-02-28 11:07:22ahopkinssetnosy: + ahopkins
pull_requests: + pull_request29736
2020-07-09 22:46:02blueyedsettitle: Decorator breaks inspect.getsource -> Decorator with paren tokens in arguments breaks inspect.getsource
2020-07-09 22:43:54blueyedsettype: behavior
components: + Library (Lib)
versions: + Python 3.9, Python 3.10
2019-11-27 05:33:22xtreaksetnosy: + xtreak
2019-11-25 08:53:31Claudiu.Popasetnosy: + Claudiu.Popa
messages: + msg357432
2019-11-25 08:50:55Claudiu.Popasetkeywords: + patch
stage: patch review
pull_requests: + pull_request16857
2019-11-19 21:37:42crusaderkycreate