]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close #12085: Fix an attribute error in subprocess.Popen destructor if the
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 31 May 2011 22:57:47 +0000 (00:57 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 31 May 2011 22:57:47 +0000 (00:57 +0200)
constructor has failed, e.g. because of an undeclared keyword argument. Patch
written by Oleg Oshmyan.

Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/ACKS
Misc/NEWS

index 28dd691090d7122fe34141640529a613c19717e6..4bcf159864fa1d97a88c92024d066319d05b571b 100644 (file)
@@ -768,7 +768,10 @@ class Popen(object):
         self.wait()
 
     def __del__(self, _maxsize=sys.maxsize, _active=_active):
-        if not self._child_created:
+        # If __init__ hasn't had a chance to execute (e.g. if it
+        # was passed an undeclared keyword argument), we don't
+        # have a _child_created attribute at all.
+        if not getattr(self, '_child_created', False):
             # We didn't get to successfully create a child process.
             return
         # In case the child hasn't been waited on, check if it's done.
index ad89864f2e3f7b5028b8857e7bd1bfc1e47838ba..9d6665998e4c601c77372ce8b0862d25878b3132 100644 (file)
@@ -121,6 +121,16 @@ class ProcessTestCase(BaseTestCase):
                              env=newenv)
         self.assertEqual(rc, 1)
 
+    def test_invalid_args(self):
+        # Popen() called with invalid arguments should raise TypeError
+        # but Popen.__del__ should not complain (issue #12085)
+        with support.captured_stderr() as s:
+            self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
+            argcount = subprocess.Popen.__init__.__code__.co_argcount
+            too_many_args = [0] * (argcount + 1)
+            self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
+        self.assertEqual(s.getvalue(), '')
+
     def test_stdin_none(self):
         # .stdin is None when not redirected
         p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
index d0e32e177833d95a5ee355887d38a6f2e6a3f3a3..aa077fca6094832eb401d3f92f709ea2a206413e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -650,6 +650,7 @@ Piet van Oostrum
 Jason Orendorff
 Douglas Orr
 Michele OrrĂ¹
+Oleg Oshmyan
 Denis S. Otkidach
 Michael Otteneder
 R. M. Oudkerk
index 4ab11a517dc1aecd433c7171a8b3bac922717120..3703ac5bd76bbd4384dfea504bbdcc3014dde69e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
+  constructor has failed, e.g. because of an undeclared keyword argument. Patch
+  written by Oleg Oshmyan.
+
 - Issue #985064: Make plistlib more resilient to faulty input plists.
   Patch by Mher Movsisyan.