2626O_CREX = O_CREAT | O_EXCL
2727
2828if os .name == "nt" :
29+ import _winapi
2930 import ctypes
3031 from ctypes import wintypes
3132
32- kernel32 = ctypes .windll . kernel32
33+ kernel32 = ctypes .WinDLL ( ' kernel32' , use_last_error = True )
3334
3435 class MEMORY_BASIC_INFORMATION (ctypes .Structure ):
3536 _fields_ = (
@@ -46,7 +47,7 @@ class MEMORY_BASIC_INFORMATION(ctypes.Structure):
4647 PAGE_READONLY = 0x02
4748 PAGE_EXECUTE_READWRITE = 0x04
4849 INVALID_HANDLE_VALUE = - 1
49- FILE_ALREADY_EXISTS = 183
50+ ERROR_ALREADY_EXISTS = 183
5051
5152 def _errcheck_bool (result , func , args ):
5253 if not result :
@@ -80,9 +81,6 @@ def _errcheck_bool(result, func, args):
8081 wintypes .LPCWSTR
8182 )
8283
83- kernel32 .GetLastError .restype = wintypes .DWORD
84- kernel32 .GetLastError .argtypes = ()
85-
8684 kernel32 .MapViewOfFile .errcheck = _errcheck_bool
8785 kernel32 .MapViewOfFile .restype = wintypes .LPVOID
8886 kernel32 .MapViewOfFile .argtypes = (
@@ -93,9 +91,6 @@ def _errcheck_bool(result, func, args):
9391 ctypes .c_size_t
9492 )
9593
96- kernel32 .CloseHandle .errcheck = _errcheck_bool
97- kernel32 .CloseHandle .argtypes = (wintypes .HANDLE ,)
98-
9994
10095class WindowsNamedSharedMemory :
10196
@@ -113,7 +108,7 @@ def __init__(self, name, flags=None, mode=384, size=0, read_only=False):
113108 try :
114109 p_buf = kernel32 .MapViewOfFile (h_map , PAGE_READONLY , 0 , 0 , 0 )
115110 finally :
116- kernel32 .CloseHandle (h_map )
111+ _winapi .CloseHandle (h_map )
117112 mbi = MEMORY_BASIC_INFORMATION ()
118113 kernel32 .VirtualQuery (p_buf , ctypes .byref (mbi ), mmap .PAGESIZE )
119114 size = mbi .RegionSize
@@ -130,12 +125,12 @@ def __init__(self, name, flags=None, mode=384, size=0, read_only=False):
130125 name
131126 )
132127 try :
133- last_error_code = kernel32 . GetLastError ()
134- if last_error_code == FILE_ALREADY_EXISTS :
128+ last_error_code = ctypes . get_last_error ()
129+ if last_error_code == ERROR_ALREADY_EXISTS :
135130 raise FileExistsError (f"File exists: { name !r} " )
136131 self ._mmap = mmap .mmap (- 1 , size , tagname = name )
137132 finally :
138- kernel32 .CloseHandle (h_map )
133+ _winapi .CloseHandle (h_map )
139134
140135 else :
141136 self ._mmap = mmap .mmap (- 1 , size , tagname = name )
0 commit comments