Skip to content

bpo-46873: Fix inspect.getsource - Count parentheses open/close in decorators #31605

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
wants to merge 1 commit into from

Conversation

ahopkins
Copy link

@ahopkins ahopkins commented Feb 27, 2022

DISCLAIMER: First time contribution to cPython itself. I have read thru the contribution guide, but please LMK if I am missing anything.

This PR fixes an issue where calls inside of a lambda passed to a decorator can prematurely breakout of the current code block when using inspect.getsource (and lower level funcs).

Prior to this PR, the following code would produce an unexpected result (only getting the decorator line of the source code).

from inspect import getsource


def bar(*funcs):
    def decorator(func):
        return func

    return decorator


@bar(lambda x: bool(True), lambda x: False)
def foo():
    ...


print(getsource(foo))

Result:

@bar(lambda x: bool(True), lambda x: False)

This appears to happen because BlockFinder.tokeneater would naively close a decorator after encountering a closing patentheses: ). As stated in the bug report:

... it seems like this requires the following conditions to be true:

  • lambdas are passed in decorator arguments
  • there is more than one lambda
  • at least one of the lambdas has a function call

The change that I made moves BlockFinder.decoratorhasargs to a computed property so that it should at least still be readable without being a breaking change. Please let me know if this (and the change that it introduces) is an acceptable solution.

Also, I opted to make a new inspect_fodder3.py because the alternative (while trying to keep like-code together) meant redoing all of the numbering of ...fodder2.py and a significant number of tests. This seemed a far nicer solution to not have to touch a significant majority of the inspect module tests. Happy to hear if this is acceptable.

https://bugs.python.org/issue46873

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@ahopkins

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@ahopkins
Copy link
Author

See also #21425. Seems to be a duplicate, sorry I didn't find this earlier.

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 28, 2022

Given that #21425 has been dormant for 2 years, and still does not have a test added, I think it would be fine to reopen this and link it to bpo-38854.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants