]> granicus.if.org Git - python/commitdiff
Close #12085: Fix an attribute error in subprocess.Popen destructor if the
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 31 May 2011 23:03:00 +0000 (01:03 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 31 May 2011 23:03:00 +0000 (01:03 +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 3734acde3340684d64b6c3402f539484306cb37d..bdf85fc108f3e71ee7157f1e80f79a4964b7aa01 100644 (file)
@@ -707,7 +707,10 @@ class Popen(object):
 
 
     def __del__(self, _maxint=sys.maxint, _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 b635ef397c47d1da642e182beaaff7bdbe9053ff..73c1b5c76b470d421f183439019cb17937d41ae3 100644 (file)
@@ -113,6 +113,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 0066b3273a62f9f99601cdba4e3848f1ff446233..4677448cbb2b77dce565b4b3133acd5ef276a0ef 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -607,6 +607,7 @@ Piet van Oostrum
 Jason Orendorff
 Douglas Orr
 Michele OrrĂ¹
+Oleg Oshmyan
 Denis S. Otkidach
 Michael Otteneder
 R. M. Oudkerk
index 517a6e51b26b94c38cbab26e9f9ae85ed65ea024..f6ecd229b5a64cca8df62c0454453964e57d1f2c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,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.
+
 Tests
 -----