diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2000-07-09 17:59:32 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2000-07-09 17:59:32 (GMT) |
commit | bb7eeff44a893f6d97f418e69c6387ac24b90a63 (patch) | |
tree | aaf15b9f5619995af43bb88359ee2551619d6f37 /Lib/popen2.py | |
parent | 766ccdcf18a8f31e1b23bcc4f3b34bcffe2e48d2 (diff) | |
download | cpython-bb7eeff44a893f6d97f418e69c6387ac24b90a63.zip cpython-bb7eeff44a893f6d97f418e69c6387ac24b90a63.tar.gz cpython-bb7eeff44a893f6d97f418e69c6387ac24b90a63.tar.bz2 |
- added popen.popen2/popen3/popen4 support for
windows.
- added optional mode argument to popen2/popen3
for unix; if the second argument is an integer,
it's assumed to be the buffer size.
- changed nt.popen2/popen3/popen4 return values
to match the popen2 module (stdout first, not
stdin).
Diffstat (limited to 'Lib/popen2.py')
-rw-r--r-- | Lib/popen2.py | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/Lib/popen2.py b/Lib/popen2.py index eb8fb9a..73ff77c 100644 --- a/Lib/popen2.py +++ b/Lib/popen2.py @@ -89,31 +89,55 @@ class Popen3: _active.remove(self) return self.sts -def popen2(cmd, bufsize=-1): - """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is - specified, it sets the buffer size for the I/O pipes. The file objects - (child_stdout, child_stdin) are returned.""" - _cleanup() - inst = Popen3(cmd, 0, bufsize) - return inst.fromchild, inst.tochild +try: + from os import popen2 +except NameError: + def popen2(cmd, mode='t', bufsize=-1): + """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is + specified, it sets the buffer size for the I/O pipes. The file objects + (child_stdout, child_stdin) are returned.""" + if type(mode) is type(0) and bufsize == -1: + bufsize = mode + mode = 't' + assert mode in ('t', 'b') + _cleanup() + inst = Popen3(cmd, 0, bufsize) + return inst.fromchild, inst.tochild -def popen3(cmd, bufsize=-1): - """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is - specified, it sets the buffer size for the I/O pipes. The file objects - (child_stdout, child_stdin, child_stderr) are returned.""" - _cleanup() - inst = Popen3(cmd, 1, bufsize) - return inst.fromchild, inst.tochild, inst.childerr +try: + from os import popen3 +except NameError: + def popen3(cmd, mode='t', bufsize=-1): + """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is + specified, it sets the buffer size for the I/O pipes. The file objects + (child_stdout, child_stdin, child_stderr) are returned.""" + if type(mode) is type(0) and bufsize == -1: + bufsize = mode + mode = 't' + assert mode in ('t', 'b') + _cleanup() + inst = Popen3(cmd, 1, bufsize) + return inst.fromchild, inst.tochild, inst.childerr + +try: + from os import popen4 +except NameError: + pass # not on unix def _test(): teststr = "abc\n" print "testing popen2..." r, w = popen2('cat') + print r, w w.write(teststr) w.close() assert r.read() == teststr print "testing popen3..." - r, w, e = popen3(['cat']) + try: + r, w, e = popen3(['cat']) + except: + r, w, e = popen3('cat') + print r, w, e w.write(teststr) w.close() assert r.read() == teststr |