diff --git a/Lib/packaging/tests/test_util.py b/Lib/packaging/tests/test_util.py --- a/Lib/packaging/tests/test_util.py +++ b/Lib/packaging/tests/test_util.py @@ -68,6 +68,16 @@ class FakePopen: self.stdout = StringIO(exes[self.cmd]) self.stderr = StringIO() + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.stdout.close() + self.stderr.close() + + def communicate(self, input=None, timeout=None): + return self.stdout.read(), self.stderr.read() + class UtilTestCase(support.EnvironRestorer, support.TempdirManager, diff --git a/Lib/packaging/util.py b/Lib/packaging/util.py --- a/Lib/packaging/util.py +++ b/Lib/packaging/util.py @@ -465,12 +465,8 @@ def _find_exe_version(cmd, pattern=_RE_V executable = cmd.split()[0] if find_executable(executable) is None: return None - pipe = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) - try: - stdout, stderr = pipe.stdout.read(), pipe.stderr.read() - finally: - pipe.stdout.close() - pipe.stderr.close() + with Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) as pipe: + stdout, stderr = pipe.communicate() # some commands like ld under MacOS X, will give the # output in the stderr, rather than stdout. if stdout != '':