blob: 4c429589133886f182feec01a5678f7824f63d29 [file] [log] [blame]
Guido van Rossum9a22de11995-01-12 12:29:47 +00001import os
2import sys
3import string
4
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00005MAXFD = 256 # Max number of file descriptors (os.getdtablesize()???)
Guido van Rossum9a22de11995-01-12 12:29:47 +00006
Guido van Rossum0357d021997-08-11 03:27:24 +00007_active = []
8
9def _cleanup():
10 for inst in _active[:]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000011 inst.poll()
Guido van Rossum0357d021997-08-11 03:27:24 +000012
13class Popen3:
Guido van Rossumda286661997-09-29 04:04:39 +000014 def __init__(self, cmd, capturestderr=0, bufsize=-1):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000015 if type(cmd) == type(''):
16 cmd = ['/bin/sh', '-c', cmd]
17 p2cread, p2cwrite = os.pipe()
18 c2pread, c2pwrite = os.pipe()
19 if capturestderr:
20 errout, errin = os.pipe()
21 self.pid = os.fork()
22 if self.pid == 0:
23 # Child
24 os.close(0)
25 os.close(1)
26 if os.dup(p2cread) <> 0:
27 sys.stderr.write('popen2: bad read dup\n')
28 if os.dup(c2pwrite) <> 1:
29 sys.stderr.write('popen2: bad write dup\n')
30 if capturestderr:
31 os.close(2)
32 if os.dup(errin) <> 2: pass
33 for i in range(3, MAXFD):
34 try:
35 os.close(i)
36 except: pass
37 try:
38 os.execvp(cmd[0], cmd)
39 finally:
40 os._exit(1)
41 # Shouldn't come here, I guess
42 os._exit(1)
43 os.close(p2cread)
44 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
45 os.close(c2pwrite)
46 self.fromchild = os.fdopen(c2pread, 'r', bufsize)
47 if capturestderr:
48 os.close(errin)
49 self.childerr = os.fdopen(errout, 'r', bufsize)
50 else:
51 self.childerr = None
52 self.sts = -1 # Child not completed yet
53 _active.append(self)
Guido van Rossum0357d021997-08-11 03:27:24 +000054 def poll(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000055 if self.sts < 0:
56 try:
57 pid, sts = os.waitpid(self.pid, os.WNOHANG)
58 if pid == self.pid:
59 self.sts = sts
60 _active.remove(self)
61 except os.error:
62 pass
63 return self.sts
Guido van Rossum0357d021997-08-11 03:27:24 +000064 def wait(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065 pid, sts = os.waitpid(self.pid, 0)
66 if pid == self.pid:
67 self.sts = sts
68 _active.remove(self)
69 return self.sts
Guido van Rossumcaa9f231997-04-21 14:15:55 +000070
Guido van Rossumda286661997-09-29 04:04:39 +000071def popen2(cmd, bufsize=-1):
Guido van Rossum0357d021997-08-11 03:27:24 +000072 _cleanup()
Guido van Rossumda286661997-09-29 04:04:39 +000073 inst = Popen3(cmd, 0, bufsize)
Guido van Rossum0357d021997-08-11 03:27:24 +000074 return inst.fromchild, inst.tochild
Guido van Rossumcaa9f231997-04-21 14:15:55 +000075
Guido van Rossumda286661997-09-29 04:04:39 +000076def popen3(cmd, bufsize=-1):
Guido van Rossum0357d021997-08-11 03:27:24 +000077 _cleanup()
Guido van Rossumda286661997-09-29 04:04:39 +000078 inst = Popen3(cmd, 1, bufsize)
Guido van Rossum0357d021997-08-11 03:27:24 +000079 return inst.fromchild, inst.tochild, inst.childerr
80
81def _test():
82 teststr = "abc\n"
83 print "testing popen2..."
84 r, w = popen2('cat')
85 w.write(teststr)
86 w.close()
87 assert r.read() == teststr
88 print "testing popen3..."
Guido van Rossum6dd48681997-09-18 20:00:39 +000089 r, w, e = popen3(['cat'])
Guido van Rossum0357d021997-08-11 03:27:24 +000090 w.write(teststr)
91 w.close()
92 assert r.read() == teststr
93 assert e.read() == ""
94 _cleanup()
95 assert not _active
96 print "All OK"
97
98if __name__ == '__main__':
99 _test()