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.
13 MAXFD
= 256 # Max number of file descriptors (os.getdtablesize()???)
18 for inst
in _active
[:]:
22 """Class representing a child process. Normally instances are created
23 by the factory functions popen2() and popen3()."""
25 def __init__(self
, cmd
, capturestderr
=0, bufsize
=-1):
26 """The parameter 'cmd' is the shell command to execute in a
27 sub-process. The 'capturestderr' flag, if true, specifies that
28 the object should capture standard error output of the child process.
29 The default is false. If the 'bufsize' parameter is specified, it
30 specifies the size of the I/O buffers to/from the child process."""
31 if type(cmd
) == type(''):
32 cmd
= ['/bin/sh', '-c', cmd
]
33 p2cread
, p2cwrite
= os
.pipe()
34 c2pread
, c2pwrite
= os
.pipe()
36 errout
, errin
= os
.pipe()
42 if os
.dup(p2cread
) <> 0:
43 sys
.stderr
.write('popen2: bad read dup\n')
44 if os
.dup(c2pwrite
) <> 1:
45 sys
.stderr
.write('popen2: bad write dup\n')
48 if os
.dup(errin
) <> 2: pass
49 for i
in range(3, MAXFD
):
54 os
.execvp(cmd
[0], cmd
)
57 # Shouldn't come here, I guess
60 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
62 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
65 self
.childerr
= os
.fdopen(errout
, 'r', bufsize
)
68 self
.sts
= -1 # Child not completed yet
72 """Return the exit status of the child process if it has finished,
73 or -1 if it hasn't finished yet."""
76 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
85 """Wait for and return the exit status of the child process."""
86 pid
, sts
= os
.waitpid(self
.pid
, 0)
93 if sys
.platform
[:3] == "win":
94 def popen2(cmd
, mode
='t', bufsize
=-1):
95 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
96 specified, it sets the buffer size for the I/O pipes. The file objects
97 (child_stdout, child_stdin) are returned."""
98 w
, r
= os
.popen2(cmd
, mode
, bufsize
)
101 def popen2(cmd
, mode
='t', bufsize
=-1):
102 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
103 specified, it sets the buffer size for the I/O pipes. The file objects
104 (child_stdout, child_stdin) are returned."""
105 if type(mode
) is type(0) and bufsize
== -1:
108 assert mode
in ('t', 'b')
110 inst
= Popen3(cmd
, 0, bufsize
)
111 return inst
.fromchild
, inst
.tochild
113 if sys
.platform
[:3] == "win":
114 def popen3(cmd
, mode
='t', bufsize
=-1):
115 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
116 specified, it sets the buffer size for the I/O pipes. The file objects
117 (child_stdout, child_stdin, child_stderr) are returned."""
118 w
, r
, e
= os
.popen3(cmd
, mode
, bufsize
)
121 def popen3(cmd
, mode
='t', bufsize
=-1):
122 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
123 specified, it sets the buffer size for the I/O pipes. The file objects
124 (child_stdout, child_stdin, child_stderr) are returned."""
125 if type(mode
) is type(0) and bufsize
== -1:
128 assert mode
in ('t', 'b')
130 inst
= Popen3(cmd
, 1, bufsize
)
131 return inst
.fromchild
, inst
.tochild
, inst
.childerr
133 if sys
.platform
[:3] == "win":
134 def popen4(cmd
, mode
='t', bufsize
=-1):
135 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
136 specified, it sets the buffer size for the I/O pipes. The file objects
137 (child_stdout_stderr, child_stdin) are returned."""
138 w
, r
= os
.popen4(cmd
, mode
, bufsize
)
141 pass # not yet on unix
149 # "more" doesn't act the same way across Windows flavors,
150 # sometimes adding an extra newline at the start or the
151 # end. So we strip whitespace off both ends for comparison.
152 expected
= teststr
.strip()
153 print "testing popen2..."
158 if got
.strip() != expected
:
159 raise ValueError("wrote %s read %s" % (`teststr`
, `got`
))
160 print "testing popen3..."
162 r
, w
, e
= popen3([cmd
])
164 r
, w
, e
= popen3(cmd
)
168 if got
.strip() != expected
:
169 raise ValueError("wrote %s read %s" % (`teststr`
, `got`
))
172 raise ValueError("unexected %s on stderr" % `got`
)
173 for inst
in _active
[:]:
176 raise ValueError("_active not empty")
179 if __name__
== '__main__':