summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSveinung Gundersen <sveinugu@gmail.com>2012-07-03 11:31:40 +0200
committerNathaniel J. Smith <njs@pobox.com>2012-07-04 18:28:54 +0100
commit4fc64c9286a3d83db9f91c57af0eb5d447e0049b (patch)
treef8d6a07a8d6e0ebb2637154875e0666cf1573fb5
parent903d9b7d2a5f899f3ef17841ec3ee12ba657d78a (diff)
downloadnumpy-4fc64c9286a3d83db9f91c57af0eb5d447e0049b.tar.gz
BUG: fix incorrect references to parents in memmap children.
[Back-ported from master by njs, with an additional tweak to memmap.__del__ to avoid unhandled-exception warnings.]
-rw-r--r--numpy/core/memmap.py11
-rw-r--r--numpy/core/tests/test_memmap.py19
2 files changed, 26 insertions, 4 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index 844e13c4e..589597647 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -4,7 +4,7 @@ import warnings
from numeric import uint8, ndarray, dtype
import sys
-from numpy.compat import asbytes
+import numpy as np
dtypedescr = dtype
valid_filemodes = ["r", "c", "r+", "w+"]
@@ -219,7 +219,7 @@ class memmap(ndarray):
if mode == 'w+' or (mode == 'r+' and flen < bytes):
fid.seek(bytes - 1, 0)
- fid.write(asbytes('\0'))
+ fid.write(np.compat.asbytes('\0'))
fid.flush()
if mode == 'c':
@@ -252,13 +252,16 @@ class memmap(ndarray):
return self
def __array_finalize__(self, obj):
- if hasattr(obj, '_mmap'):
+ if hasattr(obj, '_mmap') and np.may_share_memory(self, obj):
self._mmap = obj._mmap
self.filename = obj.filename
self.offset = obj.offset
self.mode = obj.mode
else:
self._mmap = None
+ self.filename = None
+ self.offset = None
+ self.mode = None
def flush(self):
"""
@@ -291,7 +294,7 @@ class memmap(ndarray):
# 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:
+ if self._mmap is not None and self._mmap is self.base:
try:
# First run tell() to see whether file is open
self._mmap.tell()
diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py
index 18b356ce2..3a063383d 100644
--- a/numpy/core/tests/test_memmap.py
+++ b/numpy/core/tests/test_memmap.py
@@ -87,5 +87,24 @@ class TestMemmap(TestCase):
finally:
memmap._close = _close
+ def test_arithmetic_drops_references(self):
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
+ shape=self.shape)
+ tmp = (fp + 10)
+ if isinstance(tmp, memmap):
+ assert tmp._mmap is not fp._mmap
+
+ def test_indexing_drops_references(self):
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
+ shape=self.shape)
+ tmp = fp[[(1, 2), (2, 3)]]
+ if isinstance(tmp, memmap):
+ assert tmp._mmap is not fp._mmap
+
+ def test_slicing_keeps_references(self):
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
+ shape=self.shape)
+ assert fp[:2, :2]._mmap is fp._mmap
+
if __name__ == "__main__":
run_module_suite()