changeset 483:37718f514acb

No os.kill() in Windows, use ctypes to call Win32 TerminateProcess()
author Risto Kankkunen <risto.kankkunen@iki.fi>
date Wed, 29 Jul 2009 19:01:13 +0300
parents a42fb4f1716a
children c2d0e738c899
files tests/test_push_command.py tests/test_util.py
diffstat 2 files changed, 46 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_push_command.py
+++ b/tests/test_push_command.py
@@ -106,7 +106,8 @@ class PushTests(test_util.TestBase):
             self.assertNotEqual('an_author', tip.user())
             self.assertEqual('None', tip.user().rsplit('@', 1)[0])
         finally:
-            os.kill(self.svnserve_pid, 9)
+            # TODO: use svnserve.kill() in Python >2.5
+            test_util.kill_process(svnserve)
 
     def test_push_to_default(self, commit=True):
         repo = self.repo
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -26,6 +26,50 @@ import sys
 #   stderr."
 canCloseFds='win32' not in sys.platform
 
+if not 'win32' in sys.platform:
+    def kill_process(popen_obj):
+        os.kill(popen_obj.pid, 9)
+else:
+    import ctypes
+    from ctypes.wintypes import BOOL, DWORD, HANDLE, UINT
+
+    def win_status_check(result, func, args):
+        if result == 0:
+            raise ctypes.WinError()
+        return args
+
+    def WINAPI(returns, func, *params):
+        assert len(params) % 2 == 0
+    
+        func.argtypes = tuple(params[0::2])
+        func.resvalue = returns
+        func.errcheck = win_status_check
+
+        return func
+
+    # dwDesiredAccess
+    PROCESS_TERMINATE = 0x0001
+
+    OpenProcess = WINAPI(HANDLE, ctypes.windll.kernel32.OpenProcess,
+        DWORD, 'dwDesiredAccess',
+        BOOL, 'bInheritHandle',
+        DWORD, 'dwProcessId',
+    )
+
+    CloseHandle =  WINAPI(BOOL, ctypes.windll.kernel32.CloseHandle,
+        HANDLE, 'hObject'
+    )
+
+    TerminateProcess = WINAPI(BOOL, ctypes.windll.kernel32.TerminateProcess,
+        HANDLE, 'hProcess',
+        UINT, 'uExitCode'
+    )
+
+    def kill_process(popen_obj):
+        phnd = OpenProcess(PROCESS_TERMINATE, False, popen_obj.pid)
+        TerminateProcess(phnd, 1)
+        CloseHandle(phnd)
+
 # Fixtures that need to be pulled at a subdirectory of the repo path
 subdir = {'truncatedhistory.svndump': '/project2',
           'fetch_missing_files_subdir.svndump': '/foo',