struct.pack has become picky about h (short) and H (unsigned short).
[python/dscho.git] / Lib / popen2.py
blobe1bf363996abea8ee800647240bfb45d55d8903b
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.
7 """
9 import os
10 import sys
12 MAXFD = 256 # Max number of file descriptors (os.getdtablesize()???)
14 _active = []
16 def _cleanup():
17 for inst in _active[:]:
18 inst.poll()
20 class Popen3:
21 """Class representing a child process. Normally instances are created
22 by the factory functions popen2() and popen3()."""
24 sts = -1 # Child not completed yet
26 def __init__(self, cmd, capturestderr=0, bufsize=-1):
27 """The parameter 'cmd' is the shell command to execute in a
28 sub-process. The 'capturestderr' flag, if true, specifies that
29 the object should capture standard error output of the child process.
30 The default is false. If the 'bufsize' parameter is specified, it
31 specifies the size of the I/O buffers to/from the child process."""
32 _cleanup()
33 p2cread, p2cwrite = os.pipe()
34 c2pread, c2pwrite = os.pipe()
35 if capturestderr:
36 errout, errin = os.pipe()
37 self.pid = os.fork()
38 if self.pid == 0:
39 # Child
40 os.dup2(p2cread, 0)
41 os.dup2(c2pwrite, 1)
42 if capturestderr:
43 os.dup2(errin, 2)
44 self._run_child(cmd)
45 os.close(p2cread)
46 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
47 os.close(c2pwrite)
48 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
49 if capturestderr:
50 os.close(errin)
51 self.childerr = os.fdopen(errout, 'r', bufsize)
52 else:
53 self.childerr = None
54 _active.append(self)
56 def _run_child(self, cmd):
57 if type(cmd) == type(''):
58 cmd = ['/bin/sh', '-c', cmd]
59 for i in range(3, MAXFD):
60 try:
61 os.close(i)
62 except:
63 pass
64 try:
65 os.execvp(cmd[0], cmd)
66 finally:
67 os._exit(1)
69 def poll(self):
70 """Return the exit status of the child process if it has finished,
71 or -1 if it hasn't finished yet."""
72 if self.sts < 0:
73 try:
74 pid, sts = os.waitpid(self.pid, os.WNOHANG)
75 if pid == self.pid:
76 self.sts = sts
77 _active.remove(self)
78 except os.error:
79 pass
80 return self.sts
82 def wait(self):
83 """Wait for and return the exit status of the child process."""
84 pid, sts = os.waitpid(self.pid, 0)
85 if pid == self.pid:
86 self.sts = sts
87 _active.remove(self)
88 return self.sts
91 class Popen4(Popen3):
92 childerr = None
94 def __init__(self, cmd, bufsize=-1):
95 _cleanup()
96 p2cread, p2cwrite = os.pipe()
97 c2pread, c2pwrite = os.pipe()
98 self.pid = os.fork()
99 if self.pid == 0:
100 # Child
101 os.dup2(p2cread, 0)
102 os.dup2(c2pwrite, 1)
103 os.dup2(c2pwrite, 2)
104 self._run_child(cmd)
105 os.close(p2cread)
106 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
107 os.close(c2pwrite)
108 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
109 _active.append(self)
112 if sys.platform[:3] == "win":
113 # Some things don't make sense on non-Unix platforms.
114 del Popen3, Popen4
116 def popen2(cmd, bufsize=-1, mode='t'):
117 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
118 specified, it sets the buffer size for the I/O pipes. The file objects
119 (child_stdout, child_stdin) are returned."""
120 w, r = os.popen2(cmd, mode, bufsize)
121 return r, w
123 def popen3(cmd, bufsize=-1, mode='t'):
124 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
125 specified, it sets the buffer size for the I/O pipes. The file objects
126 (child_stdout, child_stdin, child_stderr) are returned."""
127 w, r, e = os.popen3(cmd, mode, bufsize)
128 return r, w, e
130 def popen4(cmd, bufsize=-1, mode='t'):
131 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
132 specified, it sets the buffer size for the I/O pipes. The file objects
133 (child_stdout_stderr, child_stdin) are returned."""
134 w, r = os.popen4(cmd, mode, bufsize)
135 return r, w
136 else:
137 def popen2(cmd, bufsize=-1, mode='t'):
138 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
139 specified, it sets the buffer size for the I/O pipes. The file objects
140 (child_stdout, child_stdin) are returned."""
141 inst = Popen3(cmd, 0, bufsize)
142 return inst.fromchild, inst.tochild
144 def popen3(cmd, bufsize=-1, mode='t'):
145 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
146 specified, it sets the buffer size for the I/O pipes. The file objects
147 (child_stdout, child_stdin, child_stderr) are returned."""
148 inst = Popen3(cmd, 1, bufsize)
149 return inst.fromchild, inst.tochild, inst.childerr
151 def popen4(cmd, bufsize=-1, mode='t'):
152 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
153 specified, it sets the buffer size for the I/O pipes. The file objects
154 (child_stdout_stderr, child_stdin) are returned."""
155 inst = Popen4(cmd, bufsize)
156 return inst.fromchild, inst.tochild
159 def _test():
160 cmd = "cat"
161 teststr = "ab cd\n"
162 if os.name == "nt":
163 cmd = "more"
164 # "more" doesn't act the same way across Windows flavors,
165 # sometimes adding an extra newline at the start or the
166 # end. So we strip whitespace off both ends for comparison.
167 expected = teststr.strip()
168 print "testing popen2..."
169 r, w = popen2(cmd)
170 w.write(teststr)
171 w.close()
172 got = r.read()
173 if got.strip() != expected:
174 raise ValueError("wrote %s read %s" % (`teststr`, `got`))
175 print "testing popen3..."
176 try:
177 r, w, e = popen3([cmd])
178 except:
179 r, w, e = popen3(cmd)
180 w.write(teststr)
181 w.close()
182 got = r.read()
183 if got.strip() != expected:
184 raise ValueError("wrote %s read %s" % (`teststr`, `got`))
185 got = e.read()
186 if got:
187 raise ValueError("unexected %s on stderr" % `got`)
188 for inst in _active[:]:
189 inst.wait()
190 if _active:
191 raise ValueError("_active not empty")
192 print "All OK"
194 if __name__ == '__main__':
195 _test()