Skip to content

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

Closed

Description

@mhils

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

Activity

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

    @devdanzin
    Member

    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. added a commit that references this issue on Aug 14, 2024
  4. serhiy-storchaka commented on Aug 14, 2024

    @serhiy-storchaka
    Member

    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. added
    3.13only security fixes
    3.14bugs and security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    on Aug 14, 2024
  6. added a commit that references this issue on Aug 14, 2024
  7. mhils commented on Aug 14, 2024

    @mhils
    ContributorAuthor

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

  8. added a commit that references this issue on Aug 20, 2024
  9. added a commit that references this issue on Aug 20, 2024
  10. added a commit that references this issue on Aug 22, 2024
  11. added a commit that references this issue on Sep 2, 2024
  12. mhils commented on Sep 10, 2024

    @mhils
    ContributorAuthor

    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! 😃

  13. serhiy-storchaka commented on Sep 11, 2024

    @serhiy-storchaka
    Member

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

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 fixes3.14bugs and security fixesstdlibStandard 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