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

Code before:
        if ((PyArray_NDIM(out) != multi->nd)
                    || !PyArray_CompareLists(PyArray_DIMS(out),
                                             multi->dimensions,
                                             multi->nd)) {
            PyErr_SetString(PyExc_TypeError,
                            "choose: invalid shape for output array.");
            goto fail;
        }

        for (i = 0; i < n; i++) {
            if (arrays_overlap(out, mps[i])) {
                flags |= NPY_ARRAY_ENSURECOPY;
            }
        }

        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
             */
            flags |= NPY_ARRAY_ENSURECOPY;
        }
        Py_INCREF(dtype);
        if (!PyArray_EquivTypes(dtype, PyArray_DESCR(out))) {
            obj = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(out),
                                                        dtype,
                                                        multi->nd,
                                                        multi->dimensions,
                                                        NULL, NULL, 0,
                                                        (PyObject *)out);
            is_newarr = 1;
        }
        else {
            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_GetDTypeTransferFunction(
                    is_aligned,
                    dtype->elsize,

Review comment (suggestion):
I don't think there is any reason to use `Py_TYPE(out)` here, just pass `NULL` and get a base-class array, I think.

(There _is_ a subtle issue here about that, I think, in that ufuncs would ensure they call `__array_wrap__` when done and we don't.  But this a completely different issue!)

Code after:
                            "choose: invalid shape for output array.");
            goto fail;
        }

        if (PyArray_FailUnlessWriteable(out, "output array") < 0) {
            goto fail;
        }

        for (i = 0; i < n; i++) {
            if (arrays_overlap(out, mps[i])) {
                copy_existing_out = 1;
            }
        }

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