Skip to content

inspect.getsource only works for objects loaded from files, not interactive session #57129

Closed

Description

@PCManticore
BPO 12920
Nosy @terryjreedy, @ned-deily, @merwok, @bitdancer, @PCManticore, @Naddiseo, @BlckKnght, @native-api, @vmsp, @Ark-kun

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2011-09-06.18:37:57.848>
labels = ['interpreter-core', '3.8', 'expert-IDLE', 'type-bug', '3.7']
title = 'inspect.getsource only works for objects loaded from files, not interactive session'
updated_at = <Date 2020-11-30.02:13:01.074>
user = 'https://github.com/PCManticore'

bugs.python.org fields:

activity = <Date 2020-11-30.02:13:01.074>
actor = 'Ark-kun'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['IDLE', 'Interpreter Core']
creation = <Date 2011-09-06.18:37:57.848>
creator = 'Claudiu.Popa'
dependencies = []
files = []
hgrepos = []
issue_num = 12920
keywords = []
message_count = 15.0
messages = ['143645', '143773', '143790', '143831', '185179', '245714', '245721', '245723', '252846', '265815', '297768', '297797', '328836', '331659', '382107']
nosy_count = 12.0
nosy_names = ['terry.reedy', 'ned.deily', 'eric.araujo', 'r.david.murray', 'Claudiu.Popa', 'Naddiseo', 'Steven.Barker', 'Ivan.Pozdeev', 'zorceta', 'nikitakit', 'vmsp', 'Ark-kun']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12920'
versions = ['Python 3.7', 'Python 3.8']

Linked PRs

Activity

  1. PCManticore commented on Sep 6, 2011

    PCManticoremannequin
    MannequinAuthor

    inspect.getsource called with a class defined in the same file fails with
    TypeError: <module '__main__' (built-in)> is a built-in class, although the documentation says that:

    "The argument may be a module, class, method, function, traceback, frame,
    or code object. The source code is returned as a single string." I think that should be specified in documentation that this function works only for objects living in a module.

  2. merwok commented on Sep 9, 2011

    @merwok
    Member

    inspect.getsource called with a class defined in the same file fails
    with TypeError: <module '__main__' (built-in)> is a built-in class

    The error message makes me think that getsource(__main__) was used, not getsource(SomeClass). Can you check again?

  3. changed the title [-]Inspect.getsource fails to get source of local classes[/-] [+]inspect.getsource fails to get source of local classes[/+] on Sep 9, 2011
  4. PCManticore commented on Sep 9, 2011

    PCManticoremannequin
    MannequinAuthor

    Yes. On Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32, the result for the following lines:

    import inspect
    class A:
        pass
    inspect.getsource(A)

    is:

    Traceback (most recent call last):
      File "E:/Scripts/Snippets/test_inspect_bug.py", line 4, in <module>
        inspect.getsource(A)
      File "C:\Python32\lib\inspect.py", line 694, in getsource
        lines, lnum = getsourcelines(object)
      File "C:\Python32\lib\inspect.py", line 683, in getsourcelines
        lines, lnum = findsource(object)
      File "C:\Python32\lib\inspect.py", line 522, in findsource
        file = getsourcefile(object)
      File "C:\Python32\lib\inspect.py", line 441, in getsourcefile
        filename = getfile(object)
      File "C:\Python32\lib\inspect.py", line 406, in getfile
        raise TypeError('{!r} is a built-in class'.format(object))
    TypeError: <module '__main__' (built-in)> is a built-in class
    >>>
  5. PCManticore commented on Sep 10, 2011

    PCManticoremannequin
    MannequinAuthor

    I forgot to mention that I executed this code directly in IDLE. It seems to work perfectly on command line though.

  6. merwok commented on Mar 25, 2013

    @merwok
    Member

    It seems to work perfectly on command line though.

    If the code is saved in a file, yes, but not in an interactive interpreter. This is not actually related to IDLE, but to the fact that inspect.getsource merely finds the __file__ attribute of the module object for its argument. If a module object has no file, the error message indicates that it’s a built-in module (like sys), but this fails to take into account the special __main__ module in an interactive interpreter.

    It might be worth it to improve the error message, and in any case the documentation can be improved.

  7. changed the title [-]inspect.getsource fails to get source of local classes[/-] [+]Document that inspect.getsource only works for objects loaded from files, not interactive session[/+] on Mar 25, 2013
  8. ned-deily commented on Jun 24, 2015

    @ned-deily
    Member

    In duplicate bpo-24491, zorceta notes: "Both python.exe and IDLE can't. IPython is able to, as it inserts REPL input into linecache."

  9. zorceta commented on Jun 24, 2015

    zorcetamannequin
    Mannequin

    When provided object is not from a file, like input in interactive shell, inspect internals will check for it in linecache, which official Python shell and IDLE won't put interactive shell input into, yet. This can be simply solved.

    Whether interactive shell input can be put into linecache may be a problem, but it'll make life easier, as interactive shell saves time from edit-save-run 'loop'.

    btw, I changed the title, since I don't think, what original author thought need to be documented, is absolutely right.

  10. changed the title [-]Document that inspect.getsource only works for objects loaded from files, not interactive session[/-] [+]inspect.getsource only works for objects loaded from files, not interactive session[/+] on Jun 24, 2015
  11. 16 remaining items

  12. pablogsal commented on Oct 23, 2023

    @pablogsal
    Member

    This works now as part of #110775

    We can add some tests and close the issue

  13. added 3 commits that reference this issue on Oct 23, 2023
  14. liudonghua123 commented on Oct 24, 2023

    @liudonghua123

    From greenplum-db/GreenplumPython-archive#139, I found I can use dill.source.getsource as fallback to inspect.getsource.

    6d167b52a45059c758348e43584e5a7

  15. merwok commented on Oct 24, 2023

    @merwok
    Member

    Please read previous comments: the next version of Python will fix this directly.

  16. added
    3.13only security fixes
    and removed on Oct 24, 2023
  17. added a commit that references this issue on Oct 25, 2023
  18. added a commit that references this issue on Feb 11, 2024
  19. added a commit that references this issue on Sep 2, 2024
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 fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions