8000 Merge pull request #7133 from yarikoptic/bf-savez · numpy/numpy@5531371 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5531371

Browse files
committed
Merge pull request #7133 from yarikoptic/bf-savez
savez: temporary file alongside with target file and improve exception message upon IOError
2 parents c4bb7e9 + dc61b5e commit 5531371

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

numpy/lib/npyio.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,11 @@ def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
< 8000 /div>
627627
zipf = zipfile_factory(file, mode="w", compression=compression)
628628

629629
# Stage arrays in a temporary file on disk, before writing to zip.
630-
fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy')
630+
631+
# Since target file might be big enough to exceed capacity of a global
632+
# temporary directory, create temp file side-by-side with the target file.
633+
file_dir, file_prefix = os.path.split(file) if _is_string_like(file) else (None, 'tmp')
634+
fd, tmpfile = tempfile.mkstemp(prefix=file_prefix, dir=file_dir, suffix='-numpy.npy')
631635
os.close(fd)
632636
try:
633637
for key, val in namedict.items():
@@ -640,6 +644,8 @@ def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
640644
fid.close()
641645
fid = None
642646
zipf.write(tmpfile, arcname=fname)
647+
except IOError as exc:
648+
raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
643649
finally:
644650
if fid:
645651
fid.close()

0 commit comments

Comments
 (0)
0