1 | n/a | /* C Extension module to test all aspects of PEP-3118. |
---|
2 | n/a | Written by Stefan Krah. */ |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | #define PY_SSIZE_T_CLEAN |
---|
6 | n/a | |
---|
7 | n/a | #include "Python.h" |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | /* struct module */ |
---|
11 | n/a | PyObject *structmodule = NULL; |
---|
12 | n/a | PyObject *Struct = NULL; |
---|
13 | n/a | PyObject *calcsize = NULL; |
---|
14 | n/a | |
---|
15 | n/a | /* cache simple format string */ |
---|
16 | n/a | static const char *simple_fmt = "B"; |
---|
17 | n/a | PyObject *simple_format = NULL; |
---|
18 | n/a | #define SIMPLE_FORMAT(fmt) (fmt == NULL || strcmp(fmt, "B") == 0) |
---|
19 | n/a | #define FIX_FORMAT(fmt) (fmt == NULL ? "B" : fmt) |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | /**************************************************************************/ |
---|
23 | n/a | /* NDArray Object */ |
---|
24 | n/a | /**************************************************************************/ |
---|
25 | n/a | |
---|
26 | n/a | static PyTypeObject NDArray_Type; |
---|
27 | n/a | #define NDArray_Check(v) (Py_TYPE(v) == &NDArray_Type) |
---|
28 | n/a | |
---|
29 | n/a | #define CHECK_LIST_OR_TUPLE(v) \ |
---|
30 | n/a | if (!PyList_Check(v) && !PyTuple_Check(v)) { \ |
---|
31 | n/a | PyErr_SetString(PyExc_TypeError, \ |
---|
32 | n/a | #v " must be a list or a tuple"); \ |
---|
33 | n/a | return NULL; \ |
---|
34 | n/a | } \ |
---|
35 | n/a | |
---|
36 | n/a | #define PyMem_XFree(v) \ |
---|
37 | n/a | do { if (v) PyMem_Free(v); } while (0) |
---|
38 | n/a | |
---|
39 | n/a | /* Maximum number of dimensions. */ |
---|
40 | n/a | #define ND_MAX_NDIM (2 * PyBUF_MAX_NDIM) |
---|
41 | n/a | |
---|
42 | n/a | /* Check for the presence of suboffsets in the first dimension. */ |
---|
43 | n/a | #define HAVE_PTR(suboffsets) (suboffsets && suboffsets[0] >= 0) |
---|
44 | n/a | /* Adjust ptr if suboffsets are present. */ |
---|
45 | n/a | #define ADJUST_PTR(ptr, suboffsets) \ |
---|
46 | n/a | (HAVE_PTR(suboffsets) ? *((char**)ptr) + suboffsets[0] : ptr) |
---|
47 | n/a | |
---|
48 | n/a | /* Default: NumPy style (strides), read-only, no var-export, C-style layout */ |
---|
49 | n/a | #define ND_DEFAULT 0x000 |
---|
50 | n/a | /* User configurable flags for the ndarray */ |
---|
51 | n/a | #define ND_VAREXPORT 0x001 /* change layout while buffers are exported */ |
---|
52 | n/a | /* User configurable flags for each base buffer */ |
---|
53 | n/a | #define ND_WRITABLE 0x002 /* mark base buffer as writable */ |
---|
54 | n/a | #define ND_FORTRAN 0x004 /* Fortran contiguous layout */ |
---|
55 | n/a | #define ND_SCALAR 0x008 /* scalar: ndim = 0 */ |
---|
56 | n/a | #define ND_PIL 0x010 /* convert to PIL-style array (suboffsets) */ |
---|
57 | n/a | #define ND_REDIRECT 0x020 /* redirect buffer requests */ |
---|
58 | n/a | #define ND_GETBUF_FAIL 0x040 /* trigger getbuffer failure */ |
---|
59 | n/a | #define ND_GETBUF_UNDEFINED 0x080 /* undefined view.obj */ |
---|
60 | n/a | /* Internal flags for the base buffer */ |
---|
61 | n/a | #define ND_C 0x100 /* C contiguous layout (default) */ |
---|
62 | n/a | #define ND_OWN_ARRAYS 0x200 /* consumer owns arrays */ |
---|
63 | n/a | |
---|
64 | n/a | /* ndarray properties */ |
---|
65 | n/a | #define ND_IS_CONSUMER(nd) \ |
---|
66 | n/a | (((NDArrayObject *)nd)->head == &((NDArrayObject *)nd)->staticbuf) |
---|
67 | n/a | |
---|
68 | n/a | /* ndbuf->flags properties */ |
---|
69 | n/a | #define ND_C_CONTIGUOUS(flags) (!!(flags&(ND_SCALAR|ND_C))) |
---|
70 | n/a | #define ND_FORTRAN_CONTIGUOUS(flags) (!!(flags&(ND_SCALAR|ND_FORTRAN))) |
---|
71 | n/a | #define ND_ANY_CONTIGUOUS(flags) (!!(flags&(ND_SCALAR|ND_C|ND_FORTRAN))) |
---|
72 | n/a | |
---|
73 | n/a | /* getbuffer() requests */ |
---|
74 | n/a | #define REQ_INDIRECT(flags) ((flags&PyBUF_INDIRECT) == PyBUF_INDIRECT) |
---|
75 | n/a | #define REQ_C_CONTIGUOUS(flags) ((flags&PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) |
---|
76 | n/a | #define REQ_F_CONTIGUOUS(flags) ((flags&PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) |
---|
77 | n/a | #define REQ_ANY_CONTIGUOUS(flags) ((flags&PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS) |
---|
78 | n/a | #define REQ_STRIDES(flags) ((flags&PyBUF_STRIDES) == PyBUF_STRIDES) |
---|
79 | n/a | #define REQ_SHAPE(flags) ((flags&PyBUF_ND) == PyBUF_ND) |
---|
80 | n/a | #define REQ_WRITABLE(flags) (flags&PyBUF_WRITABLE) |
---|
81 | n/a | #define REQ_FORMAT(flags) (flags&PyBUF_FORMAT) |
---|
82 | n/a | |
---|
83 | n/a | |
---|
84 | n/a | /* Single node of a list of base buffers. The list is needed to implement |
---|
85 | n/a | changes in memory layout while exported buffers are active. */ |
---|
86 | n/a | static PyTypeObject NDArray_Type; |
---|
87 | n/a | |
---|
88 | n/a | struct ndbuf; |
---|
89 | n/a | typedef struct ndbuf { |
---|
90 | n/a | struct ndbuf *next; |
---|
91 | n/a | struct ndbuf *prev; |
---|
92 | n/a | Py_ssize_t len; /* length of data */ |
---|
93 | n/a | Py_ssize_t offset; /* start of the array relative to data */ |
---|
94 | n/a | char *data; /* raw data */ |
---|
95 | n/a | int flags; /* capabilities of the base buffer */ |
---|
96 | n/a | Py_ssize_t exports; /* number of exports */ |
---|
97 | n/a | Py_buffer base; /* base buffer */ |
---|
98 | n/a | } ndbuf_t; |
---|
99 | n/a | |
---|
100 | n/a | typedef struct { |
---|
101 | n/a | PyObject_HEAD |
---|
102 | n/a | int flags; /* ndarray flags */ |
---|
103 | n/a | ndbuf_t staticbuf; /* static buffer for re-exporting mode */ |
---|
104 | n/a | ndbuf_t *head; /* currently active base buffer */ |
---|
105 | n/a | } NDArrayObject; |
---|
106 | n/a | |
---|
107 | n/a | |
---|
108 | n/a | static ndbuf_t * |
---|
109 | n/a | ndbuf_new(Py_ssize_t nitems, Py_ssize_t itemsize, Py_ssize_t offset, int flags) |
---|
110 | n/a | { |
---|
111 | n/a | ndbuf_t *ndbuf; |
---|
112 | n/a | Py_buffer *base; |
---|
113 | n/a | Py_ssize_t len; |
---|
114 | n/a | |
---|
115 | n/a | len = nitems * itemsize; |
---|
116 | n/a | if (offset % itemsize) { |
---|
117 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
118 | n/a | "offset must be a multiple of itemsize"); |
---|
119 | n/a | return NULL; |
---|
120 | n/a | } |
---|
121 | n/a | if (offset < 0 || offset+itemsize > len) { |
---|
122 | n/a | PyErr_SetString(PyExc_ValueError, "offset out of bounds"); |
---|
123 | n/a | return NULL; |
---|
124 | n/a | } |
---|
125 | n/a | |
---|
126 | n/a | ndbuf = PyMem_Malloc(sizeof *ndbuf); |
---|
127 | n/a | if (ndbuf == NULL) { |
---|
128 | n/a | PyErr_NoMemory(); |
---|
129 | n/a | return NULL; |
---|
130 | n/a | } |
---|
131 | n/a | |
---|
132 | n/a | ndbuf->next = NULL; |
---|
133 | n/a | ndbuf->prev = NULL; |
---|
134 | n/a | ndbuf->len = len; |
---|
135 | n/a | ndbuf->offset= offset; |
---|
136 | n/a | |
---|
137 | n/a | ndbuf->data = PyMem_Malloc(len); |
---|
138 | n/a | if (ndbuf->data == NULL) { |
---|
139 | n/a | PyErr_NoMemory(); |
---|
140 | n/a | PyMem_Free(ndbuf); |
---|
141 | n/a | return NULL; |
---|
142 | n/a | } |
---|
143 | n/a | |
---|
144 | n/a | ndbuf->flags = flags; |
---|
145 | n/a | ndbuf->exports = 0; |
---|
146 | n/a | |
---|
147 | n/a | base = &ndbuf->base; |
---|
148 | n/a | base->obj = NULL; |
---|
149 | n/a | base->buf = ndbuf->data; |
---|
150 | n/a | base->len = len; |
---|
151 | n/a | base->itemsize = 1; |
---|
152 | n/a | base->readonly = 0; |
---|
153 | n/a | base->format = NULL; |
---|
154 | n/a | base->ndim = 1; |
---|
155 | n/a | base->shape = NULL; |
---|
156 | n/a | base->strides = NULL; |
---|
157 | n/a | base->suboffsets = NULL; |
---|
158 | n/a | base->internal = ndbuf; |
---|
159 | n/a | |
---|
160 | n/a | return ndbuf; |
---|
161 | n/a | } |
---|
162 | n/a | |
---|
163 | n/a | static void |
---|
164 | n/a | ndbuf_free(ndbuf_t *ndbuf) |
---|
165 | n/a | { |
---|
166 | n/a | Py_buffer *base = &ndbuf->base; |
---|
167 | n/a | |
---|
168 | n/a | PyMem_XFree(ndbuf->data); |
---|
169 | n/a | PyMem_XFree(base->format); |
---|
170 | n/a | PyMem_XFree(base->shape); |
---|
171 | n/a | PyMem_XFree(base->strides); |
---|
172 | n/a | PyMem_XFree(base->suboffsets); |
---|
173 | n/a | |
---|
174 | n/a | PyMem_Free(ndbuf); |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | static void |
---|
178 | n/a | ndbuf_push(NDArrayObject *nd, ndbuf_t *elt) |
---|
179 | n/a | { |
---|
180 | n/a | elt->next = nd->head; |
---|
181 | n/a | if (nd->head) nd->head->prev = elt; |
---|
182 | n/a | nd->head = elt; |
---|
183 | n/a | elt->prev = NULL; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | static void |
---|
187 | n/a | ndbuf_delete(NDArrayObject *nd, ndbuf_t *elt) |
---|
188 | n/a | { |
---|
189 | n/a | if (elt->prev) |
---|
190 | n/a | elt->prev->next = elt->next; |
---|
191 | n/a | else |
---|
192 | n/a | nd->head = elt->next; |
---|
193 | n/a | |
---|
194 | n/a | if (elt->next) |
---|
195 | n/a | elt->next->prev = elt->prev; |
---|
196 | n/a | |
---|
197 | n/a | ndbuf_free(elt); |
---|
198 | n/a | } |
---|
199 | n/a | |
---|
200 | n/a | static void |
---|
201 | n/a | ndbuf_pop(NDArrayObject *nd) |
---|
202 | n/a | { |
---|
203 | n/a | ndbuf_delete(nd, nd->head); |
---|
204 | n/a | } |
---|
205 | n/a | |
---|
206 | n/a | |
---|
207 | n/a | static PyObject * |
---|
208 | n/a | ndarray_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
209 | n/a | { |
---|
210 | n/a | NDArrayObject *nd; |
---|
211 | n/a | |
---|
212 | n/a | nd = PyObject_New(NDArrayObject, &NDArray_Type); |
---|
213 | n/a | if (nd == NULL) |
---|
214 | n/a | return NULL; |
---|
215 | n/a | |
---|
216 | n/a | nd->flags = 0; |
---|
217 | n/a | nd->head = NULL; |
---|
218 | n/a | return (PyObject *)nd; |
---|
219 | n/a | } |
---|
220 | n/a | |
---|
221 | n/a | static void |
---|
222 | n/a | ndarray_dealloc(NDArrayObject *self) |
---|
223 | n/a | { |
---|
224 | n/a | if (self->head) { |
---|
225 | n/a | if (ND_IS_CONSUMER(self)) { |
---|
226 | n/a | Py_buffer *base = &self->head->base; |
---|
227 | n/a | if (self->head->flags & ND_OWN_ARRAYS) { |
---|
228 | n/a | PyMem_XFree(base->shape); |
---|
229 | n/a | PyMem_XFree(base->strides); |
---|
230 | n/a | PyMem_XFree(base->suboffsets); |
---|
231 | n/a | } |
---|
232 | n/a | PyBuffer_Release(base); |
---|
233 | n/a | } |
---|
234 | n/a | else { |
---|
235 | n/a | while (self->head) |
---|
236 | n/a | ndbuf_pop(self); |
---|
237 | n/a | } |
---|
238 | n/a | } |
---|
239 | n/a | PyObject_Del(self); |
---|
240 | n/a | } |
---|
241 | n/a | |
---|
242 | n/a | static int |
---|
243 | n/a | ndarray_init_staticbuf(PyObject *exporter, NDArrayObject *nd, int flags) |
---|
244 | n/a | { |
---|
245 | n/a | Py_buffer *base = &nd->staticbuf.base; |
---|
246 | n/a | |
---|
247 | n/a | if (PyObject_GetBuffer(exporter, base, flags) < 0) |
---|
248 | n/a | return -1; |
---|
249 | n/a | |
---|
250 | n/a | nd->head = &nd->staticbuf; |
---|
251 | n/a | |
---|
252 | n/a | nd->head->next = NULL; |
---|
253 | n/a | nd->head->prev = NULL; |
---|
254 | n/a | nd->head->len = -1; |
---|
255 | n/a | nd->head->offset = -1; |
---|
256 | n/a | nd->head->data = NULL; |
---|
257 | n/a | |
---|
258 | n/a | nd->head->flags = base->readonly ? 0 : ND_WRITABLE; |
---|
259 | n/a | nd->head->exports = 0; |
---|
260 | n/a | |
---|
261 | n/a | return 0; |
---|
262 | n/a | } |
---|
263 | n/a | |
---|
264 | n/a | static void |
---|
265 | n/a | init_flags(ndbuf_t *ndbuf) |
---|
266 | n/a | { |
---|
267 | n/a | if (ndbuf->base.ndim == 0) |
---|
268 | n/a | ndbuf->flags |= ND_SCALAR; |
---|
269 | n/a | if (ndbuf->base.suboffsets) |
---|
270 | n/a | ndbuf->flags |= ND_PIL; |
---|
271 | n/a | if (PyBuffer_IsContiguous(&ndbuf->base, 'C')) |
---|
272 | n/a | ndbuf->flags |= ND_C; |
---|
273 | n/a | if (PyBuffer_IsContiguous(&ndbuf->base, 'F')) |
---|
274 | n/a | ndbuf->flags |= ND_FORTRAN; |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | |
---|
278 | n/a | /****************************************************************************/ |
---|
279 | n/a | /* Buffer/List conversions */ |
---|
280 | n/a | /****************************************************************************/ |
---|
281 | n/a | |
---|
282 | n/a | static Py_ssize_t *strides_from_shape(const ndbuf_t *, int flags); |
---|
283 | n/a | |
---|
284 | n/a | /* Get number of members in a struct: see issue #12740 */ |
---|
285 | n/a | typedef struct { |
---|
286 | n/a | PyObject_HEAD |
---|
287 | n/a | Py_ssize_t s_size; |
---|
288 | n/a | Py_ssize_t s_len; |
---|
289 | n/a | } PyPartialStructObject; |
---|
290 | n/a | |
---|
291 | n/a | static Py_ssize_t |
---|
292 | n/a | get_nmemb(PyObject *s) |
---|
293 | n/a | { |
---|
294 | n/a | return ((PyPartialStructObject *)s)->s_len; |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | /* Pack all items into the buffer of 'obj'. The 'format' parameter must be |
---|
298 | n/a | in struct module syntax. For standard C types, a single item is an integer. |
---|
299 | n/a | For compound types, a single item is a tuple of integers. */ |
---|
300 | n/a | static int |
---|
301 | n/a | pack_from_list(PyObject *obj, PyObject *items, PyObject *format, |
---|
302 | n/a | Py_ssize_t itemsize) |
---|
303 | n/a | { |
---|
304 | n/a | PyObject *structobj, *pack_into; |
---|
305 | n/a | PyObject *args, *offset; |
---|
306 | n/a | PyObject *item, *tmp; |
---|
307 | n/a | Py_ssize_t nitems; /* number of items */ |
---|
308 | n/a | Py_ssize_t nmemb; /* number of members in a single item */ |
---|
309 | n/a | Py_ssize_t i, j; |
---|
310 | n/a | int ret = 0; |
---|
311 | n/a | |
---|
312 | n/a | assert(PyObject_CheckBuffer(obj)); |
---|
313 | n/a | assert(PyList_Check(items) || PyTuple_Check(items)); |
---|
314 | n/a | |
---|
315 | n/a | structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL); |
---|
316 | n/a | if (structobj == NULL) |
---|
317 | n/a | return -1; |
---|
318 | n/a | |
---|
319 | n/a | nitems = PySequence_Fast_GET_SIZE(items); |
---|
320 | n/a | nmemb = get_nmemb(structobj); |
---|
321 | n/a | assert(nmemb >= 1); |
---|
322 | n/a | |
---|
323 | n/a | pack_into = PyObject_GetAttrString(structobj, "pack_into"); |
---|
324 | n/a | if (pack_into == NULL) { |
---|
325 | n/a | Py_DECREF(structobj); |
---|
326 | n/a | return -1; |
---|
327 | n/a | } |
---|
328 | n/a | |
---|
329 | n/a | /* nmemb >= 1 */ |
---|
330 | n/a | args = PyTuple_New(2 + nmemb); |
---|
331 | n/a | if (args == NULL) { |
---|
332 | n/a | Py_DECREF(pack_into); |
---|
333 | n/a | Py_DECREF(structobj); |
---|
334 | n/a | return -1; |
---|
335 | n/a | } |
---|
336 | n/a | |
---|
337 | n/a | offset = NULL; |
---|
338 | n/a | for (i = 0; i < nitems; i++) { |
---|
339 | n/a | /* Loop invariant: args[j] are borrowed references or NULL. */ |
---|
340 | n/a | PyTuple_SET_ITEM(args, 0, obj); |
---|
341 | n/a | for (j = 1; j < 2+nmemb; j++) |
---|
342 | n/a | PyTuple_SET_ITEM(args, j, NULL); |
---|
343 | n/a | |
---|
344 | n/a | Py_XDECREF(offset); |
---|
345 | n/a | offset = PyLong_FromSsize_t(i*itemsize); |
---|
346 | n/a | if (offset == NULL) { |
---|
347 | n/a | ret = -1; |
---|
348 | n/a | break; |
---|
349 | n/a | } |
---|
350 | n/a | PyTuple_SET_ITEM(args, 1, offset); |
---|
351 | n/a | |
---|
352 | n/a | item = PySequence_Fast_GET_ITEM(items, i); |
---|
353 | n/a | if ((PyBytes_Check(item) || PyLong_Check(item) || |
---|
354 | n/a | PyFloat_Check(item)) && nmemb == 1) { |
---|
355 | n/a | PyTuple_SET_ITEM(args, 2, item); |
---|
356 | n/a | } |
---|
357 | n/a | else if ((PyList_Check(item) || PyTuple_Check(item)) && |
---|
358 | n/a | PySequence_Length(item) == nmemb) { |
---|
359 | n/a | for (j = 0; j < nmemb; j++) { |
---|
360 | n/a | tmp = PySequence_Fast_GET_ITEM(item, j); |
---|
361 | n/a | PyTuple_SET_ITEM(args, 2+j, tmp); |
---|
362 | n/a | } |
---|
363 | n/a | } |
---|
364 | n/a | else { |
---|
365 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
366 | n/a | "mismatch between initializer element and format string"); |
---|
367 | n/a | ret = -1; |
---|
368 | n/a | break; |
---|
369 | n/a | } |
---|
370 | n/a | |
---|
371 | n/a | tmp = PyObject_CallObject(pack_into, args); |
---|
372 | n/a | if (tmp == NULL) { |
---|
373 | n/a | ret = -1; |
---|
374 | n/a | break; |
---|
375 | n/a | } |
---|
376 | n/a | Py_DECREF(tmp); |
---|
377 | n/a | } |
---|
378 | n/a | |
---|
379 | n/a | Py_INCREF(obj); /* args[0] */ |
---|
380 | n/a | /* args[1]: offset is either NULL or should be dealloc'd */ |
---|
381 | n/a | for (i = 2; i < 2+nmemb; i++) { |
---|
382 | n/a | tmp = PyTuple_GET_ITEM(args, i); |
---|
383 | n/a | Py_XINCREF(tmp); |
---|
384 | n/a | } |
---|
385 | n/a | Py_DECREF(args); |
---|
386 | n/a | |
---|
387 | n/a | Py_DECREF(pack_into); |
---|
388 | n/a | Py_DECREF(structobj); |
---|
389 | n/a | return ret; |
---|
390 | n/a | |
---|
391 | n/a | } |
---|
392 | n/a | |
---|
393 | n/a | /* Pack single element */ |
---|
394 | n/a | static int |
---|
395 | n/a | pack_single(char *ptr, PyObject *item, const char *fmt, Py_ssize_t itemsize) |
---|
396 | n/a | { |
---|
397 | n/a | PyObject *structobj = NULL, *pack_into = NULL, *args = NULL; |
---|
398 | n/a | PyObject *format = NULL, *mview = NULL, *zero = NULL; |
---|
399 | n/a | Py_ssize_t i, nmemb; |
---|
400 | n/a | int ret = -1; |
---|
401 | n/a | PyObject *x; |
---|
402 | n/a | |
---|
403 | n/a | if (fmt == NULL) fmt = "B"; |
---|
404 | n/a | |
---|
405 | n/a | format = PyUnicode_FromString(fmt); |
---|
406 | n/a | if (format == NULL) |
---|
407 | n/a | goto out; |
---|
408 | n/a | |
---|
409 | n/a | structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL); |
---|
410 | n/a | if (structobj == NULL) |
---|
411 | n/a | goto out; |
---|
412 | n/a | |
---|
413 | n/a | nmemb = get_nmemb(structobj); |
---|
414 | n/a | assert(nmemb >= 1); |
---|
415 | n/a | |
---|
416 | n/a | mview = PyMemoryView_FromMemory(ptr, itemsize, PyBUF_WRITE); |
---|
417 | n/a | if (mview == NULL) |
---|
418 | n/a | goto out; |
---|
419 | n/a | |
---|
420 | n/a | zero = PyLong_FromLong(0); |
---|
421 | n/a | if (zero == NULL) |
---|
422 | n/a | goto out; |
---|
423 | n/a | |
---|
424 | n/a | pack_into = PyObject_GetAttrString(structobj, "pack_into"); |
---|
425 | n/a | if (pack_into == NULL) |
---|
426 | n/a | goto out; |
---|
427 | n/a | |
---|
428 | n/a | args = PyTuple_New(2+nmemb); |
---|
429 | n/a | if (args == NULL) |
---|
430 | n/a | goto out; |
---|
431 | n/a | |
---|
432 | n/a | PyTuple_SET_ITEM(args, 0, mview); |
---|
433 | n/a | PyTuple_SET_ITEM(args, 1, zero); |
---|
434 | n/a | |
---|
435 | n/a | if ((PyBytes_Check(item) || PyLong_Check(item) || |
---|
436 | n/a | PyFloat_Check(item)) && nmemb == 1) { |
---|
437 | n/a | PyTuple_SET_ITEM(args, 2, item); |
---|
438 | n/a | } |
---|
439 | n/a | else if ((PyList_Check(item) || PyTuple_Check(item)) && |
---|
440 | n/a | PySequence_Length(item) == nmemb) { |
---|
441 | n/a | for (i = 0; i < nmemb; i++) { |
---|
442 | n/a | x = PySequence_Fast_GET_ITEM(item, i); |
---|
443 | n/a | PyTuple_SET_ITEM(args, 2+i, x); |
---|
444 | n/a | } |
---|
445 | n/a | } |
---|
446 | n/a | else { |
---|
447 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
448 | n/a | "mismatch between initializer element and format string"); |
---|
449 | n/a | goto args_out; |
---|
450 | n/a | } |
---|
451 | n/a | |
---|
452 | n/a | x = PyObject_CallObject(pack_into, args); |
---|
453 | n/a | if (x != NULL) { |
---|
454 | n/a | Py_DECREF(x); |
---|
455 | n/a | ret = 0; |
---|
456 | n/a | } |
---|
457 | n/a | |
---|
458 | n/a | |
---|
459 | n/a | args_out: |
---|
460 | n/a | for (i = 0; i < 2+nmemb; i++) |
---|
461 | n/a | Py_XINCREF(PyTuple_GET_ITEM(args, i)); |
---|
462 | n/a | Py_XDECREF(args); |
---|
463 | n/a | out: |
---|
464 | n/a | Py_XDECREF(pack_into); |
---|
465 | n/a | Py_XDECREF(zero); |
---|
466 | n/a | Py_XDECREF(mview); |
---|
467 | n/a | Py_XDECREF(structobj); |
---|
468 | n/a | Py_XDECREF(format); |
---|
469 | n/a | return ret; |
---|
470 | n/a | } |
---|
471 | n/a | |
---|
472 | n/a | static void |
---|
473 | n/a | copy_rec(const Py_ssize_t *shape, Py_ssize_t ndim, Py_ssize_t itemsize, |
---|
474 | n/a | char *dptr, const Py_ssize_t *dstrides, const Py_ssize_t *dsuboffsets, |
---|
475 | n/a | char *sptr, const Py_ssize_t *sstrides, const Py_ssize_t *ssuboffsets, |
---|
476 | n/a | char *mem) |
---|
477 | n/a | { |
---|
478 | n/a | Py_ssize_t i; |
---|
479 | n/a | |
---|
480 | n/a | assert(ndim >= 1); |
---|
481 | n/a | |
---|
482 | n/a | if (ndim == 1) { |
---|
483 | n/a | if (!HAVE_PTR(dsuboffsets) && !HAVE_PTR(ssuboffsets) && |
---|
484 | n/a | dstrides[0] == itemsize && sstrides[0] == itemsize) { |
---|
485 | n/a | memmove(dptr, sptr, shape[0] * itemsize); |
---|
486 | n/a | } |
---|
487 | n/a | else { |
---|
488 | n/a | char *p; |
---|
489 | n/a | assert(mem != NULL); |
---|
490 | n/a | for (i=0, p=mem; i<shape[0]; p+=itemsize, sptr+=sstrides[0], i++) { |
---|
491 | n/a | char *xsptr = ADJUST_PTR(sptr, ssuboffsets); |
---|
492 | n/a | memcpy(p, xsptr, itemsize); |
---|
493 | n/a | } |
---|
494 | n/a | for (i=0, p=mem; i<shape[0]; p+=itemsize, dptr+=dstrides[0], i++) { |
---|
495 | n/a | char *xdptr = ADJUST_PTR(dptr, dsuboffsets); |
---|
496 | n/a | memcpy(xdptr, p, itemsize); |
---|
497 | n/a | } |
---|
498 | n/a | } |
---|
499 | n/a | return; |
---|
500 | n/a | } |
---|
501 | n/a | |
---|
502 | n/a | for (i = 0; i < shape[0]; dptr+=dstrides[0], sptr+=sstrides[0], i++) { |
---|
503 | n/a | char *xdptr = ADJUST_PTR(dptr, dsuboffsets); |
---|
504 | n/a | char *xsptr = ADJUST_PTR(sptr, ssuboffsets); |
---|
505 | n/a | |
---|
506 | n/a | copy_rec(shape+1, ndim-1, itemsize, |
---|
507 | n/a | xdptr, dstrides+1, dsuboffsets ? dsuboffsets+1 : NULL, |
---|
508 | n/a | xsptr, sstrides+1, ssuboffsets ? ssuboffsets+1 : NULL, |
---|
509 | n/a | mem); |
---|
510 | n/a | } |
---|
511 | n/a | } |
---|
512 | n/a | |
---|
513 | n/a | static int |
---|
514 | n/a | cmp_structure(Py_buffer *dest, Py_buffer *src) |
---|
515 | n/a | { |
---|
516 | n/a | Py_ssize_t i; |
---|
517 | n/a | |
---|
518 | n/a | if (strcmp(FIX_FORMAT(dest->format), FIX_FORMAT(src->format)) != 0 || |
---|
519 | n/a | dest->itemsize != src->itemsize || |
---|
520 | n/a | dest->ndim != src->ndim) |
---|
521 | n/a | return -1; |
---|
522 | n/a | |
---|
523 | n/a | for (i = 0; i < dest->ndim; i++) { |
---|
524 | n/a | if (dest->shape[i] != src->shape[i]) |
---|
525 | n/a | return -1; |
---|
526 | n/a | if (dest->shape[i] == 0) |
---|
527 | n/a | break; |
---|
528 | n/a | } |
---|
529 | n/a | |
---|
530 | n/a | return 0; |
---|
531 | n/a | } |
---|
532 | n/a | |
---|
533 | n/a | /* Copy src to dest. Both buffers must have the same format, itemsize, |
---|
534 | n/a | ndim and shape. Copying is atomic, the function never fails with |
---|
535 | n/a | a partial copy. */ |
---|
536 | n/a | static int |
---|
537 | n/a | copy_buffer(Py_buffer *dest, Py_buffer *src) |
---|
538 | n/a | { |
---|
539 | n/a | char *mem = NULL; |
---|
540 | n/a | |
---|
541 | n/a | assert(dest->ndim > 0); |
---|
542 | n/a | |
---|
543 | n/a | if (cmp_structure(dest, src) < 0) { |
---|
544 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
545 | n/a | "ndarray assignment: lvalue and rvalue have different structures"); |
---|
546 | n/a | return -1; |
---|
547 | n/a | } |
---|
548 | n/a | |
---|
549 | n/a | if ((dest->suboffsets && dest->suboffsets[dest->ndim-1] >= 0) || |
---|
550 | n/a | (src->suboffsets && src->suboffsets[src->ndim-1] >= 0) || |
---|
551 | n/a | dest->strides[dest->ndim-1] != dest->itemsize || |
---|
552 | n/a | src->strides[src->ndim-1] != src->itemsize) { |
---|
553 | n/a | mem = PyMem_Malloc(dest->shape[dest->ndim-1] * dest->itemsize); |
---|
554 | n/a | if (mem == NULL) { |
---|
555 | n/a | PyErr_NoMemory(); |
---|
556 | n/a | return -1; |
---|
557 | n/a | } |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | copy_rec(dest->shape, dest->ndim, dest->itemsize, |
---|
561 | n/a | dest->buf, dest->strides, dest->suboffsets, |
---|
562 | n/a | src->buf, src->strides, src->suboffsets, |
---|
563 | n/a | mem); |
---|
564 | n/a | |
---|
565 | n/a | PyMem_XFree(mem); |
---|
566 | n/a | return 0; |
---|
567 | n/a | } |
---|
568 | n/a | |
---|
569 | n/a | |
---|
570 | n/a | /* Unpack single element */ |
---|
571 | n/a | static PyObject * |
---|
572 | n/a | unpack_single(char *ptr, const char *fmt, Py_ssize_t itemsize) |
---|
573 | n/a | { |
---|
574 | n/a | PyObject *x, *unpack_from, *mview; |
---|
575 | n/a | |
---|
576 | n/a | if (fmt == NULL) { |
---|
577 | n/a | fmt = "B"; |
---|
578 | n/a | itemsize = 1; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | unpack_from = PyObject_GetAttrString(structmodule, "unpack_from"); |
---|
582 | n/a | if (unpack_from == NULL) |
---|
583 | n/a | return NULL; |
---|
584 | n/a | |
---|
585 | n/a | mview = PyMemoryView_FromMemory(ptr, itemsize, PyBUF_READ); |
---|
586 | n/a | if (mview == NULL) { |
---|
587 | n/a | Py_DECREF(unpack_from); |
---|
588 | n/a | return NULL; |
---|
589 | n/a | } |
---|
590 | n/a | |
---|
591 | n/a | x = PyObject_CallFunction(unpack_from, "sO", fmt, mview); |
---|
592 | n/a | Py_DECREF(unpack_from); |
---|
593 | n/a | Py_DECREF(mview); |
---|
594 | n/a | if (x == NULL) |
---|
595 | n/a | return NULL; |
---|
596 | n/a | |
---|
597 | n/a | if (PyTuple_GET_SIZE(x) == 1) { |
---|
598 | n/a | PyObject *tmp = PyTuple_GET_ITEM(x, 0); |
---|
599 | n/a | Py_INCREF(tmp); |
---|
600 | n/a | Py_DECREF(x); |
---|
601 | n/a | return tmp; |
---|
602 | n/a | } |
---|
603 | n/a | |
---|
604 | n/a | return x; |
---|
605 | n/a | } |
---|
606 | n/a | |
---|
607 | n/a | /* Unpack a multi-dimensional matrix into a nested list. Return a scalar |
---|
608 | n/a | for ndim = 0. */ |
---|
609 | n/a | static PyObject * |
---|
610 | n/a | unpack_rec(PyObject *unpack_from, char *ptr, PyObject *mview, char *item, |
---|
611 | n/a | const Py_ssize_t *shape, const Py_ssize_t *strides, |
---|
612 | n/a | const Py_ssize_t *suboffsets, Py_ssize_t ndim, Py_ssize_t itemsize) |
---|
613 | n/a | { |
---|
614 | n/a | PyObject *lst, *x; |
---|
615 | n/a | Py_ssize_t i; |
---|
616 | n/a | |
---|
617 | n/a | assert(ndim >= 0); |
---|
618 | n/a | assert(shape != NULL); |
---|
619 | n/a | assert(strides != NULL); |
---|
620 | n/a | |
---|
621 | n/a | if (ndim == 0) { |
---|
622 | n/a | memcpy(item, ptr, itemsize); |
---|
623 | n/a | x = PyObject_CallFunctionObjArgs(unpack_from, mview, NULL); |
---|
624 | n/a | if (x == NULL) |
---|
625 | n/a | return NULL; |
---|
626 | n/a | if (PyTuple_GET_SIZE(x) == 1) { |
---|
627 | n/a | PyObject *tmp = PyTuple_GET_ITEM(x, 0); |
---|
628 | n/a | Py_INCREF(tmp); |
---|
629 | n/a | Py_DECREF(x); |
---|
630 | n/a | return tmp; |
---|
631 | n/a | } |
---|
632 | n/a | return x; |
---|
633 | n/a | } |
---|
634 | n/a | |
---|
635 | n/a | lst = PyList_New(shape[0]); |
---|
636 | n/a | if (lst == NULL) |
---|
637 | n/a | return NULL; |
---|
638 | n/a | |
---|
639 | n/a | for (i = 0; i < shape[0]; ptr+=strides[0], i++) { |
---|
640 | n/a | char *nextptr = ADJUST_PTR(ptr, suboffsets); |
---|
641 | n/a | |
---|
642 | n/a | x = unpack_rec(unpack_from, nextptr, mview, item, |
---|
643 | n/a | shape+1, strides+1, suboffsets ? suboffsets+1 : NULL, |
---|
644 | n/a | ndim-1, itemsize); |
---|
645 | n/a | if (x == NULL) { |
---|
646 | n/a | Py_DECREF(lst); |
---|
647 | n/a | return NULL; |
---|
648 | n/a | } |
---|
649 | n/a | |
---|
650 | n/a | PyList_SET_ITEM(lst, i, x); |
---|
651 | n/a | } |
---|
652 | n/a | |
---|
653 | n/a | return lst; |
---|
654 | n/a | } |
---|
655 | n/a | |
---|
656 | n/a | |
---|
657 | n/a | static PyObject * |
---|
658 | n/a | ndarray_as_list(NDArrayObject *nd) |
---|
659 | n/a | { |
---|
660 | n/a | PyObject *structobj = NULL, *unpack_from = NULL; |
---|
661 | n/a | PyObject *lst = NULL, *mview = NULL; |
---|
662 | n/a | Py_buffer *base = &nd->head->base; |
---|
663 | n/a | Py_ssize_t *shape = base->shape; |
---|
664 | n/a | Py_ssize_t *strides = base->strides; |
---|
665 | n/a | Py_ssize_t simple_shape[1]; |
---|
666 | n/a | Py_ssize_t simple_strides[1]; |
---|
667 | n/a | char *item = NULL; |
---|
668 | n/a | PyObject *format; |
---|
669 | n/a | char *fmt = base->format; |
---|
670 | n/a | |
---|
671 | n/a | base = &nd->head->base; |
---|
672 | n/a | |
---|
673 | n/a | if (fmt == NULL) { |
---|
674 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
675 | n/a | "ndarray: tolist() does not support format=NULL, use " |
---|
676 | n/a | "tobytes()"); |
---|
677 | n/a | return NULL; |
---|
678 | n/a | } |
---|
679 | n/a | if (shape == NULL) { |
---|
680 | n/a | assert(ND_C_CONTIGUOUS(nd->head->flags)); |
---|
681 | n/a | assert(base->strides == NULL); |
---|
682 | n/a | assert(base->ndim <= 1); |
---|
683 | n/a | shape = simple_shape; |
---|
684 | n/a | shape[0] = base->len; |
---|
685 | n/a | strides = simple_strides; |
---|
686 | n/a | strides[0] = base->itemsize; |
---|
687 | n/a | } |
---|
688 | n/a | else if (strides == NULL) { |
---|
689 | n/a | assert(ND_C_CONTIGUOUS(nd->head->flags)); |
---|
690 | n/a | strides = strides_from_shape(nd->head, 0); |
---|
691 | n/a | if (strides == NULL) |
---|
692 | n/a | return NULL; |
---|
693 | n/a | } |
---|
694 | n/a | |
---|
695 | n/a | format = PyUnicode_FromString(fmt); |
---|
696 | n/a | if (format == NULL) |
---|
697 | n/a | goto out; |
---|
698 | n/a | |
---|
699 | n/a | structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL); |
---|
700 | n/a | Py_DECREF(format); |
---|
701 | n/a | if (structobj == NULL) |
---|
702 | n/a | goto out; |
---|
703 | n/a | |
---|
704 | n/a | unpack_from = PyObject_GetAttrString(structobj, "unpack_from"); |
---|
705 | n/a | if (unpack_from == NULL) |
---|
706 | n/a | goto out; |
---|
707 | n/a | |
---|
708 | n/a | item = PyMem_Malloc(base->itemsize); |
---|
709 | n/a | if (item == NULL) { |
---|
710 | n/a | PyErr_NoMemory(); |
---|
711 | n/a | goto out; |
---|
712 | n/a | } |
---|
713 | n/a | |
---|
714 | n/a | mview = PyMemoryView_FromMemory(item, base->itemsize, PyBUF_WRITE); |
---|
715 | n/a | if (mview == NULL) |
---|
716 | n/a | goto out; |
---|
717 | n/a | |
---|
718 | n/a | lst = unpack_rec(unpack_from, base->buf, mview, item, |
---|
719 | n/a | shape, strides, base->suboffsets, |
---|
720 | n/a | base->ndim, base->itemsize); |
---|
721 | n/a | |
---|
722 | n/a | out: |
---|
723 | n/a | Py_XDECREF(mview); |
---|
724 | n/a | PyMem_XFree(item); |
---|
725 | n/a | Py_XDECREF(unpack_from); |
---|
726 | n/a | Py_XDECREF(structobj); |
---|
727 | n/a | if (strides != base->strides && strides != simple_strides) |
---|
728 | n/a | PyMem_XFree(strides); |
---|
729 | n/a | |
---|
730 | n/a | return lst; |
---|
731 | n/a | } |
---|
732 | n/a | |
---|
733 | n/a | |
---|
734 | n/a | /****************************************************************************/ |
---|
735 | n/a | /* Initialize ndbuf */ |
---|
736 | n/a | /****************************************************************************/ |
---|
737 | n/a | |
---|
738 | n/a | /* |
---|
739 | n/a | State of a new ndbuf during initialization. 'OK' means that initialization |
---|
740 | n/a | is complete. 'PTR' means that a pointer has been initialized, but the |
---|
741 | n/a | state of the memory is still undefined and ndbuf->offset is disregarded. |
---|
742 | n/a | |
---|
743 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
744 | n/a | | | ndbuf_new | init_simple | init_structure | |
---|
745 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
746 | n/a | | next | OK (NULL) | OK | OK | |
---|
747 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
748 | n/a | | prev | OK (NULL) | OK | OK | |
---|
749 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
750 | n/a | | len | OK | OK | OK | |
---|
751 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
752 | n/a | | offset | OK | OK | OK | |
---|
753 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
754 | n/a | | data | PTR | OK | OK | |
---|
755 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
756 | n/a | | flags | user | user | OK | |
---|
757 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
758 | n/a | | exports | OK (0) | OK | OK | |
---|
759 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
760 | n/a | | base.obj | OK (NULL) | OK | OK | |
---|
761 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
762 | n/a | | base.buf | PTR | PTR | OK | |
---|
763 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
764 | n/a | | base.len | len(data) | len(data) | OK | |
---|
765 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
766 | n/a | | base.itemsize | 1 | OK | OK | |
---|
767 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
768 | n/a | | base.readonly | 0 | OK | OK | |
---|
769 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
770 | n/a | | base.format | NULL | OK | OK | |
---|
771 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
772 | n/a | | base.ndim | 1 | 1 | OK | |
---|
773 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
774 | n/a | | base.shape | NULL | NULL | OK | |
---|
775 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
776 | n/a | | base.strides | NULL | NULL | OK | |
---|
777 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
778 | n/a | | base.suboffsets | NULL | NULL | OK | |
---|
779 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
780 | n/a | | base.internal | OK | OK | OK | |
---|
781 | n/a | +-----------------+-----------+-------------+----------------+ |
---|
782 | n/a | |
---|
783 | n/a | */ |
---|
784 | n/a | |
---|
785 | n/a | static Py_ssize_t |
---|
786 | n/a | get_itemsize(PyObject *format) |
---|
787 | n/a | { |
---|
788 | n/a | PyObject *tmp; |
---|
789 | n/a | Py_ssize_t itemsize; |
---|
790 | n/a | |
---|
791 | n/a | tmp = PyObject_CallFunctionObjArgs(calcsize, format, NULL); |
---|
792 | n/a | if (tmp == NULL) |
---|
793 | n/a | return -1; |
---|
794 | n/a | itemsize = PyLong_AsSsize_t(tmp); |
---|
795 | n/a | Py_DECREF(tmp); |
---|
796 | n/a | |
---|
797 | n/a | return itemsize; |
---|
798 | n/a | } |
---|
799 | n/a | |
---|
800 | n/a | static char * |
---|
801 | n/a | get_format(PyObject *format) |
---|
802 | n/a | { |
---|
803 | n/a | PyObject *tmp; |
---|
804 | n/a | char *fmt; |
---|
805 | n/a | |
---|
806 | n/a | tmp = PyUnicode_AsASCIIString(format); |
---|
807 | n/a | if (tmp == NULL) |
---|
808 | n/a | return NULL; |
---|
809 | n/a | fmt = PyMem_Malloc(PyBytes_GET_SIZE(tmp)+1); |
---|
810 | n/a | if (fmt == NULL) { |
---|
811 | n/a | PyErr_NoMemory(); |
---|
812 | n/a | Py_DECREF(tmp); |
---|
813 | n/a | return NULL; |
---|
814 | n/a | } |
---|
815 | n/a | strcpy(fmt, PyBytes_AS_STRING(tmp)); |
---|
816 | n/a | Py_DECREF(tmp); |
---|
817 | n/a | |
---|
818 | n/a | return fmt; |
---|
819 | n/a | } |
---|
820 | n/a | |
---|
821 | n/a | static int |
---|
822 | n/a | init_simple(ndbuf_t *ndbuf, PyObject *items, PyObject *format, |
---|
823 | n/a | Py_ssize_t itemsize) |
---|
824 | n/a | { |
---|
825 | n/a | PyObject *mview; |
---|
826 | n/a | Py_buffer *base = &ndbuf->base; |
---|
827 | n/a | int ret; |
---|
828 | n/a | |
---|
829 | n/a | mview = PyMemoryView_FromBuffer(base); |
---|
830 | n/a | if (mview == NULL) |
---|
831 | n/a | return -1; |
---|
832 | n/a | |
---|
833 | n/a | ret = pack_from_list(mview, items, format, itemsize); |
---|
834 | n/a | Py_DECREF(mview); |
---|
835 | n/a | if (ret < 0) |
---|
836 | n/a | return -1; |
---|
837 | n/a | |
---|
838 | n/a | base->readonly = !(ndbuf->flags & ND_WRITABLE); |
---|
839 | n/a | base->itemsize = itemsize; |
---|
840 | n/a | base->format = get_format(format); |
---|
841 | n/a | if (base->format == NULL) |
---|
842 | n/a | return -1; |
---|
843 | n/a | |
---|
844 | n/a | return 0; |
---|
845 | n/a | } |
---|
846 | n/a | |
---|
847 | n/a | static Py_ssize_t * |
---|
848 | n/a | seq_as_ssize_array(PyObject *seq, Py_ssize_t len, int is_shape) |
---|
849 | n/a | { |
---|
850 | n/a | Py_ssize_t *dest; |
---|
851 | n/a | Py_ssize_t x, i; |
---|
852 | n/a | |
---|
853 | n/a | /* ndim = len <= ND_MAX_NDIM, so PyMem_New() is actually not needed. */ |
---|
854 | n/a | dest = PyMem_New(Py_ssize_t, len); |
---|
855 | n/a | if (dest == NULL) { |
---|
856 | n/a | PyErr_NoMemory(); |
---|
857 | n/a | return NULL; |
---|
858 | n/a | } |
---|
859 | n/a | |
---|
860 | n/a | for (i = 0; i < len; i++) { |
---|
861 | n/a | PyObject *tmp = PySequence_Fast_GET_ITEM(seq, i); |
---|
862 | n/a | if (!PyLong_Check(tmp)) { |
---|
863 | n/a | PyErr_Format(PyExc_ValueError, |
---|
864 | n/a | "elements of %s must be integers", |
---|
865 | n/a | is_shape ? "shape" : "strides"); |
---|
866 | n/a | PyMem_Free(dest); |
---|
867 | n/a | return NULL; |
---|
868 | n/a | } |
---|
869 | n/a | x = PyLong_AsSsize_t(tmp); |
---|
870 | n/a | if (PyErr_Occurred()) { |
---|
871 | n/a | PyMem_Free(dest); |
---|
872 | n/a | return NULL; |
---|
873 | n/a | } |
---|
874 | n/a | if (is_shape && x < 0) { |
---|
875 | n/a | PyErr_Format(PyExc_ValueError, |
---|
876 | n/a | "elements of shape must be integers >= 0"); |
---|
877 | n/a | PyMem_Free(dest); |
---|
878 | n/a | return NULL; |
---|
879 | n/a | } |
---|
880 | n/a | dest[i] = x; |
---|
881 | n/a | } |
---|
882 | n/a | |
---|
883 | n/a | return dest; |
---|
884 | n/a | } |
---|
885 | n/a | |
---|
886 | n/a | static Py_ssize_t * |
---|
887 | n/a | strides_from_shape(const ndbuf_t *ndbuf, int flags) |
---|
888 | n/a | { |
---|
889 | n/a | const Py_buffer *base = &ndbuf->base; |
---|
890 | n/a | Py_ssize_t *s, i; |
---|
891 | n/a | |
---|
892 | n/a | s = PyMem_Malloc(base->ndim * (sizeof *s)); |
---|
893 | n/a | if (s == NULL) { |
---|
894 | n/a | PyErr_NoMemory(); |
---|
895 | n/a | return NULL; |
---|
896 | n/a | } |
---|
897 | n/a | |
---|
898 | n/a | if (flags & ND_FORTRAN) { |
---|
899 | n/a | s[0] = base->itemsize; |
---|
900 | n/a | for (i = 1; i < base->ndim; i++) |
---|
901 | n/a | s[i] = s[i-1] * base->shape[i-1]; |
---|
902 | n/a | } |
---|
903 | n/a | else { |
---|
904 | n/a | s[base->ndim-1] = base->itemsize; |
---|
905 | n/a | for (i = base->ndim-2; i >= 0; i--) |
---|
906 | n/a | s[i] = s[i+1] * base->shape[i+1]; |
---|
907 | n/a | } |
---|
908 | n/a | |
---|
909 | n/a | return s; |
---|
910 | n/a | } |
---|
911 | n/a | |
---|
912 | n/a | /* Bounds check: |
---|
913 | n/a | |
---|
914 | n/a | len := complete length of allocated memory |
---|
915 | n/a | offset := start of the array |
---|
916 | n/a | |
---|
917 | n/a | A single array element is indexed by: |
---|
918 | n/a | |
---|
919 | n/a | i = indices[0] * strides[0] + indices[1] * strides[1] + ... |
---|
920 | n/a | |
---|
921 | n/a | imin is reached when all indices[n] combined with positive strides are 0 |
---|
922 | n/a | and all indices combined with negative strides are shape[n]-1, which is |
---|
923 | n/a | the maximum index for the nth dimension. |
---|
924 | n/a | |
---|
925 | n/a | imax is reached when all indices[n] combined with negative strides are 0 |
---|
926 | n/a | and all indices combined with positive strides are shape[n]-1. |
---|
927 | n/a | */ |
---|
928 | n/a | static int |
---|
929 | n/a | verify_structure(Py_ssize_t len, Py_ssize_t itemsize, Py_ssize_t offset, |
---|
930 | n/a | const Py_ssize_t *shape, const Py_ssize_t *strides, |
---|
931 | n/a | Py_ssize_t ndim) |
---|
932 | n/a | { |
---|
933 | n/a | Py_ssize_t imin, imax; |
---|
934 | n/a | Py_ssize_t n; |
---|
935 | n/a | |
---|
936 | n/a | assert(ndim >= 0); |
---|
937 | n/a | |
---|
938 | n/a | if (ndim == 0 && (offset < 0 || offset+itemsize > len)) |
---|
939 | n/a | goto invalid_combination; |
---|
940 | n/a | |
---|
941 | n/a | for (n = 0; n < ndim; n++) |
---|
942 | n/a | if (strides[n] % itemsize) { |
---|
943 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
944 | n/a | "strides must be a multiple of itemsize"); |
---|
945 | n/a | return -1; |
---|
946 | n/a | } |
---|
947 | n/a | |
---|
948 | n/a | for (n = 0; n < ndim; n++) |
---|
949 | n/a | if (shape[n] == 0) |
---|
950 | n/a | return 0; |
---|
951 | n/a | |
---|
952 | n/a | imin = imax = 0; |
---|
953 | n/a | for (n = 0; n < ndim; n++) |
---|
954 | n/a | if (strides[n] <= 0) |
---|
955 | n/a | imin += (shape[n]-1) * strides[n]; |
---|
956 | n/a | else |
---|
957 | n/a | imax += (shape[n]-1) * strides[n]; |
---|
958 | n/a | |
---|
959 | n/a | if (imin + offset < 0 || imax + offset + itemsize > len) |
---|
960 | n/a | goto invalid_combination; |
---|
961 | n/a | |
---|
962 | n/a | return 0; |
---|
963 | n/a | |
---|
964 | n/a | |
---|
965 | n/a | invalid_combination: |
---|
966 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
967 | n/a | "invalid combination of buffer, shape and strides"); |
---|
968 | n/a | return -1; |
---|
969 | n/a | } |
---|
970 | n/a | |
---|
971 | n/a | /* |
---|
972 | n/a | Convert a NumPy-style array to an array using suboffsets to stride in |
---|
973 | n/a | the first dimension. Requirements: ndim > 0. |
---|
974 | n/a | |
---|
975 | n/a | Contiguous example |
---|
976 | n/a | ================== |
---|
977 | n/a | |
---|
978 | n/a | Input: |
---|
979 | n/a | ------ |
---|
980 | n/a | shape = {2, 2, 3}; |
---|
981 | n/a | strides = {6, 3, 1}; |
---|
982 | n/a | suboffsets = NULL; |
---|
983 | n/a | data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
---|
984 | n/a | buf = &data[0] |
---|
985 | n/a | |
---|
986 | n/a | Output: |
---|
987 | n/a | ------- |
---|
988 | n/a | shape = {2, 2, 3}; |
---|
989 | n/a | strides = {sizeof(char *), 3, 1}; |
---|
990 | n/a | suboffsets = {0, -1, -1}; |
---|
991 | n/a | data = {p1, p2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
---|
992 | n/a | | | ^ ^ |
---|
993 | n/a | `---'---' | |
---|
994 | n/a | | | |
---|
995 | n/a | `---------------------' |
---|
996 | n/a | buf = &data[0] |
---|
997 | n/a | |
---|
998 | n/a | So, in the example the input resembles the three-dimensional array |
---|
999 | n/a | char v[2][2][3], while the output resembles an array of two pointers |
---|
1000 | n/a | to two-dimensional arrays: char (*v[2])[2][3]. |
---|
1001 | n/a | |
---|
1002 | n/a | |
---|
1003 | n/a | Non-contiguous example: |
---|
1004 | n/a | ======================= |
---|
1005 | n/a | |
---|
1006 | n/a | Input (with offset and negative strides): |
---|
1007 | n/a | ----------------------------------------- |
---|
1008 | n/a | shape = {2, 2, 3}; |
---|
1009 | n/a | strides = {-6, 3, -1}; |
---|
1010 | n/a | offset = 8 |
---|
1011 | n/a | suboffsets = NULL; |
---|
1012 | n/a | data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
---|
1013 | n/a | |
---|
1014 | n/a | Output: |
---|
1015 | n/a | ------- |
---|
1016 | n/a | shape = {2, 2, 3}; |
---|
1017 | n/a | strides = {-sizeof(char *), 3, -1}; |
---|
1018 | n/a | suboffsets = {2, -1, -1}; |
---|
1019 | n/a | newdata = {p1, p2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
---|
1020 | n/a | | | ^ ^ ^ ^ |
---|
1021 | n/a | `---'---' | | `- p2+suboffsets[0] |
---|
1022 | n/a | | `-----------|--- p1+suboffsets[0] |
---|
1023 | n/a | `---------------------' |
---|
1024 | n/a | buf = &newdata[1] # striding backwards over the pointers. |
---|
1025 | n/a | |
---|
1026 | n/a | suboffsets[0] is the same as the offset that one would specify if |
---|
1027 | n/a | the two {2, 3} subarrays were created directly, hence the name. |
---|
1028 | n/a | */ |
---|
1029 | n/a | static int |
---|
1030 | n/a | init_suboffsets(ndbuf_t *ndbuf) |
---|
1031 | n/a | { |
---|
1032 | n/a | Py_buffer *base = &ndbuf->base; |
---|
1033 | n/a | Py_ssize_t start, step; |
---|
1034 | n/a | Py_ssize_t imin, suboffset0; |
---|
1035 | n/a | Py_ssize_t addsize; |
---|
1036 | n/a | Py_ssize_t n; |
---|
1037 | n/a | char *data; |
---|
1038 | n/a | |
---|
1039 | n/a | assert(base->ndim > 0); |
---|
1040 | n/a | assert(base->suboffsets == NULL); |
---|
1041 | n/a | |
---|
1042 | n/a | /* Allocate new data with additional space for shape[0] pointers. */ |
---|
1043 | n/a | addsize = base->shape[0] * (sizeof (char *)); |
---|
1044 | n/a | |
---|
1045 | n/a | /* Align array start to a multiple of 8. */ |
---|
1046 | n/a | addsize = 8 * ((addsize + 7) / 8); |
---|
1047 | n/a | |
---|
1048 | n/a | data = PyMem_Malloc(ndbuf->len + addsize); |
---|
1049 | n/a | if (data == NULL) { |
---|
1050 | n/a | PyErr_NoMemory(); |
---|
1051 | n/a | return -1; |
---|
1052 | n/a | } |
---|
1053 | n/a | |
---|
1054 | n/a | memcpy(data + addsize, ndbuf->data, ndbuf->len); |
---|
1055 | n/a | |
---|
1056 | n/a | PyMem_Free(ndbuf->data); |
---|
1057 | n/a | ndbuf->data = data; |
---|
1058 | n/a | ndbuf->len += addsize; |
---|
1059 | n/a | base->buf = ndbuf->data; |
---|
1060 | n/a | |
---|
1061 | n/a | /* imin: minimum index of the input array relative to ndbuf->offset. |
---|
1062 | n/a | suboffset0: offset for each sub-array of the output. This is the |
---|
1063 | n/a | same as calculating -imin' for a sub-array of ndim-1. */ |
---|
1064 | n/a | imin = suboffset0 = 0; |
---|
1065 | n/a | for (n = 0; n < base->ndim; n++) { |
---|
1066 | n/a | if (base->shape[n] == 0) |
---|
1067 | n/a | break; |
---|
1068 | n/a | if (base->strides[n] <= 0) { |
---|
1069 | n/a | Py_ssize_t x = (base->shape[n]-1) * base->strides[n]; |
---|
1070 | n/a | imin += x; |
---|
1071 | n/a | suboffset0 += (n >= 1) ? -x : 0; |
---|
1072 | n/a | } |
---|
1073 | n/a | } |
---|
1074 | n/a | |
---|
1075 | n/a | /* Initialize the array of pointers to the sub-arrays. */ |
---|
1076 | n/a | start = addsize + ndbuf->offset + imin; |
---|
1077 | n/a | step = base->strides[0] < 0 ? -base->strides[0] : base->strides[0]; |
---|
1078 | n/a | |
---|
1079 | n/a | for (n = 0; n < base->shape[0]; n++) |
---|
1080 | n/a | ((char **)base->buf)[n] = (char *)base->buf + start + n*step; |
---|
1081 | n/a | |
---|
1082 | n/a | /* Initialize suboffsets. */ |
---|
1083 | n/a | base->suboffsets = PyMem_Malloc(base->ndim * (sizeof *base->suboffsets)); |
---|
1084 | n/a | if (base->suboffsets == NULL) { |
---|
1085 | n/a | PyErr_NoMemory(); |
---|
1086 | n/a | return -1; |
---|
1087 | n/a | } |
---|
1088 | n/a | base->suboffsets[0] = suboffset0; |
---|
1089 | n/a | for (n = 1; n < base->ndim; n++) |
---|
1090 | n/a | base->suboffsets[n] = -1; |
---|
1091 | n/a | |
---|
1092 | n/a | /* Adjust strides for the first (zeroth) dimension. */ |
---|
1093 | n/a | if (base->strides[0] >= 0) { |
---|
1094 | n/a | base->strides[0] = sizeof(char *); |
---|
1095 | n/a | } |
---|
1096 | n/a | else { |
---|
1097 | n/a | /* Striding backwards. */ |
---|
1098 | n/a | base->strides[0] = -(Py_ssize_t)sizeof(char *); |
---|
1099 | n/a | if (base->shape[0] > 0) |
---|
1100 | n/a | base->buf = (char *)base->buf + (base->shape[0]-1) * sizeof(char *); |
---|
1101 | n/a | } |
---|
1102 | n/a | |
---|
1103 | n/a | ndbuf->flags &= ~(ND_C|ND_FORTRAN); |
---|
1104 | n/a | ndbuf->offset = 0; |
---|
1105 | n/a | return 0; |
---|
1106 | n/a | } |
---|
1107 | n/a | |
---|
1108 | n/a | static void |
---|
1109 | n/a | init_len(Py_buffer *base) |
---|
1110 | n/a | { |
---|
1111 | n/a | Py_ssize_t i; |
---|
1112 | n/a | |
---|
1113 | n/a | base->len = 1; |
---|
1114 | n/a | for (i = 0; i < base->ndim; i++) |
---|
1115 | n/a | base->len *= base->shape[i]; |
---|
1116 | n/a | base->len *= base->itemsize; |
---|
1117 | n/a | } |
---|
1118 | n/a | |
---|
1119 | n/a | static int |
---|
1120 | n/a | init_structure(ndbuf_t *ndbuf, PyObject *shape, PyObject *strides, |
---|
1121 | n/a | Py_ssize_t ndim) |
---|
1122 | n/a | { |
---|
1123 | n/a | Py_buffer *base = &ndbuf->base; |
---|
1124 | n/a | |
---|
1125 | n/a | base->ndim = (int)ndim; |
---|
1126 | n/a | if (ndim == 0) { |
---|
1127 | n/a | if (ndbuf->flags & ND_PIL) { |
---|
1128 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1129 | n/a | "ndim = 0 cannot be used in conjunction with ND_PIL"); |
---|
1130 | n/a | return -1; |
---|
1131 | n/a | } |
---|
1132 | n/a | ndbuf->flags |= (ND_SCALAR|ND_C|ND_FORTRAN); |
---|
1133 | n/a | return 0; |
---|
1134 | n/a | } |
---|
1135 | n/a | |
---|
1136 | n/a | /* shape */ |
---|
1137 | n/a | base->shape = seq_as_ssize_array(shape, ndim, 1); |
---|
1138 | n/a | if (base->shape == NULL) |
---|
1139 | n/a | return -1; |
---|
1140 | n/a | |
---|
1141 | n/a | /* strides */ |
---|
1142 | n/a | if (strides) { |
---|
1143 | n/a | base->strides = seq_as_ssize_array(strides, ndim, 0); |
---|
1144 | n/a | } |
---|
1145 | n/a | else { |
---|
1146 | n/a | base->strides = strides_from_shape(ndbuf, ndbuf->flags); |
---|
1147 | n/a | } |
---|
1148 | n/a | if (base->strides == NULL) |
---|
1149 | n/a | return -1; |
---|
1150 | n/a | if (verify_structure(base->len, base->itemsize, ndbuf->offset, |
---|
1151 | n/a | base->shape, base->strides, ndim) < 0) |
---|
1152 | n/a | return -1; |
---|
1153 | n/a | |
---|
1154 | n/a | /* buf */ |
---|
1155 | n/a | base->buf = ndbuf->data + ndbuf->offset; |
---|
1156 | n/a | |
---|
1157 | n/a | /* len */ |
---|
1158 | n/a | init_len(base); |
---|
1159 | n/a | |
---|
1160 | n/a | /* ndbuf->flags */ |
---|
1161 | n/a | if (PyBuffer_IsContiguous(base, 'C')) |
---|
1162 | n/a | ndbuf->flags |= ND_C; |
---|
1163 | n/a | if (PyBuffer_IsContiguous(base, 'F')) |
---|
1164 | n/a | ndbuf->flags |= ND_FORTRAN; |
---|
1165 | n/a | |
---|
1166 | n/a | |
---|
1167 | n/a | /* convert numpy array to suboffset representation */ |
---|
1168 | n/a | if (ndbuf->flags & ND_PIL) { |
---|
1169 | n/a | /* modifies base->buf, base->strides and base->suboffsets **/ |
---|
1170 | n/a | return init_suboffsets(ndbuf); |
---|
1171 | n/a | } |
---|
1172 | n/a | |
---|
1173 | n/a | return 0; |
---|
1174 | n/a | } |
---|
1175 | n/a | |
---|
1176 | n/a | static ndbuf_t * |
---|
1177 | n/a | init_ndbuf(PyObject *items, PyObject *shape, PyObject *strides, |
---|
1178 | n/a | Py_ssize_t offset, PyObject *format, int flags) |
---|
1179 | n/a | { |
---|
1180 | n/a | ndbuf_t *ndbuf; |
---|
1181 | n/a | Py_ssize_t ndim; |
---|
1182 | n/a | Py_ssize_t nitems; |
---|
1183 | n/a | Py_ssize_t itemsize; |
---|
1184 | n/a | |
---|
1185 | n/a | /* ndim = len(shape) */ |
---|
1186 | n/a | CHECK_LIST_OR_TUPLE(shape) |
---|
1187 | n/a | ndim = PySequence_Fast_GET_SIZE(shape); |
---|
1188 | n/a | if (ndim > ND_MAX_NDIM) { |
---|
1189 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1190 | n/a | "ndim must not exceed %d", ND_MAX_NDIM); |
---|
1191 | n/a | return NULL; |
---|
1192 | n/a | } |
---|
1193 | n/a | |
---|
1194 | n/a | /* len(strides) = len(shape) */ |
---|
1195 | n/a | if (strides) { |
---|
1196 | n/a | CHECK_LIST_OR_TUPLE(strides) |
---|
1197 | n/a | if (PySequence_Fast_GET_SIZE(strides) == 0) |
---|
1198 | n/a | strides = NULL; |
---|
1199 | n/a | else if (flags & ND_FORTRAN) { |
---|
1200 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1201 | n/a | "ND_FORTRAN cannot be used together with strides"); |
---|
1202 | n/a | return NULL; |
---|
1203 | n/a | } |
---|
1204 | n/a | else if (PySequence_Fast_GET_SIZE(strides) != ndim) { |
---|
1205 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1206 | n/a | "len(shape) != len(strides)"); |
---|
1207 | n/a | return NULL; |
---|
1208 | n/a | } |
---|
1209 | n/a | } |
---|
1210 | n/a | |
---|
1211 | n/a | /* itemsize */ |
---|
1212 | n/a | itemsize = get_itemsize(format); |
---|
1213 | n/a | if (itemsize <= 0) { |
---|
1214 | n/a | if (itemsize == 0) { |
---|
1215 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1216 | n/a | "itemsize must not be zero"); |
---|
1217 | n/a | } |
---|
1218 | n/a | return NULL; |
---|
1219 | n/a | } |
---|
1220 | n/a | |
---|
1221 | n/a | /* convert scalar to list */ |
---|
1222 | n/a | if (ndim == 0) { |
---|
1223 | n/a | items = Py_BuildValue("(O)", items); |
---|
1224 | n/a | if (items == NULL) |
---|
1225 | n/a | return NULL; |
---|
1226 | n/a | } |
---|
1227 | n/a | else { |
---|
1228 | n/a | CHECK_LIST_OR_TUPLE(items) |
---|
1229 | n/a | Py_INCREF(items); |
---|
1230 | n/a | } |
---|
1231 | n/a | |
---|
1232 | n/a | /* number of items */ |
---|
1233 | n/a | nitems = PySequence_Fast_GET_SIZE(items); |
---|
1234 | n/a | if (nitems == 0) { |
---|
1235 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1236 | n/a | "initializer list or tuple must not be empty"); |
---|
1237 | n/a | Py_DECREF(items); |
---|
1238 | n/a | return NULL; |
---|
1239 | n/a | } |
---|
1240 | n/a | |
---|
1241 | n/a | ndbuf = ndbuf_new(nitems, itemsize, offset, flags); |
---|
1242 | n/a | if (ndbuf == NULL) { |
---|
1243 | n/a | Py_DECREF(items); |
---|
1244 | n/a | return NULL; |
---|
1245 | n/a | } |
---|
1246 | n/a | |
---|
1247 | n/a | |
---|
1248 | n/a | if (init_simple(ndbuf, items, format, itemsize) < 0) |
---|
1249 | n/a | goto error; |
---|
1250 | n/a | if (init_structure(ndbuf, shape, strides, ndim) < 0) |
---|
1251 | n/a | goto error; |
---|
1252 | n/a | |
---|
1253 | n/a | Py_DECREF(items); |
---|
1254 | n/a | return ndbuf; |
---|
1255 | n/a | |
---|
1256 | n/a | error: |
---|
1257 | n/a | Py_DECREF(items); |
---|
1258 | n/a | ndbuf_free(ndbuf); |
---|
1259 | n/a | return NULL; |
---|
1260 | n/a | } |
---|
1261 | n/a | |
---|
1262 | n/a | /* initialize and push a new base onto the linked list */ |
---|
1263 | n/a | static int |
---|
1264 | n/a | ndarray_push_base(NDArrayObject *nd, PyObject *items, |
---|
1265 | n/a | PyObject *shape, PyObject *strides, |
---|
1266 | n/a | Py_ssize_t offset, PyObject *format, int flags) |
---|
1267 | n/a | { |
---|
1268 | n/a | ndbuf_t *ndbuf; |
---|
1269 | n/a | |
---|
1270 | n/a | ndbuf = init_ndbuf(items, shape, strides, offset, format, flags); |
---|
1271 | n/a | if (ndbuf == NULL) |
---|
1272 | n/a | return -1; |
---|
1273 | n/a | |
---|
1274 | n/a | ndbuf_push(nd, ndbuf); |
---|
1275 | n/a | return 0; |
---|
1276 | n/a | } |
---|
1277 | n/a | |
---|
1278 | n/a | #define PyBUF_UNUSED 0x10000 |
---|
1279 | n/a | static int |
---|
1280 | n/a | ndarray_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1281 | n/a | { |
---|
1282 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
1283 | n/a | static char *kwlist[] = { |
---|
1284 | n/a | "obj", "shape", "strides", "offset", "format", "flags", "getbuf", NULL |
---|
1285 | n/a | }; |
---|
1286 | n/a | PyObject *v = NULL; /* initializer: scalar, list, tuple or base object */ |
---|
1287 | n/a | PyObject *shape = NULL; /* size of each dimension */ |
---|
1288 | n/a | PyObject *strides = NULL; /* number of bytes to the next elt in each dim */ |
---|
1289 | n/a | Py_ssize_t offset = 0; /* buffer offset */ |
---|
1290 | n/a | PyObject *format = simple_format; /* struct module specifier: "B" */ |
---|
1291 | n/a | int flags = ND_DEFAULT; /* base buffer and ndarray flags */ |
---|
1292 | n/a | |
---|
1293 | n/a | int getbuf = PyBUF_UNUSED; /* re-exporter: getbuffer request flags */ |
---|
1294 | n/a | |
---|
1295 | n/a | |
---|
1296 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOnOii", kwlist, |
---|
1297 | n/a | &v, &shape, &strides, &offset, &format, &flags, &getbuf)) |
---|
1298 | n/a | return -1; |
---|
1299 | n/a | |
---|
1300 | n/a | /* NDArrayObject is re-exporter */ |
---|
1301 | n/a | if (PyObject_CheckBuffer(v) && shape == NULL) { |
---|
1302 | n/a | if (strides || offset || format != simple_format || |
---|
1303 | n/a | !(flags == ND_DEFAULT || flags == ND_REDIRECT)) { |
---|
1304 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1305 | n/a | "construction from exporter object only takes 'obj', 'getbuf' " |
---|
1306 | n/a | "and 'flags' arguments"); |
---|
1307 | n/a | return -1; |
---|
1308 | n/a | } |
---|
1309 | n/a | |
---|
1310 | n/a | getbuf = (getbuf == PyBUF_UNUSED) ? PyBUF_FULL_RO : getbuf; |
---|
1311 | n/a | |
---|
1312 | n/a | if (ndarray_init_staticbuf(v, nd, getbuf) < 0) |
---|
1313 | n/a | return -1; |
---|
1314 | n/a | |
---|
1315 | n/a | init_flags(nd->head); |
---|
1316 | n/a | nd->head->flags |= flags; |
---|
1317 | n/a | |
---|
1318 | n/a | return 0; |
---|
1319 | n/a | } |
---|
1320 | n/a | |
---|
1321 | n/a | /* NDArrayObject is the original base object. */ |
---|
1322 | n/a | if (getbuf != PyBUF_UNUSED) { |
---|
1323 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1324 | n/a | "getbuf argument only valid for construction from exporter " |
---|
1325 | n/a | "object"); |
---|
1326 | n/a | return -1; |
---|
1327 | n/a | } |
---|
1328 | n/a | if (shape == NULL) { |
---|
1329 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1330 | n/a | "shape is a required argument when constructing from " |
---|
1331 | n/a | "list, tuple or scalar"); |
---|
1332 | n/a | return -1; |
---|
1333 | n/a | } |
---|
1334 | n/a | |
---|
1335 | n/a | if (flags & ND_VAREXPORT) { |
---|
1336 | n/a | nd->flags |= ND_VAREXPORT; |
---|
1337 | n/a | flags &= ~ND_VAREXPORT; |
---|
1338 | n/a | } |
---|
1339 | n/a | |
---|
1340 | n/a | /* Initialize and push the first base buffer onto the linked list. */ |
---|
1341 | n/a | return ndarray_push_base(nd, v, shape, strides, offset, format, flags); |
---|
1342 | n/a | } |
---|
1343 | n/a | |
---|
1344 | n/a | /* Push an additional base onto the linked list. */ |
---|
1345 | n/a | static PyObject * |
---|
1346 | n/a | ndarray_push(PyObject *self, PyObject *args, PyObject *kwds) |
---|
1347 | n/a | { |
---|
1348 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
1349 | n/a | static char *kwlist[] = { |
---|
1350 | n/a | "items", "shape", "strides", "offset", "format", "flags", NULL |
---|
1351 | n/a | }; |
---|
1352 | n/a | PyObject *items = NULL; /* initializer: scalar, list or tuple */ |
---|
1353 | n/a | PyObject *shape = NULL; /* size of each dimension */ |
---|
1354 | n/a | PyObject *strides = NULL; /* number of bytes to the next elt in each dim */ |
---|
1355 | n/a | PyObject *format = simple_format; /* struct module specifier: "B" */ |
---|
1356 | n/a | Py_ssize_t offset = 0; /* buffer offset */ |
---|
1357 | n/a | int flags = ND_DEFAULT; /* base buffer flags */ |
---|
1358 | n/a | |
---|
1359 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OnOi", kwlist, |
---|
1360 | n/a | &items, &shape, &strides, &offset, &format, &flags)) |
---|
1361 | n/a | return NULL; |
---|
1362 | n/a | |
---|
1363 | n/a | if (flags & ND_VAREXPORT) { |
---|
1364 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1365 | n/a | "ND_VAREXPORT flag can only be used during object creation"); |
---|
1366 | n/a | return NULL; |
---|
1367 | n/a | } |
---|
1368 | n/a | if (ND_IS_CONSUMER(nd)) { |
---|
1369 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1370 | n/a | "structure of re-exporting object is immutable"); |
---|
1371 | n/a | return NULL; |
---|
1372 | n/a | } |
---|
1373 | n/a | if (!(nd->flags&ND_VAREXPORT) && nd->head->exports > 0) { |
---|
1374 | n/a | PyErr_Format(PyExc_BufferError, |
---|
1375 | n/a | "cannot change structure: %zd exported buffer%s", |
---|
1376 | n/a | nd->head->exports, nd->head->exports==1 ? "" : "s"); |
---|
1377 | n/a | return NULL; |
---|
1378 | n/a | } |
---|
1379 | n/a | |
---|
1380 | n/a | if (ndarray_push_base(nd, items, shape, strides, |
---|
1381 | n/a | offset, format, flags) < 0) |
---|
1382 | n/a | return NULL; |
---|
1383 | n/a | Py_RETURN_NONE; |
---|
1384 | n/a | } |
---|
1385 | n/a | |
---|
1386 | n/a | /* Pop a base from the linked list (if possible). */ |
---|
1387 | n/a | static PyObject * |
---|
1388 | n/a | ndarray_pop(PyObject *self, PyObject *dummy) |
---|
1389 | n/a | { |
---|
1390 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
1391 | n/a | if (ND_IS_CONSUMER(nd)) { |
---|
1392 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1393 | n/a | "structure of re-exporting object is immutable"); |
---|
1394 | n/a | return NULL; |
---|
1395 | n/a | } |
---|
1396 | n/a | if (nd->head->exports > 0) { |
---|
1397 | n/a | PyErr_Format(PyExc_BufferError, |
---|
1398 | n/a | "cannot change structure: %zd exported buffer%s", |
---|
1399 | n/a | nd->head->exports, nd->head->exports==1 ? "" : "s"); |
---|
1400 | n/a | return NULL; |
---|
1401 | n/a | } |
---|
1402 | n/a | if (nd->head->next == NULL) { |
---|
1403 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1404 | n/a | "list only has a single base"); |
---|
1405 | n/a | return NULL; |
---|
1406 | n/a | } |
---|
1407 | n/a | |
---|
1408 | n/a | ndbuf_pop(nd); |
---|
1409 | n/a | Py_RETURN_NONE; |
---|
1410 | n/a | } |
---|
1411 | n/a | |
---|
1412 | n/a | /**************************************************************************/ |
---|
1413 | n/a | /* getbuffer */ |
---|
1414 | n/a | /**************************************************************************/ |
---|
1415 | n/a | |
---|
1416 | n/a | static int |
---|
1417 | n/a | ndarray_getbuf(NDArrayObject *self, Py_buffer *view, int flags) |
---|
1418 | n/a | { |
---|
1419 | n/a | ndbuf_t *ndbuf = self->head; |
---|
1420 | n/a | Py_buffer *base = &ndbuf->base; |
---|
1421 | n/a | int baseflags = ndbuf->flags; |
---|
1422 | n/a | |
---|
1423 | n/a | /* redirect mode */ |
---|
1424 | n/a | if (base->obj != NULL && (baseflags&ND_REDIRECT)) { |
---|
1425 | n/a | return PyObject_GetBuffer(base->obj, view, flags); |
---|
1426 | n/a | } |
---|
1427 | n/a | |
---|
1428 | n/a | /* start with complete information */ |
---|
1429 | n/a | *view = *base; |
---|
1430 | n/a | view->obj = NULL; |
---|
1431 | n/a | |
---|
1432 | n/a | /* reconstruct format */ |
---|
1433 | n/a | if (view->format == NULL) |
---|
1434 | n/a | view->format = "B"; |
---|
1435 | n/a | |
---|
1436 | n/a | if (base->ndim != 0 && |
---|
1437 | n/a | ((REQ_SHAPE(flags) && base->shape == NULL) || |
---|
1438 | n/a | (REQ_STRIDES(flags) && base->strides == NULL))) { |
---|
1439 | n/a | /* The ndarray is a re-exporter that has been created without full |
---|
1440 | n/a | information for testing purposes. In this particular case the |
---|
1441 | n/a | ndarray is not a PEP-3118 compliant buffer provider. */ |
---|
1442 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1443 | n/a | "re-exporter does not provide format, shape or strides"); |
---|
1444 | n/a | return -1; |
---|
1445 | n/a | } |
---|
1446 | n/a | |
---|
1447 | n/a | if (baseflags & ND_GETBUF_FAIL) { |
---|
1448 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1449 | n/a | "ND_GETBUF_FAIL: forced test exception"); |
---|
1450 | n/a | if (baseflags & ND_GETBUF_UNDEFINED) |
---|
1451 | n/a | view->obj = (PyObject *)0x1; /* wrong but permitted in <= 3.2 */ |
---|
1452 | n/a | return -1; |
---|
1453 | n/a | } |
---|
1454 | n/a | |
---|
1455 | n/a | if (REQ_WRITABLE(flags) && base->readonly) { |
---|
1456 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1457 | n/a | "ndarray is not writable"); |
---|
1458 | n/a | return -1; |
---|
1459 | n/a | } |
---|
1460 | n/a | if (!REQ_FORMAT(flags)) { |
---|
1461 | n/a | /* NULL indicates that the buffer's data type has been cast to 'B'. |
---|
1462 | n/a | view->itemsize is the _previous_ itemsize. If shape is present, |
---|
1463 | n/a | the equality product(shape) * itemsize = len still holds at this |
---|
1464 | n/a | point. The equality calcsize(format) = itemsize does _not_ hold |
---|
1465 | n/a | from here on! */ |
---|
1466 | n/a | view->format = NULL; |
---|
1467 | n/a | } |
---|
1468 | n/a | |
---|
1469 | n/a | if (REQ_C_CONTIGUOUS(flags) && !ND_C_CONTIGUOUS(baseflags)) { |
---|
1470 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1471 | n/a | "ndarray is not C-contiguous"); |
---|
1472 | n/a | return -1; |
---|
1473 | n/a | } |
---|
1474 | n/a | if (REQ_F_CONTIGUOUS(flags) && !ND_FORTRAN_CONTIGUOUS(baseflags)) { |
---|
1475 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1476 | n/a | "ndarray is not Fortran contiguous"); |
---|
1477 | n/a | return -1; |
---|
1478 | n/a | } |
---|
1479 | n/a | if (REQ_ANY_CONTIGUOUS(flags) && !ND_ANY_CONTIGUOUS(baseflags)) { |
---|
1480 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1481 | n/a | "ndarray is not contiguous"); |
---|
1482 | n/a | return -1; |
---|
1483 | n/a | } |
---|
1484 | n/a | if (!REQ_INDIRECT(flags) && (baseflags & ND_PIL)) { |
---|
1485 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1486 | n/a | "ndarray cannot be represented without suboffsets"); |
---|
1487 | n/a | return -1; |
---|
1488 | n/a | } |
---|
1489 | n/a | if (!REQ_STRIDES(flags)) { |
---|
1490 | n/a | if (!ND_C_CONTIGUOUS(baseflags)) { |
---|
1491 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1492 | n/a | "ndarray is not C-contiguous"); |
---|
1493 | n/a | return -1; |
---|
1494 | n/a | } |
---|
1495 | n/a | view->strides = NULL; |
---|
1496 | n/a | } |
---|
1497 | n/a | if (!REQ_SHAPE(flags)) { |
---|
1498 | n/a | /* PyBUF_SIMPLE or PyBUF_WRITABLE: at this point buf is C-contiguous, |
---|
1499 | n/a | so base->buf = ndbuf->data. */ |
---|
1500 | n/a | if (view->format != NULL) { |
---|
1501 | n/a | /* PyBUF_SIMPLE|PyBUF_FORMAT and PyBUF_WRITABLE|PyBUF_FORMAT do |
---|
1502 | n/a | not make sense. */ |
---|
1503 | n/a | PyErr_Format(PyExc_BufferError, |
---|
1504 | n/a | "ndarray: cannot cast to unsigned bytes if the format flag " |
---|
1505 | n/a | "is present"); |
---|
1506 | n/a | return -1; |
---|
1507 | n/a | } |
---|
1508 | n/a | /* product(shape) * itemsize = len and calcsize(format) = itemsize |
---|
1509 | n/a | do _not_ hold from here on! */ |
---|
1510 | n/a | view->ndim = 1; |
---|
1511 | n/a | view->shape = NULL; |
---|
1512 | n/a | } |
---|
1513 | n/a | |
---|
1514 | n/a | /* Ascertain that the new buffer has the same contiguity as the exporter */ |
---|
1515 | n/a | if (ND_C_CONTIGUOUS(baseflags) != PyBuffer_IsContiguous(view, 'C') || |
---|
1516 | n/a | /* skip cast to 1-d */ |
---|
1517 | n/a | (view->format != NULL && view->shape != NULL && |
---|
1518 | n/a | ND_FORTRAN_CONTIGUOUS(baseflags) != PyBuffer_IsContiguous(view, 'F')) || |
---|
1519 | n/a | /* cast to 1-d */ |
---|
1520 | n/a | (view->format == NULL && view->shape == NULL && |
---|
1521 | n/a | !PyBuffer_IsContiguous(view, 'F'))) { |
---|
1522 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
1523 | n/a | "ndarray: contiguity mismatch in getbuf()"); |
---|
1524 | n/a | return -1; |
---|
1525 | n/a | } |
---|
1526 | n/a | |
---|
1527 | n/a | view->obj = (PyObject *)self; |
---|
1528 | n/a | Py_INCREF(view->obj); |
---|
1529 | n/a | self->head->exports++; |
---|
1530 | n/a | |
---|
1531 | n/a | return 0; |
---|
1532 | n/a | } |
---|
1533 | n/a | |
---|
1534 | n/a | static int |
---|
1535 | n/a | ndarray_releasebuf(NDArrayObject *self, Py_buffer *view) |
---|
1536 | n/a | { |
---|
1537 | n/a | if (!ND_IS_CONSUMER(self)) { |
---|
1538 | n/a | ndbuf_t *ndbuf = view->internal; |
---|
1539 | n/a | if (--ndbuf->exports == 0 && ndbuf != self->head) |
---|
1540 | n/a | ndbuf_delete(self, ndbuf); |
---|
1541 | n/a | } |
---|
1542 | n/a | |
---|
1543 | n/a | return 0; |
---|
1544 | n/a | } |
---|
1545 | n/a | |
---|
1546 | n/a | static PyBufferProcs ndarray_as_buffer = { |
---|
1547 | n/a | (getbufferproc)ndarray_getbuf, /* bf_getbuffer */ |
---|
1548 | n/a | (releasebufferproc)ndarray_releasebuf /* bf_releasebuffer */ |
---|
1549 | n/a | }; |
---|
1550 | n/a | |
---|
1551 | n/a | |
---|
1552 | n/a | /**************************************************************************/ |
---|
1553 | n/a | /* indexing/slicing */ |
---|
1554 | n/a | /**************************************************************************/ |
---|
1555 | n/a | |
---|
1556 | n/a | static char * |
---|
1557 | n/a | ptr_from_index(Py_buffer *base, Py_ssize_t index) |
---|
1558 | n/a | { |
---|
1559 | n/a | char *ptr; |
---|
1560 | n/a | Py_ssize_t nitems; /* items in the first dimension */ |
---|
1561 | n/a | |
---|
1562 | n/a | if (base->shape) |
---|
1563 | n/a | nitems = base->shape[0]; |
---|
1564 | n/a | else { |
---|
1565 | n/a | assert(base->ndim == 1 && SIMPLE_FORMAT(base->format)); |
---|
1566 | n/a | nitems = base->len; |
---|
1567 | n/a | } |
---|
1568 | n/a | |
---|
1569 | n/a | if (index < 0) { |
---|
1570 | n/a | index += nitems; |
---|
1571 | n/a | } |
---|
1572 | n/a | if (index < 0 || index >= nitems) { |
---|
1573 | n/a | PyErr_SetString(PyExc_IndexError, "index out of bounds"); |
---|
1574 | n/a | return NULL; |
---|
1575 | n/a | } |
---|
1576 | n/a | |
---|
1577 | n/a | ptr = (char *)base->buf; |
---|
1578 | n/a | |
---|
1579 | n/a | if (base->strides == NULL) |
---|
1580 | n/a | ptr += base->itemsize * index; |
---|
1581 | n/a | else |
---|
1582 | n/a | ptr += base->strides[0] * index; |
---|
1583 | n/a | |
---|
1584 | n/a | ptr = ADJUST_PTR(ptr, base->suboffsets); |
---|
1585 | n/a | |
---|
1586 | n/a | return ptr; |
---|
1587 | n/a | } |
---|
1588 | n/a | |
---|
1589 | n/a | static PyObject * |
---|
1590 | n/a | ndarray_item(NDArrayObject *self, Py_ssize_t index) |
---|
1591 | n/a | { |
---|
1592 | n/a | ndbuf_t *ndbuf = self->head; |
---|
1593 | n/a | Py_buffer *base = &ndbuf->base; |
---|
1594 | n/a | char *ptr; |
---|
1595 | n/a | |
---|
1596 | n/a | if (base->ndim == 0) { |
---|
1597 | n/a | PyErr_SetString(PyExc_TypeError, "invalid indexing of scalar"); |
---|
1598 | n/a | return NULL; |
---|
1599 | n/a | } |
---|
1600 | n/a | |
---|
1601 | n/a | ptr = ptr_from_index(base, index); |
---|
1602 | n/a | if (ptr == NULL) |
---|
1603 | n/a | return NULL; |
---|
1604 | n/a | |
---|
1605 | n/a | if (base->ndim == 1) { |
---|
1606 | n/a | return unpack_single(ptr, base->format, base->itemsize); |
---|
1607 | n/a | } |
---|
1608 | n/a | else { |
---|
1609 | n/a | NDArrayObject *nd; |
---|
1610 | n/a | Py_buffer *subview; |
---|
1611 | n/a | |
---|
1612 | n/a | nd = (NDArrayObject *)ndarray_new(&NDArray_Type, NULL, NULL); |
---|
1613 | n/a | if (nd == NULL) |
---|
1614 | n/a | return NULL; |
---|
1615 | n/a | |
---|
1616 | n/a | if (ndarray_init_staticbuf((PyObject *)self, nd, PyBUF_FULL_RO) < 0) { |
---|
1617 | n/a | Py_DECREF(nd); |
---|
1618 | n/a | return NULL; |
---|
1619 | n/a | } |
---|
1620 | n/a | |
---|
1621 | n/a | subview = &nd->staticbuf.base; |
---|
1622 | n/a | |
---|
1623 | n/a | subview->buf = ptr; |
---|
1624 | n/a | subview->len /= subview->shape[0]; |
---|
1625 | n/a | |
---|
1626 | n/a | subview->ndim--; |
---|
1627 | n/a | subview->shape++; |
---|
1628 | n/a | if (subview->strides) subview->strides++; |
---|
1629 | n/a | if (subview->suboffsets) subview->suboffsets++; |
---|
1630 | n/a | |
---|
1631 | n/a | init_flags(&nd->staticbuf); |
---|
1632 | n/a | |
---|
1633 | n/a | return (PyObject *)nd; |
---|
1634 | n/a | } |
---|
1635 | n/a | } |
---|
1636 | n/a | |
---|
1637 | n/a | /* |
---|
1638 | n/a | For each dimension, we get valid (start, stop, step, slicelength) quadruples |
---|
1639 | n/a | from PySlice_GetIndicesEx(). |
---|
1640 | n/a | |
---|
1641 | n/a | Slicing NumPy arrays |
---|
1642 | n/a | ==================== |
---|
1643 | n/a | |
---|
1644 | n/a | A pointer to an element in a NumPy array is defined by: |
---|
1645 | n/a | |
---|
1646 | n/a | ptr = (char *)buf + indices[0] * strides[0] + |
---|
1647 | n/a | ... + |
---|
1648 | n/a | indices[ndim-1] * strides[ndim-1] |
---|
1649 | n/a | |
---|
1650 | n/a | Adjust buf: |
---|
1651 | n/a | ----------- |
---|
1652 | n/a | Adding start[n] for each dimension effectively adds the constant: |
---|
1653 | n/a | |
---|
1654 | n/a | c = start[0] * strides[0] + ... + start[ndim-1] * strides[ndim-1] |
---|
1655 | n/a | |
---|
1656 | n/a | Therefore init_slice() adds all start[n] directly to buf. |
---|
1657 | n/a | |
---|
1658 | n/a | Adjust shape: |
---|
1659 | n/a | ------------- |
---|
1660 | n/a | Obviously shape[n] = slicelength[n] |
---|
1661 | n/a | |
---|
1662 | n/a | Adjust strides: |
---|
1663 | n/a | --------------- |
---|
1664 | n/a | In the original array, the next element in a dimension is reached |
---|
1665 | n/a | by adding strides[n] to the pointer. In the sliced array, elements |
---|
1666 | n/a | may be skipped, so the next element is reached by adding: |
---|
1667 | n/a | |
---|
1668 | n/a | strides[n] * step[n] |
---|
1669 | n/a | |
---|
1670 | n/a | Slicing PIL arrays |
---|
1671 | n/a | ================== |
---|
1672 | n/a | |
---|
1673 | n/a | Layout: |
---|
1674 | n/a | ------- |
---|
1675 | n/a | In the first (zeroth) dimension, PIL arrays have an array of pointers |
---|
1676 | n/a | to sub-arrays of ndim-1. Striding in the first dimension is done by |
---|
1677 | n/a | getting the index of the nth pointer, dereference it and then add a |
---|
1678 | n/a | suboffset to it. The arrays pointed to can best be seen a regular |
---|
1679 | n/a | NumPy arrays. |
---|
1680 | n/a | |
---|
1681 | n/a | Adjust buf: |
---|
1682 | n/a | ----------- |
---|
1683 | n/a | In the original array, buf points to a location (usually the start) |
---|
1684 | n/a | in the array of pointers. For the sliced array, start[0] can be |
---|
1685 | n/a | added to buf in the same manner as for NumPy arrays. |
---|
1686 | n/a | |
---|
1687 | n/a | Adjust suboffsets: |
---|
1688 | n/a | ------------------ |
---|
1689 | n/a | Due to the dereferencing step in the addressing scheme, it is not |
---|
1690 | n/a | possible to adjust buf for higher dimensions. Recall that the |
---|
1691 | n/a | sub-arrays pointed to are regular NumPy arrays, so for each of |
---|
1692 | n/a | those arrays adding start[n] effectively adds the constant: |
---|
1693 | n/a | |
---|
1694 | n/a | c = start[1] * strides[1] + ... + start[ndim-1] * strides[ndim-1] |
---|
1695 | n/a | |
---|
1696 | n/a | This constant is added to suboffsets[0]. suboffsets[0] in turn is |
---|
1697 | n/a | added to each pointer right after dereferencing. |
---|
1698 | n/a | |
---|
1699 | n/a | Adjust shape and strides: |
---|
1700 | n/a | ------------------------- |
---|
1701 | n/a | Shape and strides are not influenced by the dereferencing step, so |
---|
1702 | n/a | they are adjusted in the same manner as for NumPy arrays. |
---|
1703 | n/a | |
---|
1704 | n/a | Multiple levels of suboffsets |
---|
1705 | n/a | ============================= |
---|
1706 | n/a | |
---|
1707 | n/a | For a construct like an array of pointers to array of pointers to |
---|
1708 | n/a | sub-arrays of ndim-2: |
---|
1709 | n/a | |
---|
1710 | n/a | suboffsets[0] = start[1] * strides[1] |
---|
1711 | n/a | suboffsets[1] = start[2] * strides[2] + ... |
---|
1712 | n/a | */ |
---|
1713 | n/a | static int |
---|
1714 | n/a | init_slice(Py_buffer *base, PyObject *key, int dim) |
---|
1715 | n/a | { |
---|
1716 | n/a | Py_ssize_t start, stop, step, slicelength; |
---|
1717 | n/a | |
---|
1718 | n/a | if (PySlice_GetIndicesEx(key, base->shape[dim], |
---|
1719 | n/a | &start, &stop, &step, &slicelength) < 0) { |
---|
1720 | n/a | return -1; |
---|
1721 | n/a | } |
---|
1722 | n/a | |
---|
1723 | n/a | |
---|
1724 | n/a | if (base->suboffsets == NULL || dim == 0) { |
---|
1725 | n/a | adjust_buf: |
---|
1726 | n/a | base->buf = (char *)base->buf + base->strides[dim] * start; |
---|
1727 | n/a | } |
---|
1728 | n/a | else { |
---|
1729 | n/a | Py_ssize_t n = dim-1; |
---|
1730 | n/a | while (n >= 0 && base->suboffsets[n] < 0) |
---|
1731 | n/a | n--; |
---|
1732 | n/a | if (n < 0) |
---|
1733 | n/a | goto adjust_buf; /* all suboffsets are negative */ |
---|
1734 | n/a | base->suboffsets[n] = base->suboffsets[n] + base->strides[dim] * start; |
---|
1735 | n/a | } |
---|
1736 | n/a | base->shape[dim] = slicelength; |
---|
1737 | n/a | base->strides[dim] = base->strides[dim] * step; |
---|
1738 | n/a | |
---|
1739 | n/a | return 0; |
---|
1740 | n/a | } |
---|
1741 | n/a | |
---|
1742 | n/a | static int |
---|
1743 | n/a | copy_structure(Py_buffer *base) |
---|
1744 | n/a | { |
---|
1745 | n/a | Py_ssize_t *shape = NULL, *strides = NULL, *suboffsets = NULL; |
---|
1746 | n/a | Py_ssize_t i; |
---|
1747 | n/a | |
---|
1748 | n/a | shape = PyMem_Malloc(base->ndim * (sizeof *shape)); |
---|
1749 | n/a | strides = PyMem_Malloc(base->ndim * (sizeof *strides)); |
---|
1750 | n/a | if (shape == NULL || strides == NULL) |
---|
1751 | n/a | goto err_nomem; |
---|
1752 | n/a | |
---|
1753 | n/a | suboffsets = NULL; |
---|
1754 | n/a | if (base->suboffsets) { |
---|
1755 | n/a | suboffsets = PyMem_Malloc(base->ndim * (sizeof *suboffsets)); |
---|
1756 | n/a | if (suboffsets == NULL) |
---|
1757 | n/a | goto err_nomem; |
---|
1758 | n/a | } |
---|
1759 | n/a | |
---|
1760 | n/a | for (i = 0; i < base->ndim; i++) { |
---|
1761 | n/a | shape[i] = base->shape[i]; |
---|
1762 | n/a | strides[i] = base->strides[i]; |
---|
1763 | n/a | if (suboffsets) |
---|
1764 | n/a | suboffsets[i] = base->suboffsets[i]; |
---|
1765 | n/a | } |
---|
1766 | n/a | |
---|
1767 | n/a | base->shape = shape; |
---|
1768 | n/a | base->strides = strides; |
---|
1769 | n/a | base->suboffsets = suboffsets; |
---|
1770 | n/a | |
---|
1771 | n/a | return 0; |
---|
1772 | n/a | |
---|
1773 | n/a | err_nomem: |
---|
1774 | n/a | PyErr_NoMemory(); |
---|
1775 | n/a | PyMem_XFree(shape); |
---|
1776 | n/a | PyMem_XFree(strides); |
---|
1777 | n/a | PyMem_XFree(suboffsets); |
---|
1778 | n/a | return -1; |
---|
1779 | n/a | } |
---|
1780 | n/a | |
---|
1781 | n/a | static PyObject * |
---|
1782 | n/a | ndarray_subscript(NDArrayObject *self, PyObject *key) |
---|
1783 | n/a | { |
---|
1784 | n/a | NDArrayObject *nd; |
---|
1785 | n/a | ndbuf_t *ndbuf; |
---|
1786 | n/a | Py_buffer *base = &self->head->base; |
---|
1787 | n/a | |
---|
1788 | n/a | if (base->ndim == 0) { |
---|
1789 | n/a | if (PyTuple_Check(key) && PyTuple_GET_SIZE(key) == 0) { |
---|
1790 | n/a | return unpack_single(base->buf, base->format, base->itemsize); |
---|
1791 | n/a | } |
---|
1792 | n/a | else if (key == Py_Ellipsis) { |
---|
1793 | n/a | Py_INCREF(self); |
---|
1794 | n/a | return (PyObject *)self; |
---|
1795 | n/a | } |
---|
1796 | n/a | else { |
---|
1797 | n/a | PyErr_SetString(PyExc_TypeError, "invalid indexing of scalar"); |
---|
1798 | n/a | return NULL; |
---|
1799 | n/a | } |
---|
1800 | n/a | } |
---|
1801 | n/a | if (PyIndex_Check(key)) { |
---|
1802 | n/a | Py_ssize_t index = PyLong_AsSsize_t(key); |
---|
1803 | n/a | if (index == -1 && PyErr_Occurred()) |
---|
1804 | n/a | return NULL; |
---|
1805 | n/a | return ndarray_item(self, index); |
---|
1806 | n/a | } |
---|
1807 | n/a | |
---|
1808 | n/a | nd = (NDArrayObject *)ndarray_new(&NDArray_Type, NULL, NULL); |
---|
1809 | n/a | if (nd == NULL) |
---|
1810 | n/a | return NULL; |
---|
1811 | n/a | |
---|
1812 | n/a | /* new ndarray is a consumer */ |
---|
1813 | n/a | if (ndarray_init_staticbuf((PyObject *)self, nd, PyBUF_FULL_RO) < 0) { |
---|
1814 | n/a | Py_DECREF(nd); |
---|
1815 | n/a | return NULL; |
---|
1816 | n/a | } |
---|
1817 | n/a | |
---|
1818 | n/a | /* copy shape, strides and suboffsets */ |
---|
1819 | n/a | ndbuf = nd->head; |
---|
1820 | n/a | base = &ndbuf->base; |
---|
1821 | n/a | if (copy_structure(base) < 0) { |
---|
1822 | n/a | Py_DECREF(nd); |
---|
1823 | n/a | return NULL; |
---|
1824 | n/a | } |
---|
1825 | n/a | ndbuf->flags |= ND_OWN_ARRAYS; |
---|
1826 | n/a | |
---|
1827 | n/a | if (PySlice_Check(key)) { |
---|
1828 | n/a | /* one-dimensional slice */ |
---|
1829 | n/a | if (init_slice(base, key, 0) < 0) |
---|
1830 | n/a | goto err_occurred; |
---|
1831 | n/a | } |
---|
1832 | n/a | else if (PyTuple_Check(key)) { |
---|
1833 | n/a | /* multi-dimensional slice */ |
---|
1834 | n/a | PyObject *tuple = key; |
---|
1835 | n/a | Py_ssize_t i, n; |
---|
1836 | n/a | |
---|
1837 | n/a | n = PyTuple_GET_SIZE(tuple); |
---|
1838 | n/a | for (i = 0; i < n; i++) { |
---|
1839 | n/a | key = PyTuple_GET_ITEM(tuple, i); |
---|
1840 | n/a | if (!PySlice_Check(key)) |
---|
1841 | n/a | goto type_error; |
---|
1842 | n/a | if (init_slice(base, key, (int)i) < 0) |
---|
1843 | n/a | goto err_occurred; |
---|
1844 | n/a | } |
---|
1845 | n/a | } |
---|
1846 | n/a | else { |
---|
1847 | n/a | goto type_error; |
---|
1848 | n/a | } |
---|
1849 | n/a | |
---|
1850 | n/a | init_len(base); |
---|
1851 | n/a | init_flags(ndbuf); |
---|
1852 | n/a | |
---|
1853 | n/a | return (PyObject *)nd; |
---|
1854 | n/a | |
---|
1855 | n/a | |
---|
1856 | n/a | type_error: |
---|
1857 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1858 | n/a | "cannot index memory using \"%.200s\"", |
---|
1859 | n/a | key->ob_type->tp_name); |
---|
1860 | n/a | err_occurred: |
---|
1861 | n/a | Py_DECREF(nd); |
---|
1862 | n/a | return NULL; |
---|
1863 | n/a | } |
---|
1864 | n/a | |
---|
1865 | n/a | |
---|
1866 | n/a | static int |
---|
1867 | n/a | ndarray_ass_subscript(NDArrayObject *self, PyObject *key, PyObject *value) |
---|
1868 | n/a | { |
---|
1869 | n/a | NDArrayObject *nd; |
---|
1870 | n/a | Py_buffer *dest = &self->head->base; |
---|
1871 | n/a | Py_buffer src; |
---|
1872 | n/a | char *ptr; |
---|
1873 | n/a | Py_ssize_t index; |
---|
1874 | n/a | int ret = -1; |
---|
1875 | n/a | |
---|
1876 | n/a | if (dest->readonly) { |
---|
1877 | n/a | PyErr_SetString(PyExc_TypeError, "ndarray is not writable"); |
---|
1878 | n/a | return -1; |
---|
1879 | n/a | } |
---|
1880 | n/a | if (value == NULL) { |
---|
1881 | n/a | PyErr_SetString(PyExc_TypeError, "ndarray data cannot be deleted"); |
---|
1882 | n/a | return -1; |
---|
1883 | n/a | } |
---|
1884 | n/a | if (dest->ndim == 0) { |
---|
1885 | n/a | if (key == Py_Ellipsis || |
---|
1886 | n/a | (PyTuple_Check(key) && PyTuple_GET_SIZE(key) == 0)) { |
---|
1887 | n/a | ptr = (char *)dest->buf; |
---|
1888 | n/a | return pack_single(ptr, value, dest->format, dest->itemsize); |
---|
1889 | n/a | } |
---|
1890 | n/a | else { |
---|
1891 | n/a | PyErr_SetString(PyExc_TypeError, "invalid indexing of scalar"); |
---|
1892 | n/a | return -1; |
---|
1893 | n/a | } |
---|
1894 | n/a | } |
---|
1895 | n/a | if (dest->ndim == 1 && PyIndex_Check(key)) { |
---|
1896 | n/a | /* rvalue must be a single item */ |
---|
1897 | n/a | index = PyLong_AsSsize_t(key); |
---|
1898 | n/a | if (index == -1 && PyErr_Occurred()) |
---|
1899 | n/a | return -1; |
---|
1900 | n/a | else { |
---|
1901 | n/a | ptr = ptr_from_index(dest, index); |
---|
1902 | n/a | if (ptr == NULL) |
---|
1903 | n/a | return -1; |
---|
1904 | n/a | } |
---|
1905 | n/a | return pack_single(ptr, value, dest->format, dest->itemsize); |
---|
1906 | n/a | } |
---|
1907 | n/a | |
---|
1908 | n/a | /* rvalue must be an exporter */ |
---|
1909 | n/a | if (PyObject_GetBuffer(value, &src, PyBUF_FULL_RO) == -1) |
---|
1910 | n/a | return -1; |
---|
1911 | n/a | |
---|
1912 | n/a | nd = (NDArrayObject *)ndarray_subscript(self, key); |
---|
1913 | n/a | if (nd != NULL) { |
---|
1914 | n/a | dest = &nd->head->base; |
---|
1915 | n/a | ret = copy_buffer(dest, &src); |
---|
1916 | n/a | Py_DECREF(nd); |
---|
1917 | n/a | } |
---|
1918 | n/a | |
---|
1919 | n/a | PyBuffer_Release(&src); |
---|
1920 | n/a | return ret; |
---|
1921 | n/a | } |
---|
1922 | n/a | |
---|
1923 | n/a | static PyObject * |
---|
1924 | n/a | slice_indices(PyObject *self, PyObject *args) |
---|
1925 | n/a | { |
---|
1926 | n/a | PyObject *ret, *key, *tmp; |
---|
1927 | n/a | Py_ssize_t s[4]; /* start, stop, step, slicelength */ |
---|
1928 | n/a | Py_ssize_t i, len; |
---|
1929 | n/a | |
---|
1930 | n/a | if (!PyArg_ParseTuple(args, "On", &key, &len)) { |
---|
1931 | n/a | return NULL; |
---|
1932 | n/a | } |
---|
1933 | n/a | if (!PySlice_Check(key)) { |
---|
1934 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1935 | n/a | "first argument must be a slice object"); |
---|
1936 | n/a | return NULL; |
---|
1937 | n/a | } |
---|
1938 | n/a | if (PySlice_GetIndicesEx(key, len, &s[0], &s[1], &s[2], &s[3]) < 0) { |
---|
1939 | n/a | return NULL; |
---|
1940 | n/a | } |
---|
1941 | n/a | |
---|
1942 | n/a | ret = PyTuple_New(4); |
---|
1943 | n/a | if (ret == NULL) |
---|
1944 | n/a | return NULL; |
---|
1945 | n/a | |
---|
1946 | n/a | for (i = 0; i < 4; i++) { |
---|
1947 | n/a | tmp = PyLong_FromSsize_t(s[i]); |
---|
1948 | n/a | if (tmp == NULL) |
---|
1949 | n/a | goto error; |
---|
1950 | n/a | PyTuple_SET_ITEM(ret, i, tmp); |
---|
1951 | n/a | } |
---|
1952 | n/a | |
---|
1953 | n/a | return ret; |
---|
1954 | n/a | |
---|
1955 | n/a | error: |
---|
1956 | n/a | Py_DECREF(ret); |
---|
1957 | n/a | return NULL; |
---|
1958 | n/a | } |
---|
1959 | n/a | |
---|
1960 | n/a | |
---|
1961 | n/a | static PyMappingMethods ndarray_as_mapping = { |
---|
1962 | n/a | NULL, /* mp_length */ |
---|
1963 | n/a | (binaryfunc)ndarray_subscript, /* mp_subscript */ |
---|
1964 | n/a | (objobjargproc)ndarray_ass_subscript /* mp_ass_subscript */ |
---|
1965 | n/a | }; |
---|
1966 | n/a | |
---|
1967 | n/a | static PySequenceMethods ndarray_as_sequence = { |
---|
1968 | n/a | 0, /* sq_length */ |
---|
1969 | n/a | 0, /* sq_concat */ |
---|
1970 | n/a | 0, /* sq_repeat */ |
---|
1971 | n/a | (ssizeargfunc)ndarray_item, /* sq_item */ |
---|
1972 | n/a | }; |
---|
1973 | n/a | |
---|
1974 | n/a | |
---|
1975 | n/a | /**************************************************************************/ |
---|
1976 | n/a | /* getters */ |
---|
1977 | n/a | /**************************************************************************/ |
---|
1978 | n/a | |
---|
1979 | n/a | static PyObject * |
---|
1980 | n/a | ssize_array_as_tuple(Py_ssize_t *array, Py_ssize_t len) |
---|
1981 | n/a | { |
---|
1982 | n/a | PyObject *tuple, *x; |
---|
1983 | n/a | Py_ssize_t i; |
---|
1984 | n/a | |
---|
1985 | n/a | if (array == NULL) |
---|
1986 | n/a | return PyTuple_New(0); |
---|
1987 | n/a | |
---|
1988 | n/a | tuple = PyTuple_New(len); |
---|
1989 | n/a | if (tuple == NULL) |
---|
1990 | n/a | return NULL; |
---|
1991 | n/a | |
---|
1992 | n/a | for (i = 0; i < len; i++) { |
---|
1993 | n/a | x = PyLong_FromSsize_t(array[i]); |
---|
1994 | n/a | if (x == NULL) { |
---|
1995 | n/a | Py_DECREF(tuple); |
---|
1996 | n/a | return NULL; |
---|
1997 | n/a | } |
---|
1998 | n/a | PyTuple_SET_ITEM(tuple, i, x); |
---|
1999 | n/a | } |
---|
2000 | n/a | |
---|
2001 | n/a | return tuple; |
---|
2002 | n/a | } |
---|
2003 | n/a | |
---|
2004 | n/a | static PyObject * |
---|
2005 | n/a | ndarray_get_flags(NDArrayObject *self, void *closure) |
---|
2006 | n/a | { |
---|
2007 | n/a | return PyLong_FromLong(self->head->flags); |
---|
2008 | n/a | } |
---|
2009 | n/a | |
---|
2010 | n/a | static PyObject * |
---|
2011 | n/a | ndarray_get_offset(NDArrayObject *self, void *closure) |
---|
2012 | n/a | { |
---|
2013 | n/a | ndbuf_t *ndbuf = self->head; |
---|
2014 | n/a | return PyLong_FromSsize_t(ndbuf->offset); |
---|
2015 | n/a | } |
---|
2016 | n/a | |
---|
2017 | n/a | static PyObject * |
---|
2018 | n/a | ndarray_get_obj(NDArrayObject *self, void *closure) |
---|
2019 | n/a | { |
---|
2020 | n/a | Py_buffer *base = &self->head->base; |
---|
2021 | n/a | |
---|
2022 | n/a | if (base->obj == NULL) { |
---|
2023 | n/a | Py_RETURN_NONE; |
---|
2024 | n/a | } |
---|
2025 | n/a | Py_INCREF(base->obj); |
---|
2026 | n/a | return base->obj; |
---|
2027 | n/a | } |
---|
2028 | n/a | |
---|
2029 | n/a | static PyObject * |
---|
2030 | n/a | ndarray_get_nbytes(NDArrayObject *self, void *closure) |
---|
2031 | n/a | { |
---|
2032 | n/a | Py_buffer *base = &self->head->base; |
---|
2033 | n/a | return PyLong_FromSsize_t(base->len); |
---|
2034 | n/a | } |
---|
2035 | n/a | |
---|
2036 | n/a | static PyObject * |
---|
2037 | n/a | ndarray_get_readonly(NDArrayObject *self, void *closure) |
---|
2038 | n/a | { |
---|
2039 | n/a | Py_buffer *base = &self->head->base; |
---|
2040 | n/a | return PyLong_FromLong(base->readonly); |
---|
2041 | n/a | } |
---|
2042 | n/a | |
---|
2043 | n/a | static PyObject * |
---|
2044 | n/a | ndarray_get_itemsize(NDArrayObject *self, void *closure) |
---|
2045 | n/a | { |
---|
2046 | n/a | Py_buffer *base = &self->head->base; |
---|
2047 | n/a | return PyLong_FromSsize_t(base->itemsize); |
---|
2048 | n/a | } |
---|
2049 | n/a | |
---|
2050 | n/a | static PyObject * |
---|
2051 | n/a | ndarray_get_format(NDArrayObject *self, void *closure) |
---|
2052 | n/a | { |
---|
2053 | n/a | Py_buffer *base = &self->head->base; |
---|
2054 | n/a | char *fmt = base->format ? base->format : ""; |
---|
2055 | n/a | return PyUnicode_FromString(fmt); |
---|
2056 | n/a | } |
---|
2057 | n/a | |
---|
2058 | n/a | static PyObject * |
---|
2059 | n/a | ndarray_get_ndim(NDArrayObject *self, void *closure) |
---|
2060 | n/a | { |
---|
2061 | n/a | Py_buffer *base = &self->head->base; |
---|
2062 | n/a | return PyLong_FromSsize_t(base->ndim); |
---|
2063 | n/a | } |
---|
2064 | n/a | |
---|
2065 | n/a | static PyObject * |
---|
2066 | n/a | ndarray_get_shape(NDArrayObject *self, void *closure) |
---|
2067 | n/a | { |
---|
2068 | n/a | Py_buffer *base = &self->head->base; |
---|
2069 | n/a | return ssize_array_as_tuple(base->shape, base->ndim); |
---|
2070 | n/a | } |
---|
2071 | n/a | |
---|
2072 | n/a | static PyObject * |
---|
2073 | n/a | ndarray_get_strides(NDArrayObject *self, void *closure) |
---|
2074 | n/a | { |
---|
2075 | n/a | Py_buffer *base = &self->head->base; |
---|
2076 | n/a | return ssize_array_as_tuple(base->strides, base->ndim); |
---|
2077 | n/a | } |
---|
2078 | n/a | |
---|
2079 | n/a | static PyObject * |
---|
2080 | n/a | ndarray_get_suboffsets(NDArrayObject *self, void *closure) |
---|
2081 | n/a | { |
---|
2082 | n/a | Py_buffer *base = &self->head->base; |
---|
2083 | n/a | return ssize_array_as_tuple(base->suboffsets, base->ndim); |
---|
2084 | n/a | } |
---|
2085 | n/a | |
---|
2086 | n/a | static PyObject * |
---|
2087 | n/a | ndarray_c_contig(PyObject *self, PyObject *dummy) |
---|
2088 | n/a | { |
---|
2089 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
2090 | n/a | int ret = PyBuffer_IsContiguous(&nd->head->base, 'C'); |
---|
2091 | n/a | |
---|
2092 | n/a | if (ret != ND_C_CONTIGUOUS(nd->head->flags)) { |
---|
2093 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
2094 | n/a | "results from PyBuffer_IsContiguous() and flags differ"); |
---|
2095 | n/a | return NULL; |
---|
2096 | n/a | } |
---|
2097 | n/a | return PyBool_FromLong(ret); |
---|
2098 | n/a | } |
---|
2099 | n/a | |
---|
2100 | n/a | static PyObject * |
---|
2101 | n/a | ndarray_fortran_contig(PyObject *self, PyObject *dummy) |
---|
2102 | n/a | { |
---|
2103 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
2104 | n/a | int ret = PyBuffer_IsContiguous(&nd->head->base, 'F'); |
---|
2105 | n/a | |
---|
2106 | n/a | if (ret != ND_FORTRAN_CONTIGUOUS(nd->head->flags)) { |
---|
2107 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
2108 | n/a | "results from PyBuffer_IsContiguous() and flags differ"); |
---|
2109 | n/a | return NULL; |
---|
2110 | n/a | } |
---|
2111 | n/a | return PyBool_FromLong(ret); |
---|
2112 | n/a | } |
---|
2113 | n/a | |
---|
2114 | n/a | static PyObject * |
---|
2115 | n/a | ndarray_contig(PyObject *self, PyObject *dummy) |
---|
2116 | n/a | { |
---|
2117 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
2118 | n/a | int ret = PyBuffer_IsContiguous(&nd->head->base, 'A'); |
---|
2119 | n/a | |
---|
2120 | n/a | if (ret != ND_ANY_CONTIGUOUS(nd->head->flags)) { |
---|
2121 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
2122 | n/a | "results from PyBuffer_IsContiguous() and flags differ"); |
---|
2123 | n/a | return NULL; |
---|
2124 | n/a | } |
---|
2125 | n/a | return PyBool_FromLong(ret); |
---|
2126 | n/a | } |
---|
2127 | n/a | |
---|
2128 | n/a | |
---|
2129 | n/a | static PyGetSetDef ndarray_getset [] = |
---|
2130 | n/a | { |
---|
2131 | n/a | /* ndbuf */ |
---|
2132 | n/a | { "flags", (getter)ndarray_get_flags, NULL, NULL, NULL}, |
---|
2133 | n/a | { "offset", (getter)ndarray_get_offset, NULL, NULL, NULL}, |
---|
2134 | n/a | /* ndbuf.base */ |
---|
2135 | n/a | { "obj", (getter)ndarray_get_obj, NULL, NULL, NULL}, |
---|
2136 | n/a | { "nbytes", (getter)ndarray_get_nbytes, NULL, NULL, NULL}, |
---|
2137 | n/a | { "readonly", (getter)ndarray_get_readonly, NULL, NULL, NULL}, |
---|
2138 | n/a | { "itemsize", (getter)ndarray_get_itemsize, NULL, NULL, NULL}, |
---|
2139 | n/a | { "format", (getter)ndarray_get_format, NULL, NULL, NULL}, |
---|
2140 | n/a | { "ndim", (getter)ndarray_get_ndim, NULL, NULL, NULL}, |
---|
2141 | n/a | { "shape", (getter)ndarray_get_shape, NULL, NULL, NULL}, |
---|
2142 | n/a | { "strides", (getter)ndarray_get_strides, NULL, NULL, NULL}, |
---|
2143 | n/a | { "suboffsets", (getter)ndarray_get_suboffsets, NULL, NULL, NULL}, |
---|
2144 | n/a | { "c_contiguous", (getter)ndarray_c_contig, NULL, NULL, NULL}, |
---|
2145 | n/a | { "f_contiguous", (getter)ndarray_fortran_contig, NULL, NULL, NULL}, |
---|
2146 | n/a | { "contiguous", (getter)ndarray_contig, NULL, NULL, NULL}, |
---|
2147 | n/a | {NULL} |
---|
2148 | n/a | }; |
---|
2149 | n/a | |
---|
2150 | n/a | static PyObject * |
---|
2151 | n/a | ndarray_tolist(PyObject *self, PyObject *dummy) |
---|
2152 | n/a | { |
---|
2153 | n/a | return ndarray_as_list((NDArrayObject *)self); |
---|
2154 | n/a | } |
---|
2155 | n/a | |
---|
2156 | n/a | static PyObject * |
---|
2157 | n/a | ndarray_tobytes(PyObject *self, PyObject *dummy) |
---|
2158 | n/a | { |
---|
2159 | n/a | ndbuf_t *ndbuf = ((NDArrayObject *)self)->head; |
---|
2160 | n/a | Py_buffer *src = &ndbuf->base; |
---|
2161 | n/a | Py_buffer dest; |
---|
2162 | n/a | PyObject *ret = NULL; |
---|
2163 | n/a | char *mem; |
---|
2164 | n/a | |
---|
2165 | n/a | if (ND_C_CONTIGUOUS(ndbuf->flags)) |
---|
2166 | n/a | return PyBytes_FromStringAndSize(src->buf, src->len); |
---|
2167 | n/a | |
---|
2168 | n/a | assert(src->shape != NULL); |
---|
2169 | n/a | assert(src->strides != NULL); |
---|
2170 | n/a | assert(src->ndim > 0); |
---|
2171 | n/a | |
---|
2172 | n/a | mem = PyMem_Malloc(src->len); |
---|
2173 | n/a | if (mem == NULL) { |
---|
2174 | n/a | PyErr_NoMemory(); |
---|
2175 | n/a | return NULL; |
---|
2176 | n/a | } |
---|
2177 | n/a | |
---|
2178 | n/a | dest = *src; |
---|
2179 | n/a | dest.buf = mem; |
---|
2180 | n/a | dest.suboffsets = NULL; |
---|
2181 | n/a | dest.strides = strides_from_shape(ndbuf, 0); |
---|
2182 | n/a | if (dest.strides == NULL) |
---|
2183 | n/a | goto out; |
---|
2184 | n/a | if (copy_buffer(&dest, src) < 0) |
---|
2185 | n/a | goto out; |
---|
2186 | n/a | |
---|
2187 | n/a | ret = PyBytes_FromStringAndSize(mem, src->len); |
---|
2188 | n/a | |
---|
2189 | n/a | out: |
---|
2190 | n/a | PyMem_XFree(dest.strides); |
---|
2191 | n/a | PyMem_Free(mem); |
---|
2192 | n/a | return ret; |
---|
2193 | n/a | } |
---|
2194 | n/a | |
---|
2195 | n/a | /* add redundant (negative) suboffsets for testing */ |
---|
2196 | n/a | static PyObject * |
---|
2197 | n/a | ndarray_add_suboffsets(PyObject *self, PyObject *dummy) |
---|
2198 | n/a | { |
---|
2199 | n/a | NDArrayObject *nd = (NDArrayObject *)self; |
---|
2200 | n/a | Py_buffer *base = &nd->head->base; |
---|
2201 | n/a | Py_ssize_t i; |
---|
2202 | n/a | |
---|
2203 | n/a | if (base->suboffsets != NULL) { |
---|
2204 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2205 | n/a | "cannot add suboffsets to PIL-style array"); |
---|
2206 | n/a | return NULL; |
---|
2207 | n/a | } |
---|
2208 | n/a | if (base->strides == NULL) { |
---|
2209 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2210 | n/a | "cannot add suboffsets to array without strides"); |
---|
2211 | n/a | return NULL; |
---|
2212 | n/a | } |
---|
2213 | n/a | |
---|
2214 | n/a | base->suboffsets = PyMem_Malloc(base->ndim * (sizeof *base->suboffsets)); |
---|
2215 | n/a | if (base->suboffsets == NULL) { |
---|
2216 | n/a | PyErr_NoMemory(); |
---|
2217 | n/a | return NULL; |
---|
2218 | n/a | } |
---|
2219 | n/a | |
---|
2220 | n/a | for (i = 0; i < base->ndim; i++) |
---|
2221 | n/a | base->suboffsets[i] = -1; |
---|
2222 | n/a | |
---|
2223 | n/a | nd->head->flags &= ~(ND_C|ND_FORTRAN); |
---|
2224 | n/a | |
---|
2225 | n/a | Py_RETURN_NONE; |
---|
2226 | n/a | } |
---|
2227 | n/a | |
---|
2228 | n/a | /* Test PyMemoryView_FromBuffer(): return a memoryview from a static buffer. |
---|
2229 | n/a | Obviously this is fragile and only one such view may be active at any |
---|
2230 | n/a | time. Never use anything like this in real code! */ |
---|
2231 | n/a | static char *infobuf = NULL; |
---|
2232 | n/a | static PyObject * |
---|
2233 | n/a | ndarray_memoryview_from_buffer(PyObject *self, PyObject *dummy) |
---|
2234 | n/a | { |
---|
2235 | n/a | const NDArrayObject *nd = (NDArrayObject *)self; |
---|
2236 | n/a | const Py_buffer *view = &nd->head->base; |
---|
2237 | n/a | const ndbuf_t *ndbuf; |
---|
2238 | n/a | static char format[ND_MAX_NDIM+1]; |
---|
2239 | n/a | static Py_ssize_t shape[ND_MAX_NDIM]; |
---|
2240 | n/a | static Py_ssize_t strides[ND_MAX_NDIM]; |
---|
2241 | n/a | static Py_ssize_t suboffsets[ND_MAX_NDIM]; |
---|
2242 | n/a | static Py_buffer info; |
---|
2243 | n/a | char *p; |
---|
2244 | n/a | |
---|
2245 | n/a | if (!ND_IS_CONSUMER(nd)) |
---|
2246 | n/a | ndbuf = nd->head; /* self is ndarray/original exporter */ |
---|
2247 | n/a | else if (NDArray_Check(view->obj) && !ND_IS_CONSUMER(view->obj)) |
---|
2248 | n/a | /* self is ndarray and consumer from ndarray/original exporter */ |
---|
2249 | n/a | ndbuf = ((NDArrayObject *)view->obj)->head; |
---|
2250 | n/a | else { |
---|
2251 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2252 | n/a | "memoryview_from_buffer(): ndarray must be original exporter or " |
---|
2253 | n/a | "consumer from ndarray/original exporter"); |
---|
2254 | n/a | return NULL; |
---|
2255 | n/a | } |
---|
2256 | n/a | |
---|
2257 | n/a | info = *view; |
---|
2258 | n/a | p = PyMem_Realloc(infobuf, ndbuf->len); |
---|
2259 | n/a | if (p == NULL) { |
---|
2260 | n/a | PyMem_Free(infobuf); |
---|
2261 | n/a | PyErr_NoMemory(); |
---|
2262 | n/a | infobuf = NULL; |
---|
2263 | n/a | return NULL; |
---|
2264 | n/a | } |
---|
2265 | n/a | else { |
---|
2266 | n/a | infobuf = p; |
---|
2267 | n/a | } |
---|
2268 | n/a | /* copy the complete raw data */ |
---|
2269 | n/a | memcpy(infobuf, ndbuf->data, ndbuf->len); |
---|
2270 | n/a | info.buf = infobuf + ((char *)view->buf - ndbuf->data); |
---|
2271 | n/a | |
---|
2272 | n/a | if (view->format) { |
---|
2273 | n/a | if (strlen(view->format) > ND_MAX_NDIM) { |
---|
2274 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2275 | n/a | "memoryview_from_buffer: format is limited to %d characters", |
---|
2276 | n/a | ND_MAX_NDIM); |
---|
2277 | n/a | return NULL; |
---|
2278 | n/a | } |
---|
2279 | n/a | strcpy(format, view->format); |
---|
2280 | n/a | info.format = format; |
---|
2281 | n/a | } |
---|
2282 | n/a | if (view->ndim > ND_MAX_NDIM) { |
---|
2283 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2284 | n/a | "memoryview_from_buffer: ndim is limited to %d", ND_MAX_NDIM); |
---|
2285 | n/a | return NULL; |
---|
2286 | n/a | } |
---|
2287 | n/a | if (view->shape) { |
---|
2288 | n/a | memcpy(shape, view->shape, view->ndim * sizeof(Py_ssize_t)); |
---|
2289 | n/a | info.shape = shape; |
---|
2290 | n/a | } |
---|
2291 | n/a | if (view->strides) { |
---|
2292 | n/a | memcpy(strides, view->strides, view->ndim * sizeof(Py_ssize_t)); |
---|
2293 | n/a | info.strides = strides; |
---|
2294 | n/a | } |
---|
2295 | n/a | if (view->suboffsets) { |
---|
2296 | n/a | memcpy(suboffsets, view->suboffsets, view->ndim * sizeof(Py_ssize_t)); |
---|
2297 | n/a | info.suboffsets = suboffsets; |
---|
2298 | n/a | } |
---|
2299 | n/a | |
---|
2300 | n/a | return PyMemoryView_FromBuffer(&info); |
---|
2301 | n/a | } |
---|
2302 | n/a | |
---|
2303 | n/a | /* Get a single item from bufobj at the location specified by seq. |
---|
2304 | n/a | seq is a list or tuple of indices. The purpose of this function |
---|
2305 | n/a | is to check other functions against PyBuffer_GetPointer(). */ |
---|
2306 | n/a | static PyObject * |
---|
2307 | n/a | get_pointer(PyObject *self, PyObject *args) |
---|
2308 | n/a | { |
---|
2309 | n/a | PyObject *ret = NULL, *bufobj, *seq; |
---|
2310 | n/a | Py_buffer view; |
---|
2311 | n/a | Py_ssize_t indices[ND_MAX_NDIM]; |
---|
2312 | n/a | Py_ssize_t i; |
---|
2313 | n/a | void *ptr; |
---|
2314 | n/a | |
---|
2315 | n/a | if (!PyArg_ParseTuple(args, "OO", &bufobj, &seq)) { |
---|
2316 | n/a | return NULL; |
---|
2317 | n/a | } |
---|
2318 | n/a | |
---|
2319 | n/a | CHECK_LIST_OR_TUPLE(seq); |
---|
2320 | n/a | if (PyObject_GetBuffer(bufobj, &view, PyBUF_FULL_RO) < 0) |
---|
2321 | n/a | return NULL; |
---|
2322 | n/a | |
---|
2323 | n/a | if (view.ndim > ND_MAX_NDIM) { |
---|
2324 | n/a | PyErr_Format(PyExc_ValueError, |
---|
2325 | n/a | "get_pointer(): ndim > %d", ND_MAX_NDIM); |
---|
2326 | n/a | goto out; |
---|
2327 | n/a | } |
---|
2328 | n/a | if (PySequence_Fast_GET_SIZE(seq) != view.ndim) { |
---|
2329 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2330 | n/a | "get_pointer(): len(indices) != ndim"); |
---|
2331 | n/a | goto out; |
---|
2332 | n/a | } |
---|
2333 | n/a | |
---|
2334 | n/a | for (i = 0; i < view.ndim; i++) { |
---|
2335 | n/a | PyObject *x = PySequence_Fast_GET_ITEM(seq, i); |
---|
2336 | n/a | indices[i] = PyLong_AsSsize_t(x); |
---|
2337 | n/a | if (PyErr_Occurred()) |
---|
2338 | n/a | goto out; |
---|
2339 | n/a | if (indices[i] < 0 || indices[i] >= view.shape[i]) { |
---|
2340 | n/a | PyErr_Format(PyExc_ValueError, |
---|
2341 | n/a | "get_pointer(): invalid index %zd at position %zd", |
---|
2342 | n/a | indices[i], i); |
---|
2343 | n/a | goto out; |
---|
2344 | n/a | } |
---|
2345 | n/a | } |
---|
2346 | n/a | |
---|
2347 | n/a | ptr = PyBuffer_GetPointer(&view, indices); |
---|
2348 | n/a | ret = unpack_single(ptr, view.format, view.itemsize); |
---|
2349 | n/a | |
---|
2350 | n/a | out: |
---|
2351 | n/a | PyBuffer_Release(&view); |
---|
2352 | n/a | return ret; |
---|
2353 | n/a | } |
---|
2354 | n/a | |
---|
2355 | n/a | static PyObject * |
---|
2356 | n/a | get_sizeof_void_p(PyObject *self) |
---|
2357 | n/a | { |
---|
2358 | n/a | return PyLong_FromSize_t(sizeof(void *)); |
---|
2359 | n/a | } |
---|
2360 | n/a | |
---|
2361 | n/a | static char |
---|
2362 | n/a | get_ascii_order(PyObject *order) |
---|
2363 | n/a | { |
---|
2364 | n/a | PyObject *ascii_order; |
---|
2365 | n/a | char ord; |
---|
2366 | n/a | |
---|
2367 | n/a | if (!PyUnicode_Check(order)) { |
---|
2368 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2369 | n/a | "order must be a string"); |
---|
2370 | n/a | return CHAR_MAX; |
---|
2371 | n/a | } |
---|
2372 | n/a | |
---|
2373 | n/a | ascii_order = PyUnicode_AsASCIIString(order); |
---|
2374 | n/a | if (ascii_order == NULL) { |
---|
2375 | n/a | return CHAR_MAX; |
---|
2376 | n/a | } |
---|
2377 | n/a | |
---|
2378 | n/a | ord = PyBytes_AS_STRING(ascii_order)[0]; |
---|
2379 | n/a | Py_DECREF(ascii_order); |
---|
2380 | n/a | |
---|
2381 | n/a | if (ord != 'C' && ord != 'F' && ord != 'A') { |
---|
2382 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2383 | n/a | "invalid order, must be C, F or A"); |
---|
2384 | n/a | return CHAR_MAX; |
---|
2385 | n/a | } |
---|
2386 | n/a | |
---|
2387 | n/a | return ord; |
---|
2388 | n/a | } |
---|
2389 | n/a | |
---|
2390 | n/a | /* Get a contiguous memoryview. */ |
---|
2391 | n/a | static PyObject * |
---|
2392 | n/a | get_contiguous(PyObject *self, PyObject *args) |
---|
2393 | n/a | { |
---|
2394 | n/a | PyObject *obj; |
---|
2395 | n/a | PyObject *buffertype; |
---|
2396 | n/a | PyObject *order; |
---|
2397 | n/a | long type; |
---|
2398 | n/a | char ord; |
---|
2399 | n/a | |
---|
2400 | n/a | if (!PyArg_ParseTuple(args, "OOO", &obj, &buffertype, &order)) { |
---|
2401 | n/a | return NULL; |
---|
2402 | n/a | } |
---|
2403 | n/a | |
---|
2404 | n/a | if (!PyLong_Check(buffertype)) { |
---|
2405 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2406 | n/a | "buffertype must be PyBUF_READ or PyBUF_WRITE"); |
---|
2407 | n/a | return NULL; |
---|
2408 | n/a | } |
---|
2409 | n/a | |
---|
2410 | n/a | type = PyLong_AsLong(buffertype); |
---|
2411 | n/a | if (type == -1 && PyErr_Occurred()) { |
---|
2412 | n/a | return NULL; |
---|
2413 | n/a | } |
---|
2414 | n/a | if (type != PyBUF_READ && type != PyBUF_WRITE) { |
---|
2415 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2416 | n/a | "invalid buffer type"); |
---|
2417 | n/a | return NULL; |
---|
2418 | n/a | } |
---|
2419 | n/a | |
---|
2420 | n/a | ord = get_ascii_order(order); |
---|
2421 | n/a | if (ord == CHAR_MAX) |
---|
2422 | n/a | return NULL; |
---|
2423 | n/a | |
---|
2424 | n/a | return PyMemoryView_GetContiguous(obj, (int)type, ord); |
---|
2425 | n/a | } |
---|
2426 | n/a | |
---|
2427 | n/a | /* PyBuffer_ToContiguous() */ |
---|
2428 | n/a | static PyObject * |
---|
2429 | n/a | py_buffer_to_contiguous(PyObject *self, PyObject *args) |
---|
2430 | n/a | { |
---|
2431 | n/a | PyObject *obj; |
---|
2432 | n/a | PyObject *order; |
---|
2433 | n/a | PyObject *ret = NULL; |
---|
2434 | n/a | int flags; |
---|
2435 | n/a | char ord; |
---|
2436 | n/a | Py_buffer view; |
---|
2437 | n/a | char *buf = NULL; |
---|
2438 | n/a | |
---|
2439 | n/a | if (!PyArg_ParseTuple(args, "OOi", &obj, &order, &flags)) { |
---|
2440 | n/a | return NULL; |
---|
2441 | n/a | } |
---|
2442 | n/a | |
---|
2443 | n/a | if (PyObject_GetBuffer(obj, &view, flags) < 0) { |
---|
2444 | n/a | return NULL; |
---|
2445 | n/a | } |
---|
2446 | n/a | |
---|
2447 | n/a | ord = get_ascii_order(order); |
---|
2448 | n/a | if (ord == CHAR_MAX) { |
---|
2449 | n/a | goto out; |
---|
2450 | n/a | } |
---|
2451 | n/a | |
---|
2452 | n/a | buf = PyMem_Malloc(view.len); |
---|
2453 | n/a | if (buf == NULL) { |
---|
2454 | n/a | PyErr_NoMemory(); |
---|
2455 | n/a | goto out; |
---|
2456 | n/a | } |
---|
2457 | n/a | |
---|
2458 | n/a | if (PyBuffer_ToContiguous(buf, &view, view.len, ord) < 0) { |
---|
2459 | n/a | goto out; |
---|
2460 | n/a | } |
---|
2461 | n/a | |
---|
2462 | n/a | ret = PyBytes_FromStringAndSize(buf, view.len); |
---|
2463 | n/a | |
---|
2464 | n/a | out: |
---|
2465 | n/a | PyBuffer_Release(&view); |
---|
2466 | n/a | PyMem_XFree(buf); |
---|
2467 | n/a | return ret; |
---|
2468 | n/a | } |
---|
2469 | n/a | |
---|
2470 | n/a | static int |
---|
2471 | n/a | fmtcmp(const char *fmt1, const char *fmt2) |
---|
2472 | n/a | { |
---|
2473 | n/a | if (fmt1 == NULL) { |
---|
2474 | n/a | return fmt2 == NULL || strcmp(fmt2, "B") == 0; |
---|
2475 | n/a | } |
---|
2476 | n/a | if (fmt2 == NULL) { |
---|
2477 | n/a | return fmt1 == NULL || strcmp(fmt1, "B") == 0; |
---|
2478 | n/a | } |
---|
2479 | n/a | return strcmp(fmt1, fmt2) == 0; |
---|
2480 | n/a | } |
---|
2481 | n/a | |
---|
2482 | n/a | static int |
---|
2483 | n/a | arraycmp(const Py_ssize_t *a1, const Py_ssize_t *a2, const Py_ssize_t *shape, |
---|
2484 | n/a | Py_ssize_t ndim) |
---|
2485 | n/a | { |
---|
2486 | n/a | Py_ssize_t i; |
---|
2487 | n/a | |
---|
2488 | n/a | |
---|
2489 | n/a | for (i = 0; i < ndim; i++) { |
---|
2490 | n/a | if (shape && shape[i] <= 1) { |
---|
2491 | n/a | /* strides can differ if the dimension is less than 2 */ |
---|
2492 | n/a | continue; |
---|
2493 | n/a | } |
---|
2494 | n/a | if (a1[i] != a2[i]) { |
---|
2495 | n/a | return 0; |
---|
2496 | n/a | } |
---|
2497 | n/a | } |
---|
2498 | n/a | |
---|
2499 | n/a | return 1; |
---|
2500 | n/a | } |
---|
2501 | n/a | |
---|
2502 | n/a | /* Compare two contiguous buffers for physical equality. */ |
---|
2503 | n/a | static PyObject * |
---|
2504 | n/a | cmp_contig(PyObject *self, PyObject *args) |
---|
2505 | n/a | { |
---|
2506 | n/a | PyObject *b1, *b2; /* buffer objects */ |
---|
2507 | n/a | Py_buffer v1, v2; |
---|
2508 | n/a | PyObject *ret; |
---|
2509 | n/a | int equal = 0; |
---|
2510 | n/a | |
---|
2511 | n/a | if (!PyArg_ParseTuple(args, "OO", &b1, &b2)) { |
---|
2512 | n/a | return NULL; |
---|
2513 | n/a | } |
---|
2514 | n/a | |
---|
2515 | n/a | if (PyObject_GetBuffer(b1, &v1, PyBUF_FULL_RO) < 0) { |
---|
2516 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2517 | n/a | "cmp_contig: first argument does not implement the buffer " |
---|
2518 | n/a | "protocol"); |
---|
2519 | n/a | return NULL; |
---|
2520 | n/a | } |
---|
2521 | n/a | if (PyObject_GetBuffer(b2, &v2, PyBUF_FULL_RO) < 0) { |
---|
2522 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2523 | n/a | "cmp_contig: second argument does not implement the buffer " |
---|
2524 | n/a | "protocol"); |
---|
2525 | n/a | PyBuffer_Release(&v1); |
---|
2526 | n/a | return NULL; |
---|
2527 | n/a | } |
---|
2528 | n/a | |
---|
2529 | n/a | if (!(PyBuffer_IsContiguous(&v1, 'C')&&PyBuffer_IsContiguous(&v2, 'C')) && |
---|
2530 | n/a | !(PyBuffer_IsContiguous(&v1, 'F')&&PyBuffer_IsContiguous(&v2, 'F'))) { |
---|
2531 | n/a | goto result; |
---|
2532 | n/a | } |
---|
2533 | n/a | |
---|
2534 | n/a | /* readonly may differ if created from non-contiguous */ |
---|
2535 | n/a | if (v1.len != v2.len || |
---|
2536 | n/a | v1.itemsize != v2.itemsize || |
---|
2537 | n/a | v1.ndim != v2.ndim || |
---|
2538 | n/a | !fmtcmp(v1.format, v2.format) || |
---|
2539 | n/a | !!v1.shape != !!v2.shape || |
---|
2540 | n/a | !!v1.strides != !!v2.strides || |
---|
2541 | n/a | !!v1.suboffsets != !!v2.suboffsets) { |
---|
2542 | n/a | goto result; |
---|
2543 | n/a | } |
---|
2544 | n/a | |
---|
2545 | n/a | if ((v1.shape && !arraycmp(v1.shape, v2.shape, NULL, v1.ndim)) || |
---|
2546 | n/a | (v1.strides && !arraycmp(v1.strides, v2.strides, v1.shape, v1.ndim)) || |
---|
2547 | n/a | (v1.suboffsets && !arraycmp(v1.suboffsets, v2.suboffsets, NULL, |
---|
2548 | n/a | v1.ndim))) { |
---|
2549 | n/a | goto result; |
---|
2550 | n/a | } |
---|
2551 | n/a | |
---|
2552 | n/a | if (memcmp((char *)v1.buf, (char *)v2.buf, v1.len) != 0) { |
---|
2553 | n/a | goto result; |
---|
2554 | n/a | } |
---|
2555 | n/a | |
---|
2556 | n/a | equal = 1; |
---|
2557 | n/a | |
---|
2558 | n/a | result: |
---|
2559 | n/a | PyBuffer_Release(&v1); |
---|
2560 | n/a | PyBuffer_Release(&v2); |
---|
2561 | n/a | |
---|
2562 | n/a | ret = equal ? Py_True : Py_False; |
---|
2563 | n/a | Py_INCREF(ret); |
---|
2564 | n/a | return ret; |
---|
2565 | n/a | } |
---|
2566 | n/a | |
---|
2567 | n/a | static PyObject * |
---|
2568 | n/a | is_contiguous(PyObject *self, PyObject *args) |
---|
2569 | n/a | { |
---|
2570 | n/a | PyObject *obj; |
---|
2571 | n/a | PyObject *order; |
---|
2572 | n/a | PyObject *ret = NULL; |
---|
2573 | n/a | Py_buffer view, *base; |
---|
2574 | n/a | char ord; |
---|
2575 | n/a | |
---|
2576 | n/a | if (!PyArg_ParseTuple(args, "OO", &obj, &order)) { |
---|
2577 | n/a | return NULL; |
---|
2578 | n/a | } |
---|
2579 | n/a | |
---|
2580 | n/a | ord = get_ascii_order(order); |
---|
2581 | n/a | if (ord == CHAR_MAX) { |
---|
2582 | n/a | return NULL; |
---|
2583 | n/a | } |
---|
2584 | n/a | |
---|
2585 | n/a | if (NDArray_Check(obj)) { |
---|
2586 | n/a | /* Skip the buffer protocol to check simple etc. buffers directly. */ |
---|
2587 | n/a | base = &((NDArrayObject *)obj)->head->base; |
---|
2588 | n/a | ret = PyBuffer_IsContiguous(base, ord) ? Py_True : Py_False; |
---|
2589 | n/a | } |
---|
2590 | n/a | else { |
---|
2591 | n/a | if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) { |
---|
2592 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2593 | n/a | "is_contiguous: object does not implement the buffer " |
---|
2594 | n/a | "protocol"); |
---|
2595 | n/a | return NULL; |
---|
2596 | n/a | } |
---|
2597 | n/a | ret = PyBuffer_IsContiguous(&view, ord) ? Py_True : Py_False; |
---|
2598 | n/a | PyBuffer_Release(&view); |
---|
2599 | n/a | } |
---|
2600 | n/a | |
---|
2601 | n/a | Py_INCREF(ret); |
---|
2602 | n/a | return ret; |
---|
2603 | n/a | } |
---|
2604 | n/a | |
---|
2605 | n/a | static Py_hash_t |
---|
2606 | n/a | ndarray_hash(PyObject *self) |
---|
2607 | n/a | { |
---|
2608 | n/a | const NDArrayObject *nd = (NDArrayObject *)self; |
---|
2609 | n/a | const Py_buffer *view = &nd->head->base; |
---|
2610 | n/a | PyObject *bytes; |
---|
2611 | n/a | Py_hash_t hash; |
---|
2612 | n/a | |
---|
2613 | n/a | if (!view->readonly) { |
---|
2614 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
2615 | n/a | "cannot hash writable ndarray object"); |
---|
2616 | n/a | return -1; |
---|
2617 | n/a | } |
---|
2618 | n/a | if (view->obj != NULL && PyObject_Hash(view->obj) == -1) { |
---|
2619 | n/a | return -1; |
---|
2620 | n/a | } |
---|
2621 | n/a | |
---|
2622 | n/a | bytes = ndarray_tobytes(self, NULL); |
---|
2623 | n/a | if (bytes == NULL) { |
---|
2624 | n/a | return -1; |
---|
2625 | n/a | } |
---|
2626 | n/a | |
---|
2627 | n/a | hash = PyObject_Hash(bytes); |
---|
2628 | n/a | Py_DECREF(bytes); |
---|
2629 | n/a | return hash; |
---|
2630 | n/a | } |
---|
2631 | n/a | |
---|
2632 | n/a | |
---|
2633 | n/a | static PyMethodDef ndarray_methods [] = |
---|
2634 | n/a | { |
---|
2635 | n/a | { "tolist", ndarray_tolist, METH_NOARGS, NULL }, |
---|
2636 | n/a | { "tobytes", ndarray_tobytes, METH_NOARGS, NULL }, |
---|
2637 | n/a | { "push", (PyCFunction)ndarray_push, METH_VARARGS|METH_KEYWORDS, NULL }, |
---|
2638 | n/a | { "pop", ndarray_pop, METH_NOARGS, NULL }, |
---|
2639 | n/a | { "add_suboffsets", ndarray_add_suboffsets, METH_NOARGS, NULL }, |
---|
2640 | n/a | { "memoryview_from_buffer", ndarray_memoryview_from_buffer, METH_NOARGS, NULL }, |
---|
2641 | n/a | {NULL} |
---|
2642 | n/a | }; |
---|
2643 | n/a | |
---|
2644 | n/a | static PyTypeObject NDArray_Type = { |
---|
2645 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
2646 | n/a | "ndarray", /* Name of this type */ |
---|
2647 | n/a | sizeof(NDArrayObject), /* Basic object size */ |
---|
2648 | n/a | 0, /* Item size for varobject */ |
---|
2649 | n/a | (destructor)ndarray_dealloc, /* tp_dealloc */ |
---|
2650 | n/a | 0, /* tp_print */ |
---|
2651 | n/a | 0, /* tp_getattr */ |
---|
2652 | n/a | 0, /* tp_setattr */ |
---|
2653 | n/a | 0, /* tp_compare */ |
---|
2654 | n/a | 0, /* tp_repr */ |
---|
2655 | n/a | 0, /* tp_as_number */ |
---|
2656 | n/a | &ndarray_as_sequence, /* tp_as_sequence */ |
---|
2657 | n/a | &ndarray_as_mapping, /* tp_as_mapping */ |
---|
2658 | n/a | (hashfunc)ndarray_hash, /* tp_hash */ |
---|
2659 | n/a | 0, /* tp_call */ |
---|
2660 | n/a | 0, /* tp_str */ |
---|
2661 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
2662 | n/a | 0, /* tp_setattro */ |
---|
2663 | n/a | &ndarray_as_buffer, /* tp_as_buffer */ |
---|
2664 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
2665 | n/a | 0, /* tp_doc */ |
---|
2666 | n/a | 0, /* tp_traverse */ |
---|
2667 | n/a | 0, /* tp_clear */ |
---|
2668 | n/a | 0, /* tp_richcompare */ |
---|
2669 | n/a | 0, /* tp_weaklistoffset */ |
---|
2670 | n/a | 0, /* tp_iter */ |
---|
2671 | n/a | 0, /* tp_iternext */ |
---|
2672 | n/a | ndarray_methods, /* tp_methods */ |
---|
2673 | n/a | 0, /* tp_members */ |
---|
2674 | n/a | ndarray_getset, /* tp_getset */ |
---|
2675 | n/a | 0, /* tp_base */ |
---|
2676 | n/a | 0, /* tp_dict */ |
---|
2677 | n/a | 0, /* tp_descr_get */ |
---|
2678 | n/a | 0, /* tp_descr_set */ |
---|
2679 | n/a | 0, /* tp_dictoffset */ |
---|
2680 | n/a | ndarray_init, /* tp_init */ |
---|
2681 | n/a | 0, /* tp_alloc */ |
---|
2682 | n/a | ndarray_new, /* tp_new */ |
---|
2683 | n/a | }; |
---|
2684 | n/a | |
---|
2685 | n/a | /**************************************************************************/ |
---|
2686 | n/a | /* StaticArray Object */ |
---|
2687 | n/a | /**************************************************************************/ |
---|
2688 | n/a | |
---|
2689 | n/a | static PyTypeObject StaticArray_Type; |
---|
2690 | n/a | |
---|
2691 | n/a | typedef struct { |
---|
2692 | n/a | PyObject_HEAD |
---|
2693 | n/a | int legacy_mode; /* if true, use the view.obj==NULL hack */ |
---|
2694 | n/a | } StaticArrayObject; |
---|
2695 | n/a | |
---|
2696 | n/a | static char static_mem[12] = {0,1,2,3,4,5,6,7,8,9,10,11}; |
---|
2697 | n/a | static Py_ssize_t static_shape[1] = {12}; |
---|
2698 | n/a | static Py_ssize_t static_strides[1] = {1}; |
---|
2699 | n/a | static Py_buffer static_buffer = { |
---|
2700 | n/a | static_mem, /* buf */ |
---|
2701 | n/a | NULL, /* obj */ |
---|
2702 | n/a | 12, /* len */ |
---|
2703 | n/a | 1, /* itemsize */ |
---|
2704 | n/a | 1, /* readonly */ |
---|
2705 | n/a | 1, /* ndim */ |
---|
2706 | n/a | "B", /* format */ |
---|
2707 | n/a | static_shape, /* shape */ |
---|
2708 | n/a | static_strides, /* strides */ |
---|
2709 | n/a | NULL, /* suboffsets */ |
---|
2710 | n/a | NULL /* internal */ |
---|
2711 | n/a | }; |
---|
2712 | n/a | |
---|
2713 | n/a | static PyObject * |
---|
2714 | n/a | staticarray_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
2715 | n/a | { |
---|
2716 | n/a | return (PyObject *)PyObject_New(StaticArrayObject, &StaticArray_Type); |
---|
2717 | n/a | } |
---|
2718 | n/a | |
---|
2719 | n/a | static int |
---|
2720 | n/a | staticarray_init(PyObject *self, PyObject *args, PyObject *kwds) |
---|
2721 | n/a | { |
---|
2722 | n/a | StaticArrayObject *a = (StaticArrayObject *)self; |
---|
2723 | n/a | static char *kwlist[] = { |
---|
2724 | n/a | "legacy_mode", NULL |
---|
2725 | n/a | }; |
---|
2726 | n/a | PyObject *legacy_mode = Py_False; |
---|
2727 | n/a | |
---|
2728 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &legacy_mode)) |
---|
2729 | n/a | return -1; |
---|
2730 | n/a | |
---|
2731 | n/a | a->legacy_mode = (legacy_mode != Py_False); |
---|
2732 | n/a | return 0; |
---|
2733 | n/a | } |
---|
2734 | n/a | |
---|
2735 | n/a | static void |
---|
2736 | n/a | staticarray_dealloc(StaticArrayObject *self) |
---|
2737 | n/a | { |
---|
2738 | n/a | PyObject_Del(self); |
---|
2739 | n/a | } |
---|
2740 | n/a | |
---|
2741 | n/a | /* Return a buffer for a PyBUF_FULL_RO request. Flags are not checked, |
---|
2742 | n/a | which makes this object a non-compliant exporter! */ |
---|
2743 | n/a | static int |
---|
2744 | n/a | staticarray_getbuf(StaticArrayObject *self, Py_buffer *view, int flags) |
---|
2745 | n/a | { |
---|
2746 | n/a | *view = static_buffer; |
---|
2747 | n/a | |
---|
2748 | n/a | if (self->legacy_mode) { |
---|
2749 | n/a | view->obj = NULL; /* Don't use this in new code. */ |
---|
2750 | n/a | } |
---|
2751 | n/a | else { |
---|
2752 | n/a | view->obj = (PyObject *)self; |
---|
2753 | n/a | Py_INCREF(view->obj); |
---|
2754 | n/a | } |
---|
2755 | n/a | |
---|
2756 | n/a | return 0; |
---|
2757 | n/a | } |
---|
2758 | n/a | |
---|
2759 | n/a | static PyBufferProcs staticarray_as_buffer = { |
---|
2760 | n/a | (getbufferproc)staticarray_getbuf, /* bf_getbuffer */ |
---|
2761 | n/a | NULL, /* bf_releasebuffer */ |
---|
2762 | n/a | }; |
---|
2763 | n/a | |
---|
2764 | n/a | static PyTypeObject StaticArray_Type = { |
---|
2765 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
2766 | n/a | "staticarray", /* Name of this type */ |
---|
2767 | n/a | sizeof(StaticArrayObject), /* Basic object size */ |
---|
2768 | n/a | 0, /* Item size for varobject */ |
---|
2769 | n/a | (destructor)staticarray_dealloc, /* tp_dealloc */ |
---|
2770 | n/a | 0, /* tp_print */ |
---|
2771 | n/a | 0, /* tp_getattr */ |
---|
2772 | n/a | 0, /* tp_setattr */ |
---|
2773 | n/a | 0, /* tp_compare */ |
---|
2774 | n/a | 0, /* tp_repr */ |
---|
2775 | n/a | 0, /* tp_as_number */ |
---|
2776 | n/a | 0, /* tp_as_sequence */ |
---|
2777 | n/a | 0, /* tp_as_mapping */ |
---|
2778 | n/a | 0, /* tp_hash */ |
---|
2779 | n/a | 0, /* tp_call */ |
---|
2780 | n/a | 0, /* tp_str */ |
---|
2781 | n/a | 0, /* tp_getattro */ |
---|
2782 | n/a | 0, /* tp_setattro */ |
---|
2783 | n/a | &staticarray_as_buffer, /* tp_as_buffer */ |
---|
2784 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
2785 | n/a | 0, /* tp_doc */ |
---|
2786 | n/a | 0, /* tp_traverse */ |
---|
2787 | n/a | 0, /* tp_clear */ |
---|
2788 | n/a | 0, /* tp_richcompare */ |
---|
2789 | n/a | 0, /* tp_weaklistoffset */ |
---|
2790 | n/a | 0, /* tp_iter */ |
---|
2791 | n/a | 0, /* tp_iternext */ |
---|
2792 | n/a | 0, /* tp_methods */ |
---|
2793 | n/a | 0, /* tp_members */ |
---|
2794 | n/a | 0, /* tp_getset */ |
---|
2795 | n/a | 0, /* tp_base */ |
---|
2796 | n/a | 0, /* tp_dict */ |
---|
2797 | n/a | 0, /* tp_descr_get */ |
---|
2798 | n/a | 0, /* tp_descr_set */ |
---|
2799 | n/a | 0, /* tp_dictoffset */ |
---|
2800 | n/a | staticarray_init, /* tp_init */ |
---|
2801 | n/a | 0, /* tp_alloc */ |
---|
2802 | n/a | staticarray_new, /* tp_new */ |
---|
2803 | n/a | }; |
---|
2804 | n/a | |
---|
2805 | n/a | |
---|
2806 | n/a | static struct PyMethodDef _testbuffer_functions[] = { |
---|
2807 | n/a | {"slice_indices", slice_indices, METH_VARARGS, NULL}, |
---|
2808 | n/a | {"get_pointer", get_pointer, METH_VARARGS, NULL}, |
---|
2809 | n/a | {"get_sizeof_void_p", (PyCFunction)get_sizeof_void_p, METH_NOARGS, NULL}, |
---|
2810 | n/a | {"get_contiguous", get_contiguous, METH_VARARGS, NULL}, |
---|
2811 | n/a | {"py_buffer_to_contiguous", py_buffer_to_contiguous, METH_VARARGS, NULL}, |
---|
2812 | n/a | {"is_contiguous", is_contiguous, METH_VARARGS, NULL}, |
---|
2813 | n/a | {"cmp_contig", cmp_contig, METH_VARARGS, NULL}, |
---|
2814 | n/a | {NULL, NULL} |
---|
2815 | n/a | }; |
---|
2816 | n/a | |
---|
2817 | n/a | static struct PyModuleDef _testbuffermodule = { |
---|
2818 | n/a | PyModuleDef_HEAD_INIT, |
---|
2819 | n/a | "_testbuffer", |
---|
2820 | n/a | NULL, |
---|
2821 | n/a | -1, |
---|
2822 | n/a | _testbuffer_functions, |
---|
2823 | n/a | NULL, |
---|
2824 | n/a | NULL, |
---|
2825 | n/a | NULL, |
---|
2826 | n/a | NULL |
---|
2827 | n/a | }; |
---|
2828 | n/a | |
---|
2829 | n/a | |
---|
2830 | n/a | PyMODINIT_FUNC |
---|
2831 | n/a | PyInit__testbuffer(void) |
---|
2832 | n/a | { |
---|
2833 | n/a | PyObject *m; |
---|
2834 | n/a | |
---|
2835 | n/a | m = PyModule_Create(&_testbuffermodule); |
---|
2836 | n/a | if (m == NULL) |
---|
2837 | n/a | return NULL; |
---|
2838 | n/a | |
---|
2839 | n/a | Py_TYPE(&NDArray_Type) = &PyType_Type; |
---|
2840 | n/a | Py_INCREF(&NDArray_Type); |
---|
2841 | n/a | PyModule_AddObject(m, "ndarray", (PyObject *)&NDArray_Type); |
---|
2842 | n/a | |
---|
2843 | n/a | Py_TYPE(&StaticArray_Type) = &PyType_Type; |
---|
2844 | n/a | Py_INCREF(&StaticArray_Type); |
---|
2845 | n/a | PyModule_AddObject(m, "staticarray", (PyObject *)&StaticArray_Type); |
---|
2846 | n/a | |
---|
2847 | n/a | structmodule = PyImport_ImportModule("struct"); |
---|
2848 | n/a | if (structmodule == NULL) |
---|
2849 | n/a | return NULL; |
---|
2850 | n/a | |
---|
2851 | n/a | Struct = PyObject_GetAttrString(structmodule, "Struct"); |
---|
2852 | n/a | calcsize = PyObject_GetAttrString(structmodule, "calcsize"); |
---|
2853 | n/a | if (Struct == NULL || calcsize == NULL) |
---|
2854 | n/a | return NULL; |
---|
2855 | n/a | |
---|
2856 | n/a | simple_format = PyUnicode_FromString(simple_fmt); |
---|
2857 | n/a | if (simple_format == NULL) |
---|
2858 | n/a | return NULL; |
---|
2859 | n/a | |
---|
2860 | n/a | PyModule_AddIntMacro(m, ND_MAX_NDIM); |
---|
2861 | n/a | PyModule_AddIntMacro(m, ND_VAREXPORT); |
---|
2862 | n/a | PyModule_AddIntMacro(m, ND_WRITABLE); |
---|
2863 | n/a | PyModule_AddIntMacro(m, ND_FORTRAN); |
---|
2864 | n/a | PyModule_AddIntMacro(m, ND_SCALAR); |
---|
2865 | n/a | PyModule_AddIntMacro(m, ND_PIL); |
---|
2866 | n/a | PyModule_AddIntMacro(m, ND_GETBUF_FAIL); |
---|
2867 | n/a | PyModule_AddIntMacro(m, ND_GETBUF_UNDEFINED); |
---|
2868 | n/a | PyModule_AddIntMacro(m, ND_REDIRECT); |
---|
2869 | n/a | |
---|
2870 | n/a | PyModule_AddIntMacro(m, PyBUF_SIMPLE); |
---|
2871 | n/a | PyModule_AddIntMacro(m, PyBUF_WRITABLE); |
---|
2872 | n/a | PyModule_AddIntMacro(m, PyBUF_FORMAT); |
---|
2873 | n/a | PyModule_AddIntMacro(m, PyBUF_ND); |
---|
2874 | n/a | PyModule_AddIntMacro(m, PyBUF_STRIDES); |
---|
2875 | n/a | PyModule_AddIntMacro(m, PyBUF_INDIRECT); |
---|
2876 | n/a | PyModule_AddIntMacro(m, PyBUF_C_CONTIGUOUS); |
---|
2877 | n/a | PyModule_AddIntMacro(m, PyBUF_F_CONTIGUOUS); |
---|
2878 | n/a | PyModule_AddIntMacro(m, PyBUF_ANY_CONTIGUOUS); |
---|
2879 | n/a | PyModule_AddIntMacro(m, PyBUF_FULL); |
---|
2880 | n/a | PyModule_AddIntMacro(m, PyBUF_FULL_RO); |
---|
2881 | n/a | PyModule_AddIntMacro(m, PyBUF_RECORDS); |
---|
2882 | n/a | PyModule_AddIntMacro(m, PyBUF_RECORDS_RO); |
---|
2883 | n/a | PyModule_AddIntMacro(m, PyBUF_STRIDED); |
---|
2884 | n/a | PyModule_AddIntMacro(m, PyBUF_STRIDED_RO); |
---|
2885 | n/a | PyModule_AddIntMacro(m, PyBUF_CONTIG); |
---|
2886 | n/a | PyModule_AddIntMacro(m, PyBUF_CONTIG_RO); |
---|
2887 | n/a | |
---|
2888 | n/a | PyModule_AddIntMacro(m, PyBUF_READ); |
---|
2889 | n/a | PyModule_AddIntMacro(m, PyBUF_WRITE); |
---|
2890 | n/a | |
---|
2891 | n/a | return m; |
---|
2892 | n/a | } |
---|
2893 | n/a | |
---|
2894 | n/a | |
---|
2895 | n/a | |
---|