summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-05-24 00:50:02 (GMT)
committerGuido van Rossum <guido@python.org>2007-05-24 00:50:02 (GMT)
commitc2f93dc2e42b48a20578599407b0bb51a6663d09 (patch)
treea13677daceceb29f55a468cac3874e1a757dccc9 /Lib/os.py
parentd8595fe304c3d9bb15ad59b1db0b71a2b7ab3c54 (diff)
downloadcpython-c2f93dc2e42b48a20578599407b0bb51a6663d09.zip
cpython-c2f93dc2e42b48a20578599407b0bb51a6663d09.tar.gz
cpython-c2f93dc2e42b48a20578599407b0bb51a6663d09.tar.bz2
Remove native popen() and fdopen(), replacing them with subprocess calls.
Fix a path to an assert in fileio_read(). Some misc tweaks.
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 5ddf07c..0ced882 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -696,3 +696,41 @@ if not _exists("urandom"):
bs += read(_urandomfd, n - len(bs))
close(_urandomfd)
return bs
+
+# Supply os.popen()
+def popen(cmd, mode="r", buffering=None):
+ if not isinstance(cmd, basestring):
+ raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
+ if mode not in ("r", "w"):
+ raise ValueError("invalid mode %r" % mode)
+ import subprocess, io
+ if mode == "r":
+ proc = subprocess.Popen(cmd,
+ shell=True,
+ stdout=subprocess.PIPE,
+ bufsize=buffering)
+ return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
+ else:
+ proc = subprocess.Popen(cmd,
+ shell=True,
+ stdin=subprocess.PIPE,
+ bufsize=buffering)
+ return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
+
+# Helper for popen() -- a proxy for a file whose close waits for the process
+class _wrap_close:
+ def __init__(self, stream, proc):
+ self._stream = stream
+ self._proc = proc
+ def close(self):
+ self._stream.close()
+ return self._proc.wait() << 8 # Shift left to match old behavior
+ def __getattr__(self, name):
+ return getattr(self._stream, name)
+
+# Supply os.fdopen() (used by subprocess!)
+def fdopen(fd, mode="r", buffering=-1):
+ if not isinstance(fd, int):
+ raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
+ import io
+ return io.open(fd, mode, buffering)