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

Code before:
                    fa->descr = PyArray_DescrFromType(
                                    PyArray_DESCR(self)->type_num);
                }
                else {
                    fa->descr = PyArray_DescrNew(typecode);
                    if (fa->descr == NULL) {
                        Py_CLEAR(fa->mem_handler);
                        Py_DECREF(rawdata);
                        return NULL;
                    }
                    if (PyArray_DESCR(self)->byteorder == NPY_BIG) {
                        PyArray_DESCR(self)->byteorder = NPY_LITTLE;
                    }
                    else if (PyArray_DESCR(self)->byteorder == NPY_LITTLE) {
                        PyArray_DESCR(self)->byteorder = NPY_BIG;
                    }
                }
                Py_DECREF(typecode);
            }
            else {
                memcpy(PyArray_DATA(self), datastr, PyArray_NBYTES(self));
            }
            PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA);
            fa->base = NULL;
            Py_DECREF(rawdata);
        }
        else {
            /* The handlers should never be called in this case */
            fa->mem_handler = NULL;
            fa->data = datastr;
            if (PyArray_SetBaseObject(self, rawdata) < 0) {
                Py_DECREF(rawdata);
                return NULL;
            }
        }
    }
    else {
        npy_intp num = PyArray_NBYTES(self);
        if (num == 0) {
            num = 1;
        }
        /* Store the functions in case the default handler is modified */
        fa->mem_handler = PyDataMem_GetHandler();
        if (fa->mem_handler == NULL) {
            return NULL;
        }
        fa->data = PyDataMem_UserNEW(num, PyArray_HANDLER(self));
        if (PyArray_DATA(self) == NULL) {
            Py_CLEAR(fa->mem_handler);
            return PyErr_NoMemory();
        }

Review comment (suggestion):
Nice to avoid this strange dance with `mem_handler`.

Code after:
                      PyDataType_C_METADATA(PyArray_DESCR(self)))) {
                    fa->descr = PyArray_DescrFromType(
                                    PyArray_DESCR(self)->type_num);
                }
                else {
                    fa->descr = PyArray_DescrNew(typecode);
                    if (fa->descr == NULL) {
                        Py_CLEAR(fa->mem_handler);
                        Py_DECREF(rawdata);
                        return NULL;
                    }
                    if (PyArray_DESCR(self)->byteorder == NPY_BIG) {
                        PyArray_DESCR(self)->byteorder = NPY_LITTLE;
                    }
                    else if (PyArray_DESCR(self)->byteorder == NPY_LITTLE) {
                        PyArray_DESCR(self)->byteorder = NPY_BIG;
                    }
                }
                Py_DECREF(typecode);
            }
            else {
                memcpy(PyArray_DATA(self), datastr, PyArray_NBYTES(self));
            }
            PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA);
            fa->base = NULL;
            Py_DECREF(rawdata);
        }
        else {
            /* The handlers should never be called in this case */
            fa->mem_handler = NULL;
            fa->data = datastr;
            if (PyArray_SetBaseObject(self, rawdata) < 0) {
                Py_DECREF(rawdata);
                return NULL;
            }
        }
    }
    else {
        npy_intp num = PyArray_NBYTES(self);
        if (num == 0) {
            num = 1;
        }
        /* Store the functions in case the default handler is modified */
        fa->mem_handler = PyDataMem_GetHandler();
        if (fa->mem_handler == NULL) {
            return NULL;
        }
        fa->data = PyDataMem_UserNEW(num, PyArray_HANDLER(self));
        if (PyArray_DATA(self) == NULL) {
            Py_CLEAR(fa->mem_handler);
            return PyErr_NoMemory();