summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorJulian Taylor <juliantaylor108@gmail.com>2018-07-29 17:58:37 +0200
committerGitHub <noreply@github.com>2018-07-29 17:58:37 +0200
commite3c28430efb193a5ec1afe8e1ac6e1a28881185b (patch)
tree6c6e341fed3a66f0875999b0d212f63325063c9d /numpy
parent74fdbc728294700b6d495d4dd95a2c281a4420f4 (diff)
parent4c74e384a7ec961d171b7d6a0fbf20b2bc831c28 (diff)
downloadnumpy-e3c28430efb193a5ec1afe8e1ac6e1a28881185b.tar.gz
Merge pull request #11522 from jzwinck/fix-load-empty-npz
BUG: fix np.load() of empty .npz file
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/npyio.py3
-rw-r--r--numpy/lib/tests/test_format.py7
2 files changed, 9 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 7788ac319..d8cfbf769 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -412,12 +412,13 @@ def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
try:
# Code to distinguish from NumPy binary files and pickles.
_ZIP_PREFIX = b'PK\x03\x04'
+ _ZIP_SUFFIX = b'PK\x05\x06' # empty zip files start with this
N = len(format.MAGIC_PREFIX)
magic = fid.read(N)
# If the file size is less than N, we need to make sure not
# to seek past the beginning of the file
fid.seek(-min(N, len(magic)), 1) # back-up
- if magic.startswith(_ZIP_PREFIX):
+ if magic.startswith(_ZIP_PREFIX) or magic.startswith(_ZIP_SUFFIX):
# zip-file (assume .npz)
# Transfer file ownership to NpzFile
tmp = own_fid
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
index fd227595a..c7869c582 100644
--- a/numpy/lib/tests/test_format.py
+++ b/numpy/lib/tests/test_format.py
@@ -852,3 +852,10 @@ def test_large_archive():
new_a = np.load(f)["arr"]
assert_(a.shape == new_a.shape)
+
+
+def test_empty_npz():
+ # Test for gh-9989
+ fname = os.path.join(tempdir, "nothing.npz")
+ np.savez(fname)
+ np.load(fname)