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

Function npy_PyFile_DupClose2

numpy/core/include/numpy/npy_3kcompat.h:331–396  ·  view source on GitHub ↗

* Close the dup-ed file handle, and seek the Python one to the current position */

Source from the content-addressed store, hash-verified

329 * Close the dup-ed file handle, and seek the Python one to the current position
330 */
331static inline int
332npy_PyFile_DupClose2(PyObject *file, FILE* handle, npy_off_t orig_pos)
333{
334 int fd, unbuf;
335 PyObject *ret, *io, *io_raw;
336 npy_off_t position;
337
338 /* For Python 2 PyFileObject, do nothing */
339#if !defined(NPY_PY3K)
340 if (PyFile_Check(file)) {
341 return 0;
342 }
343#endif
344
345 position = npy_ftell(handle);
346
347 /* Close the FILE* handle */
348 fclose(handle);
349
350 /*
351 * Restore original file handle position, in order to not confuse
352 * Python-side data structures
353 */
354 fd = PyObject_AsFileDescriptor(file);
355 if (fd == -1) {
356 return -1;
357 }
358
359 if (npy_lseek(fd, orig_pos, SEEK_SET) == -1) {
360
361 /* The io module is needed to determine if buffering is used */
362 io = PyImport_ImportModule("io");
363 if (io == NULL) {
364 return -1;
365 }
366 /* File object instances of RawIOBase are unbuffered */
367 io_raw = PyObject_GetAttrString(io, "RawIOBase");
368 Py_DECREF(io);
369 if (io_raw == NULL) {
370 return -1;
371 }
372 unbuf = PyObject_IsInstance(file, io_raw);
373 Py_DECREF(io_raw);
374 if (unbuf == 1) {
375 /* Succeed if the IO is unbuffered */
376 return 0;
377 }
378 else {
379 PyErr_SetString(PyExc_IOError, "seeking file failed");
380 return -1;
381 }
382 }
383
384 if (position == -1) {
385 PyErr_SetString(PyExc_IOError, "obtaining file position failed");
386 return -1;
387 }
388

Callers 2

PyArray_ToFileObjectFunction · 0.85
array_fromfileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected