Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libsubprocess.tex @ 39587:bbef2ecdee33 2.4
Patch #1540329: _hotshot.c fix backports.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 19 Aug 2006 07:48:06 +0000 |
| parents | eea176e67f22 |
| children | 195559c5bacd |
| rev | line source |
|---|---|
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
1 \section{\module{subprocess} --- Subprocess management} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
2 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
3 \declaremodule{standard}{subprocess} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
4 \modulesynopsis{Subprocess management.} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
5 \moduleauthor{Peter \AA strand}{astrand@lysator.liu.se} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
6 \sectionauthor{Peter \AA strand}{astrand@lysator.liu.se} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
7 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
8 \versionadded{2.4} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
9 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
10 The \module{subprocess} module allows you to spawn new processes, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
11 connect to their input/output/error pipes, and obtain their return |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
12 codes. This module intends to replace several other, older modules |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
13 and functions, such as: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
14 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
15 % XXX Should add pointers to this module to at least the popen2 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
16 % and commands sections. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
17 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
18 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
19 os.system |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
20 os.spawn* |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
21 os.popen* |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
22 popen2.* |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
23 commands.* |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
24 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
25 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
26 Information about how the \module{subprocess} module can be used to |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
27 replace these modules and functions can be found in the following |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
28 sections. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
29 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
30 \subsection{Using the subprocess Module} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
31 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
32 This module defines one class called \class{Popen}: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
33 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
34 \begin{classdesc}{Popen}{args, bufsize=0, executable=None, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
35 stdin=None, stdout=None, stderr=None, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
36 preexec_fn=None, close_fds=False, shell=False, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
37 cwd=None, env=None, universal_newlines=False, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
38 startupinfo=None, creationflags=0} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
39 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
40 Arguments are: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
41 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
42 \var{args} should be a string, or a sequence of program arguments. The |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
43 program to execute is normally the first item in the args sequence or |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
44 string, but can be explicitly set by using the executable argument. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
45 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
46 On \UNIX{}, with \var{shell=False} (default): In this case, the Popen |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
47 class uses \method{os.execvp()} to execute the child program. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
48 \var{args} should normally be a sequence. A string will be treated as a |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
49 sequence with the string as the only item (the program to execute). |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
50 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
51 On \UNIX{}, with \var{shell=True}: If args is a string, it specifies the |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
52 command string to execute through the shell. If \var{args} is a |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
53 sequence, the first item specifies the command string, and any |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
54 additional items will be treated as additional shell arguments. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
55 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
56 On Windows: the \class{Popen} class uses CreateProcess() to execute |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
57 the child program, which operates on strings. If \var{args} is a |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
58 sequence, it will be converted to a string using the |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
59 \method{list2cmdline} method. Please note that not all MS Windows |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
60 applications interpret the command line the same way: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
61 \method{list2cmdline} is designed for applications using the same |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
62 rules as the MS C runtime. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
63 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
64 \var{bufsize}, if given, has the same meaning as the corresponding |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
65 argument to the built-in open() function: \constant{0} means unbuffered, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
66 \constant{1} means line buffered, any other positive value means use a |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
67 buffer of (approximately) that size. A negative \var{bufsize} means to |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
68 use the system default, which usually means fully buffered. The default |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
69 value for \var{bufsize} is \constant{0} (unbuffered). |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
70 |
|
33934
0ae28c3a8d24
Added more documentation about the executable argument.
Peter Astrand <astrand@lysator.liu.se>
parents:
33774
diff
changeset
|
71 The \var{executable} argument specifies the program to execute. It is |
|
0ae28c3a8d24
Added more documentation about the executable argument.
Peter Astrand <astrand@lysator.liu.se>
parents:
33774
diff
changeset
|
72 very seldom needed: Usually, the program to execute is defined by the |
|
38143
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
73 \var{args} argument. If \code{shell=True}, the \var{executable} |
|
33934
0ae28c3a8d24
Added more documentation about the executable argument.
Peter Astrand <astrand@lysator.liu.se>
parents:
33774
diff
changeset
|
74 argument specifies which shell to use. On \UNIX{}, the default shell |
|
38143
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
75 is \file{/bin/sh}. On Windows, the default shell is specified by the |
|
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
76 \envvar{COMSPEC} environment variable. |
|
33934
0ae28c3a8d24
Added more documentation about the executable argument.
Peter Astrand <astrand@lysator.liu.se>
parents:
33774
diff
changeset
|
77 |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
78 \var{stdin}, \var{stdout} and \var{stderr} specify the executed |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
79 programs' standard input, standard output and standard error file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
80 handles, respectively. Valid values are \code{PIPE}, an existing file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
81 descriptor (a positive integer), an existing file object, and |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
82 \code{None}. \code{PIPE} indicates that a new pipe to the child |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
83 should be created. With \code{None}, no redirection will occur; the |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
84 child's file handles will be inherited from the parent. Additionally, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
85 \var{stderr} can be \code{STDOUT}, which indicates that the stderr |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
86 data from the applications should be captured into the same file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
87 handle as for stdout. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
88 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
89 If \var{preexec_fn} is set to a callable object, this object will be |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
90 called in the child process just before the child is executed. |
|
38143
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
91 (\UNIX{} only) |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
92 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
93 If \var{close_fds} is true, all file descriptors except \constant{0}, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
94 \constant{1} and \constant{2} will be closed before the child process is |
|
38143
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
95 executed. (\UNIX{} only) |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
96 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
97 If \var{shell} is \constant{True}, the specified command will be |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
98 executed through the shell. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
99 |
|
38143
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
100 If \var{cwd} is not \code{None}, the child's current directory will be |
|
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
101 changed to \var{cwd} before it is executed. Note that this directory |
|
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
102 is not considered when searching the executable, so you can't specify |
|
eea176e67f22
Bug #1485447: subprocess: document that the "cwd" parameter isn't used to find the executable. Misc. other markup fixes.
Georg Brandl <georg@python.org>
parents:
36439
diff
changeset
|
103 the program's path relative to \var{cwd}. |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
104 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
105 If \var{env} is not \code{None}, it defines the environment variables |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
106 for the new process. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
107 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
108 If \var{universal_newlines} is \constant{True}, the file objects stdout |
| 36167 | 109 and stderr are opened as text files, but lines may be terminated by |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
110 any of \code{'\e n'}, the Unix end-of-line convention, \code{'\e r'}, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
111 the Macintosh convention or \code{'\e r\e n'}, the Windows convention. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
112 All of these external representations are seen as \code{'\e n'} by the |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
113 Python program. \note{This feature is only available if Python is built |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
114 with universal newline support (the default). Also, the newlines |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
115 attribute of the file objects \member{stdout}, \member{stdin} and |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
116 \member{stderr} are not updated by the communicate() method.} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
117 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
118 The \var{startupinfo} and \var{creationflags}, if given, will be |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
119 passed to the underlying CreateProcess() function. They can specify |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
120 things such as appearance of the main window and priority for the new |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
121 process. (Windows only) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
122 \end{classdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
123 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
124 \subsubsection{Convenience Functions} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
125 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
126 This module also defines one shortcut function: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
127 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
128 \begin{funcdesc}{call}{*args, **kwargs} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
129 Run command with arguments. Wait for command to complete, then |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
130 return the \member{returncode} attribute. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
131 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
132 The arguments are the same as for the Popen constructor. Example: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
133 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
134 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
135 retcode = call(["ls", "-l"]) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
136 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
137 \end{funcdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
138 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
139 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
140 \subsubsection{Exceptions} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
141 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
142 Exceptions raised in the child process, before the new program has |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
143 started to execute, will be re-raised in the parent. Additionally, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
144 the exception object will have one extra attribute called |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
145 \member{child_traceback}, which is a string containing traceback |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
146 information from the childs point of view. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
147 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
148 The most common exception raised is \exception{OSError}. This occurs, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
149 for example, when trying to execute a non-existent file. Applications |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
150 should prepare for \exception{OSError} exceptions. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
151 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
152 A \exception{ValueError} will be raised if \class{Popen} is called |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
153 with invalid arguments. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
154 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
155 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
156 \subsubsection{Security} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
157 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
158 Unlike some other popen functions, this implementation will never call |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
159 /bin/sh implicitly. This means that all characters, including shell |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
160 metacharacters, can safely be passed to child processes. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
161 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
162 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
163 \subsection{Popen Objects} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
164 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
165 Instances of the \class{Popen} class have the following methods: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
166 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
167 \begin{methoddesc}{poll}{} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
168 Check if child process has terminated. Returns returncode |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
169 attribute. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
170 \end{methoddesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
171 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
172 \begin{methoddesc}{wait}{} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
173 Wait for child process to terminate. Returns returncode attribute. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
174 \end{methoddesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
175 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
176 \begin{methoddesc}{communicate}{input=None} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
177 Interact with process: Send data to stdin. Read data from stdout and |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
178 stderr, until end-of-file is reached. Wait for process to terminate. |
|
34907
60830ad6275e
Backport relevant part of checkin:
Walter Dörwald <walter@livinglogic.de>
parents:
34051
diff
changeset
|
179 The optional \var{input} argument should be a string to be sent to the |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
180 child process, or \code{None}, if no data should be sent to the child. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
181 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
182 communicate() returns a tuple (stdout, stderr). |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
183 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
184 \note{The data read is buffered in memory, so do not use this method |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
185 if the data size is large or unlimited.} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
186 \end{methoddesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
187 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
188 The following attributes are also available: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
189 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
190 \begin{memberdesc}{stdin} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
191 If the \var{stdin} argument is \code{PIPE}, this attribute is a file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
192 object that provides input to the child process. Otherwise, it is |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
193 \code{None}. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
194 \end{memberdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
195 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
196 \begin{memberdesc}{stdout} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
197 If the \var{stdout} argument is \code{PIPE}, this attribute is a file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
198 object that provides output from the child process. Otherwise, it is |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
199 \code{None}. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
200 \end{memberdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
201 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
202 \begin{memberdesc}{stderr} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
203 If the \var{stderr} argument is \code{PIPE}, this attribute is file |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
204 object that provides error output from the child process. Otherwise, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
205 it is \code{None}. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
206 \end{memberdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
207 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
208 \begin{memberdesc}{pid} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
209 The process ID of the child process. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
210 \end{memberdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
211 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
212 \begin{memberdesc}{returncode} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
213 The child return code. A \code{None} value indicates that the process |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
214 hasn't terminated yet. A negative value -N indicates that the child |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
215 was terminated by signal N (\UNIX{} only). |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
216 \end{memberdesc} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
217 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
218 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
219 \subsection{Replacing Older Functions with the subprocess Module} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
220 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
221 In this section, "a ==> b" means that b can be used as a replacement |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
222 for a. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
223 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
224 \note{All functions in this section fail (more or less) silently if |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
225 the executed program cannot be found; this module raises an |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
226 \exception{OSError} exception.} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
227 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
228 In the following examples, we assume that the subprocess module is |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
229 imported with "from subprocess import *". |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
230 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
231 \subsubsection{Replacing /bin/sh shell backquote} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
232 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
233 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
234 output=`mycmd myarg` |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
235 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
236 output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
237 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
238 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
239 \subsubsection{Replacing shell pipe line} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
240 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
241 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
242 output=`dmesg | grep hda` |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
243 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
244 p1 = Popen(["dmesg"], stdout=PIPE) |
|
34051
8cecb8dc43a3
Corrected example for replacing shell pipeline. Fixes bug 1073790.
Peter Astrand <astrand@lysator.liu.se>
parents:
33934
diff
changeset
|
245 p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
246 output = p2.communicate()[0] |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
247 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
248 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
249 \subsubsection{Replacing os.system()} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
250 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
251 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
252 sts = os.system("mycmd" + " myarg") |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
253 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
254 p = Popen("mycmd" + " myarg", shell=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
255 sts = os.waitpid(p.pid, 0) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
256 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
257 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
258 Notes: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
259 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
260 \begin{itemize} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
261 \item Calling the program through the shell is usually not required. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
262 \item It's easier to look at the \member{returncode} attribute than |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
263 the exit status. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
264 \end{itemize} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
265 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
266 A more realistic example would look like this: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
267 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
268 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
269 try: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
270 retcode = call("mycmd" + " myarg", shell=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
271 if retcode < 0: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
272 print >>sys.stderr, "Child was terminated by signal", -retcode |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
273 else: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
274 print >>sys.stderr, "Child returned", retcode |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
275 except OSError, e: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
276 print >>sys.stderr, "Execution failed:", e |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
277 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
278 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
279 \subsubsection{Replacing os.spawn*} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
280 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
281 P_NOWAIT example: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
282 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
283 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
284 pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg") |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
285 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
286 pid = Popen(["/bin/mycmd", "myarg"]).pid |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
287 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
288 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
289 P_WAIT example: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
290 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
291 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
292 retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg") |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
293 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
294 retcode = call(["/bin/mycmd", "myarg"]) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
295 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
296 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
297 Vector example: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
298 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
299 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
300 os.spawnvp(os.P_NOWAIT, path, args) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
301 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
302 Popen([path] + args[1:]) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
303 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
304 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
305 Environment example: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
306 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
307 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
308 os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
309 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
310 Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"}) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
311 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
312 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
313 \subsubsection{Replacing os.popen*} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
314 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
315 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
316 pipe = os.popen(cmd, mode='r', bufsize) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
317 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
318 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
319 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
320 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
321 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
322 pipe = os.popen(cmd, mode='w', bufsize) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
323 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
324 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
325 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
326 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
327 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
328 (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
329 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
330 p = Popen(cmd, shell=True, bufsize=bufsize, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
331 stdin=PIPE, stdout=PIPE, close_fds=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
332 (child_stdin, child_stdout) = (p.stdin, p.stdout) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
333 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
334 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
335 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
336 (child_stdin, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
337 child_stdout, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
338 child_stderr) = os.popen3(cmd, mode, bufsize) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
339 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
340 p = Popen(cmd, shell=True, bufsize=bufsize, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
341 stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
342 (child_stdin, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
343 child_stdout, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
344 child_stderr) = (p.stdin, p.stdout, p.stderr) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
345 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
346 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
347 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
348 (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
349 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
350 p = Popen(cmd, shell=True, bufsize=bufsize, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
351 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
352 (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
353 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
354 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
355 \subsubsection{Replacing popen2.*} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
356 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
357 \note{If the cmd argument to popen2 functions is a string, the command |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
358 is executed through /bin/sh. If it is a list, the command is directly |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
359 executed.} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
360 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
361 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
362 (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
363 ==> |
|
34907
60830ad6275e
Backport relevant part of checkin:
Walter Dörwald <walter@livinglogic.de>
parents:
34051
diff
changeset
|
364 p = Popen(["somestring"], shell=True, bufsize=bufsize, |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
365 stdin=PIPE, stdout=PIPE, close_fds=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
366 (child_stdout, child_stdin) = (p.stdout, p.stdin) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
367 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
368 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
369 \begin{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
370 (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
371 ==> |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
372 p = Popen(["mycmd", "myarg"], bufsize=bufsize, |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
373 stdin=PIPE, stdout=PIPE, close_fds=True) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
374 (child_stdout, child_stdin) = (p.stdout, p.stdin) |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
375 \end{verbatim} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
376 |
|
36439
cb0dbae72a8e
SF bug 1415455, fix typo in module name
Neal Norwitz <nnorwitz@gmail.com>
parents:
36167
diff
changeset
|
377 The popen2.Popen3 and popen2.Popen4 basically works as subprocess.Popen, |
|
33774
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
378 except that: |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
379 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
380 \begin{itemize} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
381 \item subprocess.Popen raises an exception if the execution fails |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
382 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
383 \item the \var{capturestderr} argument is replaced with the \var{stderr} |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
384 argument. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
385 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
386 \item stdin=PIPE and stdout=PIPE must be specified. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
387 |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
388 \item popen2 closes all file descriptors by default, but you have to |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
389 specify close_fds=True with subprocess.Popen. |
|
fbacbb262789
SF Patch #1048341: subprocess documentation, based on PEP/docstring by
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff
changeset
|
390 \end{itemize} |
