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

Code before:
            Py_DECREF(rev_perm);
            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
         * (gh-12745).
         */
        Py_DECREF(rev_perm);
        if (transposed_array != NULL) {
            Py_DECREF(transposed_array);
        }
        PyErr_Clear();
        return array_reduce_ex_regular(self, protocol);
    }

    /* Get the _frombuffer() function for reconstruction */
    if (npy_cache_import_runtime("numpy._core.numeric", "_frombuffer",
                                 &from_buffer_func) == -1) {
        Py_DECREF(rev_perm);
        Py_DECREF(buffer);
        if (transposed_array != NULL) {
            Py_DECREF(transposed_array);
        }
        return NULL;
    }

    PyObject *shape = NULL;
    if (order == 'K') {
        shape = PyArray_IntTupleFromIntp(
                PyArray_NDIM((PyArrayObject *)transposed_array),
                PyArray_SHAPE((PyArrayObject *)transposed_array));
    }
    else {
        shape = PyArray_IntTupleFromIntp(PyArray_NDIM(self),
                                         PyArray_SHAPE(self));
    }
    if (transposed_array != NULL) {
        Py_DECREF(transposed_array);
    }
    return Py_BuildValue("N(NONNN)", from_buffer_func, buffer,
                         (PyObject *)descr, shape,
                         PyUnicode_FromStringAndSize(&order, 1), rev_perm);
}

static PyObject *
array_reduce_ex(PyArrayObject *self, PyObject *args)
{
    int protocol;

Review comment (suggestion):
```suggestion
        Py_XDECREF(transposed_array);
```

This pattern is common enough it has its own macro in the C API. There are some other common operations that have an X variant that does NULL checking.

Code after:
    if (npy_cache_import_runtime("numpy._core.numeric", "_frombuffer",
                                 &from_buffer_func) == -1) {
        Py_XDECREF(rev_perm);
        Py_XDECREF(transposed_array);
        Py_DECREF(buffer);
        return NULL;
    }

    PyObject *shape = NULL;
    if (order == 'K') {
        shape = PyArray_IntTupleFromIntp(
                PyArray_NDIM((PyArrayObject *)transposed_array),
                PyArray_SHAPE((PyArrayObject *)transposed_array));
    }
    else {
        shape = PyArray_IntTupleFromIntp(PyArray_NDIM(self),
                                         PyArray_SHAPE(self));
    }
    Py_XDECREF(transposed_array);
    if (shape == NULL) {
        Py_XDECREF(rev_perm);
        Py_DECREF(buffer);
        return NULL;
    }
    if (order == 'K') {
        return Py_BuildValue("N(NONNN)", from_buffer_func, buffer,
                             (PyObject *)descr, shape,
                             PyUnicode_FromStringAndSize(&order, 1), rev_perm);
    }
    else {
        return Py_BuildValue("N(NONN)", from_buffer_func, buffer,
                             (PyObject *)descr, shape,
                             PyUnicode_FromStringAndSize(&order, 1));
    }
}

static PyObject *
array_reduce_ex(PyArrayObject *self, PyObject *args)
{
    int protocol;
    PyArray_Descr *descr = NULL;

    if (!PyArg_ParseTuple(args, "i", &protocol)) {
        return NULL;
    }

    descr = PyArray_DESCR(self);
    if ((protocol < 5) ||
        PyDataType_FLAGCHK(descr, NPY_ITEM_HASOBJECT) ||
        (PyType_IsSubtype(((PyObject*)self)->ob_type, &PyArray_Type) &&
         ((PyObject*)self)->ob_type != &PyArray_Type) ||