MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_FromFile

Function PyArray_FromFile

numpy/core/src/multiarray/ctors.c:3496–3558  ·  view source on GitHub ↗

NUMPY_API * * Given a ``FILE *`` pointer ``fp``, and a ``PyArray_Descr``, return an * array corresponding to the data encoded in that file. * * The reference to `dtype` is stolen (it is possible that the passed in * dtype is not held on to). * * The number of elements to read is given as ``num``; if it is < 0, then * then as many as possible are read. * * If ``sep`` is NULL or empty, th

Source from the content-addressed store, hash-verified

3494 * necessary is read by this routine.
3495 */
3496NPY_NO_EXPORT PyObject *
3497PyArray_FromFile(FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep)
3498{
3499 PyArrayObject *ret;
3500 size_t nread = 0;
3501
3502 if (dtype == NULL) {
3503 return NULL;
3504 }
3505
3506 if (PyDataType_REFCHK(dtype)) {
3507 PyErr_SetString(PyExc_ValueError,
3508 "Cannot read into object array");
3509 Py_DECREF(dtype);
3510 return NULL;
3511 }
3512 if (dtype->elsize == 0) {
3513 /* Nothing to read, just create an empty array of the requested type */
3514 return PyArray_NewFromDescr_int(
3515 &PyArray_Type, dtype,
3516 1, &num, NULL, NULL,
3517 0, NULL, NULL,
3518 _NPY_ARRAY_ALLOW_EMPTY_STRING);
3519 }
3520 if ((sep == NULL) || (strlen(sep) == 0)) {
3521 ret = array_fromfile_binary(fp, dtype, num, &nread);
3522 }
3523 else {
3524 if (dtype->f->scanfunc == NULL) {
3525 PyErr_SetString(PyExc_ValueError,
3526 "Unable to read character files of that array type");
3527 Py_DECREF(dtype);
3528 return NULL;
3529 }
3530 ret = array_from_text(dtype, num, sep, &nread, fp,
3531 (next_element) fromfile_next_element,
3532 (skip_separator) fromfile_skip_separator, NULL);
3533 }
3534 if (ret == NULL) {
3535 Py_DECREF(dtype);
3536 return NULL;
3537 }
3538 if (((npy_intp) nread) < num) {
3539 /*
3540 * Realloc memory for smaller number of elements, use original dtype
3541 * which may have include a subarray (and is used for `nread`).
3542 */
3543 const size_t nsize = PyArray_MAX(nread,1) * dtype->elsize;
3544 char *tmp;
3545
3546 /* The handler is always valid */
3547 if((tmp = PyDataMem_UserRENEW(PyArray_DATA(ret), nsize,
3548 PyArray_HANDLER(ret))) == NULL) {
3549 Py_DECREF(dtype);
3550 Py_DECREF(ret);
3551 return PyErr_NoMemory();
3552 }
3553 ((PyArrayObject_fields *)ret)->data = tmp;

Callers 1

array_fromfileFunction · 0.85

Calls 7

PyArray_NewFromDescr_intFunction · 0.85
array_fromfile_binaryFunction · 0.85
array_from_textFunction · 0.85
PyDataMem_UserRENEWFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_HANDLERFunction · 0.85
PyArray_DIMSFunction · 0.85

Tested by

no test coverage detected