diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/memmap.py | 35 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 23 |
2 files changed, 22 insertions, 36 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 5a7aaba1c..1ab9f87fc 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -26,6 +26,15 @@ class memmap(ndarray): memmap's are array-like objects. This differs from Python's ``mmap`` module, which uses file-like objects. + This subclass of ndarray has some unpleasant interactions with + some operations, because it doesn't quite fit properly as a subclass. + An alternative to using this subclass is to create the ``mmap`` + object yourself, then create an ndarray with ndarray.__new__ directly, + passing the object created in its 'buffer=' parameter. + + This class may at some point be turned into a factory function + which returns a view into an mmap buffer. + Parameters ---------- filename : str or file-like object @@ -275,27 +284,5 @@ class memmap(ndarray): memmap """ - if self._mmap is not None: - self._mmap.flush() - - def _close(self): - """Close the memmap file. Only do this when deleting the object.""" - if self.base is self._mmap: - # The python mmap probably causes flush on close, but - # we put this here for safety - self._mmap.flush() - self._mmap.close() - self._mmap = None - - def __del__(self): - # We first check if we are the owner of the mmap, rather than - # a view, so deleting a view does not call _close - # on the parent mmap - if self._mmap is self.base: - try: - # First run tell() to see whether file is open - self._mmap.tell() - except ValueError: - pass - else: - self._close() + if self.base is not None and hasattr(self.base, 'flush'): + self.base.flush() diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index 18b356ce2..fa4eb9de0 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -4,7 +4,7 @@ import warnings from numpy import memmap from numpy import arange, allclose -from numpy.testing import TestCase, assert_, assert_array_equal +from numpy.testing import * class TestMemmap(TestCase): def setUp(self): @@ -69,23 +69,22 @@ class TestMemmap(TestCase): fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape) fp[:] = self.data[:] + assert_equal(fp[0], self.data[0]) fp.flush() def test_del(self): # Make sure a view does not delete the underlying mmap fp_base = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape) - fp_view = fp_base[:] - class ViewCloseError(Exception): - pass - _close = memmap._close - def replace_close(self): - raise ViewCloseError('View should not call _close on memmap') - try: - memmap._close = replace_close - del fp_view - finally: - memmap._close = _close + fp_base[0] = 5 + fp_view = fp_base[0:1] + assert_equal(fp_view[0], 5) + del fp_view + # Should still be able to access and assign values after + # deleting the view + assert_equal(fp_base[0], 5) + fp_base[0] = 6 + assert_equal(fp_base[0], 6) if __name__ == "__main__": run_module_suite() |