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

Code before:
    *mx = max;
}

/*
 * arr_bincount is registered as bincount.
 *
 * bincount accepts one, two or three arguments. The first is an array of
 * non-negative integers. The second, if present, is an array of weights,
 * which must be promotable to double. Call these arguments list and
 * weight. Both must be one-dimensional with len(weight) == len(list). If
 * weight is not present then bincount(list)[i] is the number of occurrences
 * of i in list.  If weight is present then bincount(self,list, weight)[i]
 * is the sum of all weight[j] where list [j] == i.  Self is not used.
 * The third argument, if present, is a minimum length desired for the
 * output array.
 */
NPY_NO_EXPORT PyObject *
arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
                            Py_ssize_t len_args, PyObject *kwnames)
{
    PyObject *list = NULL, *weight = Py_None, *mlength = NULL;
    PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL;
    npy_intp *numbers, *ians, len, mx, mn, ans_size;
    npy_intp minlength = 0;
    npy_intp i;
    int flags;
    double *weights , *dans;

    NPY_PREPARE_ARGPARSER;
    if (npy_parse_arguments("bincount", args, len_args, kwnames,
                "list", NULL, &list,
                "|weights", NULL, &weight,
                "|minlength", NULL, &mlength,
                NULL, NULL, NULL) < 0) {
        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,

Review comment (refactor):
Would move this down into the if, but OK.

I think we can put this in, if we change things to a `PyArray_Check` below, unless I am missing something.  It seems fine, even if it might not be 100% for array-likes.

I would lean towards *not* backporting.  It has been around forever, and the reason it came up was just cupy testing against NumPy as far as I am aware.

Code after:
    *mx = max;
}

/*
 * arr_bincount is registered as bincount.
 *
 * bincount accepts one, two or three arguments. The first is an array of
 * non-negative integers. The second, if present, is an array of weights,
 * which must be promotable to double. Call these arguments list and
 * weight. Both must be one-dimensional with len(weight) == len(list). If
 * weight is not present then bincount(list)[i] is the number of occurrences
 * of i in list.  If weight is present then bincount(self,list, weight)[i]
 * is the sum of all weight[j] where list [j] == i.  Self is not used.
 * The third argument, if present, is a minimum length desired for the
 * output array.
 */
NPY_NO_EXPORT PyObject *
arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
                            Py_ssize_t len_args, PyObject *kwnames)
{
    PyObject *list = NULL, *weight = Py_None, *mlength = NULL;
    PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL;
    npy_intp *numbers, *ians, len, mx, mn, ans_size;
    npy_intp minlength = 0;
    npy_intp i;
    double *weights , *dans;

    NPY_PREPARE_ARGPARSER;
    if (npy_parse_arguments("bincount", args, len_args, kwnames,
                "list", NULL, &list,
                "|weights", NULL, &weight,
                "|minlength", NULL, &mlength,
                NULL, NULL, NULL) < 0) {
        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);