diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/memmap.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 10 |
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+') |