diff options
author | John Zwinck <jzwinck@gmail.com> | 2020-07-21 15:50:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-21 08:50:05 +0100 |
commit | ef5656d842459a6c338ee03ee422d7bd4b8beb61 (patch) | |
tree | 70631f7198e7ae99b508fd948b4278afff16849b /numpy/lib/npyio.py | |
parent | 2fb2c250bdfadc3a92e635e9030f9aea34c9c14e (diff) | |
download | numpy-ef5656d842459a6c338ee03ee422d7bd4b8beb61.tar.gz |
MAINT: Avoid exception in NpzFile destructor if constructor raises BadZipFile (#15604)
Previously if you gave an invalid zip file to NpzFile, zipfile_factory would raise BadZipFile and NpzFile.__exit__ would be called, which accessed members which had not yet been set, leading to a confusing second exception like this:
zipfile.BadZipFile: File is not a zip file
Exception ignored in: <function NpzFile.__del__ at 0x9b8ef0>
Traceback (most recent call last):
File "numpy/lib/npyio.py", line 230, in __del__
self.close()
File "numpy/lib/npyio.py", line 221, in close
if self.zip is not None:
AttributeError: 'NpzFile' object has no attribute 'zip'
This change makes __exit__ safe even when __init__ did not complete.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 520e9c9ec..58affc2fc 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -178,6 +178,9 @@ class NpzFile(Mapping): array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ + # Make __exit__ safe if zipfile_factory raises an exception + zip = None + fid = None def __init__(self, fid, own_fid=False, allow_pickle=False, pickle_kwargs=None): @@ -197,8 +200,6 @@ class NpzFile(Mapping): self.f = BagObj(self) if own_fid: self.fid = fid - else: - self.fid = None def __enter__(self): return self |