classification
Title: Make "python -m inspect " meaningful
Type: enhancement Stage: committed/rejected
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, eric.snow, ezio.melotti, ncoghlan, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-08-02 03:12 by ncoghlan, last changed 2013-09-22 12:49 by ncoghlan. 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: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-08-02 03:12
"python -m inspect <name>" 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: Nick 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: Claudiu.Popa (Claudiu.Popa) * 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) 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: Nick 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
2013-09-22 12:49:39ncoghlansetmessages: + msg198273
2013-09-22 12:47:02python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg198272

resolution: fixed
stage: needs patch -> committed/rejected
2013-09-16 17:06:22Claudiu.Popasetfiles: + inspect.patch

nosy: + Claudiu.Popa
messages: + msg197919

keywords: + patch
2013-08-17 14:53:53ncoghlansetmessages: + msg195478
title: Make "python -m inspect <name>" dump the source of a module -> Make "python -m inspect <name>" meaningful
2013-08-08 17:30:52serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg194699
2013-08-08 16:44:14ezio.melottisetnosy: + ezio.melotti
2013-08-02 04:53:11eric.snowsetnosy: + eric.snow
messages: + msg194153
2013-08-02 03:12:40ncoghlancreate