Skip to content

inspect.getsourcefile and inspect.getcomments do not work with decorated functions #78486

Open

Description

@eric-wieser
BPO 34305
Nosy @1st1, @eric-wieser, @tirkarthi
PRs
  • gh-78486: Unwrap decorators in getfile, rather than downstream #8602
  • 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 = None
    created_at = <Date 2018-08-01.07:20:53.875>
    labels = ['library']
    title = 'inspect.getsourcefile and inspect.getcomments do not work with decorated functions'
    updated_at = <Date 2020-03-12.09:29:43.433>
    user = 'https://github.com/eric-wieser'

    bugs.python.org fields:

    activity = <Date 2020-03-12.09:29:43.433>
    actor = 'Eric Wieser'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-08-01.07:20:53.875>
    creator = 'Eric.Wieser'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34305
    keywords = ['patch']
    message_count = 4.0
    messages = ['322847', '324704', '363448', '364006']
    nosy_count = 5.0
    nosy_names = ['yselivanov', 'Eric.Wieser', 'rmccampbell7', 'xtreak', 'Eric Wieser']
    pr_nums = ['8602']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue34305'
    versions = []

    Activity

    1. eric-wieser commented on Aug 1, 2018

      eric-wiesermannequin
      MannequinAuthor

      Following on from https://bugs.python.org/issue1764286 - inspect.getsourcefile and inspect.getcomments fall afoul of the same problem.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Aug 1, 2018
    3. eric-wieser commented on Sep 6, 2018

      eric-wiesermannequin
      MannequinAuthor
    4. rmccampbell7 commented on Mar 5, 2020

      rmccampbell7mannequin
      Mannequin

      This seems like a pretty straightforward fix. What's holding it up?

    5. EricWieser commented on Mar 12, 2020

      EricWiesermannequin
      Mannequin

      Nothing holding it up from my end, just waiting on someone to review it. Perhaps this ping will help with that.

    6. transferred this issue fromon Apr 10, 2022
    7. guy4261 commented on Feb 13, 2025

      @guy4261

      Not a fix in any way, but something that got me to where I wanted (which was to get references to quickly open in my IDE):

      import importlib
      import inspect
      
      import_name = "foo.bar.some_module.some_function"
      mod_name, attr_name = import_name.rsplit(".", 1)
      mod = importlib.import_module(mod_name)
      filename = inspect.getsourcefile(mod)[len(gitroot):].strip(os.path.sep)
      for lineno, codeline in enumerate(open(filename)):
          if f"def {attr_name}(" in codeline:
              print(f"{filename}:{lineno + 1}: {codeline}")
    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

      stdlibStandard Library Python modules in the Lib/ directory

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions