Skip to content

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

Open

Description

@evle-zzz

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

Activity

  1. added
    type-bug
    on Oct 26, 2024
  2. added
    topic-repl
    on Oct 26, 2024
  3. added
    3.13
    3.14
    on Oct 26, 2024
  4. devdanzin commented on Oct 27, 2024

    @devdanzin
    Member

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

  5. evle-zzz commented on Oct 28, 2024

    @evle-zzz
    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.

  6. devdanzin commented on Oct 28, 2024

    @devdanzin
    Member

    "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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.133.14topic-repltype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions