8000 MAINT: refactor _savez by kain88-de · Pull Request #9510 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: refactor _savez #9510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
MAINT: refactor _savez
use context manager to open/close file handle.
  • Loading branch information
kain88-de committed Jul 28, 2018
commit 0c7e0412cdd63a2099dc1df51707496e719ce9b6
21 changes: 8 additions & 13 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,19 +717,14 @@ def _savez(file, args, kwds, compress, allow_pickle=True, pickle_kwargs=None):
try:
for key, val in namedict.items():
fname = key + '.npy'
fid = open(tmpfile, 'wb')
try:
format.write_array(fid, np.asanyarray(val),
allow_pickle=allow_pickle,
pickle_kwargs=pickle_kwargs)
fid.close()
fid = None
zipf.write(tmpfile, arcname=fname)
except IOError as exc:
raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
finally:
if fid:
fid.close()
with open(tmpfile, 'wb') as fid:
try:
format.write_array(fid, np.asanyarray(val),
allow_pickle=allow_pickle,
pickle_kwargs=pickle_kwargs)
except IOError as exc:
raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any policy on string interpolation while I'm cleaning up this part?

zipf.write(tmpfile, arcname=fname)
finally:
os.remove(tmpfile)

Expand Down
0