summaryrefslogtreecommitdiffstats
path: root/Lib/popen2.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-01-12 12:29:47 (GMT)
committerGuido van Rossum <guido@python.org>1995-01-12 12:29:47 (GMT)
commit9a22de101fd66c6e0d1a6dda515a7b31d8c9c9aa (patch)
treef01184c19f4618c3881a08a5c7d21a26e701570c /Lib/popen2.py
parent8d2080d043629702a5a79b968e778bcf991dfd73 (diff)
downloadcpython-9a22de101fd66c6e0d1a6dda515a7b31d8c9c9aa.zip
cpython-9a22de101fd66c6e0d1a6dda515a7b31d8c9c9aa.tar.gz
cpython-9a22de101fd66c6e0d1a6dda515a7b31d8c9c9aa.tar.bz2
new files
Diffstat (limited to 'Lib/popen2.py')
-rw-r--r--Lib/popen2.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/popen2.py b/Lib/popen2.py
new file mode 100644
index 0000000..d195fef
--- /dev/null
+++ b/Lib/popen2.py
@@ -0,0 +1,35 @@
+import os
+import sys
+import string
+
+MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???)
+
+def popen2(cmd):
+ cmd = string.split(cmd)
+ p2cread, p2cwrite = os.pipe()
+ c2pread, c2pwrite = os.pipe()
+ pid = os.fork()
+ if pid == 0:
+ # Child
+ os.close(0)
+ os.close(1)
+ if os.dup(p2cread) <> 0:
+ sys.stderr.write('popen2: bad read dup\n')
+ if os.dup(c2pwrite) <> 1:
+ sys.stderr.write('popen2: bad write dup\n')
+ for i in range(3, MAXFD):
+ try:
+ os.close(i)
+ except:
+ pass
+ try:
+ os.execv(cmd[0], cmd)
+ finally:
+ os._exit(1)
+ # Shouldn't come here, I guess
+ os._exit(1)
+ os.close(p2cread)
+ tochild = os.fdopen(p2cwrite, 'w')
+ os.close(c2pwrite)
+ fromchild = os.fdopen(c2pread, 'r')
+ return fromchild, tochild