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

inspect.getsource returns irrelevant text when used in repl on a class defined in place #126019

Open
evle-zzz opened this issue Oct 26, 2024 · 3 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@evle-zzz
Copy link

evle-zzz commented Oct 26, 2024

Bug report

Bug description:

Run interactive cpython 3.13 with following input.

>>> import inspect
>>> class B: pass
...
>>> inspect.getsource(B)
"# Important: don't add things to this module, as they will end up in the REPL's\n"

Instead of class code we get a piece of repl internals.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@evle-zzz evle-zzz added the type-bug An unexpected behavior, bug, or error label Oct 26, 2024
@Eclips4 Eclips4 added topic-repl Related to the interactive shell 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Oct 26, 2024
@devdanzin
Copy link
Contributor

Seems related to #122071 and its associated issues, probably due to how linecache works.

@evle-zzz
Copy link
Author

Just in case, similar small issue nearby:

>>> import dataclasses
>>> inspect.getsource(type(dataclasses)('test'))
Traceback (most recent call last):
  File "<python-input-72>", line 1, in <module>
    inspect.getsource(type(dataclasses)('test'))
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\evlez\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 1256, in getsource
    lines, lnum = getsourcelines(object)
                  ~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\evlez\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 1238, in getsourcelines
    lines, lnum = findsource(object)
                  ~~~~~~~~~~^^^^^^^^
  File "C:\Users\evlez\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 1060, in findsource
    file = getsourcefile(object)
  File "C:\Users\evlez\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 963, in getsourcefile
    filename = getfile(object)
  File "C:\Users\evlez\AppData\Local\Programs\Python\Python313\Lib\inspect.py", line 924, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'test'> is a built-in module

"built-in" is misleading here. That's actually a regular module.

@devdanzin
Copy link
Contributor

"built-in" is misleading here. That's actually a regular module.

We could check object.__spec__ (which is None for modules created in the REPL like you did). If there is a __spec__ then object.__spec__.origin can point to a source file, contain the string "(built-in)" or the string "(frozen)". That would allow for a better description of the module type in the error message.

Should we open a new issue for this?

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 topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants