Author: Ronny Pfannschmidt <[email protected]>
Branch:
Changeset: r58789:6dd69be8f6a5
Date: 2012-11-07 20:42 +0100
http://bitbucket.org/pypy/pypy/changeset/6dd69be8f6a5/
Log: fix issue1317 - fd returned by os.popen is always a context manager
diff --git a/pypy/module/posix/app_posix.py b/pypy/module/posix/app_posix.py
--- a/pypy/module/posix/app_posix.py
+++ b/pypy/module/posix/app_posix.py
@@ -311,6 +311,13 @@
self._stream.close()
return self._proc.wait() or None # 0 => None
__del__ = close
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *k):
+ self.close()
+
def __getattr__(self, name):
return getattr(self._stream, name)
def __iter__(self):
diff --git a/pypy/module/posix/test/test_posix2.py
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -501,6 +501,13 @@
assert res == '1\n'
assert stream.close() is None
+ def test_popen_with(self):
+ os = self.posix
+ stream = os.popen('echo 1')
+ with stream as fp:
+ res = fp.read()
+ assert res == '1\n'
+
if hasattr(__import__(os.name), '_getfullpathname'):
def test__getfullpathname(self):
# nt specific
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit