[Python-checkins] r76723 - in python/branches/py3k/Lib: os.py test/test_popen.py
antoine.pitrou
python-checkins at python.org
Wed Dec 9 01:01:27 CET 2009
Author: antoine.pitrou
Date: Wed Dec 9 01:01:27 2009
New Revision: 76723
Log:
Issue #7461: objects returned by os.popen() should support the context manager protocol
Modified:
python/branches/py3k/Lib/os.py
python/branches/py3k/Lib/test/test_popen.py
Modified: python/branches/py3k/Lib/os.py
==============================================================================
--- python/branches/py3k/Lib/os.py (original)
+++ python/branches/py3k/Lib/os.py Wed Dec 9 01:01:27 2009
@@ -650,6 +650,10 @@
return returncode
else:
return returncode << 8 # Shift left to match old behavior
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ self.close()
def __getattr__(self, name):
return getattr(self._stream, name)
def __iter__(self):
Modified: python/branches/py3k/Lib/test/test_popen.py
==============================================================================
--- python/branches/py3k/Lib/test/test_popen.py (original)
+++ python/branches/py3k/Lib/test/test_popen.py Wed Dec 9 01:01:27 2009
@@ -49,6 +49,14 @@
else:
self.assertEqual(os.popen("exit 42").close(), 42 << 8)
+ def test_contextmanager(self):
+ with os.popen("echo hello") as f:
+ self.assertEqual(f.read(), "hello\n")
+
+ def test_iterating(self):
+ with os.popen("echo hello") as f:
+ self.assertEqual(list(f), ["hello\n"])
+
def test_main():
support.run_unittest(PopenTest)
More information about the Python-checkins
mailing list