watchman: refactor `ctypes.windll.kernel32` references to a local variable
authorMatt Harbison <matt_harbison@yahoo.com>
Thu, 05 Jan 2023 17:24:11 -0500
changeset 49855 36afe74e4439
parent 49854 cb3918e5bc77
child 49856 40d3ee570d83
watchman: refactor `ctypes.windll.kernel32` references to a local variable This is flagged by pytype as an attribute-error, and it's easier to disable that in a single place.
hgext/fsmonitor/pywatchman/__init__.py
--- a/hgext/fsmonitor/pywatchman/__init__.py	Thu Jan 05 17:21:09 2023 -0500
+++ b/hgext/fsmonitor/pywatchman/__init__.py	Thu Jan 05 17:24:11 2023 -0500
@@ -92,7 +92,9 @@
 
     LPDWORD = ctypes.POINTER(wintypes.DWORD)
 
-    CreateFile = ctypes.windll.kernel32.CreateFileA
+    _kernel32 = ctypes.windll.kernel32  # pytype: disable=module-attr
+
+    CreateFile = _kernel32.CreateFileA
     CreateFile.argtypes = [
         wintypes.LPSTR,
         wintypes.DWORD,
@@ -104,11 +106,11 @@
     ]
     CreateFile.restype = wintypes.HANDLE
 
-    CloseHandle = ctypes.windll.kernel32.CloseHandle
+    CloseHandle = _kernel32.CloseHandle
     CloseHandle.argtypes = [wintypes.HANDLE]
     CloseHandle.restype = wintypes.BOOL
 
-    ReadFile = ctypes.windll.kernel32.ReadFile
+    ReadFile = _kernel32.ReadFile
     ReadFile.argtypes = [
         wintypes.HANDLE,
         wintypes.LPVOID,
@@ -118,7 +120,7 @@
     ]
     ReadFile.restype = wintypes.BOOL
 
-    WriteFile = ctypes.windll.kernel32.WriteFile
+    WriteFile = _kernel32.WriteFile
     WriteFile.argtypes = [
         wintypes.HANDLE,
         wintypes.LPVOID,
@@ -128,15 +130,15 @@
     ]
     WriteFile.restype = wintypes.BOOL
 
-    GetLastError = ctypes.windll.kernel32.GetLastError
+    GetLastError = _kernel32.GetLastError
     GetLastError.argtypes = []
     GetLastError.restype = wintypes.DWORD
 
-    SetLastError = ctypes.windll.kernel32.SetLastError
+    SetLastError = _kernel32.SetLastError
     SetLastError.argtypes = [wintypes.DWORD]
     SetLastError.restype = None
 
-    FormatMessage = ctypes.windll.kernel32.FormatMessageA
+    FormatMessage = _kernel32.FormatMessageA
     FormatMessage.argtypes = [
         wintypes.DWORD,
         wintypes.LPVOID,
@@ -148,9 +150,9 @@
     ]
     FormatMessage.restype = wintypes.DWORD
 
-    LocalFree = ctypes.windll.kernel32.LocalFree
+    LocalFree = _kernel32.LocalFree
 
-    GetOverlappedResult = ctypes.windll.kernel32.GetOverlappedResult
+    GetOverlappedResult = _kernel32.GetOverlappedResult
     GetOverlappedResult.argtypes = [
         wintypes.HANDLE,
         ctypes.POINTER(OVERLAPPED),
@@ -159,9 +161,7 @@
     ]
     GetOverlappedResult.restype = wintypes.BOOL
 
-    GetOverlappedResultEx = getattr(
-        ctypes.windll.kernel32, "GetOverlappedResultEx", None
-    )
+    GetOverlappedResultEx = getattr(_kernel32, "GetOverlappedResultEx", None)
     if GetOverlappedResultEx is not None:
         GetOverlappedResultEx.argtypes = [
             wintypes.HANDLE,
@@ -172,7 +172,7 @@
         ]
         GetOverlappedResultEx.restype = wintypes.BOOL
 
-    WaitForSingleObjectEx = ctypes.windll.kernel32.WaitForSingleObjectEx
+    WaitForSingleObjectEx = _kernel32.WaitForSingleObjectEx
     WaitForSingleObjectEx.argtypes = [
         wintypes.HANDLE,
         wintypes.DWORD,
@@ -180,7 +180,7 @@
     ]
     WaitForSingleObjectEx.restype = wintypes.DWORD
 
-    CreateEvent = ctypes.windll.kernel32.CreateEventA
+    CreateEvent = _kernel32.CreateEventA
     CreateEvent.argtypes = [
         LPDWORD,
         wintypes.BOOL,
@@ -190,7 +190,7 @@
     CreateEvent.restype = wintypes.HANDLE
 
     # Windows Vista is the minimum supported client for CancelIoEx.
-    CancelIoEx = ctypes.windll.kernel32.CancelIoEx
+    CancelIoEx = _kernel32.CancelIoEx
     CancelIoEx.argtypes = [wintypes.HANDLE, ctypes.POINTER(OVERLAPPED)]
     CancelIoEx.restype = wintypes.BOOL