summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-04-11 21:00:27 -0700
committerEric Wieser <wieser.eric@gmail.com>2019-04-11 21:00:27 -0700
commitb60f33dd0747574570764ac8072386dccc85dd44 (patch)
tree9fcb07d5d08c770c38c08593ca51629c61d6210e
parentd7a73f8c700edcf150d59a570e0173b60f84c7a7 (diff)
downloadnumpy-b60f33dd0747574570764ac8072386dccc85dd44.tar.gz
BUG: Fix bad error message in np.memmap
This previously raisd `OSError: [Errno 22] Invalid argument` while trying to seek to byte -1 of the file. It now raises `ValueError: cannot mmap an empty file` The simple fix is not to write the file at all if we know it's already long enough. In future we could consider allowing memmap to do an extra write behind the scenes to ensure the fiel is not empty, but that seems out of scope. Fixes gh-12653
-rw-r--r--numpy/core/memmap.py2
-rw-r--r--numpy/core/tests/test_memmap.py10
2 files changed, 11 insertions, 1 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index 9ba4817f4..062645551 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -246,7 +246,7 @@ class memmap(ndarray):
bytes = long(offset + size*_dbytes)
- if mode == 'w+' or (mode == 'r+' and flen < bytes):
+ if mode in ('w+', 'r+') and flen < bytes:
fid.seek(bytes - 1, 0)
fid.write(b'\0')
fid.flush()
diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py
index 990d0ae26..d2ae564b2 100644
--- a/numpy/core/tests/test_memmap.py
+++ b/numpy/core/tests/test_memmap.py
@@ -204,3 +204,13 @@ class TestMemmap(object):
self.tmpfp.write(b'a'*16)
mm = memmap(self.tmpfp, dtype='float64')
assert_equal(mm.shape, (2,))
+
+ def test_empty_array(self):
+ # gh-12653
+ with pytest.raises(ValueError, match='empty file'):
+ memmap(self.tmpfp, shape=(0,4), mode='w+')
+
+ self.tmpfp.write(b'\0')
+
+ # ok now the file is not empty
+ memmap(self.tmpfp, shape=(0,4), mode='w+')