Skip to content

Commit

Permalink
Use separate ctypes DLL instances
Browse files Browse the repository at this point in the history
This prevents us being affected or affecting other code that might use
ctypes.
  • Loading branch information
segevfiner authored and BoboTiG committed Oct 13, 2018
1 parent b4dca3f commit eea9b69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
36 changes: 19 additions & 17 deletions src/watchdog/observers/winapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def _errcheck_dword(value, func, args):
return args


ReadDirectoryChangesW = ctypes.windll.kernel32.ReadDirectoryChangesW
kernel32 = ctypes.WinDLL("kernel32")

ReadDirectoryChangesW = kernel32.ReadDirectoryChangesW
ReadDirectoryChangesW.restype = ctypes.wintypes.BOOL
ReadDirectoryChangesW.errcheck = _errcheck_bool
ReadDirectoryChangesW.argtypes = (
Expand All @@ -132,7 +134,7 @@ def _errcheck_dword(value, func, args):
LPVOID # FileIOCompletionRoutine # lpCompletionRoutine
)

CreateFileW = ctypes.windll.kernel32.CreateFileW
CreateFileW = kernel32.CreateFileW
CreateFileW.restype = ctypes.wintypes.HANDLE
CreateFileW.errcheck = _errcheck_handle
CreateFileW.argtypes = (
Expand All @@ -145,21 +147,21 @@ def _errcheck_dword(value, func, args):
ctypes.wintypes.HANDLE # hTemplateFile
)

CloseHandle = ctypes.windll.kernel32.CloseHandle
CloseHandle = kernel32.CloseHandle
CloseHandle.restype = ctypes.wintypes.BOOL
CloseHandle.argtypes = (
ctypes.wintypes.HANDLE, # hObject
)

CancelIoEx = ctypes.windll.kernel32.CancelIoEx
CancelIoEx = kernel32.CancelIoEx
CancelIoEx.restype = ctypes.wintypes.BOOL
CancelIoEx.errcheck = _errcheck_bool
CancelIoEx.argtypes = (
ctypes.wintypes.HANDLE, # hObject
ctypes.POINTER(OVERLAPPED) # lpOverlapped
)

CreateEvent = ctypes.windll.kernel32.CreateEventW
CreateEvent = kernel32.CreateEventW
CreateEvent.restype = ctypes.wintypes.HANDLE
CreateEvent.errcheck = _errcheck_handle
CreateEvent.argtypes = (
Expand All @@ -169,14 +171,14 @@ def _errcheck_dword(value, func, args):
ctypes.wintypes.LPCWSTR, # lpName
)

SetEvent = ctypes.windll.kernel32.SetEvent
SetEvent = kernel32.SetEvent
SetEvent.restype = ctypes.wintypes.BOOL
SetEvent.errcheck = _errcheck_bool
SetEvent.argtypes = (
ctypes.wintypes.HANDLE, # hEvent
)

WaitForSingleObjectEx = ctypes.windll.kernel32.WaitForSingleObjectEx
WaitForSingleObjectEx = kernel32.WaitForSingleObjectEx
WaitForSingleObjectEx.restype = ctypes.wintypes.DWORD
WaitForSingleObjectEx.errcheck = _errcheck_dword
WaitForSingleObjectEx.argtypes = (
Expand All @@ -185,7 +187,7 @@ def _errcheck_dword(value, func, args):
ctypes.wintypes.BOOL, # bAlertable
)

CreateIoCompletionPort = ctypes.windll.kernel32.CreateIoCompletionPort
CreateIoCompletionPort = kernel32.CreateIoCompletionPort
CreateIoCompletionPort.restype = ctypes.wintypes.HANDLE
CreateIoCompletionPort.errcheck = _errcheck_handle
CreateIoCompletionPort.argtypes = (
Expand All @@ -195,7 +197,7 @@ def _errcheck_dword(value, func, args):
ctypes.wintypes.DWORD, # NumberOfConcurrentThreads
)

GetQueuedCompletionStatus = ctypes.windll.kernel32.GetQueuedCompletionStatus
GetQueuedCompletionStatus = kernel32.GetQueuedCompletionStatus
GetQueuedCompletionStatus.restype = ctypes.wintypes.BOOL
GetQueuedCompletionStatus.errcheck = _errcheck_bool
GetQueuedCompletionStatus.argtypes = (
Expand All @@ -206,7 +208,7 @@ def _errcheck_dword(value, func, args):
ctypes.wintypes.DWORD, # dwMilliseconds
)

PostQueuedCompletionStatus = ctypes.windll.kernel32.PostQueuedCompletionStatus
PostQueuedCompletionStatus = kernel32.PostQueuedCompletionStatus
PostQueuedCompletionStatus.restype = ctypes.wintypes.BOOL
PostQueuedCompletionStatus.errcheck = _errcheck_bool
PostQueuedCompletionStatus.argtypes = (
Expand Down Expand Up @@ -250,7 +252,7 @@ class FILE_NOTIFY_INFORMATION(ctypes.Structure):

BUFFER_SIZE = 2048


def _parse_event_buffer(readBuffer, nBytes):
results = []
while nBytes > 0:
Expand Down Expand Up @@ -313,27 +315,27 @@ class WinAPINativeEvent(object):
def __init__(self, action, src_path):
self.action = action
self.src_path = src_path

@property
def is_added(self):
return self.action == FILE_ACTION_CREATED

@property
def is_removed(self):
return self.action == FILE_ACTION_REMOVED

@property
def is_modified(self):
return self.action == FILE_ACTION_MODIFIED

@property
def is_renamed_old(self):
return self.action == FILE_ACTION_RENAMED_OLD_NAME

@property
def is_renamed_new(self):
return self.action == FILE_ACTION_RENAMED_NEW_NAME

def __repr__(self):
return ("<WinAPINativeEvent: action=%d, src_path=%r>" % (self.action, self.src_path))

Expand Down
8 changes: 5 additions & 3 deletions src/watchdog/utils/win32stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
('nFileIndexLow', ctypes.wintypes.DWORD)]


CreateFile = ctypes.windll.kernel32.CreateFileW
kernel32 = ctypes.WinDLL("kernel32")

CreateFile = kernel32.CreateFileW
CreateFile.restype = ctypes.wintypes.HANDLE
CreateFile.argtypes = (
ctypes.c_wchar_p,
Expand All @@ -71,14 +73,14 @@ class BY_HANDLE_FILE_INFORMATION(ctypes.Structure):
ctypes.wintypes.HANDLE,
)

GetFileInformationByHandle = ctypes.windll.kernel32.GetFileInformationByHandle
GetFileInformationByHandle = kernel32.GetFileInformationByHandle
GetFileInformationByHandle.restype = ctypes.wintypes.BOOL
GetFileInformationByHandle.argtypes = (
ctypes.wintypes.HANDLE,
ctypes.wintypes.POINTER(BY_HANDLE_FILE_INFORMATION),
)

CloseHandle = ctypes.windll.kernel32.CloseHandle
CloseHandle = kernel32.CloseHandle
CloseHandle.restype = ctypes.wintypes.BOOL
CloseHandle.argtypes = (ctypes.wintypes.HANDLE,)

Expand Down

0 comments on commit eea9b69

Please sign in to comment.