diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/memmap.py | 26 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 17 |
2 files changed, 23 insertions, 20 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 643506148..85e424729 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -219,11 +219,6 @@ class memmap(ndarray): self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm, offset=offset, order=order) self._mmap = mm -# Should get rid of these... Are they used? - self._offset = offset - self._mode = mode - self._size = size - self._name = filename return self def __array_finalize__(self, obj): @@ -245,33 +240,26 @@ class memmap(ndarray): 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 - # DEV NOTE: This error is raised on the deletion of each row - # in a view of this memmap. Python traps exceptions in - # __del__ and prints them to stderr. Suppressing this for now - # until memmap code is cleaned up and and better tested for - # numpy v1.1 Objects that do not have a python mmap instance - # as their base data array, should not do anything in the - # close anyway. - #elif self._mmap is not None: - #raise ValueError, "Cannot close a memmap that is being used " \ - # "by another object." - def close(self): """Close the memmap file. Does nothing.""" warnings.warn("``close`` is deprecated on memmap arrays. Use del", DeprecationWarning) def __del__(self): - if self._mmap is not None: + # 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: - # flush any changes to disk, even if it's a view - self.flush() self._close() diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index 9d940ede9..2b8f276e3 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -2,7 +2,7 @@ from tempfile import NamedTemporaryFile, mktemp import os import warnings -from numpy.core import memmap +from numpy import memmap from numpy import arange, allclose from numpy.testing import * @@ -45,6 +45,21 @@ class TestMemmap(TestCase): fp.sync() warnings.simplefilter('default', DeprecationWarning) + 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 if __name__ == "__main__": run_module_suite() |