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

Code before:
     *    ("too big" ignores dimensios of size 0).
     * 2. Find `PyArray_NBYTES` of the result, as this is what we may need to
     *    copy from the pickled data (may not match allocation currently if 0).
     * Compare with `PyArray_NewFromDescr`, raise MemoryError for simplicity.
     */
    nbytes = typecode->elsize;
    for (int i = 0; i < nd; i++) {
        if (dimensions[i] < 0) {
            PyErr_SetString(PyExc_TypeError,
                    "impossible dimension while unpickling array");
            return NULL;
        }
        overflowed = npy_mul_sizes_with_overflow(
                &nbytes, nbytes, dimensions[i]);
        if (overflowed) {
            return PyErr_NoMemory();
        }
    }

    if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
        if (!PyList_Check(rawdata)) {
            PyErr_SetString(PyExc_TypeError,
                            "object pickle not returning list");
            return NULL;
        }
    }
    else {
        Py_INCREF(rawdata);

        /* Backward compatibility with Python 2 NumPy pickles */
        if (PyUnicode_Check(rawdata)) {
            Py_SETREF(rawdata, PyUnicode_AsLatin1String(rawdata));
            if (rawdata == NULL) {
                /* More informative error message */
                PyErr_SetString(PyExc_ValueError,
                                ("Failed to encode latin1 string when unpickling a Numpy array. "
                                 "pickle.load(a, encoding='latin1') is assumed."));
                return NULL;
            }
        }

        if (!PyBytes_Check(rawdata)) {
            PyErr_SetString(PyExc_TypeError,
                            "pickle not returning string");
            Py_DECREF(rawdata);
            return NULL;
        }

        if (PyBytes_AsStringAndSize(rawdata, &datastr, &len) < 0) {
            Py_DECREF(rawdata);
            return NULL;

Review comment (suggestion):
Unlike in `ctors`, here the code never replaced 0 by 1, so there is no need to separately track whether the result is empty: multiplication by 0 will just ensure that.

Code after:
     * 1. Validate the input, that it is neither invalid, nor "too big"
     *    ("too big" ignores dimensios of size 0).
     * 2. Find `PyArray_NBYTES` of the result, as this is what we may need to
     *    copy from the pickled data (may not match allocation currently if 0).
     * Compare with `PyArray_NewFromDescr`, raise MemoryError for simplicity.
     */
    nbytes = typecode->elsize;
    for (int i = 0; i < nd; i++) {
        if (dimensions[i] < 0) {
            PyErr_SetString(PyExc_TypeError,
                    "impossible dimension while unpickling array");
            return NULL;
        }
        overflowed = npy_mul_sizes_with_overflow(
                &nbytes, nbytes, dimensions[i]);
        if (overflowed) {
            return PyErr_NoMemory();
        }
    }

    if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
        if (!PyList_Check(rawdata)) {
            PyErr_SetString(PyExc_TypeError,
                            "object pickle not returning list");
            return NULL;
        }
    }
    else {
        Py_INCREF(rawdata);

        /* Backward compatibility with Python 2 NumPy pickles */
        if (PyUnicode_Check(rawdata)) {
            Py_SETREF(rawdata, PyUnicode_AsLatin1String(rawdata));
            if (rawdata == NULL) {
                /* More informative error message */
                PyErr_SetString(PyExc_ValueError,
                                ("Failed to encode latin1 string when unpickling a Numpy array. "
                                 "pickle.load(a, encoding='latin1') is assumed."));
                return NULL;
            }
        }

        if (!PyBytes_Check(rawdata)) {
            PyErr_SetString(PyExc_TypeError,
                            "pickle not returning string");
            Py_DECREF(rawdata);
            return NULL;
        }

        if (PyBytes_AsStringAndSize(rawdata, &datastr, &len) < 0) {
            Py_DECREF(rawdata);