You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.getsource only works for objects loaded from files, not interactive session #57129
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=Noneclosed_at=Nonecreated_at=<Date2011-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=<Date2020-11-30.02:13:01.074>user='https://github.com/PCManticore'
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.
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
PCManticore commented on Sep 9, 2011
PCManticoremannequin
MannequinAuthor
More actions
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:
importinspectclassA:
passinspect.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 findsourcefile= getsourcefile(object)
File "C:\Python32\lib\inspect.py", line 441, in getsourcefile
filename = getfile(object)
File "C:\Python32\lib\inspect.py", line 406, in getfileraiseTypeError('{!r} is a built-in class'.format(object))
TypeError: <module '__main__' (built-in)> is a built-in class
>>>
PCManticore commented on Sep 10, 2011
PCManticoremannequin
MannequinAuthor
More actions
I forgot to mention that I executed this code directly in IDLE. It seems to work perfectly on command line though.
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.
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
In duplicate bpo-24491, zorceta notes: "Both python.exe and IDLE can't. IPython is able to, as it inserts REPL input into linecache."
zorceta commented on Jun 24, 2015
zorcetamannequin
Mannequin
More actions
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.
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
Activity
PCManticore commentedon Sep 6, 2011
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.
merwok commentedon Sep 9, 2011
The error message makes me think that
getsource(__main__)was used, notgetsource(SomeClass). Can you check again?[-]Inspect.getsource fails to get source of local classes[/-][+]inspect.getsource fails to get source of local classes[/+]PCManticore commentedon Sep 9, 2011
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:
is:
PCManticore commentedon Sep 10, 2011
I forgot to mention that I executed this code directly in IDLE. It seems to work perfectly on command line though.
merwok commentedon Mar 25, 2013
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.
[-]inspect.getsource fails to get source of local classes[/-][+]Document that inspect.getsource only works for objects loaded from files, not interactive session[/+]ned-deily commentedon Jun 24, 2015
In duplicate bpo-24491, zorceta notes: "Both python.exe and IDLE can't. IPython is able to, as it inserts REPL input into linecache."
zorceta commentedon Jun 24, 2015
When provided object is not from a file, like input in interactive shell,
inspectinternals will check for it inlinecache, 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
linecachemay 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.
[-]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[/+]16 remaining items
pablogsal commentedon Oct 23, 2023
This works now as part of #110775
We can add some tests and close the issue
pythongh-57129: Add test for inspect.getsource in the REPL
pythongh-57129: Add test for inspect.getsource in the REPL
fixup! pythongh-57129: Add test for inspect.getsource in the REPL
liudonghua123 commentedon Oct 24, 2023
From greenplum-db/GreenplumPython-archive#139, I found I can use
dill.source.getsourceas fallback toinspect.getsource.merwok commentedon Oct 24, 2023
Please read previous comments: the next version of Python will fix this directly.
gh-57129: Add test for inspect.getsource in the REPL (#111197)
pythongh-57129: Add test for inspect.getsource in the REPL (python#11…
pythongh-57129: Add test for inspect.getsource in the REPL (python#11…