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

Code before:
             * we need to make sure and get a copy
             * so the input array is not changed
             * before the error is called
             */
            is_newarr = 1;
        }

        if (!PyArray_EquivTypes(dtype, PyArray_DESCR(out))) {
            is_newarr = 1;
        }

        Py_INCREF(dtype);
        if (is_newarr) {
            obj = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(out),
                                                        dtype,
                                                        multi->nd,
                                                        multi->dimensions,
                                                        NULL, NULL, 0,
                                                        (PyObject *)out);
        }
        else {
            int flags = NPY_ARRAY_CARRAY |
                        NPY_ARRAY_WRITEBACKIFCOPY |
                        NPY_ARRAY_FORCECAST;

            obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags);
        }
    }

    if (obj == NULL) {
        goto fail;
    }
    elsize = dtype->elsize;
    ret_data = PyArray_DATA(obj);
    npy_intp transfer_strides[2] = {elsize, elsize};
    npy_intp one = 1;
    NPY_ARRAYMETHOD_FLAGS transfer_flags = 0;
    if (PyDataType_REFCHK(dtype)) {
        int is_aligned = IsUintAligned(obj);
        PyArray_Descr *obj_dtype = PyArray_DESCR(obj);
        PyArray_GetDTypeTransferFunction(
                    is_aligned,
                    dtype->elsize,
                    obj_dtype->elsize,
                    dtype,
                    obj_dtype, 0, &cast_info,
                    &transfer_flags);
    }

    while (PyArray_MultiIter_NOTDONE(multi)) {
        mi = *((npy_intp *)PyArray_MultiIter_DATA(multi, n));

Review comment (bug):
This branch should now never copy, so the only thing it should be doing is check for `PyArray_FailUnlessWriteable`.  That also means you don't need the writeback-if-copy branch below.

(We can check that earlier also, since it doesn't make sense to wait until the last copy for this to fail in the other branch either.)

Code after:

        if (clipmode == NPY_RAISE) {
            /*
             * we need to make sure and get a copy
             * so the input array is not changed
             * before the error is called
             */
            copy_existing_out = 1;
        }

        if (!PyArray_EquivTypes(dtype, PyArray_DESCR(out))) {
            copy_existing_out = 1;
        }

        if (copy_existing_out) {
            Py_INCREF(dtype);
            obj = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type,
                                                        dtype,
                                                        multi->nd,
                                                        multi->dimensions,
                                                        NULL, NULL, 0,
                                                        (PyObject *)out);
        }
        else {
            obj = (PyArrayObject *)Py_NewRef(out);
        }
    }

    if (obj == NULL) {
        goto fail;
    }
    elsize = dtype->elsize;
    ret_data = PyArray_DATA(obj);
    npy_intp transfer_strides[2] = {elsize, elsize};
    npy_intp one = 1;
    NPY_ARRAYMETHOD_FLAGS transfer_flags = 0;
    if (PyDataType_REFCHK(dtype)) {
        int is_aligned = IsUintAligned(obj);
        PyArray_Descr *obj_dtype = PyArray_DESCR(obj);
        PyArray_GetDTypeTransferFunction(
                    is_aligned,
                    dtype->elsize,
                    obj_dtype->elsize,
                    dtype,
                    obj_dtype, 0, &cast_info,
                    &transfer_flags);
    }

    while (PyArray_MultiIter_NOTDONE(multi)) {
        mi = *((npy_intp *)PyArray_MultiIter_DATA(multi, n));
        if (mi < 0 || mi >= n) {