Commit 1b5b9d74 authored by Victor Stinner's avatar Victor Stinner

(Merge 3.2) Close #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.
parents 0bb29916 87b9bc38
...@@ -775,7 +775,10 @@ class Popen(object): ...@@ -775,7 +775,10 @@ class Popen(object):
self.wait() self.wait()
def __del__(self, _maxsize=sys.maxsize, _active=_active): 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. # We didn't get to successfully create a child process.
return return
# In case the child hasn't been waited on, check if it's done. # In case the child hasn't been waited on, check if it's done.
......
...@@ -146,6 +146,16 @@ class ProcessTestCase(BaseTestCase): ...@@ -146,6 +146,16 @@ class ProcessTestCase(BaseTestCase):
env=newenv) env=newenv)
self.assertEqual(rc, 1) 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): def test_stdin_none(self):
# .stdin is None when not redirected # .stdin is None when not redirected
p = subprocess.Popen([sys.executable, "-c", 'print("banana")'], p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
......
...@@ -688,6 +688,7 @@ Piet van Oostrum ...@@ -688,6 +688,7 @@ Piet van Oostrum
Jason Orendorff Jason Orendorff
Douglas Orr Douglas Orr
Michele Orrù Michele Orrù
Oleg Oshmyan
Denis S. Otkidach Denis S. Otkidach
Michael Otteneder Michael Otteneder
R. M. Oudkerk R. M. Oudkerk
......
...@@ -184,6 +184,10 @@ Core and Builtins ...@@ -184,6 +184,10 @@ Core and Builtins
Library 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 #12028: Make threading._get_ident() public, rename it to - Issue #12028: Make threading._get_ident() public, rename it to
threading.get_ident() and document it. This function was already used using threading.get_ident() and document it. This function was already used using
_thread.get_ident(). _thread.get_ident().
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment