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

Code before:
    PyObject *ret;

    /* We do not call array_reduce directly but instead lookup and call
     * the __reduce__ method to make sure that it's possible to customize
     * pickling in sub-classes. */
    subclass_array_reduce = PyObject_GetAttrString((PyObject *)self,
                                                   "__reduce__");
    if (subclass_array_reduce == NULL) {
        return NULL;
    }
    ret = PyObject_CallObject(subclass_array_reduce, NULL);
    Py_DECREF(subclass_array_reduce);
    return ret;
}

static PyObject *
array_reduce_ex_picklebuffer(PyArrayObject *self, int protocol)
{
    PyObject *from_buffer_func = NULL;
    PyObject *picklebuf_class = NULL;
    PyObject *buffer = NULL, *transposed_array = NULL;
    PyArray_Descr *descr = NULL;
    PyObject *rev_perm = NULL;  // only used in 'K' order
    char order;

    descr = PyArray_DESCR(self);

    /* we expect protocol 5 to be available in Python 3.8 */
    if (npy_cache_import_runtime("pickle", "PickleBuffer", &picklebuf_class)) {
        return NULL;
    }

    /* Construct a PickleBuffer of the array */
    if (PyArray_IS_C_CONTIGUOUS((PyArrayObject *)self)) {
        order = 'C';
        rev_perm = PyTuple_New(0);
        if (rev_perm == NULL) {
            return NULL;
        }
    }
    else if (PyArray_IS_F_CONTIGUOUS((PyArrayObject *)self)) {
        /* if the array if Fortran-contiguous and not C-contiguous,
         * the PickleBuffer instance will hold a view on the transpose
         * of the initial array, that is C-contiguous. */
        order = 'F';
        transposed_array = PyArray_Transpose((PyArrayObject *)self, NULL);
        rev_perm = PyTuple_New(0);
        if (rev_perm == NULL) {
            return NULL;
        }
    }

Review comment (bug):
This returns `NULL` on failure and you need to check for that.

Code after:
array_reduce_ex_picklebuffer(PyArrayObject *self, int protocol)
{
    PyObject *from_buffer_func = NULL;
    PyObject *picklebuf_class = NULL;
    PyObject *buffer = NULL, *transposed_array = NULL;
    PyArray_Descr *descr = NULL;
    PyObject *rev_perm = NULL;  // only used in 'K' order
    char order;

    descr = PyArray_DESCR(self);

    /* we expect protocol 5 to be available in Python 3.8 */
    if (npy_cache_import_runtime("pickle", "PickleBuffer", &picklebuf_class) == -1) {
        return NULL;
    }

    /* Construct a PickleBuffer of the array */
    if (PyArray_IS_C_CONTIGUOUS((PyArrayObject *)self)) {
        order = 'C';
    }
    else if (PyArray_IS_F_CONTIGUOUS((PyArrayObject *)self)) {
        /* if the array if Fortran-contiguous and not C-contiguous,
         * the PickleBuffer instance will hold a view on the transpose
         * of the initial array, that is C-contiguous. */
        order = 'F';
        transposed_array = PyArray_Transpose((PyArrayObject *)self, NULL);
        if (transposed_array == NULL) {
            return NULL;
        }
    }
    else {
        order = 'K';
        const int n = PyArray_NDIM(self);
        npy_stride_sort_item items[NPY_MAXDIMS];
        // sort (strde, perm) as descending = transpose to C
        PyArray_CreateSortedStridePerm(n, PyArray_STRIDES(self), items);
        rev_perm = PyTuple_New(n);
        if (rev_perm == NULL) {
            return NULL;
        }
        PyArray_Dims perm;
        npy_intp dims[NPY_MAXDIMS];
        for (int i = 0; i < n; i++) {
            dims[i] = items[i].perm;
            PyObject *idx = PyLong_FromLong(i);
            if (idx == NULL) {
                Py_DECREF(rev_perm);
                return NULL;
            }
            PyTuple_SET_ITEM(rev_perm, items[i].perm, idx);
        }