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

Code before:
        }

        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 *)PyArray_FromArray(out, dtype, 0);
        }
    }

    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 (suggestion):
```suggestion
            obj = (PyArrayObject *)Py_NewRef(out);
```

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));