File: | numpy/core/src/multiarray/getset.c |
Warning: | line 675, column 21 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* Array Descr Object */ | |||
2 | ||||
3 | #define PY_SSIZE_T_CLEAN | |||
4 | #include <Python.h> | |||
5 | #include "structmember.h" | |||
6 | ||||
7 | #define NPY_NO_DEPRECATED_API0x0000000E NPY_API_VERSION0x0000000E | |||
8 | #define _MULTIARRAYMODULE | |||
9 | #include "numpy/arrayobject.h" | |||
10 | ||||
11 | #include "npy_config.h" | |||
12 | #include "npy_pycompat.h" | |||
13 | #include "npy_import.h" | |||
14 | ||||
15 | #include "common.h" | |||
16 | #include "conversion_utils.h" | |||
17 | #include "ctors.h" | |||
18 | #include "scalartypes.h" | |||
19 | #include "descriptor.h" | |||
20 | #include "getset.h" | |||
21 | #include "arrayobject.h" | |||
22 | #include "mem_overlap.h" | |||
23 | #include "alloc.h" | |||
24 | #include "npy_buffer.h" | |||
25 | ||||
26 | /******************* array attribute get and set routines ******************/ | |||
27 | ||||
28 | static PyObject * | |||
29 | array_ndim_get(PyArrayObject *self) | |||
30 | { | |||
31 | return PyLong_FromLong(PyArray_NDIM(self)); | |||
32 | } | |||
33 | ||||
34 | static PyObject * | |||
35 | array_flags_get(PyArrayObject *self) | |||
36 | { | |||
37 | return PyArray_NewFlagsObject((PyObject *)self); | |||
38 | } | |||
39 | ||||
40 | static PyObject * | |||
41 | array_shape_get(PyArrayObject *self) | |||
42 | { | |||
43 | return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_DIMS(self)); | |||
44 | } | |||
45 | ||||
46 | ||||
47 | static int | |||
48 | array_shape_set(PyArrayObject *self, PyObject *val) | |||
49 | { | |||
50 | int nd; | |||
51 | PyArrayObject *ret; | |||
52 | ||||
53 | if (val == NULL((void*)0)) { | |||
54 | PyErr_SetString(PyExc_AttributeError, | |||
55 | "Cannot delete array shape"); | |||
56 | return -1; | |||
57 | } | |||
58 | /* Assumes C-order */ | |||
59 | ret = (PyArrayObject *)PyArray_Reshape(self, val); | |||
60 | if (ret == NULL((void*)0)) { | |||
61 | return -1; | |||
62 | } | |||
63 | if (PyArray_DATA(ret) != PyArray_DATA(self)) { | |||
64 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
65 | PyErr_SetString(PyExc_AttributeError, | |||
66 | "Incompatible shape for in-place modification. Use " | |||
67 | "`.reshape()` to make a copy with the desired shape."); | |||
68 | return -1; | |||
69 | } | |||
70 | ||||
71 | nd = PyArray_NDIM(ret); | |||
72 | if (nd > 0) { | |||
73 | /* create new dimensions and strides */ | |||
74 | npy_intp *_dimensions = npy_alloc_cache_dim(2 * nd); | |||
75 | if (_dimensions == NULL((void*)0)) { | |||
76 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
77 | PyErr_NoMemory(); | |||
78 | return -1; | |||
79 | } | |||
80 | /* Free old dimensions and strides */ | |||
81 | npy_free_cache_dim_array(self); | |||
82 | ((PyArrayObject_fields *)self)->nd = nd; | |||
83 | ((PyArrayObject_fields *)self)->dimensions = _dimensions; | |||
84 | ((PyArrayObject_fields *)self)->strides = _dimensions + nd; | |||
85 | ||||
86 | if (nd) { | |||
87 | memcpy(PyArray_DIMS(self), PyArray_DIMS(ret), nd*sizeof(npy_intp)); | |||
88 | memcpy(PyArray_STRIDES(self), PyArray_STRIDES(ret), nd*sizeof(npy_intp)); | |||
89 | } | |||
90 | } | |||
91 | else { | |||
92 | /* Free old dimensions and strides */ | |||
93 | npy_free_cache_dim_array(self); | |||
94 | ((PyArrayObject_fields *)self)->nd = 0; | |||
95 | ((PyArrayObject_fields *)self)->dimensions = NULL((void*)0); | |||
96 | ((PyArrayObject_fields *)self)->strides = NULL((void*)0); | |||
97 | } | |||
98 | ||||
99 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
100 | PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS0x0001 | NPY_ARRAY_F_CONTIGUOUS0x0002); | |||
101 | return 0; | |||
102 | } | |||
103 | ||||
104 | ||||
105 | static PyObject * | |||
106 | array_strides_get(PyArrayObject *self) | |||
107 | { | |||
108 | return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_STRIDES(self)); | |||
109 | } | |||
110 | ||||
111 | static int | |||
112 | array_strides_set(PyArrayObject *self, PyObject *obj) | |||
113 | { | |||
114 | PyArray_Dims newstrides = {NULL((void*)0), -1}; | |||
115 | PyArrayObject *new; | |||
116 | npy_intp numbytes = 0; | |||
117 | npy_intp offset = 0; | |||
118 | npy_intp lower_offset = 0; | |||
119 | npy_intp upper_offset = 0; | |||
120 | Py_buffer view; | |||
121 | ||||
122 | if (obj == NULL((void*)0)) { | |||
123 | PyErr_SetString(PyExc_AttributeError, | |||
124 | "Cannot delete array strides"); | |||
125 | return -1; | |||
126 | } | |||
127 | if (!PyArray_OptionalIntpConverter(obj, &newstrides) || | |||
128 | newstrides.len == -1) { | |||
129 | PyErr_SetString(PyExc_TypeError, "invalid strides"); | |||
130 | return -1; | |||
131 | } | |||
132 | if (newstrides.len != PyArray_NDIM(self)) { | |||
133 | PyErr_Format(PyExc_ValueError, "strides must be " \ | |||
134 | " same length as shape (%d)", PyArray_NDIM(self)); | |||
135 | goto fail; | |||
136 | } | |||
137 | new = self; | |||
138 | while(PyArray_BASE(new) && PyArray_Check(PyArray_BASE(new))((((PyObject*)(PyArray_BASE(new)))->ob_type) == (&PyArray_Type ) || PyType_IsSubtype((((PyObject*)(PyArray_BASE(new)))->ob_type ), (&PyArray_Type)))) { | |||
139 | new = (PyArrayObject *)(PyArray_BASE(new)); | |||
140 | } | |||
141 | /* | |||
142 | * Get the available memory through the buffer interface on | |||
143 | * PyArray_BASE(new) or if that fails from the current new | |||
144 | */ | |||
145 | if (PyArray_BASE(new) && | |||
146 | PyObject_GetBuffer(PyArray_BASE(new), &view, PyBUF_SIMPLE0) >= 0) { | |||
147 | offset = PyArray_BYTES(self) - (char *)view.buf; | |||
148 | numbytes = view.len + offset; | |||
149 | PyBuffer_Release(&view); | |||
150 | } | |||
151 | else { | |||
152 | PyErr_Clear(); | |||
153 | offset_bounds_from_strides(PyArray_ITEMSIZE(new), PyArray_NDIM(new), | |||
154 | PyArray_DIMS(new), PyArray_STRIDES(new), | |||
155 | &lower_offset, &upper_offset); | |||
156 | ||||
157 | offset = PyArray_BYTES(self) - (PyArray_BYTES(new) + lower_offset); | |||
158 | numbytes = upper_offset - lower_offset; | |||
159 | } | |||
160 | ||||
161 | /* numbytes == 0 is special here, but the 0-size array case always works */ | |||
162 | if (!PyArray_CheckStrides(PyArray_ITEMSIZE(self), PyArray_NDIM(self), | |||
163 | numbytes, offset, | |||
164 | PyArray_DIMS(self), newstrides.ptr)) { | |||
165 | PyErr_SetString(PyExc_ValueError, "strides is not "\ | |||
166 | "compatible with available memory"); | |||
167 | goto fail; | |||
168 | } | |||
169 | if (newstrides.len) { | |||
170 | memcpy(PyArray_STRIDES(self), newstrides.ptr, sizeof(npy_intp)*newstrides.len); | |||
171 | } | |||
172 | PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS0x0001 | NPY_ARRAY_F_CONTIGUOUS0x0002 | | |||
173 | NPY_ARRAY_ALIGNED0x0100); | |||
174 | npy_free_cache_dim_obj(newstrides); | |||
175 | return 0; | |||
176 | ||||
177 | fail: | |||
178 | npy_free_cache_dim_obj(newstrides); | |||
179 | return -1; | |||
180 | } | |||
181 | ||||
182 | ||||
183 | ||||
184 | static PyObject * | |||
185 | array_priority_get(PyArrayObject *NPY_UNUSED(self)(__NPY_UNUSED_TAGGEDself) __attribute__ ((__unused__))) | |||
186 | { | |||
187 | return PyFloat_FromDouble(NPY_PRIORITY0.0); | |||
188 | } | |||
189 | ||||
190 | static PyObject * | |||
191 | array_typestr_get(PyArrayObject *self) | |||
192 | { | |||
193 | return arraydescr_protocol_typestr_get(PyArray_DESCR(self)); | |||
194 | } | |||
195 | ||||
196 | static PyObject * | |||
197 | array_descr_get(PyArrayObject *self) | |||
198 | { | |||
199 | Py_INCREF(PyArray_DESCR(self))_Py_INCREF(((PyObject*)(PyArray_DESCR(self)))); | |||
200 | return (PyObject *)PyArray_DESCR(self); | |||
201 | } | |||
202 | ||||
203 | static PyObject * | |||
204 | array_protocol_descr_get(PyArrayObject *self) | |||
205 | { | |||
206 | PyObject *res; | |||
207 | PyObject *dobj; | |||
208 | ||||
209 | res = arraydescr_protocol_descr_get(PyArray_DESCR(self)); | |||
210 | if (res) { | |||
211 | return res; | |||
212 | } | |||
213 | PyErr_Clear(); | |||
214 | ||||
215 | /* get default */ | |||
216 | dobj = PyTuple_New(2); | |||
217 | if (dobj == NULL((void*)0)) { | |||
218 | return NULL((void*)0); | |||
219 | } | |||
220 | PyTuple_SET_ITEM(dobj, 0, PyUnicode_FromString(""))PyTuple_SetItem(dobj, 0, PyUnicode_FromString("")); | |||
221 | PyTuple_SET_ITEM(dobj, 1, array_typestr_get(self))PyTuple_SetItem(dobj, 1, array_typestr_get(self)); | |||
222 | res = PyList_New(1); | |||
223 | if (res == NULL((void*)0)) { | |||
224 | Py_DECREF(dobj)_Py_DECREF(((PyObject*)(dobj))); | |||
225 | return NULL((void*)0); | |||
226 | } | |||
227 | PyList_SET_ITEM(res, 0, dobj)PyList_SetItem(res, 0, dobj); | |||
228 | return res; | |||
229 | } | |||
230 | ||||
231 | static PyObject * | |||
232 | array_protocol_strides_get(PyArrayObject *self) | |||
233 | { | |||
234 | if (PyArray_ISCONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0001)) { | |||
235 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
236 | } | |||
237 | return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_STRIDES(self)); | |||
238 | } | |||
239 | ||||
240 | ||||
241 | ||||
242 | static PyObject * | |||
243 | array_dataptr_get(PyArrayObject *self) | |||
244 | { | |||
245 | return Py_BuildValue_Py_BuildValue_SizeT("NO", | |||
246 | PyLong_FromVoidPtr(PyArray_DATA(self)), | |||
247 | ((PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE0x0400) && | |||
248 | !(PyArray_FLAGS(self) & NPY_ARRAY_WARN_ON_WRITE)) ? | |||
249 | Py_False((PyObject *) &_Py_FalseStruct) : Py_True((PyObject *) &_Py_TrueStruct)); | |||
250 | } | |||
251 | ||||
252 | static PyObject * | |||
253 | array_ctypes_get(PyArrayObject *self) | |||
254 | { | |||
255 | PyObject *_numpy_internal; | |||
256 | PyObject *ret; | |||
257 | _numpy_internal = PyImport_ImportModule("numpy.core._internal"); | |||
258 | if (_numpy_internal == NULL((void*)0)) { | |||
259 | return NULL((void*)0); | |||
260 | } | |||
261 | ret = PyObject_CallMethod_PyObject_CallMethod_SizeT(_numpy_internal, "_ctypes", "ON", self, | |||
262 | PyLong_FromVoidPtr(PyArray_DATA(self))); | |||
263 | Py_DECREF(_numpy_internal)_Py_DECREF(((PyObject*)(_numpy_internal))); | |||
264 | return ret; | |||
265 | } | |||
266 | ||||
267 | static PyObject * | |||
268 | array_interface_get(PyArrayObject *self) | |||
269 | { | |||
270 | PyObject *dict; | |||
271 | PyObject *obj; | |||
272 | ||||
273 | dict = PyDict_New(); | |||
274 | if (dict == NULL((void*)0)) { | |||
275 | return NULL((void*)0); | |||
276 | } | |||
277 | ||||
278 | int ret; | |||
279 | ||||
280 | /* dataptr */ | |||
281 | obj = array_dataptr_get(self); | |||
282 | ret = PyDict_SetItemString(dict, "data", obj); | |||
283 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
284 | if (ret < 0) { | |||
285 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
286 | return NULL((void*)0); | |||
287 | } | |||
288 | ||||
289 | obj = array_protocol_strides_get(self); | |||
290 | ret = PyDict_SetItemString(dict, "strides", obj); | |||
291 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
292 | if (ret < 0) { | |||
293 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
294 | return NULL((void*)0); | |||
295 | } | |||
296 | ||||
297 | obj = array_protocol_descr_get(self); | |||
298 | ret = PyDict_SetItemString(dict, "descr", obj); | |||
299 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
300 | if (ret < 0) { | |||
301 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
302 | return NULL((void*)0); | |||
303 | } | |||
304 | ||||
305 | obj = arraydescr_protocol_typestr_get(PyArray_DESCR(self)); | |||
306 | ret = PyDict_SetItemString(dict, "typestr", obj); | |||
307 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
308 | if (ret < 0) { | |||
309 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
310 | return NULL((void*)0); | |||
311 | } | |||
312 | ||||
313 | obj = array_shape_get(self); | |||
314 | ret = PyDict_SetItemString(dict, "shape", obj); | |||
315 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
316 | if (ret < 0) { | |||
317 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
318 | return NULL((void*)0); | |||
319 | } | |||
320 | ||||
321 | obj = PyLong_FromLong(3); | |||
322 | ret = PyDict_SetItemString(dict, "version", obj); | |||
323 | Py_DECREF(obj)_Py_DECREF(((PyObject*)(obj))); | |||
324 | if (ret < 0) { | |||
325 | Py_DECREF(dict)_Py_DECREF(((PyObject*)(dict))); | |||
326 | return NULL((void*)0); | |||
327 | } | |||
328 | ||||
329 | return dict; | |||
330 | } | |||
331 | ||||
332 | static PyObject * | |||
333 | array_data_get(PyArrayObject *self) | |||
334 | { | |||
335 | return PyMemoryView_FromObject((PyObject *)self); | |||
336 | } | |||
337 | ||||
338 | static int | |||
339 | array_data_set(PyArrayObject *self, PyObject *op) | |||
340 | { | |||
341 | void *buf; | |||
342 | Py_ssize_t buf_len; | |||
343 | int writeable=1; | |||
344 | Py_buffer view; | |||
345 | ||||
346 | /* 2016-19-02, 1.12 */ | |||
347 | int ret = DEPRECATE("Assigning the 'data' attribute is an "PyErr_WarnEx(PyExc_DeprecationWarning,"Assigning the 'data' attribute is an " "inherently unsafe operation and will " "be removed in the future." ,1) | |||
348 | "inherently unsafe operation and will "PyErr_WarnEx(PyExc_DeprecationWarning,"Assigning the 'data' attribute is an " "inherently unsafe operation and will " "be removed in the future." ,1) | |||
349 | "be removed in the future.")PyErr_WarnEx(PyExc_DeprecationWarning,"Assigning the 'data' attribute is an " "inherently unsafe operation and will " "be removed in the future." ,1); | |||
350 | if (ret < 0) { | |||
351 | return -1; | |||
352 | } | |||
353 | ||||
354 | if (op == NULL((void*)0)) { | |||
355 | PyErr_SetString(PyExc_AttributeError, | |||
356 | "Cannot delete array data"); | |||
357 | return -1; | |||
358 | } | |||
359 | if (PyObject_GetBuffer(op, &view, PyBUF_WRITABLE0x0001|PyBUF_SIMPLE0) < 0) { | |||
360 | writeable = 0; | |||
361 | PyErr_Clear(); | |||
362 | if (PyObject_GetBuffer(op, &view, PyBUF_SIMPLE0) < 0) { | |||
363 | return -1; | |||
364 | } | |||
365 | } | |||
366 | buf = view.buf; | |||
367 | buf_len = view.len; | |||
368 | /* | |||
369 | * In Python 3 both of the deprecated functions PyObject_AsWriteBuffer and | |||
370 | * PyObject_AsReadBuffer that this code replaces release the buffer. It is | |||
371 | * up to the object that supplies the buffer to guarantee that the buffer | |||
372 | * sticks around after the release. | |||
373 | */ | |||
374 | PyBuffer_Release(&view); | |||
375 | ||||
376 | if (!PyArray_ISONESEGMENT(self)(PyArray_CHKFLAGS(self, 0x0001) || PyArray_CHKFLAGS(self, 0x0002 ))) { | |||
377 | PyErr_SetString(PyExc_AttributeError, | |||
378 | "cannot set single-segment buffer for discontiguous array"); | |||
379 | return -1; | |||
380 | } | |||
381 | if (PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self))) > buf_len) { | |||
382 | PyErr_SetString(PyExc_AttributeError, "not enough data for array"); | |||
383 | return -1; | |||
384 | } | |||
385 | if (PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA0x0004) { | |||
386 | PyArray_XDECREF(self); | |||
387 | PyDataMem_FREE(PyArray_DATA(self)); | |||
388 | } | |||
389 | if (PyArray_BASE(self)) { | |||
390 | if ((PyArray_FLAGS(self) & NPY_ARRAY_WRITEBACKIFCOPY0x2000) || | |||
391 | (PyArray_FLAGS(self) & NPY_ARRAY_UPDATEIFCOPY0x1000)) { | |||
392 | PyArray_ENABLEFLAGS((PyArrayObject *)PyArray_BASE(self), | |||
393 | NPY_ARRAY_WRITEABLE0x0400); | |||
394 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WRITEBACKIFCOPY0x2000); | |||
395 | PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY0x1000); | |||
396 | } | |||
397 | Py_DECREF(PyArray_BASE(self))_Py_DECREF(((PyObject*)(PyArray_BASE(self)))); | |||
398 | ((PyArrayObject_fields *)self)->base = NULL((void*)0); | |||
399 | } | |||
400 | Py_INCREF(op)_Py_INCREF(((PyObject*)(op))); | |||
401 | if (PyArray_SetBaseObject(self, op) < 0) { | |||
402 | return -1; | |||
403 | } | |||
404 | ((PyArrayObject_fields *)self)->data = buf; | |||
405 | ((PyArrayObject_fields *)self)->flags = NPY_ARRAY_CARRAY(0x0001 | (0x0100 | 0x0400)); | |||
406 | if (!writeable) { | |||
407 | PyArray_CLEARFLAGS(self, ~NPY_ARRAY_WRITEABLE0x0400); | |||
408 | } | |||
409 | return 0; | |||
410 | } | |||
411 | ||||
412 | ||||
413 | static PyObject * | |||
414 | array_itemsize_get(PyArrayObject *self) | |||
415 | { | |||
416 | return PyLong_FromLong((long) PyArray_DESCR(self)->elsize); | |||
417 | } | |||
418 | ||||
419 | static PyObject * | |||
420 | array_size_get(PyArrayObject *self) | |||
421 | { | |||
422 | npy_intp size=PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)); | |||
423 | #if NPY_SIZEOF_INTP8 <= NPY_SIZEOF_LONG8 | |||
424 | return PyLong_FromLong((long) size); | |||
425 | #else | |||
426 | if (size > NPY_MAX_LONG9223372036854775807L || size < NPY_MIN_LONG(-9223372036854775807L -1L)) { | |||
427 | return PyLong_FromLongLong(size); | |||
428 | } | |||
429 | else { | |||
430 | return PyLong_FromLong((long) size); | |||
431 | } | |||
432 | #endif | |||
433 | } | |||
434 | ||||
435 | static PyObject * | |||
436 | array_nbytes_get(PyArrayObject *self) | |||
437 | { | |||
438 | npy_intp nbytes = PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self))); | |||
439 | #if NPY_SIZEOF_INTP8 <= NPY_SIZEOF_LONG8 | |||
440 | return PyLong_FromLong((long) nbytes); | |||
441 | #else | |||
442 | if (nbytes > NPY_MAX_LONG9223372036854775807L || nbytes < NPY_MIN_LONG(-9223372036854775807L -1L)) { | |||
443 | return PyLong_FromLongLong(nbytes); | |||
444 | } | |||
445 | else { | |||
446 | return PyLong_FromLong((long) nbytes); | |||
447 | } | |||
448 | #endif | |||
449 | } | |||
450 | ||||
451 | ||||
452 | /* | |||
453 | * If the type is changed. | |||
454 | * Also needing change: strides, itemsize | |||
455 | * | |||
456 | * Either itemsize is exactly the same or the array is single-segment | |||
457 | * (contiguous or fortran) with compatible dimensions The shape and strides | |||
458 | * will be adjusted in that case as well. | |||
459 | */ | |||
460 | static int | |||
461 | array_descr_set(PyArrayObject *self, PyObject *arg) | |||
462 | { | |||
463 | PyArray_Descr *newtype = NULL((void*)0); | |||
464 | ||||
465 | if (arg == NULL((void*)0)) { | |||
466 | PyErr_SetString(PyExc_AttributeError, | |||
467 | "Cannot delete array dtype"); | |||
468 | return -1; | |||
469 | } | |||
470 | ||||
471 | if (!(PyArray_DescrConverter(arg, &newtype)) || | |||
472 | newtype == NULL((void*)0)) { | |||
473 | PyErr_SetString(PyExc_TypeError, | |||
474 | "invalid data-type for array"); | |||
475 | return -1; | |||
476 | } | |||
477 | ||||
478 | /* check that we are not reinterpreting memory containing Objects. */ | |||
479 | if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(newtype)) { | |||
480 | static PyObject *checkfunc = NULL((void*)0); | |||
481 | PyObject *safe; | |||
482 | ||||
483 | npy_cache_import("numpy.core._internal", "_view_is_safe", &checkfunc); | |||
484 | if (checkfunc == NULL((void*)0)) { | |||
485 | goto fail; | |||
486 | } | |||
487 | ||||
488 | safe = PyObject_CallFunction_PyObject_CallFunction_SizeT(checkfunc, "OO", | |||
489 | PyArray_DESCR(self), newtype); | |||
490 | if (safe == NULL((void*)0)) { | |||
491 | goto fail; | |||
492 | } | |||
493 | Py_DECREF(safe)_Py_DECREF(((PyObject*)(safe))); | |||
494 | } | |||
495 | ||||
496 | /* | |||
497 | * Viewing as an unsized void implies a void dtype matching the size of the | |||
498 | * current dtype. | |||
499 | */ | |||
500 | if (newtype->type_num == NPY_VOID && | |||
501 | PyDataType_ISUNSIZED(newtype)((newtype)->elsize == 0 && !(((PyArray_Descr *)(newtype ))->names != ((void*)0))) && | |||
502 | newtype->elsize != PyArray_DESCR(self)->elsize) { | |||
503 | PyArray_DESCR_REPLACE(newtype)do { PyArray_Descr *_new_; _new_ = PyArray_DescrNew(newtype); _Py_XDECREF(((PyObject*)(newtype))); newtype = _new_; } while (0); | |||
504 | if (newtype == NULL((void*)0)) { | |||
505 | return -1; | |||
506 | } | |||
507 | newtype->elsize = PyArray_DESCR(self)->elsize; | |||
508 | } | |||
509 | ||||
510 | /* Changing the size of the dtype results in a shape change */ | |||
511 | if (newtype->elsize != PyArray_DESCR(self)->elsize) { | |||
512 | int axis; | |||
513 | npy_intp newdim; | |||
514 | ||||
515 | /* forbidden cases */ | |||
516 | if (PyArray_NDIM(self) == 0) { | |||
517 | PyErr_SetString(PyExc_ValueError, | |||
518 | "Changing the dtype of a 0d array is only supported " | |||
519 | "if the itemsize is unchanged"); | |||
520 | goto fail; | |||
521 | } | |||
522 | else if (PyDataType_HASSUBARRAY(newtype)((newtype)->subarray != ((void*)0))) { | |||
523 | PyErr_SetString(PyExc_ValueError, | |||
524 | "Changing the dtype to a subarray type is only supported " | |||
525 | "if the total itemsize is unchanged"); | |||
526 | goto fail; | |||
527 | } | |||
528 | ||||
529 | /* determine which axis to resize */ | |||
530 | if (PyArray_IS_C_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0001)) { | |||
531 | axis = PyArray_NDIM(self) - 1; | |||
532 | } | |||
533 | else if (PyArray_IS_F_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0002)) { | |||
534 | /* 2015-11-27 1.11.0, gh-6747 */ | |||
535 | if (DEPRECATE(PyErr_WarnEx(PyExc_DeprecationWarning,"Changing the shape of an F-contiguous array by " "descriptor assignment is deprecated. To maintain the " "Fortran contiguity of a multidimensional Fortran " "array, use 'a.T.view(...).T' instead",1) | |||
536 | "Changing the shape of an F-contiguous array by "PyErr_WarnEx(PyExc_DeprecationWarning,"Changing the shape of an F-contiguous array by " "descriptor assignment is deprecated. To maintain the " "Fortran contiguity of a multidimensional Fortran " "array, use 'a.T.view(...).T' instead",1) | |||
537 | "descriptor assignment is deprecated. To maintain the "PyErr_WarnEx(PyExc_DeprecationWarning,"Changing the shape of an F-contiguous array by " "descriptor assignment is deprecated. To maintain the " "Fortran contiguity of a multidimensional Fortran " "array, use 'a.T.view(...).T' instead",1) | |||
538 | "Fortran contiguity of a multidimensional Fortran "PyErr_WarnEx(PyExc_DeprecationWarning,"Changing the shape of an F-contiguous array by " "descriptor assignment is deprecated. To maintain the " "Fortran contiguity of a multidimensional Fortran " "array, use 'a.T.view(...).T' instead",1) | |||
539 | "array, use 'a.T.view(...).T' instead")PyErr_WarnEx(PyExc_DeprecationWarning,"Changing the shape of an F-contiguous array by " "descriptor assignment is deprecated. To maintain the " "Fortran contiguity of a multidimensional Fortran " "array, use 'a.T.view(...).T' instead",1) < 0) { | |||
540 | goto fail; | |||
541 | } | |||
542 | axis = 0; | |||
543 | } | |||
544 | else { | |||
545 | /* Don't mention the deprecated F-contiguous support */ | |||
546 | PyErr_SetString(PyExc_ValueError, | |||
547 | "To change to a dtype of a different size, the array must " | |||
548 | "be C-contiguous"); | |||
549 | goto fail; | |||
550 | } | |||
551 | ||||
552 | if (newtype->elsize < PyArray_DESCR(self)->elsize) { | |||
553 | /* if it is compatible, increase the size of the relevant axis */ | |||
554 | if (newtype->elsize == 0 || | |||
555 | PyArray_DESCR(self)->elsize % newtype->elsize != 0) { | |||
556 | PyErr_SetString(PyExc_ValueError, | |||
557 | "When changing to a smaller dtype, its size must be a " | |||
558 | "divisor of the size of original dtype"); | |||
559 | goto fail; | |||
560 | } | |||
561 | newdim = PyArray_DESCR(self)->elsize / newtype->elsize; | |||
562 | PyArray_DIMS(self)[axis] *= newdim; | |||
563 | PyArray_STRIDES(self)[axis] = newtype->elsize; | |||
564 | } | |||
565 | else if (newtype->elsize > PyArray_DESCR(self)->elsize) { | |||
566 | /* if it is compatible, decrease the size of the relevant axis */ | |||
567 | newdim = PyArray_DIMS(self)[axis] * PyArray_DESCR(self)->elsize; | |||
568 | if ((newdim % newtype->elsize) != 0) { | |||
569 | PyErr_SetString(PyExc_ValueError, | |||
570 | "When changing to a larger dtype, its size must be a " | |||
571 | "divisor of the total size in bytes of the last axis " | |||
572 | "of the array."); | |||
573 | goto fail; | |||
574 | } | |||
575 | PyArray_DIMS(self)[axis] = newdim / newtype->elsize; | |||
576 | PyArray_STRIDES(self)[axis] = newtype->elsize; | |||
577 | } | |||
578 | } | |||
579 | ||||
580 | /* Viewing as a subarray increases the number of dimensions */ | |||
581 | if (PyDataType_HASSUBARRAY(newtype)((newtype)->subarray != ((void*)0))) { | |||
582 | /* | |||
583 | * create new array object from data and update | |||
584 | * dimensions, strides and descr from it | |||
585 | */ | |||
586 | PyArrayObject *temp; | |||
587 | /* | |||
588 | * We would decref newtype here. | |||
589 | * temp will steal a reference to it | |||
590 | */ | |||
591 | temp = (PyArrayObject *) | |||
592 | PyArray_NewFromDescr(&PyArray_Type, newtype, PyArray_NDIM(self), | |||
593 | PyArray_DIMS(self), PyArray_STRIDES(self), | |||
594 | PyArray_DATA(self), PyArray_FLAGS(self), NULL((void*)0)); | |||
595 | if (temp == NULL((void*)0)) { | |||
596 | return -1; | |||
597 | } | |||
598 | npy_free_cache_dim_array(self); | |||
599 | ((PyArrayObject_fields *)self)->dimensions = PyArray_DIMS(temp); | |||
600 | ((PyArrayObject_fields *)self)->nd = PyArray_NDIM(temp); | |||
601 | ((PyArrayObject_fields *)self)->strides = PyArray_STRIDES(temp); | |||
602 | newtype = PyArray_DESCR(temp); | |||
603 | Py_INCREF(PyArray_DESCR(temp))_Py_INCREF(((PyObject*)(PyArray_DESCR(temp)))); | |||
604 | /* Fool deallocator not to delete these*/ | |||
605 | ((PyArrayObject_fields *)temp)->nd = 0; | |||
606 | ((PyArrayObject_fields *)temp)->dimensions = NULL((void*)0); | |||
607 | Py_DECREF(temp)_Py_DECREF(((PyObject*)(temp))); | |||
608 | } | |||
609 | ||||
610 | Py_DECREF(PyArray_DESCR(self))_Py_DECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
611 | ((PyArrayObject_fields *)self)->descr = newtype; | |||
612 | PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL(0x0001 | 0x0002 | 0x0100)); | |||
613 | return 0; | |||
614 | ||||
615 | fail: | |||
616 | Py_DECREF(newtype)_Py_DECREF(((PyObject*)(newtype))); | |||
617 | return -1; | |||
618 | } | |||
619 | ||||
620 | static PyObject * | |||
621 | array_struct_get(PyArrayObject *self) | |||
622 | { | |||
623 | PyArrayInterface *inter; | |||
624 | ||||
625 | inter = (PyArrayInterface *)PyArray_mallocPyMem_RawMalloc(sizeof(PyArrayInterface)); | |||
626 | if (inter==NULL((void*)0)) { | |||
| ||||
627 | return PyErr_NoMemory(); | |||
628 | } | |||
629 | inter->two = 2; | |||
630 | inter->nd = PyArray_NDIM(self); | |||
631 | inter->typekind = PyArray_DESCR(self)->kind; | |||
632 | inter->itemsize = PyArray_DESCR(self)->elsize; | |||
633 | inter->flags = PyArray_FLAGS(self); | |||
634 | if (inter->flags & NPY_ARRAY_WARN_ON_WRITE) { | |||
635 | /* Export a warn-on-write array as read-only */ | |||
636 | inter->flags = inter->flags & ~NPY_ARRAY_WARN_ON_WRITE; | |||
637 | inter->flags = inter->flags & ~NPY_ARRAY_WRITEABLE0x0400; | |||
638 | } | |||
639 | /* reset unused flags */ | |||
640 | inter->flags &= ~(NPY_ARRAY_WRITEBACKIFCOPY0x2000 | NPY_ARRAY_UPDATEIFCOPY0x1000 |NPY_ARRAY_OWNDATA0x0004); | |||
641 | if (PyArray_ISNOTSWAPPED(self)((PyArray_DESCR(self)->byteorder) != '>')) inter->flags |= NPY_ARRAY_NOTSWAPPED0x0200; | |||
642 | /* | |||
643 | * Copy shape and strides over since these can be reset | |||
644 | *when the array is "reshaped". | |||
645 | */ | |||
646 | if (PyArray_NDIM(self) > 0) { | |||
647 | inter->shape = (npy_intp *)PyArray_mallocPyMem_RawMalloc(2*sizeof(npy_intp)*PyArray_NDIM(self)); | |||
648 | if (inter->shape == NULL((void*)0)) { | |||
649 | PyArray_freePyMem_RawFree(inter); | |||
650 | return PyErr_NoMemory(); | |||
651 | } | |||
652 | inter->strides = inter->shape + PyArray_NDIM(self); | |||
653 | if (PyArray_NDIM(self)) { | |||
654 | memcpy(inter->shape, PyArray_DIMS(self), sizeof(npy_intp)*PyArray_NDIM(self)); | |||
655 | memcpy(inter->strides, PyArray_STRIDES(self), sizeof(npy_intp)*PyArray_NDIM(self)); | |||
656 | } | |||
657 | } | |||
658 | else { | |||
659 | inter->shape = NULL((void*)0); | |||
660 | inter->strides = NULL((void*)0); | |||
661 | } | |||
662 | inter->data = PyArray_DATA(self); | |||
663 | if (PyDataType_HASFIELDS(PyArray_DESCR(self))(((PyArray_Descr *)(PyArray_DESCR(self)))->names != ((void *)0))) { | |||
664 | inter->descr = arraydescr_protocol_descr_get(PyArray_DESCR(self)); | |||
665 | if (inter->descr == NULL((void*)0)) { | |||
666 | PyErr_Clear(); | |||
667 | } | |||
668 | else { | |||
669 | inter->flags &= NPY_ARR_HAS_DESCR0x0800; | |||
670 | } | |||
671 | } | |||
672 | else { | |||
673 | inter->descr = NULL((void*)0); | |||
674 | } | |||
675 | PyObject *ret = PyCapsule_New(inter, NULL((void*)0), gentype_struct_free); | |||
| ||||
676 | if (ret == NULL((void*)0)) { | |||
677 | return NULL((void*)0); | |||
678 | } | |||
679 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
680 | if (PyCapsule_SetContext(ret, self) < 0) { | |||
681 | return NULL((void*)0); | |||
682 | } | |||
683 | return ret; | |||
684 | } | |||
685 | ||||
686 | static PyObject * | |||
687 | array_base_get(PyArrayObject *self) | |||
688 | { | |||
689 | if (PyArray_BASE(self) == NULL((void*)0)) { | |||
690 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
691 | } | |||
692 | else { | |||
693 | Py_INCREF(PyArray_BASE(self))_Py_INCREF(((PyObject*)(PyArray_BASE(self)))); | |||
694 | return PyArray_BASE(self); | |||
695 | } | |||
696 | } | |||
697 | ||||
698 | /* | |||
699 | * Create a view of a complex array with an equivalent data-type | |||
700 | * except it is real instead of complex. | |||
701 | */ | |||
702 | static PyArrayObject * | |||
703 | _get_part(PyArrayObject *self, int imag) | |||
704 | { | |||
705 | int float_type_num; | |||
706 | PyArray_Descr *type; | |||
707 | PyArrayObject *ret; | |||
708 | int offset; | |||
709 | ||||
710 | switch (PyArray_DESCR(self)->type_num) { | |||
711 | case NPY_CFLOAT: | |||
712 | float_type_num = NPY_FLOAT; | |||
713 | break; | |||
714 | case NPY_CDOUBLE: | |||
715 | float_type_num = NPY_DOUBLE; | |||
716 | break; | |||
717 | case NPY_CLONGDOUBLE: | |||
718 | float_type_num = NPY_LONGDOUBLE; | |||
719 | break; | |||
720 | default: | |||
721 | PyErr_Format(PyExc_ValueError, | |||
722 | "Cannot convert complex type number %d to float", | |||
723 | PyArray_DESCR(self)->type_num); | |||
724 | return NULL((void*)0); | |||
725 | ||||
726 | } | |||
727 | type = PyArray_DescrFromType(float_type_num); | |||
728 | ||||
729 | offset = (imag ? type->elsize : 0); | |||
730 | ||||
731 | if (!PyArray_ISNBO(PyArray_DESCR(self)->byteorder)((PyArray_DESCR(self)->byteorder) != '>')) { | |||
732 | PyArray_Descr *new; | |||
733 | new = PyArray_DescrNew(type); | |||
734 | new->byteorder = PyArray_DESCR(self)->byteorder; | |||
735 | Py_DECREF(type)_Py_DECREF(((PyObject*)(type))); | |||
736 | type = new; | |||
737 | } | |||
738 | ret = (PyArrayObject *)PyArray_NewFromDescrAndBase( | |||
739 | Py_TYPE(self)(((PyObject*)(self))->ob_type), | |||
740 | type, | |||
741 | PyArray_NDIM(self), | |||
742 | PyArray_DIMS(self), | |||
743 | PyArray_STRIDES(self), | |||
744 | PyArray_BYTES(self) + offset, | |||
745 | PyArray_FLAGS(self), (PyObject *)self, (PyObject *)self); | |||
746 | if (ret == NULL((void*)0)) { | |||
747 | return NULL((void*)0); | |||
748 | } | |||
749 | return ret; | |||
750 | } | |||
751 | ||||
752 | /* For Object arrays, we need to get and set the | |||
753 | real part of each element. | |||
754 | */ | |||
755 | ||||
756 | static PyObject * | |||
757 | array_real_get(PyArrayObject *self) | |||
758 | { | |||
759 | PyArrayObject *ret; | |||
760 | ||||
761 | if (PyArray_ISCOMPLEX(self)(((PyArray_TYPE(self)) >= NPY_CFLOAT) && ((PyArray_TYPE (self)) <= NPY_CLONGDOUBLE))) { | |||
762 | ret = _get_part(self, 0); | |||
763 | return (PyObject *)ret; | |||
764 | } | |||
765 | else { | |||
766 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
767 | return (PyObject *)self; | |||
768 | } | |||
769 | } | |||
770 | ||||
771 | ||||
772 | static int | |||
773 | array_real_set(PyArrayObject *self, PyObject *val) | |||
774 | { | |||
775 | PyArrayObject *ret; | |||
776 | PyArrayObject *new; | |||
777 | int retcode; | |||
778 | ||||
779 | if (val == NULL((void*)0)) { | |||
780 | PyErr_SetString(PyExc_AttributeError, | |||
781 | "Cannot delete array real part"); | |||
782 | return -1; | |||
783 | } | |||
784 | if (PyArray_ISCOMPLEX(self)(((PyArray_TYPE(self)) >= NPY_CFLOAT) && ((PyArray_TYPE (self)) <= NPY_CLONGDOUBLE))) { | |||
785 | ret = _get_part(self, 0); | |||
786 | if (ret == NULL((void*)0)) { | |||
787 | return -1; | |||
788 | } | |||
789 | } | |||
790 | else { | |||
791 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
792 | ret = self; | |||
793 | } | |||
794 | new = (PyArrayObject *)PyArray_FROM_O(val)PyArray_FromAny(val, ((void*)0), 0, 0, 0, ((void*)0)); | |||
795 | if (new == NULL((void*)0)) { | |||
796 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
797 | return -1; | |||
798 | } | |||
799 | retcode = PyArray_MoveInto(ret, new); | |||
800 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
801 | Py_DECREF(new)_Py_DECREF(((PyObject*)(new))); | |||
802 | return retcode; | |||
803 | } | |||
804 | ||||
805 | /* For Object arrays we need to get | |||
806 | and set the imaginary part of | |||
807 | each element | |||
808 | */ | |||
809 | ||||
810 | static PyObject * | |||
811 | array_imag_get(PyArrayObject *self) | |||
812 | { | |||
813 | PyArrayObject *ret; | |||
814 | ||||
815 | if (PyArray_ISCOMPLEX(self)(((PyArray_TYPE(self)) >= NPY_CFLOAT) && ((PyArray_TYPE (self)) <= NPY_CLONGDOUBLE))) { | |||
816 | ret = _get_part(self, 1); | |||
817 | } | |||
818 | else { | |||
819 | Py_INCREF(PyArray_DESCR(self))_Py_INCREF(((PyObject*)(PyArray_DESCR(self)))); | |||
820 | ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self)(((PyObject*)(self))->ob_type), | |||
821 | PyArray_DESCR(self), | |||
822 | PyArray_NDIM(self), | |||
823 | PyArray_DIMS(self), | |||
824 | NULL((void*)0), NULL((void*)0), | |||
825 | PyArray_ISFORTRAN(self)(PyArray_CHKFLAGS(self, 0x0002) && (!PyArray_CHKFLAGS (self, 0x0001))), | |||
826 | (PyObject *)self); | |||
827 | if (ret == NULL((void*)0)) { | |||
828 | return NULL((void*)0); | |||
829 | } | |||
830 | if (_zerofill(ret) < 0) { | |||
831 | return NULL((void*)0); | |||
832 | } | |||
833 | PyArray_CLEARFLAGS(ret, NPY_ARRAY_WRITEABLE0x0400); | |||
834 | } | |||
835 | return (PyObject *) ret; | |||
836 | } | |||
837 | ||||
838 | static int | |||
839 | array_imag_set(PyArrayObject *self, PyObject *val) | |||
840 | { | |||
841 | if (val == NULL((void*)0)) { | |||
842 | PyErr_SetString(PyExc_AttributeError, | |||
843 | "Cannot delete array imaginary part"); | |||
844 | return -1; | |||
845 | } | |||
846 | if (PyArray_ISCOMPLEX(self)(((PyArray_TYPE(self)) >= NPY_CFLOAT) && ((PyArray_TYPE (self)) <= NPY_CLONGDOUBLE))) { | |||
847 | PyArrayObject *ret; | |||
848 | PyArrayObject *new; | |||
849 | int retcode; | |||
850 | ||||
851 | ret = _get_part(self, 1); | |||
852 | if (ret == NULL((void*)0)) { | |||
853 | return -1; | |||
854 | } | |||
855 | new = (PyArrayObject *)PyArray_FROM_O(val)PyArray_FromAny(val, ((void*)0), 0, 0, 0, ((void*)0)); | |||
856 | if (new == NULL((void*)0)) { | |||
857 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
858 | return -1; | |||
859 | } | |||
860 | retcode = PyArray_MoveInto(ret, new); | |||
861 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
862 | Py_DECREF(new)_Py_DECREF(((PyObject*)(new))); | |||
863 | return retcode; | |||
864 | } | |||
865 | else { | |||
866 | PyErr_SetString(PyExc_TypeError, | |||
867 | "array does not have imaginary part to set"); | |||
868 | return -1; | |||
869 | } | |||
870 | } | |||
871 | ||||
872 | static PyObject * | |||
873 | array_flat_get(PyArrayObject *self) | |||
874 | { | |||
875 | return PyArray_IterNew((PyObject *)self); | |||
876 | } | |||
877 | ||||
878 | static int | |||
879 | array_flat_set(PyArrayObject *self, PyObject *val) | |||
880 | { | |||
881 | PyArrayObject *arr = NULL((void*)0); | |||
882 | int retval = -1; | |||
883 | PyArrayIterObject *selfit = NULL((void*)0), *arrit = NULL((void*)0); | |||
884 | PyArray_Descr *typecode; | |||
885 | int swap; | |||
886 | PyArray_CopySwapFunc *copyswap; | |||
887 | ||||
888 | if (val == NULL((void*)0)) { | |||
889 | PyErr_SetString(PyExc_AttributeError, | |||
890 | "Cannot delete array flat iterator"); | |||
891 | return -1; | |||
892 | } | |||
893 | if (PyArray_FailUnlessWriteable(self, "array") < 0) return -1; | |||
894 | typecode = PyArray_DESCR(self); | |||
895 | Py_INCREF(typecode)_Py_INCREF(((PyObject*)(typecode))); | |||
896 | arr = (PyArrayObject *)PyArray_FromAny(val, typecode, | |||
897 | 0, 0, NPY_ARRAY_FORCECAST0x0010 | PyArray_FORTRAN_IF(self)((PyArray_CHKFLAGS(self, 0x0002) ? 0x0002 : 0)), NULL((void*)0)); | |||
898 | if (arr == NULL((void*)0)) { | |||
899 | return -1; | |||
900 | } | |||
901 | arrit = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr); | |||
902 | if (arrit == NULL((void*)0)) { | |||
903 | goto exit; | |||
904 | } | |||
905 | selfit = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); | |||
906 | if (selfit == NULL((void*)0)) { | |||
907 | goto exit; | |||
908 | } | |||
909 | if (arrit->size == 0) { | |||
910 | retval = 0; | |||
911 | goto exit; | |||
912 | } | |||
913 | swap = PyArray_ISNOTSWAPPED(self)((PyArray_DESCR(self)->byteorder) != '>') != PyArray_ISNOTSWAPPED(arr)((PyArray_DESCR(arr)->byteorder) != '>'); | |||
914 | copyswap = PyArray_DESCR(self)->f->copyswap; | |||
915 | if (PyDataType_REFCHK(PyArray_DESCR(self))(((PyArray_DESCR(self))->flags & (0x01)) == (0x01))) { | |||
916 | while (selfit->index < selfit->size) { | |||
917 | PyArray_Item_XDECREF(selfit->dataptr, PyArray_DESCR(self)); | |||
918 | PyArray_Item_INCREF(arrit->dataptr, PyArray_DESCR(arr)); | |||
919 | memmove(selfit->dataptr, arrit->dataptr, sizeof(PyObject **)); | |||
920 | if (swap) { | |||
921 | copyswap(selfit->dataptr, NULL((void*)0), swap, self); | |||
922 | } | |||
923 | PyArray_ITER_NEXT(selfit)do { ((PyArrayIterObject *)(selfit))->index++; if (((PyArrayIterObject *)(selfit))->nd_m1 == 0) { do { (((PyArrayIterObject *)(selfit )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(selfit))))->strides[0]; (((PyArrayIterObject *)(selfit )))->coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(selfit))->contiguous) ((PyArrayIterObject *)(selfit))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(selfit))-> ao)->elsize; else if (((PyArrayIterObject *)(selfit))-> nd_m1 == 1) { do { if ((((PyArrayIterObject *)(selfit)))-> coordinates[1] < (((PyArrayIterObject *)(selfit)))->dims_m1 [1]) { (((PyArrayIterObject *)(selfit)))->coordinates[1]++ ; (((PyArrayIterObject *)(selfit)))->dataptr += (((PyArrayIterObject *)(selfit)))->strides[1]; } else { (((PyArrayIterObject * )(selfit)))->coordinates[1] = 0; (((PyArrayIterObject *)(selfit )))->coordinates[0]++; (((PyArrayIterObject *)(selfit)))-> dataptr += (((PyArrayIterObject *)(selfit)))->strides[0] - (((PyArrayIterObject *)(selfit)))->backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i=((PyArrayIterObject * )(selfit))->nd_m1; __npy_i >= 0; __npy_i--) { if (((PyArrayIterObject *)(selfit))->coordinates[__npy_i] < ((PyArrayIterObject *)(selfit))->dims_m1[__npy_i]) { ((PyArrayIterObject *)(selfit ))->coordinates[__npy_i]++; ((PyArrayIterObject *)(selfit) )->dataptr += ((PyArrayIterObject *)(selfit))->strides[ __npy_i]; break; } else { ((PyArrayIterObject *)(selfit))-> coordinates[__npy_i] = 0; ((PyArrayIterObject *)(selfit))-> dataptr -= ((PyArrayIterObject *)(selfit))->backstrides[__npy_i ]; } } } } while (0); | |||
924 | PyArray_ITER_NEXT(arrit)do { ((PyArrayIterObject *)(arrit))->index++; if (((PyArrayIterObject *)(arrit))->nd_m1 == 0) { do { (((PyArrayIterObject *)(arrit )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(arrit))))->strides[0]; (((PyArrayIterObject *)(arrit)) )->coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(arrit))->contiguous) ((PyArrayIterObject *)(arrit))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(arrit))->ao )->elsize; else if (((PyArrayIterObject *)(arrit))->nd_m1 == 1) { do { if ((((PyArrayIterObject *)(arrit)))->coordinates [1] < (((PyArrayIterObject *)(arrit)))->dims_m1[1]) { ( ((PyArrayIterObject *)(arrit)))->coordinates[1]++; (((PyArrayIterObject *)(arrit)))->dataptr += (((PyArrayIterObject *)(arrit)))-> strides[1]; } else { (((PyArrayIterObject *)(arrit)))->coordinates [1] = 0; (((PyArrayIterObject *)(arrit)))->coordinates[0]++ ; (((PyArrayIterObject *)(arrit)))->dataptr += (((PyArrayIterObject *)(arrit)))->strides[0] - (((PyArrayIterObject *)(arrit)) )->backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i=((PyArrayIterObject *)(arrit))->nd_m1; __npy_i >= 0; __npy_i--) { if (((PyArrayIterObject *)(arrit))->coordinates [__npy_i] < ((PyArrayIterObject *)(arrit))->dims_m1[__npy_i ]) { ((PyArrayIterObject *)(arrit))->coordinates[__npy_i]++ ; ((PyArrayIterObject *)(arrit))->dataptr += ((PyArrayIterObject *)(arrit))->strides[__npy_i]; break; } else { ((PyArrayIterObject *)(arrit))->coordinates[__npy_i] = 0; ((PyArrayIterObject *)(arrit))->dataptr -= ((PyArrayIterObject *)(arrit))-> backstrides[__npy_i]; } } } } while (0); | |||
925 | if (arrit->index == arrit->size) { | |||
926 | PyArray_ITER_RESET(arrit)do { ((PyArrayIterObject *)(arrit))->index = 0; ((PyArrayIterObject *)(arrit))->dataptr = PyArray_BYTES(((PyArrayIterObject * )(arrit))->ao); memset(((PyArrayIterObject *)(arrit))-> coordinates, 0, (((PyArrayIterObject *)(arrit))->nd_m1+1)* sizeof(npy_intp)); } while (0); | |||
927 | } | |||
928 | } | |||
929 | retval = 0; | |||
930 | goto exit; | |||
931 | } | |||
932 | ||||
933 | while(selfit->index < selfit->size) { | |||
934 | copyswap(selfit->dataptr, arrit->dataptr, swap, self); | |||
935 | PyArray_ITER_NEXT(selfit)do { ((PyArrayIterObject *)(selfit))->index++; if (((PyArrayIterObject *)(selfit))->nd_m1 == 0) { do { (((PyArrayIterObject *)(selfit )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(selfit))))->strides[0]; (((PyArrayIterObject *)(selfit )))->coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(selfit))->contiguous) ((PyArrayIterObject *)(selfit))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(selfit))-> ao)->elsize; else if (((PyArrayIterObject *)(selfit))-> nd_m1 == 1) { do { if ((((PyArrayIterObject *)(selfit)))-> coordinates[1] < (((PyArrayIterObject *)(selfit)))->dims_m1 [1]) { (((PyArrayIterObject *)(selfit)))->coordinates[1]++ ; (((PyArrayIterObject *)(selfit)))->dataptr += (((PyArrayIterObject *)(selfit)))->strides[1]; } else { (((PyArrayIterObject * )(selfit)))->coordinates[1] = 0; (((PyArrayIterObject *)(selfit )))->coordinates[0]++; (((PyArrayIterObject *)(selfit)))-> dataptr += (((PyArrayIterObject *)(selfit)))->strides[0] - (((PyArrayIterObject *)(selfit)))->backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i=((PyArrayIterObject * )(selfit))->nd_m1; __npy_i >= 0; __npy_i--) { if (((PyArrayIterObject *)(selfit))->coordinates[__npy_i] < ((PyArrayIterObject *)(selfit))->dims_m1[__npy_i]) { ((PyArrayIterObject *)(selfit ))->coordinates[__npy_i]++; ((PyArrayIterObject *)(selfit) )->dataptr += ((PyArrayIterObject *)(selfit))->strides[ __npy_i]; break; } else { ((PyArrayIterObject *)(selfit))-> coordinates[__npy_i] = 0; ((PyArrayIterObject *)(selfit))-> dataptr -= ((PyArrayIterObject *)(selfit))->backstrides[__npy_i ]; } } } } while (0); | |||
936 | PyArray_ITER_NEXT(arrit)do { ((PyArrayIterObject *)(arrit))->index++; if (((PyArrayIterObject *)(arrit))->nd_m1 == 0) { do { (((PyArrayIterObject *)(arrit )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(arrit))))->strides[0]; (((PyArrayIterObject *)(arrit)) )->coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(arrit))->contiguous) ((PyArrayIterObject *)(arrit))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(arrit))->ao )->elsize; else if (((PyArrayIterObject *)(arrit))->nd_m1 == 1) { do { if ((((PyArrayIterObject *)(arrit)))->coordinates [1] < (((PyArrayIterObject *)(arrit)))->dims_m1[1]) { ( ((PyArrayIterObject *)(arrit)))->coordinates[1]++; (((PyArrayIterObject *)(arrit)))->dataptr += (((PyArrayIterObject *)(arrit)))-> strides[1]; } else { (((PyArrayIterObject *)(arrit)))->coordinates [1] = 0; (((PyArrayIterObject *)(arrit)))->coordinates[0]++ ; (((PyArrayIterObject *)(arrit)))->dataptr += (((PyArrayIterObject *)(arrit)))->strides[0] - (((PyArrayIterObject *)(arrit)) )->backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i=((PyArrayIterObject *)(arrit))->nd_m1; __npy_i >= 0; __npy_i--) { if (((PyArrayIterObject *)(arrit))->coordinates [__npy_i] < ((PyArrayIterObject *)(arrit))->dims_m1[__npy_i ]) { ((PyArrayIterObject *)(arrit))->coordinates[__npy_i]++ ; ((PyArrayIterObject *)(arrit))->dataptr += ((PyArrayIterObject *)(arrit))->strides[__npy_i]; break; } else { ((PyArrayIterObject *)(arrit))->coordinates[__npy_i] = 0; ((PyArrayIterObject *)(arrit))->dataptr -= ((PyArrayIterObject *)(arrit))-> backstrides[__npy_i]; } } } } while (0); | |||
937 | if (arrit->index == arrit->size) { | |||
938 | PyArray_ITER_RESET(arrit)do { ((PyArrayIterObject *)(arrit))->index = 0; ((PyArrayIterObject *)(arrit))->dataptr = PyArray_BYTES(((PyArrayIterObject * )(arrit))->ao); memset(((PyArrayIterObject *)(arrit))-> coordinates, 0, (((PyArrayIterObject *)(arrit))->nd_m1+1)* sizeof(npy_intp)); } while (0); | |||
939 | } | |||
940 | } | |||
941 | retval = 0; | |||
942 | ||||
943 | exit: | |||
944 | Py_XDECREF(selfit)_Py_XDECREF(((PyObject*)(selfit))); | |||
945 | Py_XDECREF(arrit)_Py_XDECREF(((PyObject*)(arrit))); | |||
946 | Py_XDECREF(arr)_Py_XDECREF(((PyObject*)(arr))); | |||
947 | return retval; | |||
948 | } | |||
949 | ||||
950 | static PyObject * | |||
951 | array_transpose_get(PyArrayObject *self) | |||
952 | { | |||
953 | return PyArray_Transpose(self, NULL((void*)0)); | |||
954 | } | |||
955 | ||||
956 | /* If this is None, no function call is made | |||
957 | --- default sub-class behavior | |||
958 | */ | |||
959 | static PyObject * | |||
960 | array_finalize_get(PyArrayObject *NPY_UNUSED(self)(__NPY_UNUSED_TAGGEDself) __attribute__ ((__unused__))) | |||
961 | { | |||
962 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
963 | } | |||
964 | ||||
965 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyGetSetDef array_getsetlist[] = { | |||
966 | {"ndim", | |||
967 | (getter)array_ndim_get, | |||
968 | NULL((void*)0), | |||
969 | NULL((void*)0), NULL((void*)0)}, | |||
970 | {"flags", | |||
971 | (getter)array_flags_get, | |||
972 | NULL((void*)0), | |||
973 | NULL((void*)0), NULL((void*)0)}, | |||
974 | {"shape", | |||
975 | (getter)array_shape_get, | |||
976 | (setter)array_shape_set, | |||
977 | NULL((void*)0), NULL((void*)0)}, | |||
978 | {"strides", | |||
979 | (getter)array_strides_get, | |||
980 | (setter)array_strides_set, | |||
981 | NULL((void*)0), NULL((void*)0)}, | |||
982 | {"data", | |||
983 | (getter)array_data_get, | |||
984 | (setter)array_data_set, | |||
985 | NULL((void*)0), NULL((void*)0)}, | |||
986 | {"itemsize", | |||
987 | (getter)array_itemsize_get, | |||
988 | NULL((void*)0), | |||
989 | NULL((void*)0), NULL((void*)0)}, | |||
990 | {"size", | |||
991 | (getter)array_size_get, | |||
992 | NULL((void*)0), | |||
993 | NULL((void*)0), NULL((void*)0)}, | |||
994 | {"nbytes", | |||
995 | (getter)array_nbytes_get, | |||
996 | NULL((void*)0), | |||
997 | NULL((void*)0), NULL((void*)0)}, | |||
998 | {"base", | |||
999 | (getter)array_base_get, | |||
1000 | NULL((void*)0), | |||
1001 | NULL((void*)0), NULL((void*)0)}, | |||
1002 | {"dtype", | |||
1003 | (getter)array_descr_get, | |||
1004 | (setter)array_descr_set, | |||
1005 | NULL((void*)0), NULL((void*)0)}, | |||
1006 | {"real", | |||
1007 | (getter)array_real_get, | |||
1008 | (setter)array_real_set, | |||
1009 | NULL((void*)0), NULL((void*)0)}, | |||
1010 | {"imag", | |||
1011 | (getter)array_imag_get, | |||
1012 | (setter)array_imag_set, | |||
1013 | NULL((void*)0), NULL((void*)0)}, | |||
1014 | {"flat", | |||
1015 | (getter)array_flat_get, | |||
1016 | (setter)array_flat_set, | |||
1017 | NULL((void*)0), NULL((void*)0)}, | |||
1018 | {"ctypes", | |||
1019 | (getter)array_ctypes_get, | |||
1020 | NULL((void*)0), | |||
1021 | NULL((void*)0), NULL((void*)0)}, | |||
1022 | {"T", | |||
1023 | (getter)array_transpose_get, | |||
1024 | NULL((void*)0), | |||
1025 | NULL((void*)0), NULL((void*)0)}, | |||
1026 | {"__array_interface__", | |||
1027 | (getter)array_interface_get, | |||
1028 | NULL((void*)0), | |||
1029 | NULL((void*)0), NULL((void*)0)}, | |||
1030 | {"__array_struct__", | |||
1031 | (getter)array_struct_get, | |||
1032 | NULL((void*)0), | |||
1033 | NULL((void*)0), NULL((void*)0)}, | |||
1034 | {"__array_priority__", | |||
1035 | (getter)array_priority_get, | |||
1036 | NULL((void*)0), | |||
1037 | NULL((void*)0), NULL((void*)0)}, | |||
1038 | {"__array_finalize__", | |||
1039 | (getter)array_finalize_get, | |||
1040 | NULL((void*)0), | |||
1041 | NULL((void*)0), NULL((void*)0)}, | |||
1042 | {NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0)}, /* Sentinel */ | |||
1043 | }; | |||
1044 | ||||
1045 | /****************** end of attribute get and set routines *******************/ |
1 | #ifndef PyCapsule_New |
2 | struct _object; |
3 | typedef struct _object PyObject; |
4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
5 | PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor) { |
6 | return clang_analyzer_PyObject_New_Reference(); |
7 | } |
8 | #else |
9 | #warning "API PyCapsule_New is defined as a macro." |
10 | #endif |