26
26
O_CREX = O_CREAT | O_EXCL
27
27
28
28
if os .name == "nt" :
29
+ import _winapi
29
30
import ctypes
30
31
from ctypes import wintypes
31
32
32
- kernel32 = ctypes .windll . kernel32
33
+ kernel32 = ctypes .WinDLL ( ' kernel32' , use_last_error = True )
33
34
34
35
class MEMORY_BASIC_INFORMATION (ctypes .Structure ):
35
36
_fields_ = (
@@ -46,7 +47,7 @@ class MEMORY_BASIC_INFORMATION(ctypes.Structure):
46
47
PAGE_READONLY = 0x02
47
48
PAGE_EXECUTE_READWRITE = 0x04
48
49
INVALID_HANDLE_VALUE = - 1
49
- FILE_ALREADY_EXISTS = 183
50
+ ERROR_ALREADY_EXISTS = 183
50
51
51
52
def _errcheck_bool (result , func , args ):
52
53
if not result :
@@ -80,9 +81,6 @@ def _errcheck_bool(result, func, args):
80
81
wintypes .LPCWSTR
81
82
)
82
83
83
- kernel32 .GetLastError .restype = wintypes .DWORD
84
- kernel32 .GetLastError .argtypes = ()
85
-
86
84
kernel32 .MapViewOfFile .errcheck = _errcheck_bool
87
85
kernel32 .MapViewOfFile .restype = wintypes .LPVOID
88
86
kernel32 .MapViewOfFile .argtypes = (
@@ -93,9 +91,6 @@ def _errcheck_bool(result, func, args):
93
91
ctypes .c_size_t
94
92
)
95
93
96
- kernel32 .CloseHandle .errcheck = _errcheck_bool
97
- kernel32 .CloseHandle .argtypes = (wintypes .HANDLE ,)
98
-
99
94
100
95
class WindowsNamedSharedMemory :
101
96
@@ -113,7 +108,7 @@ def __init__(self, name, flags=None, mode=384, size=0, read_only=False):
113
108
try :
114
109
p_buf = kernel32 .MapViewOfFile (h_map , PAGE_READONLY , 0 , 0 , 0 )
115
110
finally :
116
- kernel32 .CloseHandle (h_map )
111
+ _winapi .CloseHandle (h_map )
117
112
mbi = MEMORY_BASIC_INFORMATION ()
118
113
kernel32 .VirtualQuery (p_buf , ctypes .byref (mbi ), mmap .PAGESIZE )
119
114
size = mbi .RegionSize
@@ -130,12 +125,12 @@ def __init__(self, name, flags=None, mode=384, size=0, read_only=False):
130
125
name
131
126
)
132
127
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 :
135
130
raise FileExistsError (f"File exists: { name !r} " )
136
131
self ._mmap = mmap .mmap (- 1 , size , tagname = name )
137
132
finally :
138
- kernel32 .CloseHandle (h_map )
133
+ _winapi .CloseHandle (h_map )
139
134
140
135
else :
141
136
self ._mmap = mmap .mmap (- 1 , size , tagname = name )
0 commit comments