Skip to content

Commit bf68f77

Browse files
committed
feat(mman-contxt): opt-out not to scream if mman not entered
1 parent 01df7f3 commit bf68f77

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

smmap/mman.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
#}END utilities
2121

2222

23-
def managed_mmaps():
23+
def managed_mmaps(check_entered=True):
2424
"""Makes a memory-map context-manager instance for the correct python-version.
2525
26-
:return: either :class:`SlidingWindowMapManager` or :class:`StaticWindowMapManager` (if PY2)
26+
:param bool check_entered:
27+
whether to scream if not used as context-manager (`with` block)
28+
:return:
29+
either :class:`SlidingWindowMapManager` or :class:`StaticWindowMapManager` (if PY2)
2730
28-
If you want to change the default parameters of these classes, use them directly.
31+
If you want to change other default parameters of these classes, use them directly.
2932
30-
.. Tip::
31-
Use it in a ``with ...:`` block, to free cached (and unused) resources.
33+
.. Tip::
34+
Use it in a ``with ...:`` block, to free cached (and unused) resources.
3235
3336
"""
3437
mman = SlidingWindowMapManager if PY3 else StaticWindowMapManager
3538

36-
return mman()
39+
return mman(check_entered=check_entered)
3740

3841

3942
class WindowCursor(object):
@@ -135,9 +138,7 @@ def use_region(self, offset=0, size=0, flags=0):
135138
136139
**Note:**: The size actually mapped may be smaller than the given size. If that is the case,
137140
either the file has reached its end, or the map was created between two existing regions"""
138-
if self._manager._entered <= 0:
139-
raise ValueError('Context-manager %s not entered for %s!' %
140-
(self._manager, self))
141+
self._manager._check_if_entered()
141142

142143
need_region = True
143144
man = self._manager
@@ -289,6 +290,7 @@ class StaticWindowMapManager(object):
289290
'_memory_size', # currently allocated memory size
290291
'_handle_count', # amount of currently allocated file handles
291292
'_entered', # updated on enter/exit, when 0, `close()`
293+
'check_entered', # bool, whether to scream if not used as context-manager (`with` block)
292294
]
293295

294296
#{ Configuration
@@ -300,7 +302,8 @@ class StaticWindowMapManager(object):
300302

301303
_MB_in_bytes = 1024 * 1024
302304

303-
def __init__(self, window_size=0, max_memory_size=0, max_open_handles=sys.maxsize):
305+
def __init__(self, window_size=0, max_memory_size=0, max_open_handles=sys.maxsize,
306+
check_entered=True):
304307
"""initialize the manager with the given parameters.
305308
:param window_size: if -1, a default window size will be chosen depending on
306309
the operating system's architecture. It will internally be quantified to a multiple of the page size
@@ -310,14 +313,17 @@ def __init__(self, window_size=0, max_memory_size=0, max_open_handles=sys.maxsiz
310313
It is a soft limit that is tried to be kept, but nothing bad happens if we have to over-allocate
311314
:param max_open_handles: if not maxint, limit the amount of open file handles to the given number.
312315
Otherwise the amount is only limited by the system itself. If a system or soft limit is hit,
313-
the manager will free as many handles as possible"""
316+
the manager will free as many handles as possible
317+
:param bool check_entered: whether to scream if not used as context-manager (`with` block)
318+
"""
314319
self._fdict = dict()
315320
self._window_size = window_size
316321
self._max_memory_size = max_memory_size
317322
self._max_handle_count = max_open_handles
318323
self._memory_size = 0
319324
self._handle_count = 0
320325
self._entered = 0
326+
self.check_entered = check_entered
321327

322328
if window_size < 0:
323329
coeff = 64
@@ -435,6 +441,10 @@ def _obtain_region(self, a, offset, size, flags, is_recursive):
435441
assert r.includes_ofs(offset)
436442
return r
437443

444+
def _check_if_entered(self):
445+
if self.check_entered and self._entered <= 0:
446+
raise ValueError('Context-manager %s not entered!' % self)
447+
438448
#}END internal methods
439449

440450
#{ Interface
@@ -454,8 +464,7 @@ def make_cursor(self, path_or_fd):
454464
455465
**Note:** Using file descriptors directly is faster once new windows are mapped as it
456466
prevents the file to be opened again just for the purpose of mapping it."""
457-
if self._entered <= 0:
458-
raise ValueError('Context-manager %s not entered!' % self)
467+
self._check_if_entered()
459468

460469
regions = self._fdict.get(path_or_fd)
461470
if regions:
@@ -545,9 +554,10 @@ class SlidingWindowMapManager(StaticWindowMapManager):
545554

546555
__slots__ = ()
547556

548-
def __init__(self, window_size=-1, max_memory_size=0, max_open_handles=sys.maxsize):
557+
def __init__(self, window_size=-1, max_memory_size=0, max_open_handles=sys.maxsize,
558+
check_entered=True):
549559
"""Adjusts the default window size to -1"""
550-
super(SlidingWindowMapManager, self).__init__(window_size, max_memory_size, max_open_handles)
560+
super(SlidingWindowMapManager, self).__init__(window_size, max_memory_size, max_open_handles, check_entered)
551561

552562
def _obtain_region(self, a, offset, size, flags, is_recursive):
553563
# bisect to find an existing region. The c++ implementation cannot

0 commit comments

Comments
 (0)