1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "arrayobject.h"
#include "arrayconvert.h"
/*NUMPY_API
* To List
*/
NPY_NO_EXPORT PyObject *
PyArray_ToList(PyArrayObject *self)
{
PyObject *lp;
PyArrayObject *v;
intp sz, i;
if (!PyArray_Check(self)) {
return (PyObject *)self;
}
if (self->nd == 0) {
return self->descr->f->getitem(self->data,self);
}
sz = self->dimensions[0];
lp = PyList_New(sz);
for (i = 0; i < sz; i++) {
v = (PyArrayObject *)array_big_item(self, i);
if (PyArray_Check(v) && (v->nd >= self->nd)) {
PyErr_SetString(PyExc_RuntimeError,
"array_item not returning smaller-" \
"dimensional array");
Py_DECREF(v);
Py_DECREF(lp);
return NULL;
}
PyList_SetItem(lp, i, PyArray_ToList(v));
Py_DECREF(v);
}
return lp;
}
/* XXX: FIXME --- add ordering argument to
Allow Fortran ordering on write
This will need the addition of a Fortran-order iterator.
*/
/*NUMPY_API
To File
*/
NPY_NO_EXPORT int
PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
{
intp size;
intp n, n2;
size_t n3, n4;
PyArrayIterObject *it;
PyObject *obj, *strobj, *tupobj;
n3 = (sep ? strlen((const char *)sep) : 0);
if (n3 == 0) {
/* binary data */
if (PyDataType_FLAGCHK(self->descr, NPY_LIST_PICKLE)) {
PyErr_SetString(PyExc_ValueError, "cannot write " \
"object arrays to a file in " \
"binary mode");
return -1;
}
if (PyArray_ISCONTIGUOUS(self)) {
size = PyArray_SIZE(self);
NPY_BEGIN_ALLOW_THREADS;
n = fwrite((const void *)self->data,
(size_t) self->descr->elsize,
(size_t) size, fp);
NPY_END_ALLOW_THREADS;
if (n < size) {
PyErr_Format(PyExc_ValueError,
"%ld requested and %ld written",
(long) size, (long) n);
return -1;
}
}
else {
NPY_BEGIN_THREADS_DEF;
it = (PyArrayIterObject *) PyArray_IterNew((PyObject *)self);
NPY_BEGIN_THREADS;
while (it->index < it->size) {
if (fwrite((const void *)it->dataptr,
(size_t) self->descr->elsize,
1, fp) < 1) {
NPY_END_THREADS;
PyErr_Format(PyExc_IOError,
"problem writing element"\
" %"INTP_FMT" to file",
it->index);
Py_DECREF(it);
return -1;
}
PyArray_ITER_NEXT(it);
}
NPY_END_THREADS;
Py_DECREF(it);
}
}
else {
/*
* text data
*/
it = (PyArrayIterObject *)
PyArray_IterNew((PyObject *)self);
n4 = (format ? strlen((const char *)format) : 0);
while (it->index < it->size) {
obj = self->descr->f->getitem(it->dataptr, self);
if (obj == NULL) {
Py_DECREF(it);
return -1;
}
if (n4 == 0) {
/*
* standard writing
*/
strobj = PyObject_Str(obj);
Py_DECREF(obj);
if (strobj == NULL) {
Py_DECREF(it);
return -1;
}
}
else {
/*
* use format string
*/
tupobj = PyTuple_New(1);
if (tupobj == NULL) {
Py_DECREF(it);
return -1;
}
PyTuple_SET_ITEM(tupobj,0,obj);
obj = PyString_FromString((const char *)format);
if (obj == NULL) {
Py_DECREF(tupobj);
Py_DECREF(it);
return -1;
}
strobj = PyString_Format(obj, tupobj);
Py_DECREF(obj);
Py_DECREF(tupobj);
if (strobj == NULL) {
Py_DECREF(it);
return -1;
}
}
NPY_BEGIN_ALLOW_THREADS;
n2 = PyString_GET_SIZE(strobj);
n = fwrite(PyString_AS_STRING(strobj), 1, n2, fp);
NPY_END_ALLOW_THREADS;
if (n < n2) {
PyErr_Format(PyExc_IOError,
"problem writing element %"INTP_FMT\
" to file", it->index);
Py_DECREF(strobj);
Py_DECREF(it);
return -1;
}
/* write separator for all but last one */
if (it->index != it->size-1) {
if (fwrite(sep, 1, n3, fp) < n3) {
PyErr_Format(PyExc_IOError,
"problem writing "\
"separator to file");
Py_DECREF(strobj);
Py_DECREF(it);
return -1;
}
}
Py_DECREF(strobj);
PyArray_ITER_NEXT(it);
}
Py_DECREF(it);
}
return 0;
}
/*NUMPY_API*/
NPY_NO_EXPORT PyObject *
PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
{
intp numbytes;
intp index;
char *dptr;
int elsize;
PyObject *ret;
PyArrayIterObject *it;
if (order == NPY_ANYORDER)
order = PyArray_ISFORTRAN(self);
/* if (PyArray_TYPE(self) == PyArray_OBJECT) {
PyErr_SetString(PyExc_ValueError, "a string for the data" \
"in an object array is not appropriate");
return NULL;
}
*/
numbytes = PyArray_NBYTES(self);
if ((PyArray_ISCONTIGUOUS(self) && (order == NPY_CORDER))
|| (PyArray_ISFORTRAN(self) && (order == NPY_FORTRANORDER))) {
ret = PyString_FromStringAndSize(self->data, (Py_ssize_t) numbytes);
}
else {
PyObject *new;
if (order == NPY_FORTRANORDER) {
/* iterators are always in C-order */
new = PyArray_Transpose(self, NULL);
if (new == NULL) {
return NULL;
}
}
else {
Py_INCREF(self);
new = (PyObject *)self;
}
it = (PyArrayIterObject *)PyArray_IterNew(new);
Py_DECREF(new);
if (it == NULL) {
return NULL;
}
ret = PyString_FromStringAndSize(NULL, (Py_ssize_t) numbytes);
if (ret == NULL) {
Py_DECREF(it);
return NULL;
}
dptr = PyString_AS_STRING(ret);
index = it->size;
elsize = self->descr->elsize;
while (index--) {
memcpy(dptr, it->dataptr, elsize);
dptr += elsize;
PyArray_ITER_NEXT(it);
}
Py_DECREF(it);
}
return ret;
}
/*NUMPY_API*/
NPY_NO_EXPORT int
PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
{
PyObject *newarr;
int itemsize, swap;
void *fromptr;
PyArray_Descr *descr;
intp size;
PyArray_CopySwapFunc *copyswap;
itemsize = arr->descr->elsize;
if (PyArray_ISOBJECT(arr)) {
fromptr = &obj;
swap = 0;
newarr = NULL;
}
else {
descr = PyArray_DESCR(arr);
Py_INCREF(descr);
newarr = PyArray_FromAny(obj, descr, 0,0, ALIGNED, NULL);
if (newarr == NULL) {
return -1;
}
fromptr = PyArray_DATA(newarr);
swap = (PyArray_ISNOTSWAPPED(arr) != PyArray_ISNOTSWAPPED(newarr));
}
size=PyArray_SIZE(arr);
copyswap = arr->descr->f->copyswap;
if (PyArray_ISONESEGMENT(arr)) {
char *toptr=PyArray_DATA(arr);
PyArray_FillWithScalarFunc* fillwithscalar =
arr->descr->f->fillwithscalar;
if (fillwithscalar && PyArray_ISALIGNED(arr)) {
copyswap(fromptr, NULL, swap, newarr);
fillwithscalar(toptr, size, fromptr, arr);
}
else {
while (size--) {
copyswap(toptr, fromptr, swap, arr);
toptr += itemsize;
}
}
}
else {
PyArrayIterObject *iter;
iter = (PyArrayIterObject *)\
PyArray_IterNew((PyObject *)arr);
if (iter == NULL) {
Py_XDECREF(newarr);
return -1;
}
while (size--) {
copyswap(iter->dataptr, fromptr, swap, arr);
PyArray_ITER_NEXT(iter);
}
Py_DECREF(iter);
}
Py_XDECREF(newarr);
return 0;
}
/*NUMPY_API
Copy an array.
*/
NPY_NO_EXPORT PyObject *
PyArray_NewCopy(PyArrayObject *m1, NPY_ORDER fortran)
{
PyArrayObject *ret;
if (fortran == PyArray_ANYORDER)
fortran = PyArray_ISFORTRAN(m1);
Py_INCREF(m1->descr);
ret = (PyArrayObject *)PyArray_NewFromDescr(m1->ob_type,
m1->descr,
m1->nd,
m1->dimensions,
NULL, NULL,
fortran,
(PyObject *)m1);
if (ret == NULL) {
return NULL;
}
if (PyArray_CopyInto(ret, m1) == -1) {
Py_DECREF(ret);
return NULL;
}
return (PyObject *)ret;
}
/*NUMPY_API
* View
* steals a reference to type -- accepts NULL
*/
NPY_NO_EXPORT PyObject *
PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype)
{
PyObject *new = NULL;
PyTypeObject *subtype;
if (pytype) {
subtype = pytype;
}
else {
subtype = self->ob_type;
}
Py_INCREF(self->descr);
new = PyArray_NewFromDescr(subtype,
self->descr,
self->nd, self->dimensions,
self->strides,
self->data,
self->flags, (PyObject *)self);
if (new == NULL) {
return NULL;
}
Py_INCREF(self);
PyArray_BASE(new) = (PyObject *)self;
if (type != NULL) {
if (PyObject_SetAttrString(new, "dtype",
(PyObject *)type) < 0) {
Py_DECREF(new);
Py_DECREF(type);
return NULL;
}
Py_DECREF(type);
}
return new;
}
|