Skip to content

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

Closed
@mhils

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions