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 source inspection fails on closures #68673
Comments
Very simple to reproduce (see attachment). |
Looks like this is a regression in 3.5. |
I verified (Win7, but should be irrelevant) that test.py works on 2.7.10 and 3.4.3. The failure in 3.5.0b2 is omitting the body of inner. Add another line to inner and both are omitted, so 'body' seems correct Add another line to outer, and nothing is omitted. def outer():
def inner():
inner1
inner2
outer2 is displayed completely. So the omission is the body of an inner function that is the last statement of outer. This rule is not recursive, in the sense that for def outer():
def middle():
def inner():
inner1 "def inner ..." is entirely omitted, not just 'inner1'. The omission seems specific to 'def' as it does not occur with the other compound statements I tested (if, while). |
The test suite must lack, and therefore needs, a simple testcase as in test.py. In 3.4 and 3.5, getsource() joins the list of lines returned by getsourcelines(). In both versions, getsourcelines uses findsource(), which seems to be unchanged. In 3.5, the output of findsource for code, function, and method objects is postprocessed by _line_number_helper(code_object). The bug applies to methods also. class C:
def outer():
def inner():
inner1
from inspect import getsource
print(getsource(C.outer)) omits 'inner1', but getsource(C) does not. I believe the regression is due to ac86e5b2d45b in bpo-21217. (At first glance, it would seem that the fixup in _line_number_helper should be in the code object part of findsource itself.) I requested on that issue that the authors take a look at this. |
Thanks, Terry. I posted some findings to bpo-21217. |
Here is a first cut patch that moves things back to using a syntax-level |
Meador, the patch looks OK. Could you please commit it yourself? |
Will do. Thanks for the review. |
New changeset 4e42a62d5648 by Yury Selivanov in branch '3.5': New changeset 98a2bbf2cce2 by Yury Selivanov in branch 'default': |
Meador, I've reverted changes introduced in bpo-21217 -- I don't want to risk shipping 3.5beta4 with broken backwards compatibility. Feel free to rebase & commit your patch (I decorated test_decorator_with_lambda with @unittest.expectedFailure). |
New changeset 5400e21e92a7 by Meador Inge in branch '3.5': New changeset 0e7d64595223 by Meador Inge in branch 'default': |
Thanks Yury! I have committed my patches to 3.5 and default. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: