summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan van der Walt <stefan@sun.ac.za>2008-03-11 02:30:12 +0000
committerStefan van der Walt <stefan@sun.ac.za>2008-03-11 02:30:12 +0000
commitc851443f9b2d40becd4533227c48feb650d37917 (patch)
tree96eeddacb60c538132d452fbf35ec2a4cd05d150
parenta804623fdc8c809616dc5c1d5852fa85ed58eb98 (diff)
downloadnumpy-c851443f9b2d40becd4533227c48feb650d37917.tar.gz
Allow file-like object to be used to create a memmap.
Fix NamedTemporaryFile problem for Windows platforms.
-rw-r--r--numpy/core/memmap.py15
-rw-r--r--numpy/core/tests/test_memmap.py21
2 files changed, 25 insertions, 11 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index 3a53f9d16..93950c5c0 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -25,8 +25,9 @@ class memmap(ndarray):
Parameters
----------
- name : filename
- The file name to be used as the array data buffer.
+ filename : string or file-like object
+ The file name or file object to be used as the array data
+ buffer.
dtype : data-type, optional
The data-type used to interpret the file contents.
Default is uint8
@@ -138,7 +139,7 @@ class memmap(ndarray):
"""
__array_priority__ = -100.0
- def __new__(subtype, name, dtype=uint8, mode='r+', offset=0,
+ def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
shape=None, order='C'):
try:
mode = mode_equivalents[mode]
@@ -147,7 +148,10 @@ class memmap(ndarray):
raise ValueError("mode must be one of %s" % \
(valid_filemodes + mode_equivalents.keys()))
- fid = file(name, (mode == 'c' and 'r' or mode)+'b')
+ if hasattr(filename,'read'):
+ fid = filename
+ else:
+ fid = file(filename, (mode == 'c' and 'r' or mode)+'b')
if (mode == 'w+') and shape is None:
raise ValueError, "shape must be given"
@@ -194,8 +198,7 @@ class memmap(ndarray):
self._offset = offset
self._mode = mode
self._size = size
- self._name = name
- fid.close()
+ self._name = filename
return self
def __array_finalize__(self, obj):
diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py
index 6ced034cb..ac597f45c 100644
--- a/numpy/core/tests/test_memmap.py
+++ b/numpy/core/tests/test_memmap.py
@@ -14,18 +14,29 @@ class TestMemmap(NumpyTestCase):
self.data = arange(12, dtype=self.dtype)
self.data.resize(self.shape)
- def test_RoundTrip(self):
- fp = memmap(self.tmpfp.name, dtype=self.dtype, mode='w+',
+ def test_roundtrip(self):
+ # Write data to file
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
fp[:] = self.data[:]
- del fp
- newfp = memmap(self.tmpfp.name, dtype=self.dtype, mode='r',
+ del fp # Test __del__ machinery, which handles cleanup
+
+ # Read data back from file
+ newfp = memmap(self.tmpfp, dtype=self.dtype, mode='r',
shape=self.shape)
assert allclose(self.data, newfp)
assert_array_equal(self.data, newfp)
+ def test_open_with_filename(self):
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
+ shape=self.shape)
+ fp[:] = self.data[:]
+
+ memmap(self.tmpfp.name, dtype=self.dtype, mode='r',
+ shape=self.shape)
+
def test_flush(self):
- fp = memmap(self.tmpfp.name, dtype=self.dtype, mode='w+',
+ fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
fp[:] = self.data[:]
fp.flush()