Skip to content

Commit de9e24e

Browse files
committed
MNT: review comments
Signed-off-by: Iason Krommydas <[email protected]>
1 parent 0a59156 commit de9e24e

4 files changed

Lines changed: 61 additions & 239 deletions

File tree

‎doc/release/upcoming_changes/32346.new_feature.rst‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ and any leading axes broadcast against the leading axes of ``a``, so one set
77
of keys can be searched in every slice. ``sorter`` may now have the same
88
shape as ``a``. To search along another axis, bring it to the end first, for
99
instance with `numpy.moveaxis`. This feature is available for all built-in
10-
dtypes except ``StringDType``, which remains one-dimensional. The search now
11-
runs as a generalized ufunc, so an argument implementing ``__array_ufunc__``
12-
is consulted and decides the return type, while an ``ndarray`` subclass
13-
without one still gets a base ``ndarray`` back.
10+
dtypes except ``StringDType``, which remains one-dimensional.

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

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "npy_partition.h"
3333
#include "npy_binsearch.h"
3434
#include "npy_import.h"
35-
#include "ufunc_override.h"
3635
#include "alloc.h"
3736
#include "arraytypes.h"
3837
#include "array_coercion.h"
@@ -2411,24 +2410,6 @@ PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2,
24112410
}
24122411

24132412

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-
24322413
/*
24332414
* Dispatch to the private searchsorted gufuncs, which search the last axis
24342415
* of `a`. As PyArray_SearchSorted, `a` and `v` are promoted to a common
@@ -2455,22 +2436,10 @@ searchsorted_gufunc(PyArrayObject *op1, PyObject *op2,
24552436
}
24562437
}
24572438

2458-
/*
2459-
* An operand that overrides ufuncs has to be handed to the gufunc
2460-
* unchanged, casting it first would hide it. An override that declines
2461-
* raises TypeError, which the caller turns into the one dimensional
2462-
* implementation, as before.
2463-
*/
2464-
if (searchsorted_has_override((PyObject *)op1)
2465-
|| searchsorted_has_override(op2)) {
2466-
ap1 = (PyArrayObject *)Py_NewRef(op1);
2467-
ap2 = (PyArrayObject *)Py_NewRef(op2);
2468-
}
24692439
/* NPY_ARRAY_ENSUREARRAY keeps the result a base ndarray, as it has
24702440
* always been for the functions returning indices. */
2471-
else if (searchsorted_cast_operands(op1, op2, 1, 0, NPY_ARRAY_ENSUREARRAY,
2472-
NPY_ARRAY_ENSUREARRAY,
2473-
&ap1, &ap2) < 0) {
2441+
if (searchsorted_cast_operands(op1, op2, 1, 0, NPY_ARRAY_ENSUREARRAY,
2442+
NPY_ARRAY_ENSUREARRAY, &ap1, &ap2) < 0) {
24742443
goto finish;
24752444
}
24762445

@@ -2507,41 +2476,6 @@ searchsorted_gufunc(PyArrayObject *op1, PyObject *op2,
25072476
}
25082477

25092478

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-
25452479
/*
25462480
* Internal version of PyArray_SearchSorted that accepts an `op1` of any
25472481
* dimensionality, searching each one dimensional slice along its last axis.
@@ -2558,20 +2492,14 @@ npy_searchsorted(PyArrayObject *op1, PyObject *op2,
25582492
/*
25592493
* Only a failure to dispatch may fall back to the generic
25602494
* implementation: dtypes with neither a loop nor a usable comparison,
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.
2495+
* StringDType above all, fail with _UFuncNoLoopError. Any other error
2496+
* (a bad `sorter`, a failing object comparison, ...) is a genuine one.
25652497
*/
2566-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2567-
return NULL;
2568-
}
2569-
int no_loop = PyErr_ExceptionMatches(
2570-
_npy_module_state->static_pydata._UFuncNoLoopError);
2571-
if (!no_loop && !searchsorted_override_declined()) {
2498+
if (!PyErr_ExceptionMatches(
2499+
_npy_module_state->static_pydata._UFuncNoLoopError)) {
25722500
return NULL;
25732501
}
2574-
if (no_loop && PyArray_NDIM(op1) > 1) {
2502+
if (PyArray_NDIM(op1) > 1) {
25752503
PyErr_Clear();
25762504
PyErr_Format(PyExc_ValueError,
25772505
"a must be 1-dimensional for dtype %R, which "

‎numpy/_core/src/umath/searchsorted.cpp‎

Lines changed: 53 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,6 @@ searchsorted_descr_carrier(PyArray_Descr *descr)
8484
}
8585

8686

87-
/*
88-
* The carrier is built once per call in `searchsorted_get_loop` rather than
89-
* once per chunk in the loop, and travels to the loop as its auxiliary data.
90-
*/
91-
typedef struct {
92-
NpyAuxData base;
93-
PyArrayObject *carrier;
94-
} searchsorted_auxdata;
95-
96-
97-
static void
98-
searchsorted_auxdata_free(NpyAuxData *data)
99-
{
100-
Py_XDECREF(((searchsorted_auxdata *)data)->carrier);
101-
PyMem_Free(data);
102-
}
103-
104-
105-
static NpyAuxData *
106-
searchsorted_auxdata_clone(NpyAuxData *data)
107-
{
108-
searchsorted_auxdata *copy = PyMem_New(searchsorted_auxdata, 1);
109-
if (copy == NULL) {
110-
return NULL;
111-
}
112-
*copy = *(searchsorted_auxdata *)data;
113-
Py_XINCREF(copy->carrier);
114-
return (NpyAuxData *)copy;
115-
}
116-
117-
11887
/*
11988
* `generic` selects the legacy comparison function, used by the dtypes
12089
* without a dedicated kernel. It needs a descriptor carrier because the
@@ -125,7 +94,7 @@ template <bool generic, NPY_SEARCHSIDE side>
12594
static int
12695
searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
12796
npy_intp const dimensions[], npy_intp const strides[],
128-
NpyAuxData *auxdata)
97+
NpyAuxData *NPY_UNUSED(auxdata))
12998
{
13099
PyArray_Descr *descr = context->descriptors[0];
131100
PyArray_BinSearchFunc *bs = get_binsearch_func(descr, side);
@@ -137,8 +106,16 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
137106
PyArrayObject *carrier = NULL;
138107
int needs_api = 0;
139108
if constexpr (generic) {
140-
carrier = ((searchsorted_auxdata *)auxdata)->carrier;
109+
/* Bytes and unicode search with the GIL released, so take it just
110+
* to build the carrier. */
141111
needs_api = PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI);
112+
NPY_ALLOW_C_API_DEF;
113+
NPY_ALLOW_C_API;
114+
carrier = searchsorted_descr_carrier(descr);
115+
NPY_DISABLE_C_API;
116+
if (carrier == NULL) {
117+
return -1;
118+
}
142119
}
143120

144121
npy_intp n_outer = dimensions[0];
@@ -166,6 +143,12 @@ searchsorted_loop(PyArrayMethod_Context *context, char *const data[],
166143
}
167144
}
168145
searchsorted_restore_floatstatus((char *)&n_outer, saved);
146+
if constexpr (generic) {
147+
NPY_ALLOW_C_API_DEF;
148+
NPY_ALLOW_C_API;
149+
Py_DECREF(carrier);
150+
NPY_DISABLE_C_API;
151+
}
169152
return ret;
170153
}
171154

@@ -174,7 +157,7 @@ template <bool generic, NPY_SEARCHSIDE side>
174157
static int
175158
searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
176159
npy_intp const dimensions[], npy_intp const strides[],
177-
NpyAuxData *auxdata)
160+
NpyAuxData *NPY_UNUSED(auxdata))
178161
{
179162
PyArray_Descr *descr = context->descriptors[0];
180163
PyArray_ArgBinSearchFunc *bs = get_argbinsearch_func(descr, side);
@@ -186,8 +169,16 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
186169
PyArrayObject *carrier = NULL;
187170
int needs_api = 0;
188171
if constexpr (generic) {
189-
carrier = ((searchsorted_auxdata *)auxdata)->carrier;
172+
/* Bytes and unicode search with the GIL released, so take it just
173+
* to build the carrier. */
190174
needs_api = PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI);
175+
NPY_ALLOW_C_API_DEF;
176+
NPY_ALLOW_C_API;
177+
carrier = searchsorted_descr_carrier(descr);
178+
NPY_DISABLE_C_API;
179+
if (carrier == NULL) {
180+
return -1;
181+
}
191182
}
192183

193184
npy_intp n_outer = dimensions[0];
@@ -224,6 +215,12 @@ searchsorted_sorter_loop(PyArrayMethod_Context *context, char *const data[],
224215
}
225216
}
226217
searchsorted_restore_floatstatus((char *)&n_outer, saved);
218+
if constexpr (generic) {
219+
NPY_ALLOW_C_API_DEF;
220+
NPY_ALLOW_C_API;
221+
Py_DECREF(carrier);
222+
NPY_DISABLE_C_API;
223+
}
227224
return ret;
228225
}
229226

@@ -263,46 +260,13 @@ searchsorted_resolve_descriptors(
263260
}
264261

265262

266-
/*
267-
* Build the descriptor carrier the legacy comparison needs, and release the
268-
* GIL for the dtypes whose comparison cannot reach Python. This is the test
269-
* PyArray_SearchSorted makes through NPY_BEGIN_THREADS_DESCR. VOID_compare
270-
* calls into Python even with no object fields, so the dtype flag rather
271-
* than a reference check decides it, and it depends on the descriptor rather
272-
* than the DType, which is why it happens here.
273-
*/
274-
static int
275-
searchsorted_get_loop(PyArrayMethod_Context *context, int aligned,
276-
int move_references, const npy_intp *strides,
277-
PyArrayMethod_StridedLoop **out_loop,
278-
NpyAuxData **out_transferdata,
279-
NPY_ARRAYMETHOD_FLAGS *flags)
263+
template <bool generic, NPY_SEARCHSIDE side>
264+
static void *
265+
searchsorted_loop_for(int with_sorter)
280266
{
281-
if (npy_default_get_strided_loop(context, aligned, move_references,
282-
strides, out_loop, out_transferdata,
283-
flags) < 0) {
284-
return -1;
285-
}
286-
searchsorted_auxdata *auxdata = PyMem_New(searchsorted_auxdata, 1);
287-
if (auxdata == NULL) {
288-
PyErr_NoMemory();
289-
return -1;
290-
}
291-
memset(auxdata, 0, sizeof(*auxdata));
292-
auxdata->base.free = &searchsorted_auxdata_free;
293-
auxdata->base.clone = &searchsorted_auxdata_clone;
294-
295-
PyArray_Descr *descr = context->descriptors[0];
296-
auxdata->carrier = searchsorted_descr_carrier(descr);
297-
if (auxdata->carrier == NULL) {
298-
PyMem_Free(auxdata);
299-
return -1;
300-
}
301-
*out_transferdata = (NpyAuxData *)auxdata;
302-
if (!PyDataType_FLAGCHK(descr, NPY_NEEDS_PYAPI)) {
303-
*flags = (NPY_ARRAYMETHOD_FLAGS)(*flags & ~NPY_METH_REQUIRES_PYAPI);
304-
}
305-
return 0;
267+
return (void *)(with_sorter
268+
? searchsorted_sorter_loop<generic, side>
269+
: searchsorted_loop<generic, side>);
306270
}
307271

308272

@@ -323,43 +287,22 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
323287
PyArray_DTypeMeta *dtypes[4] = {dt, dt, intp_dt, intp_dt};
324288

325289
void *loop;
326-
if (side == NPY_SEARCHLEFT) {
327-
if (generic) {
328-
loop = (void *)(with_sorter
329-
? searchsorted_sorter_loop<true, NPY_SEARCHLEFT>
330-
: searchsorted_loop<true, NPY_SEARCHLEFT>);
331-
}
332-
else {
333-
loop = (void *)(with_sorter
334-
? searchsorted_sorter_loop<false, NPY_SEARCHLEFT>
335-
: searchsorted_loop<false, NPY_SEARCHLEFT>);
336-
}
290+
if (generic) {
291+
loop = side == NPY_SEARCHLEFT
292+
? searchsorted_loop_for<true, NPY_SEARCHLEFT>(with_sorter)
293+
: searchsorted_loop_for<true, NPY_SEARCHRIGHT>(with_sorter);
337294
}
338295
else {
339-
if (generic) {
340-
loop = (void *)(with_sorter
341-
? searchsorted_sorter_loop<true, NPY_SEARCHRIGHT>
342-
: searchsorted_loop<true, NPY_SEARCHRIGHT>);
343-
}
344-
else {
345-
loop = (void *)(with_sorter
346-
? searchsorted_sorter_loop<false, NPY_SEARCHRIGHT>
347-
: searchsorted_loop<false, NPY_SEARCHRIGHT>);
348-
}
296+
loop = side == NPY_SEARCHLEFT
297+
? searchsorted_loop_for<false, NPY_SEARCHLEFT>(with_sorter)
298+
: searchsorted_loop_for<false, NPY_SEARCHRIGHT>(with_sorter);
349299
}
350300
PyType_Slot slots[] = {
351301
{NPY_METH_strided_loop, loop},
352302
{NPY_METH_resolve_descriptors,
353303
(void *)&searchsorted_resolve_descriptors},
354-
{0, NULL},
355304
{0, NULL}
356305
};
357-
if (generic) {
358-
/* Only the legacy comparison needs a carrier and, for some dtypes,
359-
* the GIL. */
360-
slots[2].slot = NPY_METH_get_loop;
361-
slots[2].pfunc = (void *)&searchsorted_get_loop;
362-
}
363306

364307
PyArrayMethod_Spec spec = {};
365308
spec.name = with_sorter ? "searchsorted_sorter_loop" : "searchsorted_loop";
@@ -369,9 +312,14 @@ add_searchsorted_loop(PyObject *ufunc, PyArray_DTypeMeta *dt, int with_sorter,
369312
spec.casting = NPY_SAFE_CASTING;
370313
/* Discarded by the gufunc machinery, see searchsorted_restore_floatstatus */
371314
spec.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
372-
if (generic) {
373-
/* Cleared again in searchsorted_get_loop for the dtypes whose
374-
* comparison cannot call into Python. */
315+
/*
316+
* Only the legacy comparisons that reach Python need the GIL: object
317+
* always does, and VOID_compare does even without object fields. Bytes
318+
* and unicode compare their raw buffers, so they search with the GIL
319+
* released as PyArray_SearchSorted does.
320+
*/
321+
if (generic && dt->type_num != NPY_STRING
322+
&& dt->type_num != NPY_UNICODE) {
375323
spec.flags = (NPY_ARRAYMETHOD_FLAGS)(spec.flags
376324
| NPY_METH_REQUIRES_PYAPI);
377325
}

0 commit comments

Comments
 (0)