summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuhair Ali-Khan <zaak7179@gmail.com>2020-06-04 00:28:58 -0500
committerGitHub <noreply@github.com>2020-06-04 08:28:58 +0300
commit489de42a9585615ca9962a83882896069357ab97 (patch)
treeb9bbdd256032d1f0e95a70dac89c88f08d518d9d
parent9ec27a3f5b81e6a7e808836410c9b6a73204c27e (diff)
downloadnumpy-489de42a9585615ca9962a83882896069357ab97.tar.gz
MAINT: Chain some exceptions. (#16418)
* ENH: Chain extensions in numpy and numpy/core Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> Co-authored-by: Zuhair Ali-Khan <54608785+zalikh2@users.noreply.github.com>
-rw-r--r--numpy/core/_dtype.py4
-rw-r--r--numpy/core/fromnumeric.py4
-rw-r--r--numpy/core/records.py10
-rw-r--r--numpy/ctypeslib.py8
4 files changed, 14 insertions, 12 deletions
diff --git a/numpy/core/_dtype.py b/numpy/core/_dtype.py
index 6b0ec5903..76d0b8149 100644
--- a/numpy/core/_dtype.py
+++ b/numpy/core/_dtype.py
@@ -24,11 +24,11 @@ _kind_to_stem = {
def _kind_name(dtype):
try:
return _kind_to_stem[dtype.kind]
- except KeyError:
+ except KeyError as e:
raise RuntimeError(
"internal dtype error, unknown kind {!r}"
.format(dtype.kind)
- )
+ ) from None
def __str__(dtype):
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index 0c63bcf73..2b88ccedf 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -536,9 +536,9 @@ def put(a, ind, v, mode='raise'):
"""
try:
put = a.put
- except AttributeError:
+ except AttributeError as e:
raise TypeError("argument 1 must be numpy.ndarray, "
- "not {name}".format(name=type(a).__name__))
+ "not {name}".format(name=type(a).__name__)) from e
return put(ind, v, mode=mode)
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 7e1c0d591..464615450 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -461,8 +461,8 @@ class recarray(ndarray):
fielddict = ndarray.__getattribute__(self, 'dtype').fields
try:
res = fielddict[attr][:2]
- except (TypeError, KeyError):
- raise AttributeError("recarray has no attribute %s" % attr)
+ except (TypeError, KeyError) as e:
+ raise AttributeError("recarray has no attribute %s" % attr) from e
obj = self.getfield(*res)
# At this point obj will always be a recarray, since (see
@@ -509,8 +509,10 @@ class recarray(ndarray):
return ret
try:
res = fielddict[attr][:2]
- except (TypeError, KeyError):
- raise AttributeError("record array has no attribute %s" % attr)
+ except (TypeError, KeyError) as e:
+ raise AttributeError(
+ "record array has no attribute %s" % attr
+ ) from e
return self.setfield(val, *res)
def __getitem__(self, indx):
diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py
index ec3cdc33d..76ba838b7 100644
--- a/numpy/ctypeslib.py
+++ b/numpy/ctypeslib.py
@@ -297,8 +297,8 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
if num is None:
try:
flags = [x.strip().upper() for x in flags]
- except Exception:
- raise TypeError("invalid flags specification")
+ except Exception as e:
+ raise TypeError("invalid flags specification") from e
num = _num_fromflags(flags)
# normalize shape to an Optional[tuple]
@@ -377,10 +377,10 @@ if ctypes is not None:
dtype_native = dtype.newbyteorder('=')
try:
ctype = _scalar_type_map[dtype_native]
- except KeyError:
+ except KeyError as e:
raise NotImplementedError(
"Converting {!r} to a ctypes type".format(dtype)
- )
+ ) from None
if dtype_with_endian.byteorder == '>':
ctype = ctype.__ctype_be__