diff options
author | Guido van Rossum <guido@python.org> | 1997-04-21 14:15:55 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-04-21 14:15:55 (GMT) |
commit | caa9f234bc72886041580ee50b8dd75ccbe76c71 (patch) | |
tree | 6bf39b6722a5397acc36591ec5578a07e7b18961 /Lib | |
parent | 5fd026dc5eb993c703d6f76c5aa0ff81dd1d1753 (diff) | |
download | cpython-caa9f234bc72886041580ee50b8dd75ccbe76c71.zip cpython-caa9f234bc72886041580ee50b8dd75ccbe76c71.tar.gz cpython-caa9f234bc72886041580ee50b8dd75ccbe76c71.tar.bz2 |
Change inspired by Tommy Burnette to add an interface to get stderr, too.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/popen2.py | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/Lib/popen2.py b/Lib/popen2.py index 45dcbb9..d2867c6 100644 --- a/Lib/popen2.py +++ b/Lib/popen2.py @@ -4,10 +4,12 @@ import string MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???) -def popen2(cmd): +def _popen2(cmd, capturestderr=0): cmd = ['/bin/sh', '-c', cmd] p2cread, p2cwrite = os.pipe() c2pread, c2pwrite = os.pipe() + if capturestderr: + errout, errin = os.pipe() pid = os.fork() if pid == 0: # Child @@ -17,11 +19,13 @@ def popen2(cmd): sys.stderr.write('popen2: bad read dup\n') if os.dup(c2pwrite) <> 1: sys.stderr.write('popen2: bad write dup\n') + if capturestderr: + os.close(2) + if os.dup(errin) <> 2: pass for i in range(3, MAXFD): try: os.close(i) - except: - pass + except: pass try: os.execv(cmd[0], cmd) finally: @@ -32,4 +36,15 @@ def popen2(cmd): tochild = os.fdopen(p2cwrite, 'w') os.close(c2pwrite) fromchild = os.fdopen(c2pread, 'r') - return fromchild, tochild + if capturestderr: + os.close(errin) + childerr = os.fdopen(errout, 'r') + return fromchild, tochild, childerr + else: + return fromchild, tochild, None + +def popen2(cmd): + return _popen2(cmd, 0)[:2] + +def popen3(cmd): + return _popen2(cmd, 1) |