20
20
#}END utilities
21
21
22
22
23
- def managed_mmaps ():
23
+ def managed_mmaps (check_entered = True ):
24
24
"""Makes a memory-map context-manager instance for the correct python-version.
25
25
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)
27
30
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.
29
32
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.
32
35
33
36
"""
34
37
mman = SlidingWindowMapManager if PY3 else StaticWindowMapManager
35
38
36
- return mman ()
39
+ return mman (check_entered = check_entered )
37
40
38
41
39
42
class WindowCursor (object ):
@@ -135,9 +138,7 @@ def use_region(self, offset=0, size=0, flags=0):
135
138
136
139
**Note:**: The size actually mapped may be smaller than the given size. If that is the case,
137
140
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 ()
141
142
142
143
need_region = True
143
144
man = self ._manager
@@ -289,6 +290,7 @@ class StaticWindowMapManager(object):
289
290
'_memory_size' , # currently allocated memory size
290
291
'_handle_count' , # amount of currently allocated file handles
291
292
'_entered' , # updated on enter/exit, when 0, `close()`
293
+ 'check_entered' , # bool, whether to scream if not used as context-manager (`with` block)
292
294
]
293
295
294
296
#{ Configuration
@@ -300,7 +302,8 @@ class StaticWindowMapManager(object):
300
302
301
303
_MB_in_bytes = 1024 * 1024
302
304
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 ):
304
307
"""initialize the manager with the given parameters.
305
308
:param window_size: if -1, a default window size will be chosen depending on
306
309
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
310
313
It is a soft limit that is tried to be kept, but nothing bad happens if we have to over-allocate
311
314
:param max_open_handles: if not maxint, limit the amount of open file handles to the given number.
312
315
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
+ """
314
319
self ._fdict = dict ()
315
320
self ._window_size = window_size
316
321
self ._max_memory_size = max_memory_size
317
322
self ._max_handle_count = max_open_handles
318
323
self ._memory_size = 0
319
324
self ._handle_count = 0
320
325
self ._entered = 0
326
+ self .check_entered = check_entered
321
327
322
328
if window_size < 0 :
323
329
coeff = 64
@@ -435,6 +441,10 @@ def _obtain_region(self, a, offset, size, flags, is_recursive):
435
441
assert r .includes_ofs (offset )
436
442
return r
437
443
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
+
438
448
#}END internal methods
439
449
440
450
#{ Interface
@@ -454,8 +464,7 @@ def make_cursor(self, path_or_fd):
454
464
455
465
**Note:** Using file descriptors directly is faster once new windows are mapped as it
456
466
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 ()
459
468
460
469
regions = self ._fdict .get (path_or_fd )
461
470
if regions :
@@ -545,9 +554,10 @@ class SlidingWindowMapManager(StaticWindowMapManager):
545
554
546
555
__slots__ = ()
547
556
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 ):
549
559
"""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 )
551
561
552
562
def _obtain_region (self , a , offset , size , flags , is_recursive ):
553
563
# bisect to find an existing region. The c++ implementation cannot
0 commit comments