Repository: numpy/numpy
File: numpy/_core/src/multiarray/item_selection.c (C)

Code before:
    if (dtype == NULL) {
        return NULL;
    }

    /* Look for binary search function */
    if (perm) {
        argbinsearch = get_argbinsearch_func(dtype, side);
    }
    else {
        binsearch = get_binsearch_func(dtype, side);
    }
    if (binsearch == NULL && argbinsearch == NULL) {
        PyErr_SetString(PyExc_TypeError, "compare not supported for type");
        Py_DECREF(dtype);
        return NULL;
    }

    /* need ap2 as contiguous array and of right dtype (steals and may be replace it) */
    ap2 = (PyArrayObject *)PyArray_CheckFromAny(op2, dtype,
                                0, 0,
                                NPY_ARRAY_CARRAY_RO | NPY_ARRAY_NOTSWAPPED,
                                NULL);
    if (ap2 == NULL) {
        return NULL;
    }
    /* dtype was stolen, replace it in case the array creation replaced it. */
    dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));

    /*
     * If the needle (ap2) is larger than the haystack (op1) we copy the
     * haystack to a contiguous array for improved cache utilization.
     */
    if (PyArray_SIZE(ap2) > PyArray_SIZE(op1)) {
        ap1_flags |= NPY_ARRAY_CARRAY_RO;
    }
    /* dtype is stolen, after this we have no reference */
    ap1 = (PyArrayObject *)PyArray_CheckFromAny((PyObject *)op1, dtype,
                                1, 1, ap1_flags, NULL);
    if (ap1 == NULL) {
        goto fail;
    }

    if (perm) {
        /* need ap3 as a 1D aligned, not swapped, array of right type */
        ap3 = (PyArrayObject *)PyArray_CheckFromAny(perm, NULL,
                                    1, 1,
                                    NPY_ARRAY_ALIGNED | NPY_ARRAY_NOTSWAPPED,
                                    NULL);
        if (ap3 == NULL) {
            PyErr_SetString(PyExc_TypeError,
                        "could not parse sorter argument");

Review comment (suggestion):
Combine with l.2162? And write there,
```
/*
 * The dtype reference we had was used for creating ap2, which may have
 * replaced it with another. So here we copy the dtype of ap2 and use it for `ap1`.
 */
dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));
```

Code after:
    if (dtype == NULL) {
        return NULL;
    }

    /* Look for binary search function */
    if (perm) {
        argbinsearch = get_argbinsearch_func(dtype, side);
    }
    else {
        binsearch = get_binsearch_func(dtype, side);
    }
    if (binsearch == NULL && argbinsearch == NULL) {
        PyErr_SetString(PyExc_TypeError, "compare not supported for type");
        Py_DECREF(dtype);
        return NULL;
    }

    /* need ap2 as contiguous array and of right dtype (note: steals dtype reference) */
    ap2 = (PyArrayObject *)PyArray_CheckFromAny(op2, dtype,
                                0, 0,
                                NPY_ARRAY_CARRAY_RO | NPY_ARRAY_NOTSWAPPED,
                                NULL);
    if (ap2 == NULL) {
        return NULL;
    }
    /*
     * The dtype reference we had was used for creating ap2, which may have
     * replaced it with another. So here we copy the dtype of ap2 and use it for `ap1`.
     */
     dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));

    /*
     * If the needle (ap2) is larger than the haystack (op1) we copy the
     * haystack to a contiguous array for improved cache utilization.
     */
    if (PyArray_SIZE(ap2) > PyArray_SIZE(op1)) {
        ap1_flags |= NPY_ARRAY_CARRAY_RO;
    }
    /* dtype is stolen, after this we have no reference */
    ap1 = (PyArrayObject *)PyArray_CheckFromAny((PyObject *)op1, dtype,
                                1, 1, ap1_flags, NULL);
    if (ap1 == NULL) {
        goto fail;
    }

    if (perm) {
        /* need ap3 as a 1D aligned, not swapped, array of right type */
        ap3 = (PyArrayObject *)PyArray_CheckFromAny(perm, NULL,
                                    1, 1,
                                    NPY_ARRAY_ALIGNED | NPY_ARRAY_NOTSWAPPED,
                                    NULL);