@@ -707,6 +707,9 @@ def getoutput(cmd):
707707708708709709class Popen(object):
710+711+_child_created = False # Set here since __del__ checks it
712+710713def __init__(self, args, bufsize=-1, executable=None,
711714stdin=None, stdout=None, stderr=None,
712715preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
@@ -717,7 +720,6 @@ def __init__(self, args, bufsize=-1, executable=None,
717720"""Create new Popen instance."""
718721_cleanup()
719722720-self._child_created = False
721723self._input = None
722724self._communication_started = False
723725if bufsize is None:
@@ -859,11 +861,8 @@ def __exit__(self, type, value, traceback):
859861# Wait for the process to terminate, to avoid zombies.
860862self.wait()
861863862-def __del__(self, _maxsize=sys.maxsize, _active=_active):
863-# If __init__ hasn't had a chance to execute (e.g. if it
864-# was passed an undeclared keyword argument), we don't
865-# have a _child_created attribute at all.
866-if not getattr(self, '_child_created', False):
864+def __del__(self, _maxsize=sys.maxsize):
865+if not self._child_created:
867866# We didn't get to successfully create a child process.
868867return
869868# In case the child hasn't been waited on, check if it's done.
@@ -1446,7 +1445,7 @@ def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
14461445_WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
14471446_WEXITSTATUS=os.WEXITSTATUS):
14481447# This method is called (indirectly) by __del__, so it cannot
1449-# refer to anything outside of its local scope."""
1448+# refer to anything outside of its local scope.
14501449if _WIFSIGNALED(sts):
14511450self.returncode = -_WTERMSIG(sts)
14521451elif _WIFEXITED(sts):