Skip to content

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

Closed

Description

@ggenellina
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']

    Activity

    1. ggenellina commented on Aug 14, 2009

      ggenellinamannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Aug 14, 2009
    3. devdanzin commented on Apr 8, 2010

      devdanzinmannequin
      Mannequin

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

    4. jamesls commented on May 23, 2013

      jameslsmannequin
      Mannequin

      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.

    5. changed the title [-]inspect.getsource() returns incorrect source lines[/-] [+]inspect.getsource() returns incorrect source lines at the module level[/+] on Aug 17, 2018
    6. taleinat commented on Aug 24, 2018

      @taleinat
      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

    7. miss-islington commented on Aug 24, 2018

      @miss-islington
      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

    8. miss-islington commented on Aug 24, 2018

      @miss-islington
      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

    9. taleinat commented on Aug 26, 2018

      @taleinat
      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

    10. taleinat commented on Aug 26, 2018

      @taleinat
      Contributor

      Thanks for reporting this, Gabriel!

      Thanks for the PR, Vladimir!

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions