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 — 3.13 Regression: `inspect.getsource()` returns incorrect source code · Issue #122981 · python/cpython

3.13 Regression: inspect.getsource() returns incorrect source code #122981

New issue
Copy link
New issue
Copy link
Closed
Closed
3.13 Regression: inspect.getsource() returns incorrect source code#122981
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard 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

@mhils
mhils
opened on Aug 13, 2024
Issue body actions

Bug report

Bug description:

In Python 3.13rc1, inspect.getsource() sometimes returns a "random" line for some objects.
For example, here's ssl.AlertDescription:

$ docker run --rm python:3.13-rc python -c "import inspect, ssl; print(inspect.getsource(ssl.AlertDescription))"
        super().shutdown(how)

This line is likely https://github.com/python/cpython/blob/v3.13.0rc1/Lib/ssl.py#L1334.

In contrast, on 3.12 and below, this raises an OSError (not ideal but also not wrong):

$ docker run --rm python:3.12-rc python -c "import inspect, ssl; print(inspect.getsource(ssl.AlertDescription))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.12/inspect.py", line 1285, in getsource
    lines, lnum = getsourcelines(object)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/inspect.py", line 1267, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/inspect.py", line 1112, in findsource
    raise OSError('could not find class definition')
OSError: could not find class definition

I'm not sure why this is happening, but it also affects pytest.PytestAssertRewriteWarning and pluggy.PluggyTeardownRaisedWarning, which both extend custom classes that override __module__.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

  • gh-122981: Fix inspect.getsource() for generated classes with Python base classes #123001
  • [3.13] gh-122981: Fix inspect.getsource() for generated classes with Python base classes (GH-123001) #123182

Activity

  1. mhils
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 13, 2024
  2. devdanzin commented on Aug 14, 2024

    @devdanzin
    devdanzin
    on Aug 14, 2024
    Member
    More actions

    Bisected to 153b3f7, introduced by #118475.

    It happens because class ssl.AlertDescription now has a __firstlineno__ attribute, and that is erroneously interpreted as a line number in ssl.py. It shares the same line number (1334) with other classes, like SSLErrorNumber, created via IntEnum._convert_. This line number corresponds to IntEnum's line number in enum.py instead.

    So ISTM the issue is that the construction of __firstlineno__ for these classes is capturing the line number of the class generator definition instead of the class creation site.

    cc @serhiy-storchaka

  3. serhiy-storchaka
    added a commit that references this issue on Aug 14, 2024

    pythongh-122981: Fix inspect.getsource() for generated classes with P…

    6ee8a73
  4. serhiy-storchaka commented on Aug 14, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 14, 2024
    Member
    More actions

    Thank you for your report @mhils. I agree with @devdanzin analysis. __firstlineno__ is not set for these enum classes, but inherited from the base class. There is a test for generated class, but it does not have a base class that has __firstlineno__.

    #123001 should fix this.

  5. serhiy-storchaka
    added
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Aug 14, 2024
  6. serhiy-storchaka
    added this to Inspect issues 🔍on Aug 14, 2024
  7. serhiy-storchaka
    moved this to In Progress in Inspect issues 🔍on Aug 14, 2024
  8. serhiy-storchaka
    added a commit that references this issue on Aug 14, 2024

    pythongh-122981: Fix inspect.getsource() for generated classes with P…

    36e96ba
  9. mhils commented on Aug 14, 2024

    @mhils
    mhils
    on Aug 14, 2024
    ContributorAuthor
    More actions

    Awesome - thank you two for getting to the bottom of this with lightning speed! ⚡🍰 😃

  10. serhiy-storchaka
    added a commit that references this issue on Aug 20, 2024

    gh-122981: Fix inspect.getsource() for generated classes with Python …

    f88c14d
  11. miss-islington
    added a commit that references this issue on Aug 20, 2024

    pythongh-122981: Fix inspect.getsource() for generated classes with P…

    f19222e
  12. blhsing
    added a commit that references this issue on Aug 22, 2024

    pythongh-122981: Fix inspect.getsource() for generated classes with P…

    7fb2041
  13. Yhg1s
    added a commit that references this issue on Sep 2, 2024

    [3.13] gh-122981: Fix inspect.getsource() for generated classes with …

    2b4c31d
  14. mhils commented on Sep 10, 2024

    @mhils
    mhils
    on Sep 10, 2024
    ContributorAuthor
    More actions

    Thank you two again! I can confirm that the issue is fixed now for ssl.AlertDescription with 3.13rc2. Unfortunately our smoke tests still show a very similar error in this case here:

    $ docker run --rm python:3.13.0rc2 bash -c \
        'pip install pluggy==1.5.0; python -c "import inspect, pluggy; print(inspect.getsource(pluggy.PluggyTeardownRaisedWarning))"'
    [...]
        "PluginManager",
    docker run --rm python:3.12 bash -c \
        'pip install pluggy==1.5.0; python -c "import inspect, pluggy; print(inspect.getsource(pluggy.PluggyTeardownRaisedWarning))"'
    [...]
    OSError: could not find class definition

    (https://github.com/mitmproxy/pdoc/actions/runs/10772390037/job/29935976461?pr=730)

    Would you like me to file a new issue, or do we keep it here?

    Thanks! 😃

  15. serhiy-storchaka commented on Sep 11, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 11, 2024
    Member
    More actions

    pluggy.PluggyTeardownRaisedWarning is not the part of the stdlib, so I cannot say what exactly happened, but this may be related to #123339.

  16. serhiy-storchaka
    closed this as completedon Sep 11, 2024
  17. github-project-automation
    moved this from In Progress to Done in Inspect issues 🔍on Sep 11, 2024
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.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard 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
      Done
      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.