diff -r 482f60d6a687 Doc/library/subprocess.rst --- a/Doc/library/subprocess.rst Wed May 11 22:37:50 2011 +0800 +++ b/Doc/library/subprocess.rst Wed May 11 18:35:39 2011 +0200 @@ -219,13 +219,19 @@ *creationflags*, if given, can be :data:`CREATE_NEW_CONSOLE` or :data:`CREATE_NEW_PROCESS_GROUP`. (Windows only) - Popen objects are supported as context managers via the :keyword:`with` statement, - closing any open file descriptors on exit. + Popen objects are supported as context managers via the :keyword:`with` statement: + on exit, standard file descriptors are closed, and the process is waited for. :: with Popen(["ifconfig"], stdout=PIPE) as proc: log.write(proc.stdout.read()) + .. note:: + + In the initial implementation, the process was not automatically waited + for upon exit from the context manager. Code intending to be compatible + with Python 2.7.1 and earlier should explicitly call :meth:`Popen.wait`. + .. versionchanged:: 3.2 Added context manager support. diff -r 482f60d6a687 Lib/subprocess.py --- a/Lib/subprocess.py Wed May 11 22:37:50 2011 +0800 +++ b/Lib/subprocess.py Wed May 11 18:35:39 2011 +0200 @@ -796,6 +796,8 @@ self.stderr.close() if self.stdin: self.stdin.close() + # Wait for the process to terminate, to avoid zombies. + self.wait() def __del__(self, _maxsize=sys.maxsize, _active=_active): if not self._child_created: diff -r 482f60d6a687 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Wed May 11 22:37:50 2011 +0800 +++ b/Lib/test/test_subprocess.py Wed May 11 18:35:39 2011 +0200 @@ -1590,7 +1590,8 @@ def test_returncode(self): with subprocess.Popen([sys.executable, "-c", "import sys; sys.exit(100)"]) as proc: - proc.wait() + pass + # __exit__ calls wait(), so the returncode should be set self.assertEqual(proc.returncode, 100) def test_communicate_stdin(self):