diff options
author | Brian Curtin <brian.curtin@gmail.com> | 2010-12-03 02:46:02 (GMT) |
---|---|---|
committer | Brian Curtin <brian.curtin@gmail.com> | 2010-12-03 02:46:02 (GMT) |
commit | 79cdb661f5a6cf8bba07aa50f4451f6c409bb067 (patch) | |
tree | 951cc547f5023ab2965b790fe01617e19952d3cf /Lib/subprocess.py | |
parent | 2d93e6ee637f3ec62114919835a32ed009b99a0b (diff) | |
download | cpython-79cdb661f5a6cf8bba07aa50f4451f6c409bb067.zip cpython-79cdb661f5a6cf8bba07aa50f4451f6c409bb067.tar.gz cpython-79cdb661f5a6cf8bba07aa50f4451f6c409bb067.tar.bz2 |
Fix #10554. Added context manager support to Popen objects.
Added a few common Popen uses to the tests like we've done for a few other
instances of adding context managers. Eventually the entire test suite
could be converted to use the context manager format.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 192185d..bdbcc6b 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -697,6 +697,16 @@ class Popen(object): data = data.replace(b"\r\n", b"\n").replace(b"\r", b"\n") return data.decode(encoding) + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + if self.stdout: + self.stdout.close() + if self.stderr: + self.stderr.close() + if self.stdin: + self.stdin.close() def __del__(self, _maxsize=sys.maxsize, _active=_active): if not self._child_created: |