[Numpy-svn] r5686 - in trunk/numpy/core: . tests

numpy-svn@scip... numpy-svn@scip...
Sat Aug 23 20:19:26 CDT 2008


Author: matthew.brett@gmail.com
Date: 2008-08-23 20:19:20 -0500 (Sat, 23 Aug 2008)
New Revision: 5686

Modified:
   trunk/numpy/core/memmap.py
   trunk/numpy/core/tests/test_memmap.py
Log:
Cleaned up logic and annotation of __del__ method, with test

Modified: trunk/numpy/core/memmap.py
===================================================================
--- trunk/numpy/core/memmap.py	2008-08-24 00:56:32 UTC (rev 5685)
+++ trunk/numpy/core/memmap.py	2008-08-24 01:19:20 UTC (rev 5686)
@@ -219,11 +219,6 @@
         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 @@
     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()

Modified: trunk/numpy/core/tests/test_memmap.py
===================================================================
--- trunk/numpy/core/tests/test_memmap.py	2008-08-24 00:56:32 UTC (rev 5685)
+++ trunk/numpy/core/tests/test_memmap.py	2008-08-24 01:19:20 UTC (rev 5686)
@@ -2,7 +2,7 @@
 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 @@
         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()



More information about the Numpy-svn mailing list