Skip to content

Commit 651bb25

Browse files
authored
Merge pull request numpy#16064 from keremh/fix-exception-raise
ENH: Fix exception causes in four .py files
2 parents f80584a + cadea53 commit 651bb25

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

numpy/lib/histograms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ def _get_bin_edges(a, bins, range, weights):
417417
elif np.ndim(bins) == 0:
418418
try:
419419
n_equal_bins = operator.index(bins)
420-
except TypeError:
420+
except TypeError as e:
421421
raise TypeError(
422-
'`bins` must be an integer, a string, or an array')
422+
'`bins` must be an integer, a string, or an array') from e
423423
if n_equal_bins < 1:
424424
raise ValueError('`bins` must be positive, when an integer')
425425

numpy/lib/index_tricks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,10 @@ def __getitem__(self, key):
368368
if len(vec) == 3:
369369
trans1d = int(vec[2])
370370
continue
371-
except Exception:
372-
raise ValueError("unknown special directive")
371+
except Exception as e:
372+
raise ValueError(
373+
"unknown special directive {!r}".format(item)
374+
) from e
373375
try:
374376
axis = int(item)
375377
continue

numpy/lib/shape_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs):
372372
# invoke the function on the first item
373373
try:
374374
ind0 = next(inds)
375-
except StopIteration:
376-
raise ValueError('Cannot apply_along_axis when any iteration dimensions are 0')
375+
except StopIteration as e:
376+
raise ValueError(
377+
'Cannot apply_along_axis when any iteration dimensions are 0'
378+
) from None
377379
res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
378380

379381
# build a buffer for storing evaluations of func1d.

numpy/lib/ufunclike.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ def isposinf(x, out=None):
188188
is_inf = nx.isinf(x)
189189
try:
190190
signbit = ~nx.signbit(x)
191-
except TypeError:
191+
except TypeError as e:
192192
raise TypeError('This operation is not supported for complex values '
193-
'because it would be ambiguous.')
193+
'because it would be ambiguous.') from e
194194
else:
195195
return nx.logical_and(is_inf, signbit, out)
196196

@@ -259,8 +259,8 @@ def isneginf(x, out=None):
259259
is_inf = nx.isinf(x)
260260
try:
261261
signbit = nx.signbit(x)
262-
except TypeError:
262+
except TypeError as e:
263263
raise TypeError('This operation is not supported for complex values '
264-
'because it would be ambiguous.')
264+
'because it would be ambiguous.') from e
265265
else:
266266
return nx.logical_and(is_inf, signbit, out)

0 commit comments

Comments
 (0)