8000 MAINT: Chain exceptions in npyio.py by smitgajjar · Pull Request #16218 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Chain exceptions in npyio.py #16218

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 1 commit into from
Closed
Changes from all commits
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
25 changes: 15 additions & 10 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,10 @@ def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True,
"when allow_pickle=False")
try:
return pickle.load(fid, **pickle_kwargs)
except Exception:
except Exception as e:
raise IOError(
"Failed to interpret file %s as a pickle" % repr(file))
"Failed to interpret file %s as a pickle" % repr(file)
) from None


def _save_dispatcher(file, arr, allow_pickle=None, fix_imports=None):
Expand Down Expand Up @@ -965,8 +966,10 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
else:
fh = iter(fname)
fencoding = getattr(fname, 'encoding', 'latin1')
except TypeError:
raise ValueError('fname must be a string, file handle, or generator')
except TypeError as e:
raise ValueError(
'fname must be a string, file handle, or generator'
) from None

# input may be a python2 io stream
if encoding is not None:
Expand Down Expand Up @@ -1422,10 +1425,11 @@ def first_write(self, v):
for row in X:
try:
v = format % tuple(row) + newline
except TypeError:
except TypeError as e:
raise TypeError("Mismatch between array dtype ('%s') and "
"format specifier ('%s')"
% (str(X.dtype), format))
% (str(X.dtype), format)
) from None
fh.write(v)

if len(footer) > 0:
Expand Down Expand Up @@ -1752,10 +1756,11 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
fid = fname
fid_ctx = contextlib_nullcontext(fid)
fhd = iter(fid)
except TypeError:
except TypeError as e:
raise TypeError(
"fname must be a string, filehandle, list of strings, "
"or generator. Got %s instead." % type(fname))
"or generator. Got %s instead." % type(fname)
) from None

with fid_ctx:
split_line = LineSplitter(delimiter=delimiter, comments=comments,
Expand Down Expand Up @@ -2049,10 +2054,10 @@ def tobytes_first(x, conv):
for (j, value) in enumerate(current_column):
try:
converter.upgrade(value)
except (ConverterError, ValueError):
except (ConverterError, ValueError) as e:
errmsg += "(occurred line #%i for value '%s')"
errmsg %= (j + 1 + skip_header, value)
raise ConverterError(errmsg)
raise ConverterError(errmsg) from None

# Check that we don't have invalid values
nbinvalid = len(invalid)
Expand Down
0