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

Function array_from_text

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

Source from the content-addressed store, hash-verified

3361 */
3362#define FROM_BUFFER_SIZE 4096
3363static PyArrayObject *
3364array_from_text(PyArray_Descr *dtype, npy_intp num, char const *sep, size_t *nread,
3365 void *stream, next_element next, skip_separator skip_sep,
3366 void *stream_data)
3367{
3368 PyArrayObject *r;
3369 npy_intp i;
3370 char *dptr, *clean_sep, *tmp;
3371 int err = 0;
3372 int stop_reading_flag = 0; /* -1 means end reached; -2 a parsing error */
3373 npy_intp thisbuf = 0;
3374 npy_intp size;
3375 npy_intp bytes, totalbytes;
3376
3377 size = (num >= 0) ? num : FROM_BUFFER_SIZE;
3378
3379 /*
3380 * Array creation may move sub-array dimensions from the dtype to array
3381 * dimensions, so we need to use the original dtype when reading.
3382 */
3383 Py_INCREF(dtype);
3384
3385 r = (PyArrayObject *)
3386 PyArray_NewFromDescr(&PyArray_Type, dtype, 1, &size,
3387 NULL, NULL, 0, NULL);
3388 if (r == NULL) {
3389 return NULL;
3390 }
3391
3392 clean_sep = swab_separator(sep);
3393 if (clean_sep == NULL) {
3394 err = 1;
3395 goto fail;
3396 }
3397
3398 NPY_BEGIN_ALLOW_THREADS;
3399 totalbytes = bytes = size * dtype->elsize;
3400 dptr = PyArray_DATA(r);
3401 for (i = 0; num < 0 || i < num; i++) {
3402 stop_reading_flag = next(&stream, dptr, dtype, stream_data);
3403 if (stop_reading_flag < 0) {
3404 break;
3405 }
3406 *nread += 1;
3407 thisbuf += 1;
3408 dptr += dtype->elsize;
3409 if (num < 0 && thisbuf == size) {
3410 totalbytes += bytes;
3411 /* The handler is always valid */
3412 tmp = PyDataMem_UserRENEW(PyArray_DATA(r), totalbytes,
3413 PyArray_HANDLER(r));
3414 if (tmp == NULL) {
3415 err = 1;
3416 break;
3417 }
3418 ((PyArrayObject_fields *)r)->data = tmp;
3419 dptr = tmp + (totalbytes - bytes);
3420 thisbuf = 0;

Callers 2

PyArray_FromFileFunction · 0.85
PyArray_FromStringFunction · 0.85

Calls 7

PyArray_NewFromDescrFunction · 0.85
swab_separatorFunction · 0.85
PyArray_DATAFunction · 0.85
PyDataMem_UserRENEWFunction · 0.85
PyArray_HANDLERFunction · 0.85
PyArray_DIMSFunction · 0.85
nextFunction · 0.50

Tested by

no test coverage detected