Skip to content
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

Closed
malthe mannequin opened this issue Jun 22, 2015 · 12 comments
Closed

Function source inspection fails on closures #68673

malthe mannequin opened this issue Jun 22, 2015 · 12 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@malthe
Copy link
Mannequin

malthe mannequin commented Jun 22, 2015

BPO 24485
Nosy @terryjreedy, @ncoghlan, @pitrou, @meadori, @1st1, @malthe
Files
  • test.py: Python script that demonstrates bug
  • issue24485-v0.patch
  • 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 = 'https://github.com/meadori'
    closed_at = <Date 2015-07-24.04:01:16.627>
    created_at = <Date 2015-06-22.09:41:28.658>
    labels = ['type-bug', 'library']
    title = 'Function source inspection fails on closures'
    updated_at = <Date 2015-07-24.04:01:16.626>
    user = 'https://github.com/malthe'

    bugs.python.org fields:

    activity = <Date 2015-07-24.04:01:16.626>
    actor = 'meador.inge'
    assignee = 'meador.inge'
    closed = True
    closed_date = <Date 2015-07-24.04:01:16.627>
    closer = 'meador.inge'
    components = ['Library (Lib)']
    creation = <Date 2015-06-22.09:41:28.658>
    creator = 'malthe'
    dependencies = []
    files = ['39768', '39825']
    hgrepos = []
    issue_num = 24485
    keywords = ['patch']
    message_count = 12.0
    messages = ['245621', '245634', '245862', '245867', '245873', '245914', '247041', '247060', '247203', '247204', '247244', '247246']
    nosy_count = 8.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'pitrou', 'meador.inge', 'python-dev', 'yselivanov', 'malthe', 'bkcsfi sfi']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24485'
    versions = ['Python 3.5', 'Python 3.6']

    @malthe
    Copy link
    Mannequin Author

    malthe mannequin commented Jun 22, 2015

    Very simple to reproduce (see attachment).

    @malthe malthe mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 22, 2015
    @1st1
    Copy link
    Member

    1st1 commented Jun 22, 2015

    Looks like this is a regression in 3.5.

    @terryjreedy
    Copy link
    Member

    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).

    @terryjreedy
    Copy link
    Member

    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.

    @1st1
    Copy link
    Member

    1st1 commented Jun 26, 2015

    Thanks, Terry. I posted some findings to bpo-21217.

    @meadori
    Copy link
    Member

    meadori commented Jun 28, 2015

    Here is a first cut patch that moves things back to using a syntax-level
    analysis instead of a bytecode-level one. I extended BlockFinder
    to skip decorators.

    @1st1
    Copy link
    Member

    1st1 commented Jul 21, 2015

    Meador, the patch looks OK. Could you please commit it yourself?

    @meadori
    Copy link
    Member

    meadori commented Jul 21, 2015

    Will do. Thanks for the review.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 23, 2015

    New changeset 4e42a62d5648 by Yury Selivanov in branch '3.5':
    Issue bpo-24485: Revert backwards compatibility breaking changes of bpo-21217.
    https://hg.python.org/cpython/rev/4e42a62d5648

    New changeset 98a2bbf2cce2 by Yury Selivanov in branch 'default':
    Merge 3.5 (issues bpo-21217, bpo-24485).
    https://hg.python.org/cpython/rev/98a2bbf2cce2

    @1st1
    Copy link
    Member

    1st1 commented Jul 23, 2015

    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).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 24, 2015

    New changeset 5400e21e92a7 by Meador Inge in branch '3.5':
    Issue bpo-24485: Function source inspection fails on closures.
    https://hg.python.org/cpython/rev/5400e21e92a7

    New changeset 0e7d64595223 by Meador Inge in branch 'default':
    Issue bpo-24485: Function source inspection fails on closures.
    https://hg.python.org/cpython/rev/0e7d64595223

    @meadori
    Copy link
    Member

    meadori commented Jul 24, 2015

    Thanks Yury! I have committed my patches to 3.5 and default.

    @meadori meadori closed this as completed Jul 24, 2015
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants