8000 Merge pull request #11522 from jzwinck/fix-load-empty-npz · convexset/numpy@e3c2843 · GitHub
[go: up one dir, main page]

Skip to content

Commit e3c2843

Browse files
authored
Merge pull request numpy#11522 from jzwinck/fix-load-empty-npz
BUG: fix np.load() of empty .npz file
2 parents 74fdbc7 + 4c74e38 commit e3c2843

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

numpy/lib/npyio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,13 @@ def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
412412
try:
413413
# Code to distinguish from NumPy binary files and pickles.
414414
_ZIP_PREFIX = b'PK\x03\x04'
415+
_ZIP_SUFFIX = b'PK\x05\x06' # empty zip files start with this
415416
N = len(format.MAGIC_PREFIX)
416417
magic = fid.read(N)
417418
# If the file size is less than N, we need to make sure not
418419
# to seek past the beginning of the file
419420
fid.seek(-min(N, len(magic)), 1) # back-up
420-
if magic.startswith(_ZIP_PREFIX):
421+
if magic.startswith(_ZIP_PREFIX) or magic.startswith(_ZIP_SUFFIX):
421422
# zip-file (assume .npz)
422423
# Transfer file ownership to NpzFile
423424
tmp = own_fid

numpy/lib/tests/test_format.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,10 @@ def test_large_archive():
852852
new_a = np.load(f)["arr"]
853853

854854
assert_(a.shape == new_a.shape)
855+
856+
857+
def test_empty_npz():
858+
# Test for gh-9989
859+
fname = os.path.join(tempdir, "nothing.npz")
860+
np.savez(fname)
861+
np.load(fname)

0 commit comments

Comments
 (0)
0