From ae6ba5d215451273990f2826f2cf28f091d71446 Mon Sep 17 00:00:00 2001 From: Chris Holland <41524756+ChrisAHolland@users.noreply.github.com> Date: Fri, 1 May 2020 05:01:45 -0700 Subject: MAINT: Chain exceptions in memmap.py and core.py (#16067) * Improve chained exception reporting Co-authored-by: Eric Wieser --- numpy/core/memmap.py | 8 +++++--- numpy/ma/core.py | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index ad66446c2..cb025736e 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -209,10 +209,12 @@ class memmap(ndarray): import os.path try: mode = mode_equivalents[mode] - except KeyError: + except KeyError as e: if mode not in valid_filemodes: - raise ValueError("mode must be one of %s" % - (valid_filemodes + list(mode_equivalents.keys()))) + raise ValueError( + "mode must be one of {!r} (got {!r})" + .format(valid_filemodes + list(mode_equivalents.keys()), mode) + ) from None if mode == 'w+' and shape is None: raise ValueError("shape must be given") diff --git a/numpy/ma/core.py b/numpy/ma/core.py index a5e59bb74..a7214f9bf 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -285,8 +285,10 @@ def _extremum_fill_value(obj, extremum, extremum_name): def _scalar_fill_value(dtype): try: return extremum[dtype] - except KeyError: - raise TypeError(f"Unsuitable type {dtype} for calculating {extremum_name}.") + except KeyError as e: + raise TypeError( + f"Unsuitable type {dtype} for calculating {extremum_name}." + ) from None dtype = _get_dtype_of(obj) return _recursive_fill_value(dtype, _scalar_fill_value) -- cgit v1.2.1