File: | numpy/core/src/multiarray/array_method.c |
Warning: | line 474, column 24 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * This file implements an abstraction layer for "Array methods", which | |||
3 | * work with a specific DType class input and provide low-level C function | |||
4 | * pointers to do fast operations on the given input functions. | |||
5 | * It thus adds an abstraction layer around individual ufunc loops. | |||
6 | * | |||
7 | * Unlike methods, a ArrayMethod can have multiple inputs and outputs. | |||
8 | * This has some serious implication for garbage collection, and as far | |||
9 | * as I (@seberg) understands, it is not possible to always guarantee correct | |||
10 | * cyclic garbage collection of dynamically created DTypes with methods. | |||
11 | * The keyword (or rather the solution) for this seems to be an "ephemeron" | |||
12 | * which I believe should allow correct garbage collection but seems | |||
13 | * not implemented in Python at this time. | |||
14 | * The vast majority of use-cases will not require correct garbage collection. | |||
15 | * Some use cases may require the user to be careful. | |||
16 | * | |||
17 | * Generally there are two main ways to solve this issue: | |||
18 | * | |||
19 | * 1. A method with a single input (or inputs of all the same DTypes) can | |||
20 | * be "owned" by that DType (it becomes unusable when the DType is deleted). | |||
21 | * This holds especially for all casts, which must have a defined output | |||
22 | * DType and must hold on to it strongly. | |||
23 | * 2. A method which can infer the output DType(s) from the input types does | |||
24 | * not need to keep the output type alive. (It can use NULL for the type, | |||
25 | * or an abstract base class which is known to be persistent.) | |||
26 | * It is then sufficient for a ufunc (or other owner) to only hold a | |||
27 | * weak reference to the input DTypes. | |||
28 | */ | |||
29 | ||||
30 | ||||
31 | #define NPY_NO_DEPRECATED_API0x0000000E NPY_API_VERSION0x0000000E | |||
32 | #define _MULTIARRAYMODULE | |||
33 | #include <npy_pycompat.h> | |||
34 | #include "arrayobject.h" | |||
35 | #include "array_method.h" | |||
36 | #include "dtypemeta.h" | |||
37 | #include "common_dtype.h" | |||
38 | #include "convert_datatype.h" | |||
39 | ||||
40 | ||||
41 | /* | |||
42 | * The default descriptor resolution function. The logic is as follows: | |||
43 | * | |||
44 | * 1. The output is ensured to be canonical (currently native byte order), | |||
45 | * if it is of the correct DType. | |||
46 | * 2. If any DType is was not defined, it is replaced by the common DType | |||
47 | * of all inputs. (If that common DType is parametric, this is an error.) | |||
48 | * | |||
49 | * We could allow setting the output descriptors specifically to simplify | |||
50 | * this step. | |||
51 | */ | |||
52 | static NPY_CASTING | |||
53 | default_resolve_descriptors( | |||
54 | PyArrayMethodObject *method, | |||
55 | PyArray_DTypeMeta **dtypes, | |||
56 | PyArray_Descr **input_descrs, | |||
57 | PyArray_Descr **output_descrs) | |||
58 | { | |||
59 | int nin = method->nin; | |||
60 | int nout = method->nout; | |||
61 | int all_defined = 1; | |||
62 | ||||
63 | for (int i = 0; i < nin + nout; i++) { | |||
64 | PyArray_DTypeMeta *dtype = dtypes[i]; | |||
65 | if (dtype == NULL((void*)0)) { | |||
66 | output_descrs[i] = NULL((void*)0); | |||
67 | all_defined = 0; | |||
68 | continue; | |||
69 | } | |||
70 | if (NPY_DTYPE(input_descrs[i])((PyArray_DTypeMeta *)(((PyObject*)(input_descrs[i]))->ob_type )) == dtype) { | |||
71 | output_descrs[i] = ensure_dtype_nbo(input_descrs[i]); | |||
72 | } | |||
73 | else { | |||
74 | output_descrs[i] = dtype->default_descr(dtype); | |||
75 | } | |||
76 | if (NPY_UNLIKELY(output_descrs[i] == NULL)__builtin_expect(!!(output_descrs[i] == ((void*)0)), 0)) { | |||
77 | goto fail; | |||
78 | } | |||
79 | } | |||
80 | if (all_defined) { | |||
81 | return method->casting; | |||
82 | } | |||
83 | ||||
84 | if (NPY_UNLIKELY(nin == 0 || dtypes[0] == NULL)__builtin_expect(!!(nin == 0 || dtypes[0] == ((void*)0)), 0)) { | |||
85 | /* Registration should reject this, so this would be indicates a bug */ | |||
86 | PyErr_SetString(PyExc_RuntimeError, | |||
87 | "Invalid use of default resolver without inputs or with " | |||
88 | "input or output DType incorrectly missing."); | |||
89 | goto fail; | |||
90 | } | |||
91 | /* We find the common dtype of all inputs, and use it for the unknowns */ | |||
92 | PyArray_DTypeMeta *common_dtype = dtypes[0]; | |||
93 | assert(common_dtype != NULL)((void) (0)); | |||
94 | for (int i = 1; i < nin; i++) { | |||
95 | Py_SETREF(common_dtype, PyArray_CommonDType(common_dtype, dtypes[i]))do { PyObject *_py_tmp = ((PyObject*)(common_dtype)); (common_dtype ) = (PyArray_CommonDType(common_dtype, dtypes[i])); _Py_DECREF (((PyObject*)(_py_tmp))); } while (0); | |||
96 | if (common_dtype == NULL((void*)0)) { | |||
97 | goto fail; | |||
98 | } | |||
99 | } | |||
100 | for (int i = nin; i < nin + nout; i++) { | |||
101 | if (output_descrs[i] != NULL((void*)0)) { | |||
102 | continue; | |||
103 | } | |||
104 | if (NPY_DTYPE(input_descrs[i])((PyArray_DTypeMeta *)(((PyObject*)(input_descrs[i]))->ob_type )) == common_dtype) { | |||
105 | output_descrs[i] = ensure_dtype_nbo(input_descrs[i]); | |||
106 | } | |||
107 | else { | |||
108 | output_descrs[i] = common_dtype->default_descr(common_dtype); | |||
109 | } | |||
110 | if (NPY_UNLIKELY(output_descrs[i] == NULL)__builtin_expect(!!(output_descrs[i] == ((void*)0)), 0)) { | |||
111 | goto fail; | |||
112 | } | |||
113 | } | |||
114 | ||||
115 | return method->casting; | |||
116 | ||||
117 | fail: | |||
118 | for (int i = 0; i < nin + nout; i++) { | |||
119 | Py_XDECREF(output_descrs[i])_Py_XDECREF(((PyObject*)(output_descrs[i]))); | |||
120 | } | |||
121 | return -1; | |||
122 | } | |||
123 | ||||
124 | ||||
125 | NPY_INLINEinline static int | |||
126 | is_contiguous( | |||
127 | npy_intp const *strides, PyArray_Descr *const *descriptors, int nargs) | |||
128 | { | |||
129 | for (int i = 0; i < nargs; i++) { | |||
130 | if (strides[i] != descriptors[i]->elsize) { | |||
131 | return 0; | |||
132 | } | |||
133 | } | |||
134 | return 1; | |||
135 | } | |||
136 | ||||
137 | ||||
138 | /** | |||
139 | * The default method to fetch the correct loop for a cast or ufunc | |||
140 | * (at the time of writing only casts). | |||
141 | * The default version can return loops explicitly registered during method | |||
142 | * creation. It does specialize contiguous loops, although has to check | |||
143 | * all descriptors itemsizes for this. | |||
144 | * | |||
145 | * @param context | |||
146 | * @param aligned | |||
147 | * @param move_references UNUSED. | |||
148 | * @param strides | |||
149 | * @param descriptors | |||
150 | * @param out_loop | |||
151 | * @param out_transferdata | |||
152 | * @param flags | |||
153 | * @return 0 on success -1 on failure. | |||
154 | */ | |||
155 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) int | |||
156 | npy_default_get_strided_loop( | |||
157 | PyArrayMethod_Context *context, | |||
158 | int aligned, int NPY_UNUSED(move_references)(__NPY_UNUSED_TAGGEDmove_references) __attribute__ ((__unused__ )), npy_intp *strides, | |||
159 | PyArrayMethod_StridedLoop **out_loop, NpyAuxData **out_transferdata, | |||
160 | NPY_ARRAYMETHOD_FLAGS *flags) | |||
161 | { | |||
162 | PyArray_Descr **descrs = context->descriptors; | |||
163 | PyArrayMethodObject *meth = context->method; | |||
164 | *flags = meth->flags & NPY_METH_RUNTIME_FLAGS; | |||
165 | *out_transferdata = NULL((void*)0); | |||
166 | ||||
167 | int nargs = meth->nin + meth->nout; | |||
168 | if (aligned) { | |||
169 | if (meth->contiguous_loop == NULL((void*)0) || | |||
170 | !is_contiguous(strides, descrs, nargs)) { | |||
171 | *out_loop = meth->strided_loop; | |||
172 | return 0; | |||
173 | } | |||
174 | *out_loop = meth->contiguous_loop; | |||
175 | } | |||
176 | else { | |||
177 | if (meth->unaligned_contiguous_loop == NULL((void*)0) || | |||
178 | !is_contiguous(strides, descrs, nargs)) { | |||
179 | *out_loop = meth->unaligned_strided_loop; | |||
180 | return 0; | |||
181 | } | |||
182 | *out_loop = meth->unaligned_contiguous_loop; | |||
183 | } | |||
184 | return 0; | |||
185 | } | |||
186 | ||||
187 | ||||
188 | /** | |||
189 | * Validate that the input is usable to create a new ArrayMethod. | |||
190 | * | |||
191 | * @param spec | |||
192 | * @return 0 on success -1 on error. | |||
193 | */ | |||
194 | static int | |||
195 | validate_spec(PyArrayMethod_Spec *spec) | |||
196 | { | |||
197 | int nargs = spec->nin + spec->nout; | |||
198 | /* Check the passed spec for invalid fields/values */ | |||
199 | if (spec->nin < 0 || spec->nout < 0 || nargs > NPY_MAXARGS32) { | |||
200 | PyErr_Format(PyExc_ValueError, | |||
201 | "ArrayMethod inputs and outputs must be greater zero and" | |||
202 | "not exceed %d. (method: %s)", NPY_MAXARGS32, spec->name); | |||
203 | return -1; | |||
204 | } | |||
205 | switch (spec->casting & ~_NPY_CAST_IS_VIEW) { | |||
206 | case NPY_NO_CASTING: | |||
207 | case NPY_EQUIV_CASTING: | |||
208 | case NPY_SAFE_CASTING: | |||
209 | case NPY_SAME_KIND_CASTING: | |||
210 | case NPY_UNSAFE_CASTING: | |||
211 | break; | |||
212 | default: | |||
213 | if (spec->casting != -1) { | |||
214 | PyErr_Format(PyExc_TypeError, | |||
215 | "ArrayMethod has invalid casting `%d`. (method: %s)", | |||
216 | spec->casting, spec->name); | |||
217 | return -1; | |||
218 | } | |||
219 | } | |||
220 | ||||
221 | for (int i = 0; i < nargs; i++) { | |||
222 | if (spec->dtypes[i] == NULL((void*)0) && i < spec->nin) { | |||
223 | PyErr_Format(PyExc_TypeError, | |||
224 | "ArrayMethod must have well defined input DTypes. " | |||
225 | "(method: %s)", spec->name); | |||
226 | return -1; | |||
227 | } | |||
228 | if (!PyObject_TypeCheck(spec->dtypes[i], &PyArrayDTypeMeta_Type)((((PyObject*)(spec->dtypes[i]))->ob_type) == (&PyArrayDTypeMeta_Type ) || PyType_IsSubtype((((PyObject*)(spec->dtypes[i]))-> ob_type), (&PyArrayDTypeMeta_Type)))) { | |||
229 | PyErr_Format(PyExc_TypeError, | |||
230 | "ArrayMethod provided object %R is not a DType." | |||
231 | "(method: %s)", spec->dtypes[i], spec->name); | |||
232 | return -1; | |||
233 | } | |||
234 | if (spec->dtypes[i]->abstract && i < spec->nin) { | |||
235 | PyErr_Format(PyExc_TypeError, | |||
236 | "abstract DType %S are currently not allowed for inputs." | |||
237 | "(method: %s defined at %s)", spec->dtypes[i], spec->name); | |||
238 | return -1; | |||
239 | } | |||
240 | } | |||
241 | return 0; | |||
242 | } | |||
243 | ||||
244 | ||||
245 | /** | |||
246 | * Initialize a new BoundArrayMethodObject from slots. Slots which are | |||
247 | * not provided may be filled with defaults. | |||
248 | * | |||
249 | * @param res The new PyBoundArrayMethodObject to be filled. | |||
250 | * @param spec The specification list passed by the user. | |||
251 | * @param private Private flag to limit certain slots to use in NumPy. | |||
252 | * @return -1 on error 0 on success | |||
253 | */ | |||
254 | static int | |||
255 | fill_arraymethod_from_slots( | |||
256 | PyBoundArrayMethodObject *res, PyArrayMethod_Spec *spec, | |||
257 | int private) | |||
258 | { | |||
259 | PyArrayMethodObject *meth = res->method; | |||
260 | ||||
261 | /* Set the defaults */ | |||
262 | meth->get_strided_loop = &npy_default_get_strided_loop; | |||
263 | meth->resolve_descriptors = &default_resolve_descriptors; | |||
264 | ||||
265 | /* Fill in the slots passed by the user */ | |||
266 | /* | |||
267 | * TODO: This is reasonable for now, but it would be nice to find a | |||
268 | * shorter solution, and add some additional error checking (e.g. | |||
269 | * the same slot used twice). Python uses an array of slot offsets. | |||
270 | */ | |||
271 | for (PyType_Slot *slot = &spec->slots[0]; slot->slot != 0; slot++) { | |||
272 | switch (slot->slot) { | |||
273 | case NPY_METH_resolve_descriptors1: | |||
274 | meth->resolve_descriptors = slot->pfunc; | |||
275 | continue; | |||
276 | case NPY_METH_get_loop2: | |||
277 | if (private) { | |||
278 | /* Only allow override for private functions initially */ | |||
279 | meth->get_strided_loop = slot->pfunc; | |||
280 | continue; | |||
281 | } | |||
282 | break; | |||
283 | case NPY_METH_strided_loop3: | |||
284 | meth->strided_loop = slot->pfunc; | |||
285 | continue; | |||
286 | case NPY_METH_contiguous_loop4: | |||
287 | meth->contiguous_loop = slot->pfunc; | |||
288 | continue; | |||
289 | case NPY_METH_unaligned_strided_loop5: | |||
290 | meth->unaligned_strided_loop = slot->pfunc; | |||
291 | continue; | |||
292 | case NPY_METH_unaligned_contiguous_loop6: | |||
293 | meth->unaligned_contiguous_loop = slot->pfunc; | |||
294 | continue; | |||
295 | default: | |||
296 | break; | |||
297 | } | |||
298 | PyErr_Format(PyExc_RuntimeError, | |||
299 | "invalid slot number %d to ArrayMethod: %s", | |||
300 | slot->slot, spec->name); | |||
301 | return -1; | |||
302 | } | |||
303 | ||||
304 | /* Check whether the slots are valid: */ | |||
305 | if (meth->resolve_descriptors == &default_resolve_descriptors) { | |||
306 | if (spec->casting == -1) { | |||
307 | PyErr_Format(PyExc_TypeError, | |||
308 | "Cannot set casting to -1 (invalid) when not providing " | |||
309 | "the default `resolve_descriptors` function. " | |||
310 | "(method: %s)", spec->name); | |||
311 | return -1; | |||
312 | } | |||
313 | for (int i = 0; i < meth->nin + meth->nout; i++) { | |||
314 | if (res->dtypes[i] == NULL((void*)0)) { | |||
315 | if (i < meth->nin) { | |||
316 | PyErr_Format(PyExc_TypeError, | |||
317 | "All input DTypes must be specified when using " | |||
318 | "the default `resolve_descriptors` function. " | |||
319 | "(method: %s)", spec->name); | |||
320 | return -1; | |||
321 | } | |||
322 | else if (meth->nin == 0) { | |||
323 | PyErr_Format(PyExc_TypeError, | |||
324 | "Must specify output DTypes or use custom " | |||
325 | "`resolve_descriptors` when there are no inputs. " | |||
326 | "(method: %s defined at %s)", spec->name); | |||
327 | return -1; | |||
328 | } | |||
329 | } | |||
330 | if (i >= meth->nin && res->dtypes[i]->parametric) { | |||
331 | PyErr_Format(PyExc_TypeError, | |||
332 | "must provide a `resolve_descriptors` function if any " | |||
333 | "output DType is parametric. (method: %s)", | |||
334 | spec->name); | |||
335 | return -1; | |||
336 | } | |||
337 | } | |||
338 | } | |||
339 | if (meth->get_strided_loop != &npy_default_get_strided_loop) { | |||
340 | /* Do not check the actual loop fields. */ | |||
341 | return 0; | |||
342 | } | |||
343 | ||||
344 | /* Check whether the provided loops make sense. */ | |||
345 | if (meth->strided_loop == NULL((void*)0)) { | |||
346 | PyErr_Format(PyExc_TypeError, | |||
347 | "Must provide a strided inner loop function. (method: %s)", | |||
348 | spec->name); | |||
349 | return -1; | |||
350 | } | |||
351 | if (meth->contiguous_loop == NULL((void*)0)) { | |||
352 | meth->contiguous_loop = meth->strided_loop; | |||
353 | } | |||
354 | if (meth->unaligned_contiguous_loop != NULL((void*)0) && | |||
355 | meth->unaligned_strided_loop == NULL((void*)0)) { | |||
356 | PyErr_Format(PyExc_TypeError, | |||
357 | "Must provide unaligned strided inner loop when providing " | |||
358 | "a contiguous version. (method: %s)", spec->name); | |||
359 | return -1; | |||
360 | } | |||
361 | if ((meth->unaligned_strided_loop == NULL((void*)0)) != | |||
362 | !(meth->flags & NPY_METH_SUPPORTS_UNALIGNED)) { | |||
363 | PyErr_Format(PyExc_TypeError, | |||
364 | "Must provide unaligned strided inner loop when providing " | |||
365 | "a contiguous version. (method: %s)", spec->name); | |||
366 | return -1; | |||
367 | } | |||
368 | ||||
369 | return 0; | |||
370 | } | |||
371 | ||||
372 | ||||
373 | /** | |||
374 | * Create a new ArrayMethod (internal version). | |||
375 | * | |||
376 | * @param name A name for the individual method, may be NULL. | |||
377 | * @param spec A filled context object to pass generic information about | |||
378 | * the method (such as usually needing the API, and the DTypes). | |||
379 | * Unused fields must be NULL. | |||
380 | * @param slots Slots with the correct pair of IDs and (function) pointers. | |||
381 | * @param private Some slots are currently considered private, if not true, | |||
382 | * these will be rejected. | |||
383 | * | |||
384 | * @returns A new (bound) ArrayMethod object. | |||
385 | */ | |||
386 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyBoundArrayMethodObject * | |||
387 | PyArrayMethod_FromSpec_int(PyArrayMethod_Spec *spec, int private) | |||
388 | { | |||
389 | int nargs = spec->nin + spec->nout; | |||
390 | ||||
391 | if (spec->name == NULL((void*)0)) { | |||
392 | spec->name = "<unknown>"; | |||
393 | } | |||
394 | ||||
395 | if (validate_spec(spec) < 0) { | |||
396 | return NULL((void*)0); | |||
397 | } | |||
398 | ||||
399 | PyBoundArrayMethodObject *res; | |||
400 | res = PyObject_New(PyBoundArrayMethodObject, &PyBoundArrayMethod_Type)( (PyBoundArrayMethodObject *) _PyObject_New(&PyBoundArrayMethod_Type ) ); | |||
401 | if (res == NULL((void*)0)) { | |||
402 | return NULL((void*)0); | |||
403 | } | |||
404 | res->method = NULL((void*)0); | |||
405 | ||||
406 | res->dtypes = PyMem_Malloc(sizeof(PyArray_DTypeMeta *) * nargs); | |||
407 | if (res->dtypes == NULL((void*)0)) { | |||
408 | Py_DECREF(res)_Py_DECREF(((PyObject*)(res))); | |||
409 | PyErr_NoMemory(); | |||
410 | return NULL((void*)0); | |||
411 | } | |||
412 | for (int i = 0; i < nargs ; i++) { | |||
413 | Py_XINCREF(spec->dtypes[i])_Py_XINCREF(((PyObject*)(spec->dtypes[i]))); | |||
414 | res->dtypes[i] = spec->dtypes[i]; | |||
415 | } | |||
416 | ||||
417 | res->method = PyObject_New(PyArrayMethodObject, &PyArrayMethod_Type)( (PyArrayMethodObject *) _PyObject_New(&PyArrayMethod_Type ) ); | |||
418 | if (res->method == NULL((void*)0)) { | |||
419 | Py_DECREF(res)_Py_DECREF(((PyObject*)(res))); | |||
420 | PyErr_NoMemory(); | |||
421 | return NULL((void*)0); | |||
422 | } | |||
423 | memset((char *)(res->method) + sizeof(PyObject), 0, | |||
424 | sizeof(PyArrayMethodObject) - sizeof(PyObject)); | |||
425 | ||||
426 | res->method->nin = spec->nin; | |||
427 | res->method->nout = spec->nout; | |||
428 | res->method->flags = spec->flags; | |||
429 | res->method->casting = spec->casting; | |||
430 | if (fill_arraymethod_from_slots(res, spec, private) < 0) { | |||
431 | Py_DECREF(res)_Py_DECREF(((PyObject*)(res))); | |||
432 | return NULL((void*)0); | |||
433 | } | |||
434 | ||||
435 | Py_ssize_t length = strlen(spec->name); | |||
436 | res->method->name = PyMem_Malloc(length + 1); | |||
437 | if (res->method->name == NULL((void*)0)) { | |||
438 | Py_DECREF(res)_Py_DECREF(((PyObject*)(res))); | |||
439 | PyErr_NoMemory(); | |||
440 | return NULL((void*)0); | |||
441 | } | |||
442 | strcpy(res->method->name, spec->name); | |||
443 | ||||
444 | return res; | |||
445 | } | |||
446 | ||||
447 | ||||
448 | static void | |||
449 | arraymethod_dealloc(PyObject *self) | |||
450 | { | |||
451 | PyArrayMethodObject *meth; | |||
452 | meth = ((PyArrayMethodObject *)self); | |||
453 | ||||
454 | PyMem_Free(meth->name); | |||
455 | ||||
456 | Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_free(self); | |||
457 | } | |||
458 | ||||
459 | ||||
460 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyTypeObject PyArrayMethod_Type = { | |||
461 | PyVarObject_HEAD_INIT(NULL, 0){ { 1, ((void*)0) }, 0 }, | |||
462 | .tp_name = "numpy._ArrayMethod", | |||
463 | .tp_basicsize = sizeof(PyArrayMethodObject), | |||
464 | .tp_flags = Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), | |||
465 | .tp_dealloc = arraymethod_dealloc, | |||
466 | }; | |||
467 | ||||
468 | ||||
469 | ||||
470 | static PyObject * | |||
471 | boundarraymethod_repr(PyBoundArrayMethodObject *self) | |||
472 | { | |||
473 | int nargs = self->method->nin + self->method->nout; | |||
474 | PyObject *dtypes = PyTuple_New(nargs); | |||
| ||||
| ||||
475 | if (dtypes == NULL((void*)0)) { | |||
476 | return NULL((void*)0); | |||
477 | } | |||
478 | for (int i = 0; i < nargs; i++) { | |||
479 | Py_INCREF(self->dtypes[i])_Py_INCREF(((PyObject*)(self->dtypes[i]))); | |||
480 | PyTuple_SET_ITEM(dtypes, i, (PyObject *)self->dtypes[i])PyTuple_SetItem(dtypes, i, (PyObject *)self->dtypes[i]); | |||
481 | } | |||
482 | return PyUnicode_FromFormat( | |||
483 | "<np._BoundArrayMethod `%s` for dtypes %S>", | |||
484 | self->method->name, dtypes); | |||
485 | } | |||
486 | ||||
487 | ||||
488 | static void | |||
489 | boundarraymethod_dealloc(PyObject *self) | |||
490 | { | |||
491 | PyBoundArrayMethodObject *meth; | |||
492 | meth = ((PyBoundArrayMethodObject *)self); | |||
493 | int nargs = meth->method->nin + meth->method->nout; | |||
494 | ||||
495 | for (int i = 0; i < nargs; i++) { | |||
496 | Py_XDECREF(meth->dtypes[i])_Py_XDECREF(((PyObject*)(meth->dtypes[i]))); | |||
497 | } | |||
498 | PyMem_Free(meth->dtypes); | |||
499 | ||||
500 | Py_XDECREF(meth->method)_Py_XDECREF(((PyObject*)(meth->method))); | |||
501 | ||||
502 | Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_free(self); | |||
503 | } | |||
504 | ||||
505 | ||||
506 | /* | |||
507 | * Calls resolve_descriptors() and returns the casting level and the resolved | |||
508 | * descriptors as a tuple. If the operation is impossible returns (-1, None). | |||
509 | * May raise an error, but usually should not. | |||
510 | * The function validates the casting attribute compared to the returned | |||
511 | * casting level. | |||
512 | * | |||
513 | * TODO: This function is not public API, and certain code paths will need | |||
514 | * changes and especially testing if they were to be made public. | |||
515 | */ | |||
516 | static PyObject * | |||
517 | boundarraymethod__resolve_descripors( | |||
518 | PyBoundArrayMethodObject *self, PyObject *descr_tuple) | |||
519 | { | |||
520 | int nin = self->method->nin; | |||
521 | int nout = self->method->nout; | |||
522 | ||||
523 | PyArray_Descr *given_descrs[NPY_MAXARGS32]; | |||
524 | PyArray_Descr *loop_descrs[NPY_MAXARGS32]; | |||
525 | ||||
526 | if (!PyTuple_CheckExact(descr_tuple)((((PyObject*)(descr_tuple))->ob_type) == &PyTuple_Type ) || | |||
527 | PyTuple_Size(descr_tuple) != nin + nout) { | |||
528 | PyErr_Format(PyExc_TypeError, | |||
529 | "_resolve_descriptors() takes exactly one tuple with as many " | |||
530 | "elements as the method takes arguments (%d+%d).", nin, nout); | |||
531 | return NULL((void*)0); | |||
532 | } | |||
533 | ||||
534 | for (int i = 0; i < nin + nout; i++) { | |||
535 | PyObject *tmp = PyTuple_GetItem(descr_tuple, i); | |||
536 | if (tmp == NULL((void*)0)) { | |||
537 | return NULL((void*)0); | |||
538 | } | |||
539 | else if (tmp == Py_None(&_Py_NoneStruct)) { | |||
540 | if (i < nin) { | |||
541 | PyErr_SetString(PyExc_TypeError, | |||
542 | "only output dtypes may be omitted (set to None)."); | |||
543 | return NULL((void*)0); | |||
544 | } | |||
545 | given_descrs[i] = NULL((void*)0); | |||
546 | } | |||
547 | else if (PyArray_DescrCheck(tmp)((((PyObject*)(tmp))->ob_type) == (&(*(PyTypeObject *) (&PyArrayDescr_TypeFull))) || PyType_IsSubtype((((PyObject *)(tmp))->ob_type), (&(*(PyTypeObject *)(&PyArrayDescr_TypeFull )))))) { | |||
548 | if (Py_TYPE(tmp)(((PyObject*)(tmp))->ob_type) != (PyTypeObject *)self->dtypes[i]) { | |||
549 | PyErr_Format(PyExc_TypeError, | |||
550 | "input dtype %S was not an exact instance of the bound " | |||
551 | "DType class %S.", tmp, self->dtypes[i]); | |||
552 | return NULL((void*)0); | |||
553 | } | |||
554 | given_descrs[i] = (PyArray_Descr *)tmp; | |||
555 | } | |||
556 | else { | |||
557 | PyErr_SetString(PyExc_TypeError, | |||
558 | "dtype tuple can only contain dtype instances or None."); | |||
559 | return NULL((void*)0); | |||
560 | } | |||
561 | } | |||
562 | ||||
563 | NPY_CASTING casting = self->method->resolve_descriptors( | |||
564 | self->method, self->dtypes, given_descrs, loop_descrs); | |||
565 | ||||
566 | if (casting < 0 && PyErr_Occurred()) { | |||
567 | return NULL((void*)0); | |||
568 | } | |||
569 | else if (casting < 0) { | |||
570 | return Py_BuildValue("iO", casting, Py_None(&_Py_NoneStruct)); | |||
571 | } | |||
572 | ||||
573 | PyObject *result_tuple = PyTuple_New(nin + nout); | |||
574 | if (result_tuple == NULL((void*)0)) { | |||
575 | return NULL((void*)0); | |||
576 | } | |||
577 | for (int i = 0; i < nin + nout; i++) { | |||
578 | /* transfer ownership to the tuple. */ | |||
579 | PyTuple_SET_ITEM(result_tuple, i, (PyObject *)loop_descrs[i])PyTuple_SetItem(result_tuple, i, (PyObject *)loop_descrs[i]); | |||
580 | } | |||
581 | ||||
582 | /* | |||
583 | * The casting flags should be the most generic casting level (except the | |||
584 | * cast-is-view flag. If no input is parametric, it must match exactly. | |||
585 | * | |||
586 | * (Note that these checks are only debugging checks.) | |||
587 | */ | |||
588 | int parametric = 0; | |||
589 | for (int i = 0; i < nin + nout; i++) { | |||
590 | if (self->dtypes[i]->parametric) { | |||
591 | parametric = 1; | |||
592 | break; | |||
593 | } | |||
594 | } | |||
595 | if (self->method->casting != -1) { | |||
596 | NPY_CASTING cast = casting & ~_NPY_CAST_IS_VIEW; | |||
597 | if (self->method->casting != | |||
598 | PyArray_MinCastSafety(cast, self->method->casting)) { | |||
599 | PyErr_Format(PyExc_RuntimeError, | |||
600 | "resolve_descriptors cast level did not match stored one. " | |||
601 | "(set level is %d, got %d for method %s)", | |||
602 | self->method->casting, cast, self->method->name); | |||
603 | Py_DECREF(result_tuple)_Py_DECREF(((PyObject*)(result_tuple))); | |||
604 | return NULL((void*)0); | |||
605 | } | |||
606 | if (!parametric) { | |||
607 | /* | |||
608 | * Non-parametric can only mismatch if it switches from equiv to no | |||
609 | * (e.g. due to byteorder changes). | |||
610 | */ | |||
611 | if (cast != self->method->casting && | |||
612 | self->method->casting != NPY_EQUIV_CASTING) { | |||
613 | PyErr_Format(PyExc_RuntimeError, | |||
614 | "resolve_descriptors cast level changed even though " | |||
615 | "the cast is non-parametric where the only possible " | |||
616 | "change should be from equivalent to no casting. " | |||
617 | "(set level is %d, got %d for method %s)", | |||
618 | self->method->casting, cast, self->method->name); | |||
619 | Py_DECREF(result_tuple)_Py_DECREF(((PyObject*)(result_tuple))); | |||
620 | return NULL((void*)0); | |||
621 | } | |||
622 | } | |||
623 | } | |||
624 | ||||
625 | return Py_BuildValue("iN", casting, result_tuple); | |||
626 | } | |||
627 | ||||
628 | ||||
629 | /* | |||
630 | * TODO: This function is not public API, and certain code paths will need | |||
631 | * changes and especially testing if they were to be made public. | |||
632 | */ | |||
633 | static PyObject * | |||
634 | boundarraymethod__simple_strided_call( | |||
635 | PyBoundArrayMethodObject *self, PyObject *arr_tuple) | |||
636 | { | |||
637 | PyArrayObject *arrays[NPY_MAXARGS32]; | |||
638 | PyArray_Descr *descrs[NPY_MAXARGS32]; | |||
639 | PyArray_Descr *out_descrs[NPY_MAXARGS32]; | |||
640 | Py_ssize_t length = -1; | |||
641 | int aligned = 1; | |||
642 | char *args[NPY_MAXARGS32]; | |||
643 | npy_intp strides[NPY_MAXARGS32]; | |||
644 | int nin = self->method->nin; | |||
645 | int nout = self->method->nout; | |||
646 | ||||
647 | if (!PyTuple_CheckExact(arr_tuple)((((PyObject*)(arr_tuple))->ob_type) == &PyTuple_Type) || | |||
648 | PyTuple_Size(arr_tuple) != nin + nout) { | |||
649 | PyErr_Format(PyExc_TypeError, | |||
650 | "_simple_strided_call() takes exactly one tuple with as many " | |||
651 | "arrays as the method takes arguments (%d+%d).", nin, nout); | |||
652 | return NULL((void*)0); | |||
653 | } | |||
654 | ||||
655 | for (int i = 0; i < nin + nout; i++) { | |||
656 | PyObject *tmp = PyTuple_GetItem(arr_tuple, i); | |||
657 | if (tmp == NULL((void*)0)) { | |||
658 | return NULL((void*)0); | |||
659 | } | |||
660 | else if (!PyArray_CheckExact(tmp)(((PyObject*)(tmp))->ob_type == &PyArray_Type)) { | |||
661 | PyErr_SetString(PyExc_TypeError, | |||
662 | "All inputs must be NumPy arrays."); | |||
663 | return NULL((void*)0); | |||
664 | } | |||
665 | arrays[i] = (PyArrayObject *)tmp; | |||
666 | descrs[i] = PyArray_DESCR(arrays[i]); | |||
667 | ||||
668 | /* Check that the input is compatible with a simple method call. */ | |||
669 | if (Py_TYPE(descrs[i])(((PyObject*)(descrs[i]))->ob_type) != (PyTypeObject *)self->dtypes[i]) { | |||
670 | PyErr_Format(PyExc_TypeError, | |||
671 | "input dtype %S was not an exact instance of the bound " | |||
672 | "DType class %S.", descrs[i], self->dtypes[i]); | |||
673 | return NULL((void*)0); | |||
674 | } | |||
675 | if (PyArray_NDIM(arrays[i]) != 1) { | |||
676 | PyErr_SetString(PyExc_ValueError, | |||
677 | "All arrays must be one dimensional."); | |||
678 | return NULL((void*)0); | |||
679 | } | |||
680 | if (i == 0) { | |||
681 | length = PyArray_SIZE(arrays[i])PyArray_MultiplyList(PyArray_DIMS(arrays[i]), PyArray_NDIM(arrays [i])); | |||
682 | } | |||
683 | else if (PyArray_SIZE(arrays[i])PyArray_MultiplyList(PyArray_DIMS(arrays[i]), PyArray_NDIM(arrays [i])) != length) { | |||
684 | PyErr_SetString(PyExc_ValueError, | |||
685 | "All arrays must have the same length."); | |||
686 | return NULL((void*)0); | |||
687 | } | |||
688 | if (i >= nout) { | |||
689 | if (PyArray_FailUnlessWriteable( | |||
690 | arrays[i], "_simple_strided_call() output") < 0) { | |||
691 | return NULL((void*)0); | |||
692 | } | |||
693 | } | |||
694 | ||||
695 | args[i] = PyArray_BYTES(arrays[i]); | |||
696 | strides[i] = PyArray_STRIDES(arrays[i])[0]; | |||
697 | /* TODO: We may need to distinguish aligned and itemsize-aligned */ | |||
698 | aligned &= PyArray_ISALIGNED(arrays[i])PyArray_CHKFLAGS((arrays[i]), 0x0100); | |||
699 | } | |||
700 | if (!aligned && !(self->method->flags & NPY_METH_SUPPORTS_UNALIGNED)) { | |||
701 | PyErr_SetString(PyExc_ValueError, | |||
702 | "method does not support unaligned input."); | |||
703 | return NULL((void*)0); | |||
704 | } | |||
705 | ||||
706 | NPY_CASTING casting = self->method->resolve_descriptors( | |||
707 | self->method, self->dtypes, descrs, out_descrs); | |||
708 | ||||
709 | if (casting < 0) { | |||
710 | PyObject *err_type = NULL((void*)0), *err_value = NULL((void*)0), *err_traceback = NULL((void*)0); | |||
711 | PyErr_Fetch(&err_type, &err_value, &err_traceback); | |||
712 | PyErr_SetString(PyExc_TypeError, | |||
713 | "cannot perform method call with the given dtypes."); | |||
714 | npy_PyErr_ChainExceptions(err_type, err_value, err_traceback); | |||
715 | return NULL((void*)0); | |||
716 | } | |||
717 | ||||
718 | int dtypes_were_adapted = 0; | |||
719 | for (int i = 0; i < nin + nout; i++) { | |||
720 | /* NOTE: This check is probably much stricter than necessary... */ | |||
721 | dtypes_were_adapted |= descrs[i] != out_descrs[i]; | |||
722 | Py_DECREF(out_descrs[i])_Py_DECREF(((PyObject*)(out_descrs[i]))); | |||
723 | } | |||
724 | if (dtypes_were_adapted) { | |||
725 | PyErr_SetString(PyExc_TypeError, | |||
726 | "_simple_strided_call(): requires dtypes to not require a cast " | |||
727 | "(must match exactly with `_resolve_descriptors()`)."); | |||
728 | return NULL((void*)0); | |||
729 | } | |||
730 | ||||
731 | PyArrayMethod_Context context = { | |||
732 | .caller = NULL((void*)0), | |||
733 | .method = self->method, | |||
734 | .descriptors = descrs, | |||
735 | }; | |||
736 | PyArrayMethod_StridedLoop *strided_loop = NULL((void*)0); | |||
737 | NpyAuxData *loop_data = NULL((void*)0); | |||
738 | NPY_ARRAYMETHOD_FLAGS flags = 0; | |||
739 | ||||
740 | if (self->method->get_strided_loop( | |||
741 | &context, aligned, 0, strides, | |||
742 | &strided_loop, &loop_data, &flags) < 0) { | |||
743 | return NULL((void*)0); | |||
744 | } | |||
745 | ||||
746 | /* | |||
747 | * TODO: Add floating point error checks if requested and | |||
748 | * possibly release GIL if allowed by the flags. | |||
749 | */ | |||
750 | int res = strided_loop(&context, args, &length, strides, loop_data); | |||
751 | if (loop_data != NULL((void*)0)) { | |||
752 | loop_data->free(loop_data); | |||
753 | } | |||
754 | if (res < 0) { | |||
755 | return NULL((void*)0); | |||
756 | } | |||
757 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
758 | } | |||
759 | ||||
760 | ||||
761 | PyMethodDef boundarraymethod_methods[] = { | |||
762 | {"_resolve_descriptors", (PyCFunction)boundarraymethod__resolve_descripors, | |||
763 | METH_O0x0008, "Resolve the given dtypes."}, | |||
764 | {"_simple_strided_call", (PyCFunction)boundarraymethod__simple_strided_call, | |||
765 | METH_O0x0008, "call on 1-d inputs and pre-allocated outputs (single call)."}, | |||
766 | {NULL((void*)0), 0, 0, NULL((void*)0)}, | |||
767 | }; | |||
768 | ||||
769 | ||||
770 | static PyObject * | |||
771 | boundarraymethod__supports_unaligned(PyBoundArrayMethodObject *self) | |||
772 | { | |||
773 | return PyBool_FromLong(self->method->flags & NPY_METH_SUPPORTS_UNALIGNED); | |||
774 | } | |||
775 | ||||
776 | ||||
777 | PyGetSetDef boundarraymethods_getters[] = { | |||
778 | {"_supports_unaligned", | |||
779 | (getter)boundarraymethod__supports_unaligned, NULL((void*)0), | |||
780 | "whether the method supports unaligned inputs/outputs.", NULL((void*)0)}, | |||
781 | {NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0), NULL((void*)0)}, | |||
782 | }; | |||
783 | ||||
784 | ||||
785 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyTypeObject PyBoundArrayMethod_Type = { | |||
786 | PyVarObject_HEAD_INIT(NULL, 0){ { 1, ((void*)0) }, 0 }, | |||
787 | .tp_name = "numpy._BoundArrayMethod", | |||
788 | .tp_basicsize = sizeof(PyBoundArrayMethodObject), | |||
789 | .tp_flags = Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), | |||
790 | .tp_repr = (reprfunc)boundarraymethod_repr, | |||
791 | .tp_dealloc = boundarraymethod_dealloc, | |||
792 | .tp_methods = boundarraymethod_methods, | |||
793 | .tp_getset = boundarraymethods_getters, | |||
794 | }; |
1 | #ifndef PyTuple_New |
2 | struct _object; |
3 | typedef struct _object PyObject; |
4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
5 | PyObject* PyTuple_New(Py_ssize_t len) { |
6 | return clang_analyzer_PyObject_New_Reference(); |
7 | } |
8 | #else |
9 | #warning "API PyTuple_New is defined as a macro." |
10 | #endif |