From a952b00983c8cf113572554105648a3978e951de Mon Sep 17 00:00:00 2001 From: Zuhair Ali-Khan <54608785+zalikh2@users.noreply.github.com> Date: Thu, 28 May 2020 12:55:14 -0500 Subject: [PATCH 1/3] ENH: Chain extensions in numpy and numpy/core --- numpy/core/_dtype.py | 4 ++-- numpy/core/fromnumeric.py | 4 ++-- numpy/core/records.py | 8 ++++---- numpy/ctypeslib.py | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/numpy/core/_dtype.py b/numpy/core/_dtype.py index 6b0ec59039a8..e4d92bda2c96 100644 --- a/numpy/core/_dtype.py +++ b/numpy/core/_dtype.py @@ -24,11 +24,11 @@ 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 e def __str__(dtype): diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 0c63bcf7363b..2b88ccedfe65 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 7e1c0d591020..293d56599279 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -461,8 +461,8 @@ def __getattribute__(self, attr): 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,8 @@ def __setattr__(self, attr, val): 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 ec3cdc33d5c1..94dc0553fa37 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 @@ def _ctype_from_dtype_scalar(dtype): 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 e if dtype_with_endian.byteorder == '>': ctype = ctype.__ctype_be__ From eb371c1a127b09bb7e60865a4764850b40584186 Mon Sep 17 00:00:00 2001 From: Zuhair Ali-Khan Date: Fri, 29 May 2020 15:20:49 -0500 Subject: [PATCH 2/3] MAINT: Implement feedback to change extension chaining --- numpy/core/_dtype.py | 2 +- numpy/ctypeslib.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/core/_dtype.py b/numpy/core/_dtype.py index e4d92bda2c96..76d0b814968a 100644 --- a/numpy/core/_dtype.py +++ b/numpy/core/_dtype.py @@ -28,7 +28,7 @@ def _kind_name(dtype): raise RuntimeError( "internal dtype error, unknown kind {!r}" .format(dtype.kind) - ) from e + ) from None def __str__(dtype): diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 94dc0553fa37..76ba838b7a5a 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -380,7 +380,7 @@ def _ctype_from_dtype_scalar(dtype): except KeyError as e: raise NotImplementedError( "Converting {!r} to a ctypes type".format(dtype) - ) from e + ) from None if dtype_with_endian.byteorder == '>': ctype = ctype.__ctype_be__ From a4f28efbb2fa4cdde9b36de97226ee61df81d7e0 Mon Sep 17 00:00:00 2001 From: Zuhair Ali-Khan Date: Fri, 29 May 2020 16:41:01 -0500 Subject: [PATCH 3/3] MAINT: Update numpy/core/records.py Co-authored-by: Ross Barnowski --- numpy/core/records.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/numpy/core/records.py b/numpy/core/records.py index 293d56599279..464615450605 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -510,7 +510,9 @@ def __setattr__(self, attr, val): try: res = fielddict[attr][:2] except (TypeError, KeyError) as e: - raise AttributeError("record array has no attribute %s" % attr) from e + raise AttributeError( + "record array has no attribute %s" % attr + ) from e return self.setfield(val, *res) def __getitem__(self, indx):