Mercurial > cpython-lru_cache-weakref
annotate Lib/popen2.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 44145eeb6100 |
| children | 67995bc17f62 |
| rev | line source |
|---|---|
| 2135 | 1 import os |
| 2 import sys | |
| 3 import string | |
| 4 | |
| 5 MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???) | |
| 6 | |
| 7 def popen2(cmd): | |
|
2924
44145eeb6100
pass the command to sh -c
Guido van Rossum <guido@python.org>
parents:
2135
diff
changeset
|
8 cmd = ['/bin/sh', '-c', cmd] |
| 2135 | 9 p2cread, p2cwrite = os.pipe() |
| 10 c2pread, c2pwrite = os.pipe() | |
| 11 pid = os.fork() | |
| 12 if pid == 0: | |
| 13 # Child | |
| 14 os.close(0) | |
| 15 os.close(1) | |
| 16 if os.dup(p2cread) <> 0: | |
| 17 sys.stderr.write('popen2: bad read dup\n') | |
| 18 if os.dup(c2pwrite) <> 1: | |
| 19 sys.stderr.write('popen2: bad write dup\n') | |
| 20 for i in range(3, MAXFD): | |
| 21 try: | |
| 22 os.close(i) | |
| 23 except: | |
| 24 pass | |
| 25 try: | |
| 26 os.execv(cmd[0], cmd) | |
| 27 finally: | |
| 28 os._exit(1) | |
| 29 # Shouldn't come here, I guess | |
| 30 os._exit(1) | |
| 31 os.close(p2cread) | |
| 32 tochild = os.fdopen(p2cwrite, 'w') | |
| 33 os.close(c2pwrite) | |
| 34 fromchild = os.fdopen(c2pread, 'r') | |
| 35 return fromchild, tochild |
