Skip to content

inspect.{getsourcelines, findsource} return 0 as the starting line number for modules #103319

Open

Description

@artemmukhin

Bug report

Run the following program

import inspect
import inspect
_, lineno1 = inspect.getsourcelines(inspect.currentframe())
_, lineno2 = inspect.findsource(inspect.currentframe())
print(lineno1, lineno2)

Expected Result
The line numbers are expected to be 1-based, so the expected output is 1 1.

Actual Result
The actual output is 0 0.

Related discussion: #103226

Your environment

  • CPython versions tested on: main branch
  • Operating system and architecture: macOS

Linked PRs

Activity

  1. changed the title [-]`inspect.getsourcelines` returns 0 as the starting line number for modules[/-] [+]`inspect.{getsourcelines, findsource}` return 0 as the starting line number for modules[/+] on Apr 6, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 6, 2023
  3. George-Ogden commented on Jul 20, 2025

    @George-Ogden
    Contributor

    inspect.getsourcelines(frame) seems to return the 1-based index or 0 (when the whole file is returned).

    Here are some examples:

    frame = inspect.currentframe()
    inspect.getsourcelines(frame) # _, 0
    def foo(): # start of file
        import inspect
        frame = inspect.currentframe()
        inspect.getsourcelines(frame) # _, 1
    
    foo()
    import inspect
    
    ...
    
    def bar():
        frame = inspect.currentframe()
        inspect.getsourcelines(frame) # _, n (1-based index)
    
    bar()

    Currently, I work around this with:

    source, offset = inspect.getsourcelines(frame)
    offset = max(1, offset)
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/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions