[issue12920] Inspect.getsource fails to get source of local classes

New submission from Popa Claudiu <pcmanticore@gmail.com>: 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. ---------- assignee: docs@python components: Documentation messages: 143645 nosy: Popa.Claudiu, docs@python priority: normal severity: normal status: open title: Inspect.getsource fails to get source of local classes type: behavior versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Éric Araujo <merwok@netwok.org> added the comment:
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? ---------- nosy: +eric.araujo title: Inspect.getsource fails to get source of local classes -> inspect.getsource fails to get source of local classes versions: -Python 3.1 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Popa Claudiu <pcmanticore@gmail.com> added the comment: 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
---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Popa Claudiu <pcmanticore@gmail.com> added the comment: I forgot to mention that I executed this code directly in IDLE. It seems to work perfectly on command line though. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Changes by Richard Eames <naddiseo@gmail.com>: ---------- nosy: +Naddiseo _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Éric Araujo added the comment:
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. ---------- stage: -> needs patch title: inspect.getsource fails to get source of local classes -> Document that inspect.getsource only works for objects loaded from files, not interactive session versions: +Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Ned Deily added the comment: In duplicate Issue24491, zorceta notes: "Both python.exe and IDLE can't. IPython is able to, as it inserts REPL input into linecache." ---------- nosy: +ned.deily, zorceta versions: +Python 3.5, Python 3.6 -Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Zorceta added the comment: 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. ---------- 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 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________

Changes by Zorceta <zorceta@gmail.com>: ---------- components: +IDLE, Interpreter Core -Documentation _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12920> _______________________________________
participants (5)
-
Ned Deily
-
Popa Claudiu
-
Richard Eames
-
Zorceta
-
Éric Araujo