Skip to content

Commit

Permalink
Merge pull request #24031 from seberg/array-ufunc-no-kwargs
Browse files Browse the repository at this point in the history
BUG: Ensure __array_ufunc__ works without any kwargs passed
  • Loading branch information
seberg committed Jun 24, 2023
2 parents dd231b0 + 0538f9e commit 0e2af84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions numpy/core/src/multiarray/methods.c
Expand Up @@ -1135,6 +1135,9 @@ any_array_ufunc_overrides(PyObject *args, PyObject *kwds)
}
}
Py_DECREF(fast);
if (kwds == NULL) {
return 0;
}
/* check outputs, if any */
nout = PyUFuncOverride_GetOutObjects(kwds, &out_kwd_obj, &out_objs);
if (nout < 0) {
Expand Down
13 changes: 13 additions & 0 deletions numpy/core/tests/test_umath.py
Expand Up @@ -3916,6 +3916,19 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
assert_equal(a, check)
assert_(a.info, {'inputs': [0, 2]})

def test_array_ufunc_direct_call(self):
# This is mainly a regression test for gh-24023 (shouldn't segfault)
a = np.array(1)
with pytest.raises(TypeError):
a.__array_ufunc__()

# No kwargs means kwargs may be NULL on the C-level
with pytest.raises(TypeError):
a.__array_ufunc__(1, 2)

# And the same with a valid call:
res = a.__array_ufunc__(np.add, "__call__", a, a)
assert_array_equal(res, a + a)

class TestChoose:
def test_mixed(self):
Expand Down

0 comments on commit 0e2af84

Please sign in to comment.