GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `inspect.{getsourcelines, findsource}` return 0 as the starting line number for modules · Issue #103319 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
inspect.{getsourcelines, findsource} return 0 as the starting line number for modules#103319
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@artemmukhin
artemmukhin
opened on Apr 6, 2023
Issue body actions

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

  • GH-103319: Fix inspect.getsourcelines() to return 1-based line numbers #103226

Activity

  1. artemmukhin
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 6, 2023
  2. artemmukhin
    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
  3. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Apr 6, 2023
  4. serhiy-storchaka
    added this to Inspect issues 🔍on Mar 5, 2024
  5. George-Ogden commented on Jul 20, 2025

    @George-Ogden
    George-Ogden
    on Jul 20, 2025
    Contributor
    More actions

    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/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Inspect issues 🔍
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.