[Python-checkins] r81155 - in python/branches/py3k: Lib/subprocess.py

brett.cannon python-checkins at python.org
Fri May 14 02:33:40 CEST 2010


Author: brett.cannon
Date: Fri May 14 02:33:40 2010
New Revision: 81155

Log:
Merged revisions 81154 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81154 | brett.cannon | 2010-05-13 17:21:48 -0700 (Thu, 13 May 2010) | 15 lines
  
  subprocess.Popen.__del__ referenced global objects, which is a no-no thanks to
  interpreter shutdown semantics. Same issue goes for the methods that __del__
  called. Now all the methods capture the global objects it needs as default
  values to private parameters (could have stuck them on the class object itself,
  but since the objects have nothing directly to do with the class that seemed
  wrong).
  
  There is no test as making one that works is hard. This patch was
  verified against a consistently failing test in Mercurial's test suite, though,
  so it has been tested in some regard.
  
  Closes issue #5099. Thanks to Mary Stern for the bug report and Gabriel
  Genellina for writing another patch for the same issue and attempting to write
  a test.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/subprocess.py

Modified: python/branches/py3k/Lib/subprocess.py
==============================================================================
--- python/branches/py3k/Lib/subprocess.py	(original)
+++ python/branches/py3k/Lib/subprocess.py	Fri May 14 02:33:40 2010
@@ -356,7 +356,6 @@
 
 
 if mswindows:
-    from _subprocess import CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP
     import threading
     import msvcrt
     import _subprocess
@@ -394,6 +393,7 @@
            "getoutput", "check_output", "CalledProcessError"]
 
 if mswindows:
+    from _subprocess import CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP
     __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP"])
 try:
     MAXFD = os.sysconf("SC_OPEN_MAX")
@@ -698,12 +698,12 @@
         return data.decode(encoding)
 
 
-    def __del__(self, sys=sys):
+    def __del__(self, _maxsize=sys.maxsize, _active=_active):
         if not self._child_created:
             # 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.
-        self._internal_poll(_deadstate=sys.maxsize)
+        self._internal_poll(_deadstate=_maxsize)
         if self.returncode is None and _active is not None:
             # Child is still running, keep us alive until we can wait on it.
             _active.append(self)
@@ -907,13 +907,20 @@
                 errwrite.Close()
 
 
-        def _internal_poll(self, _deadstate=None):
+        def _internal_poll(self, _deadstate=None,
+                _WaitForSingleObject=WaitForSingleObject,
+                _WAIT_OBJECT_0=WAIT_OBJECT_0,
+                _GetExitCodeProcess=GetExitCodeProcess):
             """Check if child process has terminated.  Returns returncode
-            attribute."""
+            attribute.
+
+            This method is called by __del__, so it can only refer to objects
+            in its local scope.
+
+            """
             if self.returncode is None:
-                if(_subprocess.WaitForSingleObject(self._handle, 0) ==
-                   _subprocess.WAIT_OBJECT_0):
-                    self.returncode = _subprocess.GetExitCodeProcess(self._handle)
+                if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
+                    self.returncode = _GetExitCodeProcess(self._handle)
             return self.returncode
 
 
@@ -1252,25 +1259,35 @@
                 raise child_exception_type(err_msg)
 
 
-        def _handle_exitstatus(self, sts):
-            if os.WIFSIGNALED(sts):
-                self.returncode = -os.WTERMSIG(sts)
-            elif os.WIFEXITED(sts):
-                self.returncode = os.WEXITSTATUS(sts)
+        def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
+                _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
+                _WEXITSTATUS=os.WEXITSTATUS):
+            # This method is called (indirectly) by __del__, so it cannot
+            # refer to anything outside of its local scope."""
+            if _WIFSIGNALED(sts):
+                self.returncode = -_WTERMSIG(sts)
+            elif _WIFEXITED(sts):
+                self.returncode = _WEXITSTATUS(sts)
             else:
                 # Should never happen
                 raise RuntimeError("Unknown child exit status!")
 
 
-        def _internal_poll(self, _deadstate=None):
+        def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
+                _WNOHANG=os.WNOHANG, _os_error=os.error):
             """Check if child process has terminated.  Returns returncode
-            attribute."""
+            attribute.
+
+            This method is called by __del__, so it cannot reference anything
+            outside of the local scope (nor can any methods it calls).
+
+            """
             if self.returncode is None:
                 try:
-                    pid, sts = os.waitpid(self.pid, os.WNOHANG)
+                    pid, sts = _waitpid(self.pid, _WNOHANG)
                     if pid == self.pid:
                         self._handle_exitstatus(sts)
-                except os.error:
+                except _os_error:
                     if _deadstate is not None:
                         self.returncode = _deadstate
             return self.returncode


More information about the Python-checkins mailing list