Skip to content

Commit f1d2936

Browse files
committed
MNT: fix override
Signed-off-by: Iason Krommydas <[email protected]>
1 parent 8891f0e commit f1d2936

2 files changed

Lines changed: 94 additions & 14 deletions

File tree

‎numpy/_core/src/multiarray/item_selection.c‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,24 @@ PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2,
24112411
}
24122412

24132413

2414+
/*
2415+
* `__array_ufunc__ = None` opts out of ufuncs rather than implementing them,
2416+
* and searchsorted has always simply converted such an operand. Keep doing
2417+
* that, only an override that can be called is handed to the gufunc.
2418+
*/
2419+
static int
2420+
searchsorted_has_override(PyObject *obj)
2421+
{
2422+
PyObject *method = PyUFuncOverride_GetNonDefaultArrayUfunc(obj);
2423+
if (method == NULL) {
2424+
return 0;
2425+
}
2426+
int has_override = method != Py_None;
2427+
Py_DECREF(method);
2428+
return has_override;
2429+
}
2430+
2431+
24142432
/*
24152433
* Dispatch to the private searchsorted gufuncs, which search the last axis
24162434
* of `a`. As PyArray_SearchSorted, `a` and `v` are promoted to a common
@@ -2443,7 +2461,8 @@ searchsorted_gufunc(PyArrayObject *op1, PyObject *op2,
24432461
* raises TypeError, which the caller turns into the one dimensional
24442462
* implementation, as before.
24452463
*/
2446-
if (PyUFunc_HasOverride((PyObject *)op1) || PyUFunc_HasOverride(op2)) {
2464+
if (searchsorted_has_override((PyObject *)op1)
2465+
|| searchsorted_has_override(op2)) {
24472466
ap1 = (PyArrayObject *)Py_NewRef(op1);
24482467
ap2 = (PyArrayObject *)Py_NewRef(op2);
24492468
}
@@ -2488,6 +2507,41 @@ searchsorted_gufunc(PyArrayObject *op1, PyObject *op2,
24882507
}
24892508

24902509

2510+
/*
2511+
* Whether the pending exception is the TypeError the ufunc machinery raises
2512+
* once every `__array_ufunc__` has returned NotImplemented, rather than one
2513+
* an override raised itself. Only the former may fall back. The message is
2514+
* built by `array_ufunc_errmsg_formatter` in numpy._core._internal.
2515+
*/
2516+
static int
2517+
searchsorted_override_declined(void)
2518+
{
2519+
PyObject *exc = PyErr_GetRaisedException();
2520+
if (exc == NULL) {
2521+
return 0;
2522+
}
2523+
int declined = 0;
2524+
PyObject *msg = PyObject_Str(exc);
2525+
if (msg == NULL) {
2526+
PyErr_Clear();
2527+
}
2528+
else {
2529+
static const char prefix[] = "operand type(s) all returned "
2530+
"NotImplemented";
2531+
const char *text = PyUnicode_AsUTF8(msg);
2532+
if (text == NULL) {
2533+
PyErr_Clear();
2534+
}
2535+
else {
2536+
declined = strncmp(text, prefix, sizeof(prefix) - 1) == 0;
2537+
}
2538+
Py_DECREF(msg);
2539+
}
2540+
PyErr_SetRaisedException(exc);
2541+
return declined;
2542+
}
2543+
2544+
24912545
/*
24922546
* Internal version of PyArray_SearchSorted that accepts an `op1` of any
24932547
* dimensionality, searching each one dimensional slice along its last axis.
@@ -2504,25 +2558,18 @@ npy_searchsorted(PyArrayObject *op1, PyObject *op2,
25042558
/*
25052559
* Only a failure to dispatch may fall back to the generic
25062560
* implementation: dtypes with neither a loop nor a usable comparison,
2507-
* StringDType above all, fail with _UFuncNoLoopError, and an operand
2508-
* whose `__array_ufunc__` declined the call raises a plain TypeError.
2509-
* Any other TypeError (a bad `sorter`, a failing object comparison, ...)
2510-
* is a genuine error.
2561+
* StringDType above all, fail with _UFuncNoLoopError, and a call every
2562+
* `__array_ufunc__` declined raises a plain TypeError. Any other
2563+
* TypeError (a bad `sorter`, an override raising one of its own, a
2564+
* failing object comparison, ...) is a genuine error.
25112565
*/
25122566
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
25132567
return NULL;
25142568
}
25152569
int no_loop = PyErr_ExceptionMatches(
25162570
_npy_module_state->static_pydata._UFuncNoLoopError);
2517-
if (!no_loop) {
2518-
/* Stash the error: the attribute lookups must not run under it. */
2519-
PyObject *exc = PyErr_GetRaisedException();
2520-
int declined_override = PyUFunc_HasOverride((PyObject *)op1)
2521-
|| PyUFunc_HasOverride(op2);
2522-
PyErr_SetRaisedException(exc);
2523-
if (!declined_override) {
2524-
return NULL;
2525-
}
2571+
if (!no_loop && !searchsorted_override_declined()) {
2572+
return NULL;
25262573
}
25272574
if (no_loop && PyArray_NDIM(op1) > 1) {
25282575
PyErr_Clear();

‎numpy/_core/tests/test_multiarray.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,39 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
37283728
assert_equal(a.searchsorted(v.view(Declines)), [3, 8])
37293729
assert_equal(a.view(Declines).searchsorted(v), [3, 8])
37303730

3731+
def test_searchsorted_array_ufunc_override_raises(self):
3732+
# an error from an override is its own, not a decline, so it must not
3733+
# be swallowed by the fallback
3734+
class RaisesType(np.ndarray):
3735+
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
3736+
raise TypeError("boom")
3737+
3738+
class RaisesValue(np.ndarray):
3739+
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
3740+
raise ValueError("bang")
3741+
3742+
a = np.arange(10.)
3743+
v = np.array([2.5, 7.5])
3744+
with pytest.raises(TypeError, match="boom"):
3745+
a.searchsorted(v.view(RaisesType))
3746+
with pytest.raises(TypeError, match="boom"):
3747+
a.view(RaisesType).searchsorted(v)
3748+
with pytest.raises(ValueError, match="bang"):
3749+
a.searchsorted(v.view(RaisesValue))
3750+
with pytest.raises(ValueError, match="bang"):
3751+
a.view(RaisesValue).searchsorted(v)
3752+
3753+
def test_searchsorted_array_ufunc_none(self):
3754+
# `__array_ufunc__ = None` opts out of ufuncs rather than handling
3755+
# them, and searchsorted has always just converted such an operand
3756+
class OptsOut(np.ndarray):
3757+
__array_ufunc__ = None
3758+
3759+
a = np.arange(10.)
3760+
v = np.array([2.5, 7.5])
3761+
assert_equal(a.searchsorted(v.view(OptsOut)), [3, 8])
3762+
assert_equal(a.view(OptsOut).searchsorted(v), [3, 8])
3763+
37313764
def test_searchsorted_gufunc_descr_mismatch(self):
37323765
# the loops read both searched operands through `a`'s descriptor, so
37333766
# one dtype with two descriptors, differing in unit or itemsize, has

0 commit comments

Comments
 (0)