Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mhils opened this issue Aug 13, 2024 · 5 comments
Closed

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

mhils opened this issue Aug 13, 2024 · 5 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mhils
Copy link
Contributor

mhils commented Aug 13, 2024

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

@mhils mhils added the type-bug An unexpected behavior, bug, or error label Aug 13, 2024
@devdanzin
Copy link
Contributor

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

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 14, 2024
…ython base classes

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
@serhiy-storchaka
Copy link
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.

@serhiy-storchaka serhiy-storchaka added 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir labels Aug 14, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 14, 2024
…ython base classes

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
@mhils
Copy link
Contributor Author

mhils commented Aug 14, 2024

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

serhiy-storchaka added a commit that referenced this issue Aug 20, 2024
…base classes (GH-123001)

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 20, 2024
…ython base classes (pythonGH-123001)

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
(cherry picked from commit f88c14d)

Co-authored-by: Serhiy Storchaka <[email protected]>
blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
…ython base classes (pythonGH-123001)

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
Yhg1s pushed a commit that referenced this issue Sep 2, 2024
…Python base classes (GH-123001) (#123182)

gh-122981: Fix inspect.getsource() for generated classes with Python base classes (GH-123001)

Look up __firstlineno__ only in the class' dict, without searching in
base classes.
(cherry picked from commit f88c14d)

Co-authored-by: Serhiy Storchaka <[email protected]>
@mhils
Copy link
Contributor Author

mhils commented Sep 10, 2024

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

@serhiy-storchaka
Copy link
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
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants