[issue23427] Python should expose command when invoked with -c
New submission from Jan-Philip Gehrcke: When Python is invoked with the `-c command` switch, the command string does not get exposed in sys.argv: $ python -c "import sys; print(sys.argv)" ['-c'] $ python -c "import sys; print(sys.argv)" arg1 ['-c', 'arg1'] The command string does not get exposed anywhere, AFAIK, so it is inaccessible from within Python programs. There might be application scenarios in which it is useful to access the command string, such as for debugging purposes. One scenario is when a Python session should be able to "re-spawn" itself in a subprocess (I came across this question on StackOverflow: http://stackoverflow.com/q/28412903/145400) I propose to make the command string accessible. If you agree that it might make sense, the question is *how/where* to expose it. One possible way is to retain it in sys.argv, as in this example: $ python -c "import sys; print(sys.argv)" "arg1" ['-c', 'import sys; print(sys.argv)', 'arg1'] The current sys.argv docs say
If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'.
This sentence could then be adjusted to
"[...], argv[0] is set to the string '-c', and argv[1] contains the command."
This method breaks existing applications that are started with the -c method and that consume command line arguments in a sys.argv[1:] fashion. The tests in Lib/test/test_cmd_line.py all pass, however.
A second method would be to change sys.argv[0] from '-c' to '-c command'. This would break existing applications that check for sys.argv[0] == 'c'.
A third method would be to leave sys.argv as it is, and expose the command with a new attribute in the sys module.
I have attached a patch for variant 1 (passes all tests in Lib/test/test_cmd_line.py), to demonstrate which code is affected: the translation from the "real" argv to sys' argv is triggered in Modules/main.c. The patch does not change behavior of '-m' (it's funny, however, that the current version of main.c at first replaces the module string with '-m', whereas the runpy module later on replaces '-m' with the path to the module file anyway.).
As a side node, I figure that the sys.argv documentation should be adjusted to properly reflect the -m behavior, which is:
$ ./python -m testmodule foo
testmodule sys.argv: ['/data/local/pythondev/pythontip/cpython/testmodule.py', 'foo']
Let me hear your comments, and I am willing to work on code and doc patches, thanks!
----------
assignee: docs@python
components: Documentation, Interpreter Core
files: sys_argv_cmd.patch
keywords: patch
messages: 235633
nosy: docs@python, georg.brandl, haypo, jgehrcke, pitrou
priority: normal
severity: normal
status: open
title: Python should expose command when invoked with -c
type: enhancement
versions: Python 3.5, Python 3.6
Added file: http://bugs.python.org/file38065/sys_argv_cmd.patch
_______________________________________
Python tracker
STINNER Victor added the comment:
sys.argv must not be changed. It would break too many Python applications.
*If* we decide to expose the command line parameter in Python, we can
add a new variable like sys.command for example. "command" name in
used in the C code of Python, and also comes from "c" of "-c".
----------
_______________________________________
Python tracker
Михаил Кривушин added the comment:
Hello, I have find some workaround to get actual argv, but it broken:
python -c 'import ctypes; argv = ctypes.POINTER(ctypes.c_char_p)(); argc = ctypes.c_int(); ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv)); print([argv[i] for i in xrange(0, argc.value)])'
And this will output:
['python', '-c', '-c']
May be we just need to fix this behaviour, due this is error, as far as i can see. But may broke something.
----------
nosy: +Михаил.Кривушин
_______________________________________
Python tracker
Jan-Philip Gehrcke added the comment:
Victor,
I support the idea of sys.command. However, it would be unpopulated most of the time (e.g. set to None by default). Now, is that something we should push forward or not? I would work on a patch, but we should have an agreement first, I guess.
Mihail,
the original argv becomes modified in the very early bootstrap phase, and the command gets lost within that process: it gets *overwritten* with "-c", which is exactly why you are observing two "-c". This happens here:
https://hg.python.org/cpython/file/default/Modules/main.c#l684
So, no, without a code change in main.c there will be no way to retain the command for later usage.
----------
_______________________________________
Python tracker
Mihail Krivushin added the comment:
Jan-Philip, yes, I see that Main.c needs modification, but we can fix orig_argv with not just assignment but with full copy. So then we can get unmodified argv.
----------
_______________________________________
Python tracker
Changes by Mihail Krivushin
Zachary Ware added the comment:
Rather than add a variable to sys that will be empty 99% of the time, I think I'd rather stick a '__command__' constant in the __main__ module namespace when running with '-c' (think of '__file__'). You could then get at it elsewhere with 'from __main__ import __command__' (probably wrapped in a try/except ImportError, since it will usually not exist).
This should probably be discussed on python-ideas.
(Removing all versions but 3.5, as this is a feature request.)
----------
assignee: docs@python ->
components: -Documentation
nosy: +zach.ware
versions: -Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.6
_______________________________________
Python tracker
Changes by R. David Murray
STINNER Victor
Change by STINNER Victor
Change by Jakub Wilk
STINNER Victor
STINNER Victor
STINNER Victor
STINNER Victor
STINNER Victor
Change by STINNER Victor
Change by STINNER Victor
STINNER Victor
STINNER Victor
STINNER Victor
participants (7)
-
Jakub Wilk
-
Jan-Philip Gehrcke
-
Mihail Krivushin
-
R. David Murray
-
STINNER Victor
-
Zachary Ware
-
Михаил Кривушин