From 6e5c0c8c97a5693ec48b03b20bfa7be88e60f3e7 Mon Sep 17 00:00:00 2001 From: keremh Date: Thu, 30 Apr 2020 18:18:10 +0300 Subject: [PATCH 1/4] MAINT: Chain exceptions in several places --- numpy/core/tests/test_indexing.py | 6 ++++-- numpy/lib/npyio.py | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index f6e263774ae6..d3e13ca8f86a 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -995,10 +995,12 @@ def _get_multi_index(self, arr, indices): try: mi = np.ravel_multi_index(indx[1:], orig_slice, mode='raise') - except Exception: + except Exception as e: # This happens with 0-sized orig_slice (sometimes?) # here it is a ValueError, but indexing gives a: - raise IndexError('invalid index into 0-sized') + raise IndexError( + 'invalid index into 0-sized' + ) from e else: mi = np.ravel_multi_index(indx[1:], orig_slice, mode='wrap') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 6d6222d3eb95..3d7d3e08f9fc 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1083,8 +1083,10 @@ def read_data(chunk_size): 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 e # input may be a python2 io stream if encoding is not None: @@ -1768,10 +1770,10 @@ 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 e with fid_ctx: split_line = LineSplitter(delimiter=delimiter, comments=comments, From b6f74c91ab655a6af5b73d04f0ed1b32b9727ab9 Mon Sep 17 00:00:00 2001 From: keremh Date: Thu, 30 Apr 2020 18:18:10 +0300 Subject: [PATCH 2/4] MAINT: Chain exceptions in several places --- numpy/lib/tests/test_regression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 55df2a6752c2..f545bb3f4b09 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -167,8 +167,8 @@ def test_who_with_0dim_array(self): try: try: np.who({'foo': np.array(1)}) - except Exception: - raise AssertionError("ticket #1243") + except Exception as e: + raise AssertionError("ticket #1243") from e finally: sys.stdout.close() sys.stdout = oldstdout From 750d4014db543a2c6d17fd631febd8aaa92a65ba Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Mon, 29 Jun 2020 16:05:49 -0700 Subject: [PATCH 3/4] MAINT: Undo exception chaining in tests --- numpy/core/tests/test_indexing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index d3e13ca8f86a..f6e263774ae6 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -995,12 +995,10 @@ def _get_multi_index(self, arr, indices): try: mi = np.ravel_multi_index(indx[1:], orig_slice, mode='raise') - except Exception as e: + except Exception: # This happens with 0-sized orig_slice (sometimes?) # here it is a ValueError, but indexing gives a: - raise IndexError( - 'invalid index into 0-sized' - ) from e + raise IndexError('invalid index into 0-sized') else: mi = np.ravel_multi_index(indx[1:], orig_slice, mode='wrap') From f9a1d03bb8de4e8b6d1440cdcfb10b8dfe05fd58 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Mon, 29 Jun 2020 16:34:12 -0700 Subject: [PATCH 4/4] Revert change to test. Original reversion lost in rebase. --- numpy/lib/tests/test_regression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index f545bb3f4b09..55df2a6752c2 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -167,8 +167,8 @@ def test_who_with_0dim_array(self): try: try: np.who({'foo': np.array(1)}) - except Exception as e: - raise AssertionError("ticket #1243") from e + except Exception: + raise AssertionError("ticket #1243") finally: sys.stdout.close() sys.stdout = oldstdout