Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b5b9d7

Browse files
author
Victor Stinner
committed
(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.
2 parents 0bb2991 + 87b9bc3 commit 1b5b9d7

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/subprocess.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,10 @@ def __exit__(self, type, value, traceback):
775775
self.wait()
776776

777777
def __del__(self, _maxsize=sys.maxsize, _active=_active):
778-
if not self._child_created:
778+
# If __init__ hasn't had a chance to execute (e.g. if it
779+
# was passed an undeclared keyword argument), we don't
780+
# have a _child_created attribute at all.
781+
if not getattr(self, '_child_created', False):
779782
# We didn't get to successfully create a child process.
780783
return
781784
# In case the child hasn't been waited on, check if it's done.

Lib/test/test_subprocess.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ def test_call_kwargs(self):
146146
env=newenv)
147147
self.assertEqual(rc, 1)
148148

149+
def test_invalid_args(self):
150+
# Popen() called with invalid arguments should raise TypeError
151+
# but Popen.__del__ should not complain (issue #12085)
152+
with support.captured_stderr() as s:
153+
self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
154+
argcount = subprocess.Popen.__init__.__code__.co_argcount
155+
too_many_args = [0] * (argcount + 1)
156+
self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
157+
self.assertEqual(s.getvalue(), '')
158+
149159
def test_stdin_none(self):
150160
# .stdin is None when not redirected
151161
p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ Piet van Oostrum
688688
Jason Orendorff
689689
Douglas Orr
690690
Michele Orrù
691+
Oleg Oshmyan
691692
Denis S. Otkidach
692693
Michael Otteneder
693694
R. M. Oudkerk

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ Core and Builtins
184184
Library
185185
-------
186186

187+
- Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
188+
constructor has failed, e.g. because of an undeclared keyword argument. Patch
189+
written by Oleg Oshmyan.
190+
187191
- Issue #12028: Make threading._get_ident() public, rename it to
188192
threading.get_ident() and document it. This function was already used using
189193
_thread.get_ident().

0 commit comments

Comments
 (0)