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

Function array_fromfile

numpy/core/src/multiarray/multiarraymodule.c:2380–2470  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2378
2379
2380static PyObject *
2381array_fromfile(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *keywds)
2382{
2383 PyObject *file = NULL, *ret = NULL;
2384 PyObject *err_type = NULL, *err_value = NULL, *err_traceback = NULL;
2385 char *sep = "";
2386 Py_ssize_t nin = -1;
2387 static char *kwlist[] = {"file", "dtype", "count", "sep", "offset", "like", NULL};
2388 PyObject *like = Py_None;
2389 PyArray_Descr *type = NULL;
2390 int own;
2391 npy_off_t orig_pos = 0, offset = 0;
2392 FILE *fp;
2393
2394 if (!PyArg_ParseTupleAndKeywords(args, keywds,
2395 "O|O&" NPY_SSIZE_T_PYFMT "s" NPY_OFF_T_PYFMT "$O:fromfile", kwlist,
2396 &file, PyArray_DescrConverter, &type, &nin, &sep, &offset, &like)) {
2397 Py_XDECREF(type);
2398 return NULL;
2399 }
2400
2401 if (like != Py_None) {
2402 PyObject *deferred = array_implement_c_array_function_creation(
2403 "fromfile", like, args, keywds, NULL, 0, NULL);
2404 if (deferred != Py_NotImplemented) {
2405 Py_XDECREF(type);
2406 return deferred;
2407 }
2408 }
2409
2410 file = NpyPath_PathlikeToFspath(file);
2411 if (file == NULL) {
2412 Py_XDECREF(type);
2413 return NULL;
2414 }
2415
2416 if (offset != 0 && strcmp(sep, "") != 0) {
2417 PyErr_SetString(PyExc_TypeError, "'offset' argument only permitted for binary files");
2418 Py_XDECREF(type);
2419 Py_DECREF(file);
2420 return NULL;
2421 }
2422 if (PyBytes_Check(file) || PyUnicode_Check(file)) {
2423 Py_SETREF(file, npy_PyFile_OpenFile(file, "rb"));
2424 if (file == NULL) {
2425 Py_XDECREF(type);
2426 return NULL;
2427 }
2428 own = 1;
2429 }
2430 else {
2431 own = 0;
2432 }
2433 fp = npy_PyFile_Dup2(file, "rb", &orig_pos);
2434 if (fp == NULL) {
2435 Py_DECREF(file);
2436 Py_XDECREF(type);
2437 return NULL;

Callers

nothing calls this directly

Calls 8

NpyPath_PathlikeToFspathFunction · 0.85
npy_PyFile_OpenFileFunction · 0.85
npy_PyFile_Dup2Function · 0.85
PyArray_FromFileFunction · 0.85
npy_PyFile_DupClose2Function · 0.85
npy_PyFile_CloseFileFunction · 0.85

Tested by

no test coverage detected