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 warnings
.warn("The popen2 module is deprecated. Use the subprocess module.",
13 DeprecationWarning, stacklevel
=2)
15 __all__
= ["popen2", "popen3", "popen4"]
18 MAXFD
= os
.sysconf('SC_OPEN_MAX')
19 except (AttributeError, ValueError):
25 for inst
in _active
[:]:
26 if inst
.poll(_deadstate
=sys
.maxint
) >= 0:
30 # This can happen if two threads create a new Popen instance.
31 # It's harmless that it was already removed, so ignore.
35 """Class representing a child process. Normally, instances are created
36 internally by the functions popen2() and popen3()."""
38 sts
= -1 # Child not completed yet
40 def __init__(self
, cmd
, capturestderr
=False, bufsize
=-1):
41 """The parameter 'cmd' is the shell command to execute in a
42 sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
43 will be passed directly to the program without shell intervention (as
44 with os.spawnv()). If 'cmd' is a string it will be passed to the shell
45 (as with os.system()). The 'capturestderr' flag, if true, specifies
46 that the object should capture standard error output of the child
47 process. The default is false. If the 'bufsize' parameter is
48 specified, it specifies the size of the I/O buffers to/from the child
52 p2cread
, p2cwrite
= os
.pipe()
53 c2pread
, c2pwrite
= os
.pipe()
55 errout
, errin
= os
.pipe()
65 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
67 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
70 self
.childerr
= os
.fdopen(errout
, 'r', bufsize
)
75 # In case the child hasn't been waited on, check if it's done.
76 self
.poll(_deadstate
=sys
.maxint
)
78 if _active
is not None:
79 # Child is still running, keep us alive until we can wait on it.
82 def _run_child(self
, cmd
):
83 if isinstance(cmd
, basestring
):
84 cmd
= ['/bin/sh', '-c', cmd
]
85 os
.closerange(3, MAXFD
)
87 os
.execvp(cmd
[0], cmd
)
91 def poll(self
, _deadstate
=None):
92 """Return the exit status of the child process if it has finished,
93 or -1 if it hasn't finished yet."""
96 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
97 # pid will be 0 if self.pid hasn't terminated
101 if _deadstate
is not None:
102 self
.sts
= _deadstate
106 """Wait for and return the exit status of the child process."""
108 pid
, sts
= os
.waitpid(self
.pid
, 0)
109 # This used to be a test, but it is believed to be
110 # always true, so I changed it to an assertion - mvl
111 assert pid
== self
.pid
116 class Popen4(Popen3
):
119 def __init__(self
, cmd
, bufsize
=-1):
122 p2cread
, p2cwrite
= os
.pipe()
123 c2pread
, c2pwrite
= os
.pipe()
132 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
134 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
137 if sys
.platform
[:3] == "win" or sys
.platform
== "os2emx":
138 # Some things don't make sense on non-Unix platforms.
141 def popen2(cmd
, bufsize
=-1, mode
='t'):
142 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
143 be a sequence, in which case arguments will be passed directly to the
144 program without shell intervention (as with os.spawnv()). If 'cmd' is a
145 string it will be passed to the shell (as with os.system()). If
146 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
147 file objects (child_stdout, child_stdin) are returned."""
148 w
, r
= os
.popen2(cmd
, mode
, bufsize
)
151 def popen3(cmd
, bufsize
=-1, mode
='t'):
152 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
153 be a sequence, in which case arguments will be passed directly to the
154 program without shell intervention (as with os.spawnv()). If 'cmd' is a
155 string it will be passed to the shell (as with os.system()). If
156 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
157 file objects (child_stdout, child_stdin, child_stderr) are returned."""
158 w
, r
, e
= os
.popen3(cmd
, mode
, bufsize
)
161 def popen4(cmd
, bufsize
=-1, mode
='t'):
162 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
163 be a sequence, in which case arguments will be passed directly to the
164 program without shell intervention (as with os.spawnv()). If 'cmd' is a
165 string it will be passed to the shell (as with os.system()). If
166 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
167 file objects (child_stdout_stderr, child_stdin) are returned."""
168 w
, r
= os
.popen4(cmd
, mode
, bufsize
)
171 def popen2(cmd
, bufsize
=-1, mode
='t'):
172 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
173 be a sequence, in which case arguments will be passed directly to the
174 program without shell intervention (as with os.spawnv()). If 'cmd' is a
175 string it will be passed to the shell (as with os.system()). If
176 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
177 file objects (child_stdout, child_stdin) are returned."""
178 inst
= Popen3(cmd
, False, bufsize
)
179 return inst
.fromchild
, inst
.tochild
181 def popen3(cmd
, bufsize
=-1, mode
='t'):
182 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
183 be a sequence, in which case arguments will be passed directly to the
184 program without shell intervention (as with os.spawnv()). If 'cmd' is a
185 string it will be passed to the shell (as with os.system()). If
186 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
187 file objects (child_stdout, child_stdin, child_stderr) are returned."""
188 inst
= Popen3(cmd
, True, bufsize
)
189 return inst
.fromchild
, inst
.tochild
, inst
.childerr
191 def popen4(cmd
, bufsize
=-1, mode
='t'):
192 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
193 be a sequence, in which case arguments will be passed directly to the
194 program without shell intervention (as with os.spawnv()). If 'cmd' is a
195 string it will be passed to the shell (as with os.system()). If
196 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
197 file objects (child_stdout_stderr, child_stdin) are returned."""
198 inst
= Popen4(cmd
, bufsize
)
199 return inst
.fromchild
, inst
.tochild
201 __all__
.extend(["Popen3", "Popen4"])