summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2010-02-04 12:43:58 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2010-02-04 12:43:58 (GMT)
commit7dfc9e1cd42c65edd211b58033d01e54b754e35a (patch)
tree2f7530ce905df68877c05e51dffe3b282686876c /Doc
parentdc84f8fbb4e6ac4a565330281bf4d68c6332b6e2 (diff)
downloadcpython-7dfc9e1cd42c65edd211b58033d01e54b754e35a.zip
cpython-7dfc9e1cd42c65edd211b58033d01e54b754e35a.tar.gz
cpython-7dfc9e1cd42c65edd211b58033d01e54b754e35a.tar.bz2
Issue 6760: Clarify args handling for subprocess.Popen. Patch by Chris Rebert
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/subprocess.rst37
1 files changed, 31 insertions, 6 deletions
diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst
index f5d5a8e..e422fde 100644
--- a/Doc/library/subprocess.rst
+++ b/Doc/library/subprocess.rst
@@ -48,13 +48,38 @@ This module defines one class called :class:`Popen`:
On Unix, with *shell=False* (default): In this case, the Popen class uses
:meth:`os.execvp` to execute the child program. *args* should normally be a
- sequence. A string will be treated as a sequence with the string as the only
- item (the program to execute).
+ sequence. If a string is specified for *args*, it will be used as the name
+ or path of the program to execute; this will only work if the program is
+ being given no arguments.
- On Unix, with *shell=True*: If args is a string, it specifies the command string
- to execute through the shell. If *args* is a sequence, the first item specifies
- the command string, and any additional items will be treated as additional shell
- arguments.
+ .. note::
+
+ :meth:`shlex.split` can be useful when determining the correct
+ tokenization for *args*, especially in complex cases::
+
+ >>> import shlex, subprocess
+ >>> command_line = raw_input()
+ /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
+ >>> args = shlex.split(command_line)
+ >>> print args
+ ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
+ >>> p = subprocess.Popen(args) # Success!
+
+ Note in particular that options (such as *-input*) and arguments (such
+ as *eggs.txt*) that are separated by whitespace in the shell go in separate
+ list elements, while arguments that need quoting or backslash escaping when
+ used in the shell (such as filenames containing spaces or the *echo* command
+ shown above) are single list elements.
+
+ On Unix, with *shell=True*: If args is a string, it specifies the command
+ string to execute through the shell. This means that the string must be
+ formatted exactly as it would be when typed at the shell prompt. This
+ includes, for example, quoting or backslash escaping filenames with spaces in
+ them. If *args* is a sequence, the first item specifies the command string, and
+ any additional items will be treated as additional arguments to the shell
+ itself. That is to say, *Popen* does the equivalent of::
+
+ Popen(['/bin/sh', '-c', args[0], args[1], ...])
On Windows: the :class:`Popen` class uses CreateProcess() to execute the child
program, which operates on strings. If *args* is a sequence, it will be