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

Function PyArray_ToFile

numpy/core/src/multiarray/convert.c:132–320  ·  view source on GitHub ↗

NUMPY_API To File */

Source from the content-addressed store, hash-verified

130 To File
131*/
132NPY_NO_EXPORT int
133PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
134{
135 npy_intp size;
136 npy_intp n, n2;
137 size_t n3, n4;
138 PyArrayIterObject *it;
139 PyObject *obj, *strobj, *tupobj, *byteobj;
140
141 n3 = (sep ? strlen((const char *)sep) : 0);
142 if (n3 == 0) {
143 /* binary data */
144 if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_LIST_PICKLE)) {
145 PyErr_SetString(PyExc_OSError,
146 "cannot write object arrays to a file in binary mode");
147 return -1;
148 }
149 if (PyArray_DESCR(self)->elsize == 0) {
150 /* For zero-width data types there's nothing to write */
151 return 0;
152 }
153 if (npy_fallocate(PyArray_NBYTES(self), fp) != 0) {
154 return -1;
155 }
156
157 if (PyArray_ISCONTIGUOUS(self)) {
158 size = PyArray_SIZE(self);
159 NPY_BEGIN_ALLOW_THREADS;
160
161#if defined(_WIN64)
162 /*
163 * Workaround Win64 fwrite() bug. Issue gh-2256
164 * The native 64 windows runtime has this issue, the above will
165 * also trigger UCRT (which doesn't), so it could be more precise.
166 *
167 * If you touch this code, please run this test which is so slow
168 * it was removed from the test suite. Note that the original
169 * failure mode involves an infinite loop during tofile()
170 *
171 * import tempfile, numpy as np
172 * from numpy.testing import (assert_)
173 * fourgbplus = 2**32 + 2**16
174 * testbytes = np.arange(8, dtype=np.int8)
175 * n = len(testbytes)
176 * flike = tempfile.NamedTemporaryFile()
177 * f = flike.file
178 * np.tile(testbytes, fourgbplus // testbytes.nbytes).tofile(f)
179 * flike.seek(0)
180 * a = np.fromfile(f, dtype=np.int8)
181 * flike.close()
182 * assert_(len(a) == fourgbplus)
183 * # check only start and end for speed:
184 * assert_((a[:n] == testbytes).all())
185 * assert_((a[-n:] == testbytes).all())
186 */
187 {
188 size_t maxsize = 2147483648 / (size_t)PyArray_DESCR(self)->elsize;
189 size_t chunksize;

Callers 1

PyArray_ToFileObjectFunction · 0.85

Calls 5

PyArray_DESCRFunction · 0.85
npy_fallocateFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_IterNewFunction · 0.85
PyArray_GETITEMFunction · 0.85

Tested by

no test coverage detected