1 """Spawn a command with pipes to its stdin, stdout, and optionally stderr.
3 The normal os.popen(cmd, mode) call spawns a shell command and provides a
4 file interface to just the input or output of the process depending on
5 whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
6 and popen3(cmd) which return two or three pipes to the spawned command.
12 __all__
= ["popen2", "popen3", "popen4"]
15 MAXFD
= os
.sysconf('SC_OPEN_MAX')
16 except (AttributeError, ValueError):
22 for inst
in _active
[:]:
23 if inst
.poll(_deadstate
=sys
.maxint
) >= 0:
27 # This can happen if two threads create a new Popen instance.
28 # It's harmless that it was already removed, so ignore.
32 """Class representing a child process. Normally instances are created
33 by the factory functions popen2() and popen3()."""
35 sts
= -1 # Child not completed yet
37 def __init__(self
, cmd
, capturestderr
=False, bufsize
=-1):
38 """The parameter 'cmd' is the shell command to execute in a
39 sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
40 will be passed directly to the program without shell intervention (as
41 with os.spawnv()). If 'cmd' is a string it will be passed to the shell
42 (as with os.system()). The 'capturestderr' flag, if true, specifies
43 that the object should capture standard error output of the child
44 process. The default is false. If the 'bufsize' parameter is
45 specified, it specifies the size of the I/O buffers to/from the child
49 p2cread
, p2cwrite
= os
.pipe()
50 c2pread
, c2pwrite
= os
.pipe()
52 errout
, errin
= os
.pipe()
62 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
64 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
67 self
.childerr
= os
.fdopen(errout
, 'r', bufsize
)
72 # In case the child hasn't been waited on, check if it's done.
73 self
.poll(_deadstate
=sys
.maxint
)
75 if _active
is not None:
76 # Child is still running, keep us alive until we can wait on it.
79 def _run_child(self
, cmd
):
80 if isinstance(cmd
, basestring
):
81 cmd
= ['/bin/sh', '-c', cmd
]
82 for i
in xrange(3, MAXFD
):
88 os
.execvp(cmd
[0], cmd
)
92 def poll(self
, _deadstate
=None):
93 """Return the exit status of the child process if it has finished,
94 or -1 if it hasn't finished yet."""
97 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
98 # pid will be 0 if self.pid hasn't terminated
102 if _deadstate
is not None:
103 self
.sts
= _deadstate
107 """Wait for and return the exit status of the child process."""
109 pid
, sts
= os
.waitpid(self
.pid
, 0)
110 # This used to be a test, but it is believed to be
111 # always true, so I changed it to an assertion - mvl
112 assert pid
== self
.pid
117 class Popen4(Popen3
):
120 def __init__(self
, cmd
, bufsize
=-1):
123 p2cread
, p2cwrite
= os
.pipe()
124 c2pread
, c2pwrite
= os
.pipe()
133 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
135 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
138 if sys
.platform
[:3] == "win" or sys
.platform
== "os2emx":
139 # Some things don't make sense on non-Unix platforms.
142 def popen2(cmd
, bufsize
=-1, mode
='t'):
143 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
144 be a sequence, in which case arguments will be passed directly to the
145 program without shell intervention (as with os.spawnv()). If 'cmd' is a
146 string it will be passed to the shell (as with os.system()). If
147 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
148 file objects (child_stdout, child_stdin) are returned."""
149 w
, r
= os
.popen2(cmd
, mode
, bufsize
)
152 def popen3(cmd
, bufsize
=-1, mode
='t'):
153 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
154 be a sequence, in which case arguments will be passed directly to the
155 program without shell intervention (as with os.spawnv()). If 'cmd' is a
156 string it will be passed to the shell (as with os.system()). If
157 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
158 file objects (child_stdout, child_stdin, child_stderr) are returned."""
159 w
, r
, e
= os
.popen3(cmd
, mode
, bufsize
)
162 def popen4(cmd
, bufsize
=-1, mode
='t'):
163 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
164 be a sequence, in which case arguments will be passed directly to the
165 program without shell intervention (as with os.spawnv()). If 'cmd' is a
166 string it will be passed to the shell (as with os.system()). If
167 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
168 file objects (child_stdout_stderr, child_stdin) are returned."""
169 w
, r
= os
.popen4(cmd
, mode
, bufsize
)
172 def popen2(cmd
, bufsize
=-1, mode
='t'):
173 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
174 be a sequence, in which case arguments will be passed directly to the
175 program without shell intervention (as with os.spawnv()). If 'cmd' is a
176 string it will be passed to the shell (as with os.system()). If
177 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
178 file objects (child_stdout, child_stdin) are returned."""
179 inst
= Popen3(cmd
, False, bufsize
)
180 return inst
.fromchild
, inst
.tochild
182 def popen3(cmd
, bufsize
=-1, mode
='t'):
183 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
184 be a sequence, in which case arguments will be passed directly to the
185 program without shell intervention (as with os.spawnv()). If 'cmd' is a
186 string it will be passed to the shell (as with os.system()). If
187 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
188 file objects (child_stdout, child_stdin, child_stderr) are returned."""
189 inst
= Popen3(cmd
, True, bufsize
)
190 return inst
.fromchild
, inst
.tochild
, inst
.childerr
192 def popen4(cmd
, bufsize
=-1, mode
='t'):
193 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
194 be a sequence, in which case arguments will be passed directly to the
195 program without shell intervention (as with os.spawnv()). If 'cmd' is a
196 string it will be passed to the shell (as with os.system()). If
197 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
198 file objects (child_stdout_stderr, child_stdin) are returned."""
199 inst
= Popen4(cmd
, bufsize
)
200 return inst
.fromchild
, inst
.tochild
202 __all__
.extend(["Popen3", "Popen4"])