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

Code before:
    if ((sep == NULL) || (strlen(sep) == 0)) {
        ret = array_fromfile_binary(fp, dtype, num, &nread);
    }
    else {
        if (PyDataType_GetArrFuncs(dtype)->scanfunc == NULL) {
            PyErr_SetString(PyExc_ValueError,
                    "Unable to read character files of that array type");
            Py_DECREF(dtype);
            return NULL;
        }
        ret = array_from_text(dtype, num, sep, &nread, fp,
                (next_element) fromfile_next_element,
                (skip_separator) fromfile_skip_separator, NULL);
    }
    if (ret == NULL) {
        Py_DECREF(dtype);
        return NULL;
    }
    if (((npy_intp) nread) < num) {
        /*
         * Resize array to smaller number of elements. Note that original
         * dtype may have included a subarray, so we may not be 1-d.
         */
        npy_intp *dims = npy_alloc_cache_dim(PyArray_NDIM(ret));
        dims[0] = (npy_intp)nread;
        for (int i = 1; i < PyArray_NDIM(ret); i++) {
            dims[i] = PyArray_DIMS(ret)[i];
        }
        PyArray_Dims new_dims = {dims, PyArray_NDIM(ret)};
        PyObject *tmp = PyArray_Resize(ret, &new_dims, 0, 0);
        npy_free_cache_dim_obj(new_dims);
        if(tmp == NULL) {
            Py_DECREF(dtype);
            Py_DECREF(ret);
            return NULL;
        }
    }
    Py_DECREF(dtype);
    return (PyObject *)ret;
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type,
                   npy_intp count, npy_intp offset)
{
    PyArrayObject *ret;
    char *data;
    Py_buffer view;
    Py_ssize_t ts;
    npy_intp s, n;

Review comment (refactor):
Hmmm, seems a bit inconvenient here with the size changes, might a more raw resize helper make it nicer?
(I.e. something internal that `PyArray_Resize()` itself just calls.)

Maybe that doesn't make sense... Although just the fact that `PyArray_Resize` returns None (and uses that `PyArray_Dims` struct?) might be worth a helper?

Code after:
    }
    if ((sep == NULL) || (strlen(sep) == 0)) {
        ret = array_fromfile_binary(fp, dtype, num, &nread);
    }
    else {
        if (PyDataType_GetArrFuncs(dtype)->scanfunc == NULL) {
            PyErr_SetString(PyExc_ValueError,
                    "Unable to read character files of that array type");
            Py_DECREF(dtype);
            return NULL;
        }
        ret = array_from_text(dtype, num, sep, &nread, fp,
                (next_element) fromfile_next_element,
                (skip_separator) fromfile_skip_separator, NULL);
    }
    if (ret == NULL) {
        Py_DECREF(dtype);
        return NULL;
    }
    if (((npy_intp) nread) < num) {
        /*
         * Resize array to smaller number of elements. Note that original
         * dtype may have included a subarray, so we may not be 1-d.
         */
        npy_intp dims[NPY_MAXDIMS];
        dims[0] = (npy_intp)nread;
        for (int i = 1; i < PyArray_NDIM(ret); i++) {
            dims[i] = PyArray_DIMS(ret)[i];
        }
        PyArray_Dims new_dims = {dims, PyArray_NDIM(ret)};
        if (PyArray_Resize_int(ret, &new_dims, 0) < 0) {
            Py_DECREF(dtype);
            Py_DECREF(ret);
            return NULL;
        }
    }
    Py_DECREF(dtype);
    return (PyObject *)ret;
}

/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type,
                   npy_intp count, npy_intp offset)
{
    PyArrayObject *ret;
    char *data;
    Py_buffer view;
    Py_ssize_t ts;
    npy_intp s, n;
    int itemsize;