diff options
author | Gary Gurlaskie <garygurlaskie@gmail.com> | 2019-07-17 14:54:28 -0700 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2019-07-17 14:54:28 -0700 |
commit | ea965e4cd328fbcf76b03ff749ef06d2aa38c28b (patch) | |
tree | b48c9fd25c0b056ead26bde63214fb3b975c030d /numpy/core/tests | |
parent | 80ebdba587924b1b5704a68deb479e9f079e666b (diff) | |
download | numpy-ea965e4cd328fbcf76b03ff749ef06d2aa38c28b.tar.gz |
BUG: Fix incorrect GIL release in array.nonzero (#13930)
Added checks to array.nonzero to ensure GIL is not released if
it is needed.
Closes gh-13753
Diffstat (limited to 'numpy/core/tests')
-rw-r--r-- | numpy/core/tests/test_numeric.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 935b84234..3e85054b7 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1254,6 +1254,39 @@ class TestNonzero(object): a = np.array([[False], [TrueThenFalse()]]) assert_raises(RuntimeError, np.nonzero, a) + def test_nonzero_exception_safe(self): + # gh-13930 + + class ThrowsAfter: + def __init__(self, iters): + self.iters_left = iters + + def __bool__(self): + if self.iters_left == 0: + raise ValueError("called `iters` times") + + self.iters_left -= 1 + return True + + """ + Test that a ValueError is raised instead of a SystemError + + If the __bool__ function is called after the error state is set, + Python (cpython) will raise a SystemError. + """ + + # assert that an exception in first pass is handled correctly + a = np.array([ThrowsAfter(5)]*10) + assert_raises(ValueError, np.nonzero, a) + + # raise exception in second pass for 1-dimensional loop + a = np.array([ThrowsAfter(15)]*10) + assert_raises(ValueError, np.nonzero, a) + + # raise exception in second pass for n-dimensional loop + a = np.array([[ThrowsAfter(15)]]*10) + assert_raises(ValueError, np.nonzero, a) + class TestIndex(object): def test_boolean(self): |