NUMPY_API To File */
| 130 | To File |
| 131 | */ |
| 132 | NPY_NO_EXPORT int |
| 133 | PyArray_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; |
no test coverage detected