Fixes to testpid() for Windows.
authorLee Cantey <lcantey@gmail.com>
Thu, 30 Mar 2006 18:20:08 -0800
changeset 2024 6328445b0e71
parent 2023 3bdd3bf17cfa
child 2025 581d9a8b5fb9
Fixes to testpid() for Windows. Handle processes that no longer exist and processes that belong to another user. Enables the lock breaking changes from d314a89fa4f1 and subsequently "fixes" the left over locks reported in bug 112.
mercurial/util.py
--- a/mercurial/util.py	Thu Mar 30 18:43:46 2006 +0200
+++ b/mercurial/util.py	Thu Mar 30 18:20:08 2006 -0800
@@ -535,9 +535,10 @@
             pf = pf[1:-1] # Remove the quotes
         return pf
 
-    try: # ActivePython can create hard links using win32file module
-        import win32api, win32con, win32file
+    try: # Mark Hammond's win32all package allows better functionality on Windows
+        import win32api, win32con, win32file, pywintypes
 
+        # create hard links using win32file module
         def os_link(src, dst): # NB will only succeed on NTFS
             win32file.CreateHardLink(dst, src)
 
@@ -554,12 +555,19 @@
                 return os.stat(pathname).st_nlink
 
         def testpid(pid):
-            '''return False if pid is dead, True if running or not known'''
+            '''return True if pid is still running or unable to determine, False otherwise'''
             try:
-                win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION,
-                                     False, pid)
-            except:
-                return True
+                handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, pid) 
+                if handle:
+                    status = win32process.GetExitCodeProcess(handle)
+                    if status == win32con.STILL_ACTIVE:
+                        return True
+                    else:
+                        return False
+            except pywintypes.error, details:
+                if details[0] == 87: # ERROR_INVALID_PARAMETER
+                    return False
+            return True
 
     except ImportError:
         def testpid(pid):