Skip to content

Commit 6dfdc37

Browse files
authored
Merge pull request #29412 from charris/backport-29313
BUG: Allow reading non-npy files in npz and add test
2 parents 20d973c + 92a6d59 commit 6dfdc37

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

numpy/lib/_npyio_impl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def __getitem__(self, key):
261261
max_header_size=self.max_header_size
262262
)
263263
else:
264-
return bytes.read(key)
264+
return bytes.read()
265265

266266
def __contains__(self, key):
267267
return (key in self._files)

numpy/lib/tests/test_io.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import threading
88
import time
99
import warnings
10+
import zipfile
1011
from ctypes import c_bool
1112
from datetime import datetime
1213
from io import BytesIO, StringIO
@@ -217,6 +218,22 @@ def roundtrip(self, *args, **kwargs):
217218
self.arr_reloaded.fid.close()
218219
os.remove(self.arr_reloaded.fid.name)
219220

221+
def test_load_non_npy(self):
222+
"""Test loading non-.npy files and name mapping in .npz."""
223+
with temppath(prefix="numpy_test_npz_load_non_npy_", suffix=".npz") as tmp:
224+
with zipfile.ZipFile(tmp, "w") as npz:
225+
with npz.open("test1.npy", "w") as out_file:
226+
np.save(out_file, np.arange(10))
227+
with npz.open("test2", "w") as out_file:
228+
np.save(out_file, np.arange(10))
229+
with npz.open("metadata", "w") as out_file:
230+
out_file.write(b"Name: Test")
231+
with np.load(tmp) as npz:
232+
assert len(npz["test1"]) == 10
233+
assert len(npz["test1.npy"]) == 10
234+
assert len(npz["test2"]) == 10
235+
assert npz["metadata"] == b"Name: Test"
236+
220237
@pytest.mark.skipif(IS_PYPY, reason="Hangs on PyPy")
221238
@pytest.mark.skipif(not IS_64BIT, reason="Needs 64bit platform")
222239
@pytest.mark.slow

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy