Issue 18626: Make "python -m inspect " meaningful (original) (raw)

Created on 2013-08-02 03:12 by ncoghlan, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
inspect.patch Claudiu.Popa,2013-09-16 17:06 review
Messages (7)
msg194148 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-08-02 03:12
"python -m inspect " doesn't currently do anything. It would be handy if this: python -m inspect site Was roughly equivalent to: python -c "import inspect, site; print(inspect.getsource(site))" Even better would be if it understood entry point notation so you could use "modname:qualname" to get the source code of a particular item within a module.
msg194153 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2013-08-02 04:53
At the risk of unnecessary complication, there is also other information that could be output, depending on the referenced object (module vs. class/func via qualname). For instance, a module's __file__ would be handy. So, the output could have a "header" with the relevant info, followed by a blank line, and then the source code. I suppose something like that could be added later with a commandline option, like "python -m inspect --info site", rather than being default behavior. Then this issue can stay focused on the simpler idea. :) Regardless, your proposal sounds good to me. I don't see any other meaningful use for "-m inspect".
msg194699 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-08-08 17:30
I agree that it will be better to output a formalized report about a module and it's contents using inspect.getmoduleinfo() and inspect.getmembers().
msg195478 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-08-17 14:53
I realised that with the "module:qualname" syntax, it's straightforward to expand this beyond module introspection to arbitrary objects. What I suggest we could output: - a header with key module info (names taken from PEP 451): Origin Cached Submodule search path Loader (repr output) - for non-module objects, also include the line within the file (if it can be determined) - the output of getsource() if a "--source" option is given
msg197919 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-09-16 17:06
Hello, here's a basic patch. Currently, the header info is printed by default, while the source can be retrieved by using --source (although I would prefer them to be switched, the source should be shown by default and the header info only when requested). It understands the entry point notation, like 'unittest:util.safe_repr'.
msg198272 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-09-22 12:47
New changeset 2e1335245f8f by Nick Coghlan in branch 'default': Close #18626: add a basic CLI for the inspect module http://hg.python.org/cpython/rev/2e1335245f8f
msg198273 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2013-09-22 12:49
Thanks for the initial patch Claudiu - I tweaked it a bit before committing it. * as you suggested, displaying the source is the default, with a --details option to display the formatted info instead * changed the displayed details (e.g. only displaying the search path for packages, displaying the line number for classes and functions * a few tweaks to the actual implementation (e.g. using partition over find, avoiding tracebacks for a couple of likely user errors)
History
Date User Action Args
2022-04-11 14:57:48 admin set github: 62826
2013-09-22 12:49:39 ncoghlan set messages: +
2013-09-22 12:47:02 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: needs patch -> resolved
2013-09-16 17:06:22 Claudiu.Popa set files: + inspect.patchnosy: + Claudiu.Popamessages: + keywords: + patch
2013-08-17 14:53:53 ncoghlan set messages: + title: Make "python -m inspect " dump the source of a module -> Make "python -m inspect " meaningful
2013-08-08 17:30:52 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2013-08-08 16:44:14 ezio.melotti set nosy: + ezio.melotti
2013-08-02 04:53:11 eric.snow set nosy: + eric.snowmessages: +
2013-08-02 03:12:40 ncoghlan create