Title: add program passed as string to dis module.
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CCLDArjun, eamanu, ncoghlan, serhiy.storchaka, steven.daprano, terry.reedy, yselivanov
Priority: normal Keywords: patch

Created on 2021-06-13 00:44 by CCLDArjun, last changed 2021-07-23 11:13 by ncoghlan.

File name Uploaded Description Edit
dis.patch CCLDArjun, 2021-06-13 00:44
Pull Requests
URL Status Linked Edit
PR 26714 open CCLDArjun, 2021-06-13 23:23
Messages (8)
msg395720 - (view) Author: Arjun (CCLDArjun) * Date: 2021-06-13 00:44
python dis module should have a program passed in as string.

Currently, you can do this:
$ echo "x = 6" | python -m dis /dev/stdin
  1           0 LOAD_CONST               0 (6)
              2 STORE_NAME               0 (x)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE

would be convenient like this:
$ ./python.exe -m dis -c "x=6"
  1           0 LOAD_CONST               0 (6)
              2 STORE_NAME               0 (x)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE

these changes seem to be working.
msg395722 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-13 02:34
I checked and there is no mention of dis having a command-line interface.  This suggests that _test is likely present only for testing the module, not for using it.  This was common a couple of decades ago before we had unittests.  If _test were considered obsolete, it could be removed.

By suggesting that the command line interface by upgraded, I suspect that you are implicitly suggesting that it be considered 'official'.  If so, that '_test' should become '_main' (where the underscore leaves it out of __all__), and the CLI documented as in other modules with a CLI.  (I am not up on the details and policy around this issue.)

It may be appropriate to start a discussion of python-ideas.
msg395723 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-13 02:44
What is unusual, I think, for a CLI test function is that it requires a passed in argument.  As a sanity-check test, 'python -m dis' could (should) disassemble itself.  Perhaps there once were some true tests that were moved to unittests.  (I started IDLE unittests by doing this with module test functions.)  If you know git, you could check the history and checkin messages.
msg395724 - (view) Author: Arjun (CCLDArjun) * Date: 2021-06-13 03:27
Huh, that's actually weird that the "exisisting command line interface" (quotes because it was added as a test) isn't official. I've found it to be convinient as a newcomer to the cpython codebase. 

> What is unusual, I think, for a CLI test function is that it requires a passed in argument

git blame shows the commit hash is 095668914c3, which is for bpo: "python -m dis now uses argparse". The Misc/HISTORY file references the change in section "What's New in Python 3.4.0 Alpha 3?"

Regardless of the cli being official, personally I think, it's a good feature to add. But also, maybe I should start a discussion in python-ideas about making it official?
msg395725 - (view) Author: Arjun (CCLDArjun) * Date: 2021-06-13 03:33
> If _test were considered obsolete, it could be removed.

Yup, originally it was added 2 decades ago (in 2000) originally as a test: 1fdae12c932
msg395727 - (view) Author: Arjun (CCLDArjun) * Date: 2021-06-13 03:57
> If _test were considered obsolete, it could be removed.

removing _test: does not break dis tests.
msg395731 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-13 08:12
I often use the command-line interface of dis, ast, and sometimes tokenize modules. They are mature enough and not only for self-testing. If they are not documented, we need to document them.

Since they read from stdin by default (no need to specify /dev/stdin or CON explicitly) I never needed the -c option. It would not be particularly useful in any case because Python code is usually multi-line.

BTW sometimes I want to implement GUI for these modules in IDLE.
msg398046 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2021-07-23 11:13
I suspect the only reason the dis CLI isn't documented is because it's been around for so long (22 years!) that nobody previously noticed it was missing:

Folks then either didn't know about it, or already knew how to use it.

Even the migration to use argparse was a code cleanup from a user that happened to be reading the module source code and noticed that it didn't already do so:

I can see value in supporting `-c` and `-m` options to dis to align with the main interpreter CLI.
Date User Action Args
2021-07-23 11:13:22ncoghlansetnosy: + ncoghlan
messages: + msg398046
2021-06-17 11:49:35eamanusetnosy: + eamanu
2021-06-13 23:23:45CCLDArjunsetstage: patch review
pull_requests: + pull_request25302
2021-06-13 08:12:10serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg395731
2021-06-13 05:58:04steven.dapranosetnosy: + steven.daprano

versions: + Python 3.11
2021-06-13 03:57:07CCLDArjunsetmessages: + msg395727
2021-06-13 03:33:29CCLDArjunsetmessages: + msg395725
2021-06-13 03:27:11CCLDArjunsetmessages: + msg395724
2021-06-13 02:44:14terry.reedysetmessages: + msg395723
2021-06-13 02:34:08terry.reedysetnosy: + yselivanov, terry.reedy
messages: + msg395722
2021-06-13 00:44:23CCLDArjuncreate