bpo-46873: Fix inspect.getsource - Count parentheses open/close in decorators #31605
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).
Result:
This appears to happen because
BlockFinder.tokeneater
would naively close a decorator after encountering a closing patentheses:)
. As stated in the bug report: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