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

Code before:
                goto failed_building_indices;
            }

            /*
             * For example an empty list can be cast to an integer array,
             * however it will default to a float one.
             */
            if (PyArray_SIZE(tmp_arr) == 0
                || (is_flatiter_object && !PyArray_ISINTEGER(tmp_arr) && !PyArray_ISBOOL(tmp_arr))) {

                PyArray_Descr *indtype = PyArray_DescrFromType(NPY_INTP);
                arr = (PyArrayObject *)PyArray_FromArray(tmp_arr, indtype,
                                                         NPY_ARRAY_FORCECAST);

                // If the cast succeeded (which means that the previous flat iterator
                // indexing routine would have succeeded as well), we need to issue a
                // deprecation warning.
                if (arr
                    && is_flatiter_object
                    && PyArray_SIZE(tmp_arr) != 0
                    && !PyArray_ISINTEGER(tmp_arr)
                    && !PyArray_ISBOOL(tmp_arr)
                    && DEPRECATE("Invalid non-array indices for iterator objects are deprecated and will be "
                                 "removed in a future version. (Deprecated NumPy 2.4)") < 0) {
                    Py_DECREF(tmp_arr);
                    Py_DECREF(arr);
                    goto failed_building_indices;
                }
                Py_DECREF(tmp_arr);
                if (arr == NULL) {
                    // Raise a helpful error if this was a ValueError (i.e. could not cast)
                    if (PyErr_ExceptionMatches(PyExc_ValueError)) {
                        PyErr_Format(PyExc_IndexError,
                            "only integers, slices (`:`), ellipsis (`...`)%s and integer or boolean "
                            "arrays are valid indices",
                            is_flatiter_object ? "" : ", numpy.newaxis (`None`)"
                        );
                    }
                    goto failed_building_indices;
                }
            }
            else {
                arr = tmp_arr;
            }
        }
        else {
            Py_INCREF(obj);
            arr = (PyArrayObject *)obj;
        }

        /* Check if the array is valid and fill the information */

Review comment (bug):
I think we're also missing a `Py_DECREF(arr)` in the error path for `PyObject_IsTrue` below. Maybe it would be easier to keep track if `failed_building_indices` had a `Py_XDECREF(arr)` in it.

Code after:
                goto failed_building_indices;
            }

            /*
             * For example an empty list can be cast to an integer array,
             * however it will default to a float one.
             */
            if (PyArray_SIZE(tmp_arr) == 0
                || (is_flatiter_object && !PyArray_ISINTEGER(tmp_arr) && !PyArray_ISBOOL(tmp_arr))) {

                PyArray_Descr *indtype = PyArray_DescrFromType(NPY_INTP);
                arr = (PyArrayObject *)PyArray_FromArray(tmp_arr, indtype,
                                                         NPY_ARRAY_FORCECAST);

                // If the cast succeeded (which means that the previous flat iterator
                // indexing routine would have succeeded as well), we need to issue a
                // deprecation warning.
                if (arr
                    && is_flatiter_object
                    && PyArray_SIZE(tmp_arr) != 0
                    && !PyArray_ISINTEGER(tmp_arr)
                    && !PyArray_ISBOOL(tmp_arr)
                    && DEPRECATE("Invalid non-array indices for iterator objects are deprecated and will be "
                                 "removed in a future version. (Deprecated NumPy 2.4)") < 0) {
                    Py_DECREF(tmp_arr);
                    goto failed_building_indices;
                }
                Py_DECREF(tmp_arr);
                if (arr == NULL) {
                    // Raise a helpful error if this was a ValueError (i.e. could not cast)
                    if (PyErr_ExceptionMatches(PyExc_ValueError)) {
                        PyErr_Format(PyExc_IndexError,
                            "only integers, slices (`:`), ellipsis (`...`)%s and integer or boolean "
                            "arrays are valid indices",
                            is_flatiter_object ? "" : ", numpy.newaxis (`None`)"
                        );
                    }
                    goto failed_building_indices;
                }
            }
            else {
                arr = tmp_arr;
            }
        }
        else {
            Py_INCREF(obj);
            arr = (PyArrayObject *)obj;
        }

        /* Check if the array is valid and fill the information */
        if (PyArray_ISBOOL(arr)) {