Index: Lib/os.py =================================================================== --- Lib/os.py (revision 76625) +++ Lib/os.py (working copy) @@ -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): Index: Lib/test/test_popen.py =================================================================== --- Lib/test/test_popen.py (revision 76625) +++ Lib/test/test_popen.py (working copy) @@ -49,6 +49,10 @@ 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_main(): support.run_unittest(PopenTest)