@@ -99,7 +99,13 @@ class MapRegion(object):
9999 if _need_compat_layer :
100100 __slots__ .append ('_mfb' ) # mapped memory buffer to provide offset
101101 #END handle additional slot
102-
102+
103+ #{ Configuration
104+ # Used for testing only. If True, all data will be loaded into memory at once.
105+ # This makes sure no file handles will remain open.
106+ _test_read_into_memory = False
107+ #} END configuration
108+
103109
104110 def __init__ (self , path_or_fd , ofs , size , flags = 0 ):
105111 """Initialize a region, allocate the memory map
@@ -132,7 +138,13 @@ def __init__(self, path_or_fd, ofs, size, flags = 0):
132138 # have to correct size, otherwise (instead of the c version) it will
133139 # bark that the size is too large ... many extra file accesses because
134140 # if this ... argh !
135- self ._mf = mmap (fd , min (os .fstat (fd ).st_size - sizeofs , corrected_size ), ** kwargs )
141+ actual_size = min (os .fstat (fd ).st_size - sizeofs , corrected_size )
142+ if self ._test_read_into_memory :
143+ self ._mf = self ._read_into_memory (fd , ofs , actual_size )
144+ else :
145+ self ._mf = mmap (fd , actual_size , ** kwargs )
146+ #END handle memory mode
147+
136148 self ._size = len (self ._mf )
137149
138150 if self ._need_compat_layer :
@@ -144,6 +156,19 @@ def __init__(self, path_or_fd, ofs, size, flags = 0):
144156 #END only close it if we opened it
145157 #END close file handle
146158
159+ def _read_into_memory (self , fd , offset , size ):
160+ """:return: string data as read from the given file descriptor, offset and size """
161+ os .lseek (fd , offset , os .SEEK_SET )
162+ mf = ''
163+ bytes_todo = size
164+ while bytes_todo :
165+ chunk = 1024 * 1024
166+ d = os .read (fd , chunk )
167+ bytes_todo -= len (d )
168+ mf += d
169+ #END loop copy items
170+ return mf
171+
147172 def __repr__ (self ):
148173 return "MapRegion<%i, %i>" % (self ._b , self .size ())
149174
@@ -204,7 +229,7 @@ def includes_ofs(self, ofs):
204229
205230 #} END interface
206231
207-
232+
208233class MapRegionList (list ):
209234 """List of MapRegion instances associating a path with a list of regions."""
210235 __slots__ = (
0 commit comments