]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
subprocess now emits a ResourceWarning warning
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 20 May 2016 10:11:15 +0000 (12:11 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 20 May 2016 10:11:15 +0000 (12:11 +0200)
Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning
if the child process is still running.

Doc/library/subprocess.rst
Doc/whatsnew/3.6.rst
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index 2ec94bfe138d5de607d5e55b0bc1d91195516d3f..0545267bfa3ce909674cfbf56e27d9b0f99efef4 100644 (file)
@@ -497,6 +497,10 @@ functions.
    .. 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
 ^^^^^^^^^^
index 67fd50f486eb0301c6c79995efad733b5fd735f2..2ec8f004b26f3963f3ce5c31397855acdce4d71d 100644 (file)
@@ -330,6 +330,16 @@ protocol.
 (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
 ---------
 
index 41a9de10f5efd70eea9c140685c202b267eba3b7..b853f4d59323e042bfe428f4cfe4a67d2086a04b 100644 (file)
@@ -1006,6 +1006,9 @@ class Popen(object):
         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:
index 5239e5aa4e0a5d12fea9159dc991225ad96fb88e..a8f0a64e96e0d10620599affa347daccf544c380 100644 (file)
@@ -2286,7 +2286,9 @@ class POSIXProcessTestCase(BaseTestCase):
         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 @@ class POSIXProcessTestCase(BaseTestCase):
         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])
index ba66c4e5d59852195c0192750fe154d096761a39..5dccc5c5ba7aef00b0c4fa60f6bd48db800e0834 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@ Core and Builtins
 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.