Skip to content

Commit

Permalink
Fix a SystemError if unboxing fails in a ufunc
Browse files Browse the repository at this point in the history
Previously the conversion function could be called while an error was in flight, which isn't legal.
  • Loading branch information
eric-wieser committed Mar 25, 2020
1 parent afd5c67 commit e9e9982
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions numba/np/ufunc/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def _build_ufunc_loop_body_objmode(load, store, context, func, builder,
# Release owned reference to arguments
for elem in elems:
pyapi.decref(elem)
# NOTE: if an error occurred, it will be caught by the Numpy machinery
# NOTE: if an error occurred, it will be caught by the Numpy machinery

# Store
store(retval)
# Store. This may error too when unboxing.
store(retval)

# increment indices
for off, ary in zip(offsets, arrays):
Expand Down
20 changes: 20 additions & 0 deletions numba/tests/npyufunc/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def power(a, b):
return a ** b


class BadType:
def __int__(self):
raise TypeError()


def bad_type_if_odd(a):
if a % 2 == 0:
return a
return BadType()


class TestExceptions(TestCase):
"""
Test raising exceptions inside ufuncs.
Expand All @@ -55,6 +66,15 @@ def test_ufunc_raise(self):
def test_ufunc_raise_objmode(self):
self.check_ufunc_raise(forceobj=True)

def test_ufunc_conversion_raise_objmode(self):
f = vectorize(['int64(int64)'], forceobj=True)(bad_type_if_odd)
arr = np.array([10, 11, 12, 13], dtype=np.int64)
out = np.zeros_like(arr)
with self.assertRaises(TypeError) as cm:
f(arr, out)
# All values were computed except for the ones giving an error
self.assertEqual(list(out), [10, 0, 12, 0])

def check_gufunc_raise(self, **vectorize_args):
f = guvectorize(['int32[:], int32[:], int32[:]'], '(n),()->(n)',
**vectorize_args)(gufunc_foo)
Expand Down

0 comments on commit e9e9982

Please sign in to comment.