This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add program passed as string to dis module.
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
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 2022-04-11 14:59 by admin.

Files
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 (9)
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 https://docs.python.org/3/library/dis.html 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: https://bugs.python.org/issue18538 "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: https://github.com/CCLDArjun/cpython/commit/8b3b8ccef0ef693f8f4105fd1eb56e9386675301 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: https://github.com/python/cpython/commit/1fdae12c93246fcf4abbf882ba08df789070dfcc

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: https://bugs.python.org/issue18538

I can see value in supporting `-c` and `-m` options to dis to align with the main interpreter CLI.
msg415505 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-03-18 15:17
Neither of tokenize, ast or symtable modules support passing the source string as argument. If add this feature in the dis module, we will need to add it in all other modules with similar CLI. I do not think it is practical. You always can pass the source string via stdin. And you do not even need to pass /dev/stdin as argument, stdin is the default.

I usually use it with rlwrap. It allows to use some editing and history.

    rlwrap ./python -m dis

I propose to close this issue and open a new issue for documenting the CLI of the dis module.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88571
2022-03-18 15:17:19serhiy.storchakasetmessages: + msg415505
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