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

inspect.getsource() returns incorrect source lines at the module level #50949

Closed
ggenellina mannequin opened this issue Aug 14, 2009 · 8 comments
Closed

inspect.getsource() returns incorrect source lines at the module level #50949

ggenellina mannequin opened this issue Aug 14, 2009 · 8 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ggenellina
Copy link
Mannequin

ggenellina mannequin commented Aug 14, 2009

BPO 6700
Nosy @taleinat, @devdanzin, @PCManticore, @aivarannamaa, @1st1, @jamesls, @miss-islington
PRs
  • bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks #8864
  • [3.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) #8898
  • [3.6] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) #8899
  • [2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) #8900
  • Files
  • inspect.diff: patch against trunk
  • show_inspect_bug.py
  • issue6700.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 = None
    closed_at = <Date 2018-08-26.08:47:22.187>
    created_at = <Date 2009-08-14.04:19:21.635>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'inspect.getsource() returns incorrect source lines at the module level'
    updated_at = <Date 2018-08-26.08:47:22.186>
    user = 'https://bugs.python.org/ggenellina'

    bugs.python.org fields:

    activity = <Date 2018-08-26.08:47:22.186>
    actor = 'taleinat'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-08-26.08:47:22.187>
    closer = 'taleinat'
    components = ['Library (Lib)']
    creation = <Date 2009-08-14.04:19:21.635>
    creator = 'ggenellina'
    dependencies = []
    files = ['14716', '14717', '30344']
    hgrepos = []
    issue_num = 6700
    keywords = ['patch']
    message_count = 8.0
    messages = ['91541', '102651', '189842', '324000', '324002', '324004', '324114', '324115']
    nosy_count = 8.0
    nosy_names = ['ggenellina', 'taleinat', 'ajaksu2', 'Claudiu.Popa', 'Aivar.Annamaa', 'yselivanov', 'jamesls', 'miss-islington']
    pr_nums = ['8864', '8898', '8899', '8900']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6700'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @ggenellina
    Copy link
    Mannequin Author

    ggenellina mannequin commented Aug 14, 2009

    inspect.getsource(obj) returns incorrect results when obj is a
    traceback or frame object outside any function (that is, at the module
    level).

    This demo script shows the problem. The correct output should contain
    all source lines in the module, but it only returns the first two:

    D:\temp>type show_inspect_bug.py
    def foo(x):
    return y + z

    import inspect
    frame = inspect.currentframe()
    print inspect.getsource(frame)

    D:\temp>python show_inspect_bug.py
    def foo(x):
    return y + z

    The attached patch fixes the problem and adds some missing test cases.
    Originally reported by Juanjo Conti at the local Python group.

    @ggenellina ggenellina mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 14, 2009
    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Apr 8, 2010

    Confirmed in trunk and py3k. Also affects inspect.getsourcelines.

    @jamesls
    Copy link
    Mannequin

    jamesls mannequin commented May 23, 2013

    I confirmed the issue in tip. One of the issues with the original patch is
    that it modifies the tokeneater method used by getblock which won't work
    if the first token is any of the special cased tokens in the original patch
    ('@', 'def', 'class'). I've added additional tests that show where the
    original patch fails.

    An alternative approach is to check in getsourcelines whether or not we're
    dealing with a traceback or frame object in the module scope, and if so,
    return the lines of the entire module. I've attached an updated patch
    that implements this along with additional tests.

    @aivarannamaa aivarannamaa mannequin added the 3.7 (EOL) end of life label Aug 17, 2018
    @aivarannamaa aivarannamaa mannequin changed the title inspect.getsource() returns incorrect source lines inspect.getsource() returns incorrect source lines at the module level Aug 17, 2018
    @taleinat taleinat added the 3.8 only security fixes label Aug 23, 2018
    @taleinat
    Copy link
    Contributor

    New changeset 91cb298 by Tal Einat (Vladimir Matveev) in branch 'master':
    bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
    91cb298

    @miss-islington
    Copy link
    Contributor

    New changeset 0e707b4 by Miss Islington (bot) in branch '3.6':
    bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
    0e707b4

    @miss-islington
    Copy link
    Contributor

    New changeset 3e6020c by Miss Islington (bot) in branch '3.7':
    bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
    3e6020c

    @taleinat
    Copy link
    Contributor

    New changeset 491740f by Tal Einat in branch '2.7':
    [2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
    491740f

    @taleinat
    Copy link
    Contributor

    Thanks for reporting this, Gabriel!

    Thanks for the PR, Vladimir!

    @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
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants