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

Code before:
        return NULL;
    }

    /*
     * Accepting arbitrary lists that are cast to NPY_INTP, possibly
     * losing precision because of unsafe casts, is deprecated.  We
     * continue to use PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1)
     * to convert the input during the deprecation period, but we also
     * check to see if a deprecation warning should be generated.
     * Some refactoring will be needed when the deprecation expires.
     */

    /* Check to see if we should generate a deprecation warning. */
    if (!PyArray_Check(list)) {
        /* list is not a numpy array, so convert it. */
        PyArrayObject *tmp1 = (PyArrayObject *)PyArray_FromAny(
                                                    list, NULL, 1, 1,
                                                    NPY_ARRAY_DEFAULT, NULL);
        if (tmp1 == NULL) {
            goto fail;
        }
        if (PyArray_SIZE(tmp1) > 0) {
            /* The input is not empty, so convert it to NPY_INTP. */
            int flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
            if (PyArray_Check((PyObject *)tmp1) &&
                PyArray_ISINTEGER((PyArrayObject *)tmp1)) {
                flags = flags | NPY_ARRAY_FORCECAST;
            }
            PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
            lst = (PyArrayObject *)PyArray_FromAny((PyObject *)tmp1, local_dtype, 1, 1, flags, NULL);
            Py_DECREF(tmp1);
            if (lst == NULL) {
                /* Failed converting to NPY_INTP. */
                if (PyErr_ExceptionMatches(PyExc_TypeError)) {
                    PyErr_Clear();
                    /* Deprecated 2024-08-02, NumPy 2.1 */
                    if (DEPRECATE("Non-integer input passed to bincount. In a "
                                  "future version of NumPy, this will be an "
                                  "error. (Deprecated NumPy 2.1)") < 0) {
                        goto fail;
                    }
                }
                else {
                    /* Failure was not a TypeError. */
                    goto fail;
                }
            }
        }
        else {
            /* Got an empty list. */
            Py_DECREF(tmp1);

Review comment (performance):
```suggestion
            if (PyArray_ISINTEGER(tmp1)) {
```
`tmp1` is already an array (not that the check is expensive).

Code after:
        return NULL;
    }

    /*
     * Accepting arbitrary lists that are cast to NPY_INTP, possibly
     * losing precision because of unsafe casts, is deprecated.  We
     * continue to use PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1)
     * to convert the input during the deprecation period, but we also
     * check to see if a deprecation warning should be generated.
     * Some refactoring will be needed when the deprecation expires.
     */

    /* Check to see if we should generate a deprecation warning. */
    if (!PyArray_Check(list)) {
        /* list is not a numpy array, so convert it. */
        PyArrayObject *tmp1 = (PyArrayObject *)PyArray_FromAny(
                                                    list, NULL, 1, 1,
                                                    NPY_ARRAY_DEFAULT, NULL);
        if (tmp1 == NULL) {
            goto fail;
        }
        if (PyArray_SIZE(tmp1) > 0) {
            /* The input is not empty, so convert it to NPY_INTP. */
            int flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
            if (PyArray_ISINTEGER(tmp1)) {
                flags = flags | NPY_ARRAY_FORCECAST;
            }
            PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
            lst = (PyArrayObject *)PyArray_FromAny((PyObject *)tmp1, local_dtype, 1, 1, flags, NULL);
            Py_DECREF(tmp1);
            if (lst == NULL) {
                /* Failed converting to NPY_INTP. */
                if (PyErr_ExceptionMatches(PyExc_TypeError)) {
                    PyErr_Clear();
                    /* Deprecated 2024-08-02, NumPy 2.1 */
                    if (DEPRECATE("Non-integer input passed to bincount. In a "
                                  "future version of NumPy, this will be an "
                                  "error. (Deprecated NumPy 2.1)") < 0) {
                        goto fail;
                    }
                }
                else {
                    /* Failure was not a TypeError. */
                    goto fail;
                }
            }
        }
        else {
            /* Got an empty list. */
            Py_DECREF(tmp1);
        }