Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 2e7dbef

Browse files
committed
Port from PR 91579 and PR 20684 in Python
1 parent 4d45943 commit 2e7dbef

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

shared_memory/shared_memory.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Provides shared memory for direct access across processes.
2+
23
The API of this package is currently provisional. Refer to the
34
documentation for details.
45
"""
@@ -22,6 +23,7 @@
2223
from . import _posixshmem
2324
_USE_POSIX = True
2425

26+
from . import resource_tracker
2527

2628
_O_CREX = os.O_CREAT | os.O_EXCL
2729

@@ -113,8 +115,7 @@ def __init__(self, name=None, create=False, size=0):
113115
self.unlink()
114116
raise
115117

116-
from .resource_tracker import register
117-
register(self._name, "shared_memory")
118+
resource_tracker.register(self._name, "shared_memory")
118119

119120
else:
120121

@@ -170,7 +171,10 @@ def __init__(self, name=None, create=False, size=0):
170171
)
171172
finally:
172173
_winapi.CloseHandle(h_map)
173-
size = _winshmem.VirtualQuerySize(p_buf)
174+
try:
175+
size = _winshmem.VirtualQuerySize(p_buf)
176+
finally:
177+
_winshmem.UnmapViewOfFile(p_buf)
174178
self._mmap = mmap.mmap(-1, size, tagname=name)
175179

176180
self._size = size
@@ -233,9 +237,8 @@ def unlink(self):
233237
called once (and only once) across all processes which have access
234238
to the shared memory block."""
235239
if _USE_POSIX and self._name:
236-
from .resource_tracker import unregister
237240
_posixshmem.shm_unlink(self._name)
238-
unregister(self._name, "shared_memory")
241+
resource_tracker.unregister(self._name, "shared_memory")
239242

240243

241244
_encoding = "utf8"

shared_memory/winshmem.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,37 @@ _winshmem_VirtualQuerySize_impl(PyObject *module, LPCVOID address)
241241
return region_size;
242242
}
243243

244+
/*[clinic input]
245+
_winshmem.UnmapViewOfFile
246+
address: LPCVOID
247+
/
248+
[clinic start generated code]*/
249+
250+
static PyObject *
251+
_winshmem_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
252+
/*[clinic end generated code: output=0c5e521bc21e44f6 input=094db9950e24bbbe]*/
253+
{
254+
BOOL success;
255+
256+
Py_BEGIN_ALLOW_THREADS
257+
success = UnmapViewOfFile(address);
258+
Py_END_ALLOW_THREADS
259+
260+
if (!success) {
261+
return PyErr_SetFromWindowsErr(0);
262+
}
263+
264+
Py_RETURN_NONE;
265+
}
266+
244267
#include "clinic/winshmem.c.h"
245268

246269
static PyMethodDef module_methods[ ] = {
247270
_WINSHMEM_CREATEFILEMAPPING_METHODDEF
248271
_WINSHMEM_MAPVIEWOFFILE_METHODDEF
249272
_WINSHMEM_OPENFILEMAPPING_METHODDEF
250273
_WINSHMEM_VIRTUALQUERYSIZE_METHODDEF
274+
_WINSHMEM_UNMAPVIEWOFFILE_METHODDEF
251275
{NULL} /* Sentinel */
252276
};
253277

0 commit comments

Comments
 (0)