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

Code before:
    PyObject *numeric_mod = NULL, *from_buffer_func = NULL;
    PyObject *pickle_module = NULL, *picklebuf_class = NULL;
    PyObject *picklebuf_args = 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 */
    pickle_module = PyImport_ImportModule("pickle");
    if (pickle_module == NULL){
        return NULL;
    }
    picklebuf_class = PyObject_GetAttrString(pickle_module, "PickleBuffer");
    Py_DECREF(pickle_module);
    if (picklebuf_class == NULL) {
        return NULL;
    }

    /* Construct a PickleBuffer of the array */
    if (PyArray_IS_C_CONTIGUOUS((PyArrayObject *)self)) {
        order = 'C';
        picklebuf_args = Py_BuildValue("(O)", self);
        rev_perm = PyTuple_Pack(0);
    }
    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);
        picklebuf_args = Py_BuildValue("(N)", transposed_array);
        rev_perm = PyTuple_Pack(0);
    }
    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) {
            Py_DECREF(picklebuf_class);
            return NULL;
        }
        PyArray_Dims dims;
        npy_intp d[NPY_MAXDIMS];
        for (int i = 0; i < n; i++) {
            d[i] = items[i].perm;

Review comment (suggestion):
```suggestion
        picklebuf_args = PyTuple_Pack(1, self);
        rev_perm = PyTuple_New(0);
```
The first one is actually the obvious one to use packing.  Below, you would have to manually Decref with it, though.  I think for an empty tuple, might as well use `New(0)`.

Code after:
        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);
        }
        perm.ptr = dims;
        perm.len = n;
        transposed_array = PyArray_Transpose((PyArrayObject *)self, &perm);
        if (transposed_array == NULL) {
            Py_DECREF(rev_perm);
            return NULL;
        }
        if (!PyArray_IS_C_CONTIGUOUS((PyArrayObject *)transposed_array)) {
            // self is non-contiguous
            Py_DECREF(rev_perm);
            Py_DECREF(transposed_array);
            return array_reduce_ex_regular(self, protocol);
        }
    }
    buffer = PyObject_CallOneArg(picklebuf_class, transposed_array == NULL ? (PyObject*) self: transposed_array);
    if (buffer == NULL) {
        /* Some arrays may refuse to export a buffer, in which case
         * just fall back on regular __reduce_ex__ implementation