Patchwork [2,of,3] killdaemons: explicitly set the ctypes signatures

login
register
mail settings
Submitter Matt Harbison
Date June 16, 2017, 3:58 a.m.
Message ID <60b027e4e7b605ab5934.1497585528@Envy>
Download mbox | patch
Permalink /patch/21420/
State Accepted
Headers show

Comments

Matt Harbison - June 16, 2017, 3:58 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1496794686 14400
#      Tue Jun 06 20:18:06 2017 -0400
# Node ID 60b027e4e7b605ab59348ddb983e631fdf24e61e
# Parent  6b3ae1f8640922b49467a52f197bc1014b3f34ff
killdaemons: explicitly set the ctypes signatures

When I tried importing util.posixfile to work around removing a file opened by
another process on Windows, it brought along the declarations in win32.py, which
broke the error handling[1].  It doesn't seem worth hacking killdaemons[2] just
to isolate these declarations in win32.py, so just declare them here to prevent
any future issues.  (win32.py mentions the declarations are required by pypy.)

[1] https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-May/097905.html
[2] https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-May/097907.html

Patch

diff --git a/tests/killdaemons.py b/tests/killdaemons.py
--- a/tests/killdaemons.py
+++ b/tests/killdaemons.py
@@ -10,6 +10,26 @@ 
 if os.name =='nt':
     import ctypes
 
+    _BOOL = ctypes.c_long
+    _DWORD = ctypes.c_ulong
+    _UINT = ctypes.c_uint
+    _HANDLE = ctypes.c_void_p
+
+    ctypes.windll.kernel32.CloseHandle.argtypes = [_HANDLE]
+    ctypes.windll.kernel32.CloseHandle.restype = _BOOL
+
+    ctypes.windll.kernel32.GetLastError.argtypes = []
+    ctypes.windll.kernel32.GetLastError.restype = _DWORD
+
+    ctypes.windll.kernel32.OpenProcess.argtypes = [_DWORD, _BOOL, _DWORD]
+    ctypes.windll.kernel32.OpenProcess.restype = _HANDLE
+
+    ctypes.windll.kernel32.TerminateProcess.argtypes = [_HANDLE, _UINT]
+    ctypes.windll.kernel32.TerminateProcess.restype = _BOOL
+
+    ctypes.windll.kernel32.WaitForSingleObject.argtypes = [_HANDLE, _DWORD]
+    ctypes.windll.kernel32.WaitForSingleObject.restype = _DWORD
+
     def _check(ret, expectederr=None):
         if ret == 0:
             winerrno = ctypes.GetLastError()
@@ -27,7 +47,7 @@ 
         handle = ctypes.windll.kernel32.OpenProcess(
                 PROCESS_TERMINATE|SYNCHRONIZE|PROCESS_QUERY_INFORMATION,
                 False, pid)
-        if handle == 0:
+        if handle is None:
             _check(0, 87) # err 87 when process not found
             return # process not found, already finished
         try: