@@ -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 ();
0 commit comments