[Python-checkins] cpython: subprocess now emits a ResourceWarning warning

victor.stinner python-checkins at python.org
Fri May 20 06:48:09 EDT 2016


https://hg.python.org/cpython/rev/b7f3494deb2c
changeset:   101446:b7f3494deb2c
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri May 20 12:11:15 2016 +0200
summary:
  subprocess now emits a ResourceWarning warning

Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning
if the child process is still running.

files:
  Doc/library/subprocess.rst  |   4 ++++
  Doc/whatsnew/3.6.rst        |  10 ++++++++++
  Lib/subprocess.py           |   3 +++
  Lib/test/test_subprocess.py |   8 ++++++--
  Misc/NEWS                   |   3 +++
  5 files changed, 26 insertions(+), 2 deletions(-)


diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst
--- a/Doc/library/subprocess.rst
+++ b/Doc/library/subprocess.rst
@@ -497,6 +497,10 @@
    .. versionchanged:: 3.2
       Added context manager support.
 
+   .. versionchanged:: 3.6
+      Popen destructor now emits a :exc:`ResourceWarning` warning if the child
+      process is still running.
+
 
 Exceptions
 ^^^^^^^^^^
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -330,6 +330,16 @@
 (Contributed by Aviv Palivoda in :issue:`26404`.)
 
 
+subprocess
+----------
+
+:class:`subprocess.Popen` destructor now emits a :exc:`ResourceWarning` warning
+if the child process is still running. Use the context manager protocol (``with
+proc: ...``) or call explicitly the :meth:`~subprocess.Popen.wait` method to
+read the exit status of the child process (Contributed by Victor Stinner in
+:issue:`26741`).
+
+
 telnetlib
 ---------
 
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1006,6 +1006,9 @@
         if not self._child_created:
             # We didn't get to successfully create a child process.
             return
+        if self.returncode is None:
+            warnings.warn("running subprocess %r" % self, ResourceWarning,
+                          source=self)
         # In case the child hasn't been waited on, check if it's done.
         self._internal_poll(_deadstate=_maxsize)
         if self.returncode is None and _active is not None:
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -2286,7 +2286,9 @@
         self.addCleanup(p.stderr.close)
         ident = id(p)
         pid = p.pid
-        del p
+        with support.check_warnings(('', ResourceWarning)):
+            p = None
+
         # check that p is in the active processes list
         self.assertIn(ident, [id(o) for o in subprocess._active])
 
@@ -2305,7 +2307,9 @@
         self.addCleanup(p.stderr.close)
         ident = id(p)
         pid = p.pid
-        del p
+        with support.check_warnings(('', ResourceWarning)):
+            p = None
+
         os.kill(pid, signal.SIGKILL)
         # check that p is in the active processes list
         self.assertIn(ident, [id(o) for o in subprocess._active])
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@
 Library
 -------
 
+- Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning
+  if the child process is still running.
+
 - Issue #27056: Optimize pickle.load() and pickle.loads(), up to 10% faster
   to deserialize a lot of small objects.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list