| File: | numpy/core/src/multiarray/methods.c |
| Warning: | line 2005, column 19 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #define PY_SSIZE_T_CLEAN | |||
| 2 | #include <stdarg.h> | |||
| 3 | #include <Python.h> | |||
| 4 | #include "structmember.h" | |||
| 5 | ||||
| 6 | #define NPY_NO_DEPRECATED_API0x0000000E NPY_API_VERSION0x0000000E | |||
| 7 | #define _MULTIARRAYMODULE | |||
| 8 | #include "numpy/arrayobject.h" | |||
| 9 | #include "arrayobject.h" | |||
| 10 | #include "numpy/arrayscalars.h" | |||
| 11 | ||||
| 12 | #include "arrayfunction_override.h" | |||
| 13 | #include "npy_argparse.h" | |||
| 14 | #include "npy_config.h" | |||
| 15 | #include "npy_pycompat.h" | |||
| 16 | #include "npy_import.h" | |||
| 17 | #include "ufunc_override.h" | |||
| 18 | #include "array_coercion.h" | |||
| 19 | #include "common.h" | |||
| 20 | #include "templ_common.h" /* for npy_mul_with_overflow_intp */ | |||
| 21 | #include "ctors.h" | |||
| 22 | #include "calculation.h" | |||
| 23 | #include "convert_datatype.h" | |||
| 24 | #include "item_selection.h" | |||
| 25 | #include "conversion_utils.h" | |||
| 26 | #include "shape.h" | |||
| 27 | #include "strfuncs.h" | |||
| 28 | #include "array_assign.h" | |||
| 29 | ||||
| 30 | #include "methods.h" | |||
| 31 | #include "alloc.h" | |||
| 32 | ||||
| 33 | ||||
| 34 | /* NpyArg_ParseKeywords | |||
| 35 | * | |||
| 36 | * Utility function that provides the keyword parsing functionality of | |||
| 37 | * PyArg_ParseTupleAndKeywords without having to have an args argument. | |||
| 38 | * | |||
| 39 | */ | |||
| 40 | static int | |||
| 41 | NpyArg_ParseKeywords(PyObject *keys, const char *format, char **kwlist, ...) | |||
| 42 | { | |||
| 43 | PyObject *args = PyTuple_New(0); | |||
| 44 | int ret; | |||
| 45 | va_list va; | |||
| 46 | ||||
| 47 | if (args == NULL((void*)0)) { | |||
| 48 | PyErr_SetString(PyExc_RuntimeError, | |||
| 49 | "Failed to allocate new tuple"); | |||
| 50 | return 0; | |||
| 51 | } | |||
| 52 | va_start(va, kwlist)__builtin_va_start(va, kwlist); | |||
| 53 | ret = PyArg_VaParseTupleAndKeywords_PyArg_VaParseTupleAndKeywords_SizeT(args, keys, format, kwlist, va); | |||
| 54 | va_end(va)__builtin_va_end(va); | |||
| 55 | Py_DECREF(args)_Py_DECREF(((PyObject*)(args))); | |||
| 56 | return ret; | |||
| 57 | } | |||
| 58 | ||||
| 59 | ||||
| 60 | /* | |||
| 61 | * Forwards an ndarray method to a the Python function | |||
| 62 | * numpy.core._methods.<name>(...) | |||
| 63 | */ | |||
| 64 | static PyObject * | |||
| 65 | forward_ndarray_method(PyArrayObject *self, PyObject *args, PyObject *kwds, | |||
| 66 | PyObject *forwarding_callable) | |||
| 67 | { | |||
| 68 | PyObject *sargs, *ret; | |||
| 69 | int i, n; | |||
| 70 | ||||
| 71 | /* Combine 'self' and 'args' together into one tuple */ | |||
| 72 | n = PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size); | |||
| 73 | sargs = PyTuple_New(n + 1); | |||
| 74 | if (sargs == NULL((void*)0)) { | |||
| 75 | return NULL((void*)0); | |||
| 76 | } | |||
| 77 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
| 78 | PyTuple_SET_ITEM(sargs, 0, (PyObject *)self)PyTuple_SetItem(sargs, 0, (PyObject *)self); | |||
| 79 | for (i = 0; i < n; ++i) { | |||
| 80 | PyObject *item = PyTuple_GET_ITEM(args, i)((((void) (0)), (PyTupleObject *)(args))->ob_item[i]); | |||
| 81 | Py_INCREF(item)_Py_INCREF(((PyObject*)(item))); | |||
| 82 | PyTuple_SET_ITEM(sargs, i+1, item)PyTuple_SetItem(sargs, i+1, item); | |||
| 83 | } | |||
| 84 | ||||
| 85 | /* Call the function and return */ | |||
| 86 | ret = PyObject_Call(forwarding_callable, sargs, kwds); | |||
| 87 | Py_DECREF(sargs)_Py_DECREF(((PyObject*)(sargs))); | |||
| 88 | return ret; | |||
| 89 | } | |||
| 90 | ||||
| 91 | /* | |||
| 92 | * Forwards an ndarray method to the function numpy.core._methods.<name>(...), | |||
| 93 | * caching the callable in a local static variable. Note that the | |||
| 94 | * initialization is not thread-safe, but relies on the CPython GIL to | |||
| 95 | * be correct. | |||
| 96 | */ | |||
| 97 | #define NPY_FORWARD_NDARRAY_METHOD(name)static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , name, &callable); if (callable == ((void*)0)) { return ( (void*)0); } return forward_ndarray_method(self, args, kwds, callable ) \ | |||
| 98 | static PyObject *callable = NULL((void*)0); \ | |||
| 99 | npy_cache_import("numpy.core._methods", name, &callable); \ | |||
| 100 | if (callable == NULL((void*)0)) { \ | |||
| 101 | return NULL((void*)0); \ | |||
| 102 | } \ | |||
| 103 | return forward_ndarray_method(self, args, kwds, callable) | |||
| 104 | ||||
| 105 | ||||
| 106 | static PyObject * | |||
| 107 | array_take(PyArrayObject *self, | |||
| 108 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 109 | { | |||
| 110 | int dimension = NPY_MAXDIMS32; | |||
| 111 | PyObject *indices; | |||
| 112 | PyArrayObject *out = NULL((void*)0); | |||
| 113 | NPY_CLIPMODE mode = NPY_RAISE; | |||
| 114 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 115 | ||||
| 116 | if (npy_parse_arguments("take", args, len_args, kwnames,_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) | |||
| 117 | "indices", NULL, &indices,_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) | |||
| 118 | "|axis", &PyArray_AxisConverter, &dimension,_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) | |||
| 119 | "|out", &PyArray_OutputConverter, &out,_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) | |||
| 120 | "|mode", &PyArray_ClipmodeConverter, &mode,_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) | |||
| 121 | NULL, NULL, NULL)_npy_parse_arguments("take", &__argparse_cache, args, len_args , kwnames, "indices", ((void*)0), &indices, "|axis", & PyArray_AxisConverter, &dimension, "|out", &PyArray_OutputConverter , &out, "|mode", &PyArray_ClipmodeConverter, &mode , ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 122 | return NULL((void*)0); | |||
| 123 | } | |||
| 124 | ||||
| 125 | PyObject *ret = PyArray_TakeFrom(self, indices, dimension, out, mode); | |||
| 126 | ||||
| 127 | /* this matches the unpacking behavior of ufuncs */ | |||
| 128 | if (out == NULL((void*)0)) { | |||
| 129 | return PyArray_Return((PyArrayObject *)ret); | |||
| 130 | } | |||
| 131 | else { | |||
| 132 | return ret; | |||
| 133 | } | |||
| 134 | } | |||
| 135 | ||||
| 136 | static PyObject * | |||
| 137 | array_fill(PyArrayObject *self, PyObject *args) | |||
| 138 | { | |||
| 139 | PyObject *obj; | |||
| 140 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O:fill", &obj)) { | |||
| 141 | return NULL((void*)0); | |||
| 142 | } | |||
| 143 | if (PyArray_FillWithScalar(self, obj) < 0) { | |||
| 144 | return NULL((void*)0); | |||
| 145 | } | |||
| 146 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 147 | } | |||
| 148 | ||||
| 149 | static PyObject * | |||
| 150 | array_put(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 151 | { | |||
| 152 | PyObject *indices, *values; | |||
| 153 | NPY_CLIPMODE mode = NPY_RAISE; | |||
| 154 | static char *kwlist[] = {"indices", "values", "mode", NULL((void*)0)}; | |||
| 155 | ||||
| 156 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "OO|O&:put", kwlist, | |||
| 157 | &indices, | |||
| 158 | &values, | |||
| 159 | PyArray_ClipmodeConverter, &mode)) | |||
| 160 | return NULL((void*)0); | |||
| 161 | return PyArray_PutTo(self, values, indices, mode); | |||
| 162 | } | |||
| 163 | ||||
| 164 | static PyObject * | |||
| 165 | array_reshape(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 166 | { | |||
| 167 | static char *keywords[] = {"order", NULL((void*)0)}; | |||
| 168 | PyArray_Dims newshape; | |||
| 169 | PyObject *ret; | |||
| 170 | NPY_ORDER order = NPY_CORDER; | |||
| 171 | Py_ssize_t n = PyTuple_Size(args); | |||
| 172 | ||||
| 173 | if (!NpyArg_ParseKeywords(kwds, "|O&", keywords, | |||
| 174 | PyArray_OrderConverter, &order)) { | |||
| 175 | return NULL((void*)0); | |||
| 176 | } | |||
| 177 | ||||
| 178 | if (n <= 1) { | |||
| 179 | if (n != 0 && PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]) == Py_None(&_Py_NoneStruct)) { | |||
| 180 | return PyArray_View(self, NULL((void*)0), NULL((void*)0)); | |||
| 181 | } | |||
| 182 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O&:reshape", PyArray_IntpConverter, | |||
| 183 | &newshape)) { | |||
| 184 | return NULL((void*)0); | |||
| 185 | } | |||
| 186 | } | |||
| 187 | else { | |||
| 188 | if (!PyArray_IntpConverter(args, &newshape)) { | |||
| 189 | if (!PyErr_Occurred()) { | |||
| 190 | PyErr_SetString(PyExc_TypeError, | |||
| 191 | "invalid shape"); | |||
| 192 | } | |||
| 193 | goto fail; | |||
| 194 | } | |||
| 195 | } | |||
| 196 | ret = PyArray_Newshape(self, &newshape, order); | |||
| 197 | npy_free_cache_dim_obj(newshape); | |||
| 198 | return ret; | |||
| 199 | ||||
| 200 | fail: | |||
| 201 | npy_free_cache_dim_obj(newshape); | |||
| 202 | return NULL((void*)0); | |||
| 203 | } | |||
| 204 | ||||
| 205 | static PyObject * | |||
| 206 | array_squeeze(PyArrayObject *self, | |||
| 207 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 208 | { | |||
| 209 | PyObject *axis_in = NULL((void*)0); | |||
| 210 | npy_bool axis_flags[NPY_MAXDIMS32]; | |||
| 211 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 212 | ||||
| 213 | if (npy_parse_arguments("squeeze", args, len_args, kwnames,_npy_parse_arguments("squeeze", &__argparse_cache, args, len_args , kwnames, "|axis", ((void*)0), &axis_in, ((void*)0), ((void *)0), ((void*)0)) | |||
| 214 | "|axis", NULL, &axis_in,_npy_parse_arguments("squeeze", &__argparse_cache, args, len_args , kwnames, "|axis", ((void*)0), &axis_in, ((void*)0), ((void *)0), ((void*)0)) | |||
| 215 | NULL, NULL, NULL)_npy_parse_arguments("squeeze", &__argparse_cache, args, len_args , kwnames, "|axis", ((void*)0), &axis_in, ((void*)0), ((void *)0), ((void*)0)) < 0) { | |||
| 216 | return NULL((void*)0); | |||
| 217 | } | |||
| 218 | ||||
| 219 | if (axis_in == NULL((void*)0) || axis_in == Py_None(&_Py_NoneStruct)) { | |||
| 220 | return PyArray_Squeeze(self); | |||
| 221 | } | |||
| 222 | else { | |||
| 223 | if (PyArray_ConvertMultiAxis(axis_in, PyArray_NDIM(self), | |||
| 224 | axis_flags) != NPY_SUCCEED1) { | |||
| 225 | return NULL((void*)0); | |||
| 226 | } | |||
| 227 | ||||
| 228 | return PyArray_SqueezeSelected(self, axis_flags); | |||
| 229 | } | |||
| 230 | } | |||
| 231 | ||||
| 232 | static PyObject * | |||
| 233 | array_view(PyArrayObject *self, | |||
| 234 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 235 | { | |||
| 236 | PyObject *out_dtype = NULL((void*)0); | |||
| 237 | PyObject *out_type = NULL((void*)0); | |||
| 238 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 239 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 240 | ||||
| 241 | if (npy_parse_arguments("view", args, len_args, kwnames,_npy_parse_arguments("view", &__argparse_cache, args, len_args , kwnames, "|dtype", ((void*)0), &out_dtype, "|type", ((void *)0), &out_type, ((void*)0), ((void*)0), ((void*)0)) | |||
| 242 | "|dtype", NULL, &out_dtype,_npy_parse_arguments("view", &__argparse_cache, args, len_args , kwnames, "|dtype", ((void*)0), &out_dtype, "|type", ((void *)0), &out_type, ((void*)0), ((void*)0), ((void*)0)) | |||
| 243 | "|type", NULL, &out_type,_npy_parse_arguments("view", &__argparse_cache, args, len_args , kwnames, "|dtype", ((void*)0), &out_dtype, "|type", ((void *)0), &out_type, ((void*)0), ((void*)0), ((void*)0)) | |||
| 244 | NULL, NULL, NULL)_npy_parse_arguments("view", &__argparse_cache, args, len_args , kwnames, "|dtype", ((void*)0), &out_dtype, "|type", ((void *)0), &out_type, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 245 | return NULL((void*)0); | |||
| 246 | } | |||
| 247 | ||||
| 248 | /* If user specified a positional argument, guess whether it | |||
| 249 | represents a type or a dtype for backward compatibility. */ | |||
| 250 | if (out_dtype) { | |||
| 251 | /* type specified? */ | |||
| 252 | if (PyType_Check(out_dtype)((((((PyObject*)(out_dtype))->ob_type))->tp_flags & ((1UL << 31))) != 0) && | |||
| 253 | PyType_IsSubtype((PyTypeObject *)out_dtype, | |||
| 254 | &PyArray_Type)) { | |||
| 255 | if (out_type) { | |||
| 256 | PyErr_SetString(PyExc_ValueError, | |||
| 257 | "Cannot specify output type twice."); | |||
| 258 | return NULL((void*)0); | |||
| 259 | } | |||
| 260 | out_type = out_dtype; | |||
| 261 | out_dtype = NULL((void*)0); | |||
| 262 | } | |||
| 263 | } | |||
| 264 | ||||
| 265 | if ((out_type) && (!PyType_Check(out_type)((((((PyObject*)(out_type))->ob_type))->tp_flags & ( (1UL << 31))) != 0) || | |||
| 266 | !PyType_IsSubtype((PyTypeObject *)out_type, | |||
| 267 | &PyArray_Type))) { | |||
| 268 | PyErr_SetString(PyExc_ValueError, | |||
| 269 | "Type must be a sub-type of ndarray type"); | |||
| 270 | return NULL((void*)0); | |||
| 271 | } | |||
| 272 | ||||
| 273 | if ((out_dtype) && | |||
| 274 | (PyArray_DescrConverter(out_dtype, &dtype) == NPY_FAIL0)) { | |||
| 275 | return NULL((void*)0); | |||
| 276 | } | |||
| 277 | ||||
| 278 | return PyArray_View(self, dtype, (PyTypeObject*)out_type); | |||
| 279 | } | |||
| 280 | ||||
| 281 | static PyObject * | |||
| 282 | array_argmax(PyArrayObject *self, | |||
| 283 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 284 | { | |||
| 285 | int axis = NPY_MAXDIMS32; | |||
| 286 | PyArrayObject *out = NULL((void*)0); | |||
| 287 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 288 | ||||
| 289 | if (npy_parse_arguments("argmax", args, len_args, kwnames,_npy_parse_arguments("argmax", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 290 | "|axis", &PyArray_AxisConverter, &axis,_npy_parse_arguments("argmax", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 291 | "|out", &PyArray_OutputConverter, &out,_npy_parse_arguments("argmax", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 292 | NULL, NULL, NULL)_npy_parse_arguments("argmax", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) < 0) { | |||
| 293 | return NULL((void*)0); | |||
| 294 | } | |||
| 295 | ||||
| 296 | PyObject *ret = PyArray_ArgMax(self, axis, out); | |||
| 297 | ||||
| 298 | /* this matches the unpacking behavior of ufuncs */ | |||
| 299 | if (out == NULL((void*)0)) { | |||
| 300 | return PyArray_Return((PyArrayObject *)ret); | |||
| 301 | } | |||
| 302 | else { | |||
| 303 | return ret; | |||
| 304 | } | |||
| 305 | } | |||
| 306 | ||||
| 307 | static PyObject * | |||
| 308 | array_argmin(PyArrayObject *self, | |||
| 309 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 310 | { | |||
| 311 | int axis = NPY_MAXDIMS32; | |||
| 312 | PyArrayObject *out = NULL((void*)0); | |||
| 313 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 314 | ||||
| 315 | if (npy_parse_arguments("argmin", args, len_args, kwnames,_npy_parse_arguments("argmin", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 316 | "|axis", &PyArray_AxisConverter, &axis,_npy_parse_arguments("argmin", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 317 | "|out", &PyArray_OutputConverter, &out,_npy_parse_arguments("argmin", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) | |||
| 318 | NULL, NULL, NULL)_npy_parse_arguments("argmin", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|out" , &PyArray_OutputConverter, &out, ((void*)0), ((void* )0), ((void*)0)) < 0) { | |||
| 319 | return NULL((void*)0); | |||
| 320 | } | |||
| 321 | ||||
| 322 | PyObject *ret = PyArray_ArgMin(self, axis, out); | |||
| 323 | ||||
| 324 | /* this matches the unpacking behavior of ufuncs */ | |||
| 325 | if (out == NULL((void*)0)) { | |||
| 326 | return PyArray_Return((PyArrayObject *)ret); | |||
| 327 | } | |||
| 328 | else { | |||
| 329 | return ret; | |||
| 330 | } | |||
| 331 | } | |||
| 332 | ||||
| 333 | static PyObject * | |||
| 334 | array_max(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 335 | { | |||
| 336 | NPY_FORWARD_NDARRAY_METHOD("_amax")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_amax", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 337 | } | |||
| 338 | ||||
| 339 | static PyObject * | |||
| 340 | array_min(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 341 | { | |||
| 342 | NPY_FORWARD_NDARRAY_METHOD("_amin")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_amin", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 343 | } | |||
| 344 | ||||
| 345 | static PyObject * | |||
| 346 | array_ptp(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 347 | { | |||
| 348 | NPY_FORWARD_NDARRAY_METHOD("_ptp")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_ptp", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 349 | } | |||
| 350 | ||||
| 351 | ||||
| 352 | static PyObject * | |||
| 353 | array_swapaxes(PyArrayObject *self, PyObject *args) | |||
| 354 | { | |||
| 355 | int axis1, axis2; | |||
| 356 | ||||
| 357 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii:swapaxes", &axis1, &axis2)) { | |||
| 358 | return NULL((void*)0); | |||
| 359 | } | |||
| 360 | return PyArray_SwapAxes(self, axis1, axis2); | |||
| 361 | } | |||
| 362 | ||||
| 363 | ||||
| 364 | /*NUMPY_API | |||
| 365 | Get a subset of bytes from each element of the array | |||
| 366 | steals reference to typed, must not be NULL | |||
| 367 | */ | |||
| 368 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyObject * | |||
| 369 | PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) | |||
| 370 | { | |||
| 371 | PyObject *ret = NULL((void*)0); | |||
| 372 | PyObject *safe; | |||
| 373 | static PyObject *checkfunc = NULL((void*)0); | |||
| 374 | int self_elsize, typed_elsize; | |||
| 375 | ||||
| 376 | /* check that we are not reinterpreting memory containing Objects. */ | |||
| 377 | if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(typed)) { | |||
| 378 | npy_cache_import("numpy.core._internal", "_getfield_is_safe", | |||
| 379 | &checkfunc); | |||
| 380 | if (checkfunc == NULL((void*)0)) { | |||
| 381 | Py_DECREF(typed)_Py_DECREF(((PyObject*)(typed))); | |||
| 382 | return NULL((void*)0); | |||
| 383 | } | |||
| 384 | ||||
| 385 | /* only returns True or raises */ | |||
| 386 | safe = PyObject_CallFunction_PyObject_CallFunction_SizeT(checkfunc, "OOi", PyArray_DESCR(self), | |||
| 387 | typed, offset); | |||
| 388 | if (safe == NULL((void*)0)) { | |||
| 389 | Py_DECREF(typed)_Py_DECREF(((PyObject*)(typed))); | |||
| 390 | return NULL((void*)0); | |||
| 391 | } | |||
| 392 | Py_DECREF(safe)_Py_DECREF(((PyObject*)(safe))); | |||
| 393 | } | |||
| 394 | self_elsize = PyArray_ITEMSIZE(self); | |||
| 395 | typed_elsize = typed->elsize; | |||
| 396 | ||||
| 397 | /* check that values are valid */ | |||
| 398 | if (typed_elsize > self_elsize) { | |||
| 399 | PyErr_SetString(PyExc_ValueError, "new type is larger than original type"); | |||
| 400 | Py_DECREF(typed)_Py_DECREF(((PyObject*)(typed))); | |||
| 401 | return NULL((void*)0); | |||
| 402 | } | |||
| 403 | if (offset < 0) { | |||
| 404 | PyErr_SetString(PyExc_ValueError, "offset is negative"); | |||
| 405 | Py_DECREF(typed)_Py_DECREF(((PyObject*)(typed))); | |||
| 406 | return NULL((void*)0); | |||
| 407 | } | |||
| 408 | if (offset > self_elsize - typed_elsize) { | |||
| 409 | PyErr_SetString(PyExc_ValueError, "new type plus offset is larger than original type"); | |||
| 410 | Py_DECREF(typed)_Py_DECREF(((PyObject*)(typed))); | |||
| 411 | return NULL((void*)0); | |||
| 412 | } | |||
| 413 | ||||
| 414 | ret = PyArray_NewFromDescr_int( | |||
| 415 | Py_TYPE(self)(((PyObject*)(self))->ob_type), typed, | |||
| 416 | PyArray_NDIM(self), PyArray_DIMS(self), PyArray_STRIDES(self), | |||
| 417 | PyArray_BYTES(self) + offset, | |||
| 418 | PyArray_FLAGS(self) & ~NPY_ARRAY_F_CONTIGUOUS0x0002, | |||
| 419 | (PyObject *)self, (PyObject *)self, | |||
| 420 | 0, 1); | |||
| 421 | return ret; | |||
| 422 | } | |||
| 423 | ||||
| 424 | static PyObject * | |||
| 425 | array_getfield(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 426 | { | |||
| 427 | ||||
| 428 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 429 | int offset = 0; | |||
| 430 | static char *kwlist[] = {"dtype", "offset", 0}; | |||
| 431 | ||||
| 432 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O&|i:getfield", kwlist, | |||
| 433 | PyArray_DescrConverter, &dtype, | |||
| 434 | &offset)) { | |||
| 435 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 436 | return NULL((void*)0); | |||
| 437 | } | |||
| 438 | ||||
| 439 | return PyArray_GetField(self, dtype, offset); | |||
| 440 | } | |||
| 441 | ||||
| 442 | ||||
| 443 | /*NUMPY_API | |||
| 444 | Set a subset of bytes from each element of the array | |||
| 445 | steals reference to dtype, must not be NULL | |||
| 446 | */ | |||
| 447 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) int | |||
| 448 | PyArray_SetField(PyArrayObject *self, PyArray_Descr *dtype, | |||
| 449 | int offset, PyObject *val) | |||
| 450 | { | |||
| 451 | PyObject *ret = NULL((void*)0); | |||
| 452 | int retval = 0; | |||
| 453 | ||||
| 454 | if (PyArray_FailUnlessWriteable(self, "assignment destination") < 0) { | |||
| 455 | Py_DECREF(dtype)_Py_DECREF(((PyObject*)(dtype))); | |||
| 456 | return -1; | |||
| 457 | } | |||
| 458 | ||||
| 459 | /* getfield returns a view we can write to */ | |||
| 460 | ret = PyArray_GetField(self, dtype, offset); | |||
| 461 | if (ret == NULL((void*)0)) { | |||
| 462 | return -1; | |||
| 463 | } | |||
| 464 | ||||
| 465 | retval = PyArray_CopyObject((PyArrayObject *)ret, val); | |||
| 466 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 467 | return retval; | |||
| 468 | } | |||
| 469 | ||||
| 470 | static PyObject * | |||
| 471 | array_setfield(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 472 | { | |||
| 473 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 474 | int offset = 0; | |||
| 475 | PyObject *value; | |||
| 476 | static char *kwlist[] = {"value", "dtype", "offset", 0}; | |||
| 477 | ||||
| 478 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "OO&|i:setfield", kwlist, | |||
| 479 | &value, | |||
| 480 | PyArray_DescrConverter, &dtype, | |||
| 481 | &offset)) { | |||
| 482 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 483 | return NULL((void*)0); | |||
| 484 | } | |||
| 485 | ||||
| 486 | if (PyArray_SetField(self, dtype, offset, value) < 0) { | |||
| 487 | return NULL((void*)0); | |||
| 488 | } | |||
| 489 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 490 | } | |||
| 491 | ||||
| 492 | /* This doesn't change the descriptor just the actual data... | |||
| 493 | */ | |||
| 494 | ||||
| 495 | /*NUMPY_API*/ | |||
| 496 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyObject * | |||
| 497 | PyArray_Byteswap(PyArrayObject *self, npy_bool inplace) | |||
| 498 | { | |||
| 499 | PyArrayObject *ret; | |||
| 500 | npy_intp size; | |||
| 501 | PyArray_CopySwapNFunc *copyswapn; | |||
| 502 | PyArrayIterObject *it; | |||
| 503 | ||||
| 504 | copyswapn = PyArray_DESCR(self)->f->copyswapn; | |||
| 505 | if (inplace) { | |||
| 506 | if (PyArray_FailUnlessWriteable(self, "array to be byte-swapped") < 0) { | |||
| 507 | return NULL((void*)0); | |||
| 508 | } | |||
| 509 | size = PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)); | |||
| 510 | if (PyArray_ISONESEGMENT(self)(PyArray_CHKFLAGS(self, 0x0001) || PyArray_CHKFLAGS(self, 0x0002 ))) { | |||
| 511 | copyswapn(PyArray_DATA(self), PyArray_DESCR(self)->elsize, NULL((void*)0), -1, size, 1, self); | |||
| 512 | } | |||
| 513 | else { /* Use iterator */ | |||
| 514 | int axis = -1; | |||
| 515 | npy_intp stride; | |||
| 516 | it = (PyArrayIterObject *) \ | |||
| 517 | PyArray_IterAllButAxis((PyObject *)self, &axis); | |||
| 518 | stride = PyArray_STRIDES(self)[axis]; | |||
| 519 | size = PyArray_DIMS(self)[axis]; | |||
| 520 | while (it->index < it->size) { | |||
| 521 | copyswapn(it->dataptr, stride, NULL((void*)0), -1, size, 1, self); | |||
| 522 | PyArray_ITER_NEXT(it)do { ((PyArrayIterObject *)(it))->index++; if (((PyArrayIterObject *)(it))->nd_m1 == 0) { do { (((PyArrayIterObject *)(it))) ->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *) (it))))->strides[0]; (((PyArrayIterObject *)(it)))->coordinates [0]++; } while (0); } else if (((PyArrayIterObject *)(it))-> contiguous) ((PyArrayIterObject *)(it))->dataptr += PyArray_DESCR (((PyArrayIterObject *)(it))->ao)->elsize; else if (((PyArrayIterObject *)(it))->nd_m1 == 1) { do { if ((((PyArrayIterObject *)(it )))->coordinates[1] < (((PyArrayIterObject *)(it)))-> dims_m1[1]) { (((PyArrayIterObject *)(it)))->coordinates[1 ]++; (((PyArrayIterObject *)(it)))->dataptr += (((PyArrayIterObject *)(it)))->strides[1]; } else { (((PyArrayIterObject *)(it )))->coordinates[1] = 0; (((PyArrayIterObject *)(it)))-> coordinates[0]++; (((PyArrayIterObject *)(it)))->dataptr += (((PyArrayIterObject *)(it)))->strides[0] - (((PyArrayIterObject *)(it)))->backstrides[1]; } } while (0); } else { int __npy_i ; for (__npy_i=((PyArrayIterObject *)(it))->nd_m1; __npy_i >= 0; __npy_i--) { if (((PyArrayIterObject *)(it))->coordinates [__npy_i] < ((PyArrayIterObject *)(it))->dims_m1[__npy_i ]) { ((PyArrayIterObject *)(it))->coordinates[__npy_i]++; ( (PyArrayIterObject *)(it))->dataptr += ((PyArrayIterObject *)(it))->strides[__npy_i]; break; } else { ((PyArrayIterObject *)(it))->coordinates[__npy_i] = 0; ((PyArrayIterObject *) (it))->dataptr -= ((PyArrayIterObject *)(it))->backstrides [__npy_i]; } } } } while (0); | |||
| 523 | } | |||
| 524 | Py_DECREF(it)_Py_DECREF(((PyObject*)(it))); | |||
| 525 | } | |||
| 526 | ||||
| 527 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
| 528 | return (PyObject *)self; | |||
| 529 | } | |||
| 530 | else { | |||
| 531 | PyObject *new; | |||
| 532 | if ((ret = (PyArrayObject *)PyArray_NewCopy(self,-1)) == NULL((void*)0)) { | |||
| 533 | return NULL((void*)0); | |||
| 534 | } | |||
| 535 | new = PyArray_Byteswap(ret, NPY_TRUE1); | |||
| 536 | Py_DECREF(new)_Py_DECREF(((PyObject*)(new))); | |||
| 537 | return (PyObject *)ret; | |||
| 538 | } | |||
| 539 | } | |||
| 540 | ||||
| 541 | ||||
| 542 | static PyObject * | |||
| 543 | array_byteswap(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 544 | { | |||
| 545 | npy_bool inplace = NPY_FALSE0; | |||
| 546 | static char *kwlist[] = {"inplace", NULL((void*)0)}; | |||
| 547 | ||||
| 548 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|O&:byteswap", kwlist, | |||
| 549 | PyArray_BoolConverter, &inplace)) { | |||
| 550 | return NULL((void*)0); | |||
| 551 | } | |||
| 552 | return PyArray_Byteswap(self, inplace); | |||
| 553 | } | |||
| 554 | ||||
| 555 | static PyObject * | |||
| 556 | array_tolist(PyArrayObject *self, PyObject *args) | |||
| 557 | { | |||
| 558 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "")) { | |||
| 559 | return NULL((void*)0); | |||
| 560 | } | |||
| 561 | return PyArray_ToList(self); | |||
| 562 | } | |||
| 563 | ||||
| 564 | ||||
| 565 | static PyObject * | |||
| 566 | array_tobytes(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 567 | { | |||
| 568 | NPY_ORDER order = NPY_CORDER; | |||
| 569 | static char *kwlist[] = {"order", NULL((void*)0)}; | |||
| 570 | ||||
| 571 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|O&:tobytes", kwlist, | |||
| 572 | PyArray_OrderConverter, &order)) { | |||
| 573 | return NULL((void*)0); | |||
| 574 | } | |||
| 575 | return PyArray_ToString(self, order); | |||
| 576 | } | |||
| 577 | ||||
| 578 | static PyObject * | |||
| 579 | array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 580 | { | |||
| 581 | NPY_ORDER order = NPY_CORDER; | |||
| 582 | static char *kwlist[] = {"order", NULL((void*)0)}; | |||
| 583 | ||||
| 584 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|O&:tostring", kwlist, | |||
| 585 | PyArray_OrderConverter, &order)) { | |||
| 586 | return NULL((void*)0); | |||
| 587 | } | |||
| 588 | /* 2020-03-30, NumPy 1.19 */ | |||
| 589 | if (DEPRECATE("tostring() is deprecated. Use tobytes() instead.")PyErr_WarnEx(PyExc_DeprecationWarning,"tostring() is deprecated. Use tobytes() instead." ,1) < 0) { | |||
| 590 | return NULL((void*)0); | |||
| 591 | } | |||
| 592 | return PyArray_ToString(self, order); | |||
| 593 | } | |||
| 594 | ||||
| 595 | /* Like PyArray_ToFile but takes the file as a python object */ | |||
| 596 | static int | |||
| 597 | PyArray_ToFileObject(PyArrayObject *self, PyObject *file, char *sep, char *format) | |||
| 598 | { | |||
| 599 | npy_off_toff_t orig_pos = 0; | |||
| 600 | FILE *fd = npy_PyFile_Dup2(file, "wb", &orig_pos); | |||
| 601 | ||||
| 602 | if (fd == NULL((void*)0)) { | |||
| 603 | return -1; | |||
| 604 | } | |||
| 605 | ||||
| 606 | int write_ret = PyArray_ToFile(self, fd, sep, format); | |||
| 607 | PyObject *err_type, *err_value, *err_traceback; | |||
| 608 | PyErr_Fetch(&err_type, &err_value, &err_traceback); | |||
| 609 | int close_ret = npy_PyFile_DupClose2(file, fd, orig_pos); | |||
| 610 | npy_PyErr_ChainExceptions(err_type, err_value, err_traceback); | |||
| 611 | ||||
| 612 | if (write_ret || close_ret) { | |||
| 613 | return -1; | |||
| 614 | } | |||
| 615 | return 0; | |||
| 616 | } | |||
| 617 | ||||
| 618 | /* This should grow an order= keyword to be consistent | |||
| 619 | */ | |||
| 620 | ||||
| 621 | static PyObject * | |||
| 622 | array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 623 | { | |||
| 624 | int own; | |||
| 625 | PyObject *file; | |||
| 626 | char *sep = ""; | |||
| 627 | char *format = ""; | |||
| 628 | static char *kwlist[] = {"file", "sep", "format", NULL((void*)0)}; | |||
| 629 | ||||
| 630 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O|ss:tofile", kwlist, | |||
| 631 | &file, | |||
| 632 | &sep, | |||
| 633 | &format)) { | |||
| 634 | return NULL((void*)0); | |||
| 635 | } | |||
| 636 | ||||
| 637 | file = NpyPath_PathlikeToFspath(file); | |||
| 638 | if (file == NULL((void*)0)) { | |||
| 639 | return NULL((void*)0); | |||
| 640 | } | |||
| 641 | if (PyBytes_Check(file)((((((PyObject*)(file))->ob_type))->tp_flags & ((1UL << 27))) != 0) || PyUnicode_Check(file)((((((PyObject*)(file))->ob_type))->tp_flags & ((1UL << 28))) != 0)) { | |||
| 642 | Py_SETREF(file, npy_PyFile_OpenFile(file, "wb"))do { PyObject *_py_tmp = ((PyObject*)(file)); (file) = (npy_PyFile_OpenFile (file, "wb")); _Py_DECREF(((PyObject*)(_py_tmp))); } while (0 ); | |||
| 643 | if (file == NULL((void*)0)) { | |||
| 644 | return NULL((void*)0); | |||
| 645 | } | |||
| 646 | own = 1; | |||
| 647 | } | |||
| 648 | else { | |||
| 649 | own = 0; | |||
| 650 | } | |||
| 651 | ||||
| 652 | int file_ret = PyArray_ToFileObject(self, file, sep, format); | |||
| 653 | int close_ret = 0; | |||
| 654 | ||||
| 655 | if (own) { | |||
| 656 | PyObject *err_type, *err_value, *err_traceback; | |||
| 657 | PyErr_Fetch(&err_type, &err_value, &err_traceback); | |||
| 658 | close_ret = npy_PyFile_CloseFile(file); | |||
| 659 | npy_PyErr_ChainExceptions(err_type, err_value, err_traceback); | |||
| 660 | } | |||
| 661 | ||||
| 662 | Py_DECREF(file)_Py_DECREF(((PyObject*)(file))); | |||
| 663 | ||||
| 664 | if (file_ret || close_ret) { | |||
| 665 | return NULL((void*)0); | |||
| 666 | } | |||
| 667 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 668 | } | |||
| 669 | ||||
| 670 | static PyObject * | |||
| 671 | array_toscalar(PyArrayObject *self, PyObject *args) | |||
| 672 | { | |||
| 673 | npy_intp multi_index[NPY_MAXDIMS32]; | |||
| 674 | int n = PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size); | |||
| 675 | int idim, ndim = PyArray_NDIM(self); | |||
| 676 | ||||
| 677 | /* If there is a tuple as a single argument, treat it as the argument */ | |||
| 678 | if (n == 1 && PyTuple_Check(PyTuple_GET_ITEM(args, 0))((((((PyObject*)(((((void) (0)), (PyTupleObject *)(args))-> ob_item[0])))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
| 679 | args = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 680 | n = PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size); | |||
| 681 | } | |||
| 682 | ||||
| 683 | if (n == 0) { | |||
| 684 | if (PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)) == 1) { | |||
| 685 | for (idim = 0; idim < ndim; ++idim) { | |||
| 686 | multi_index[idim] = 0; | |||
| 687 | } | |||
| 688 | } | |||
| 689 | else { | |||
| 690 | PyErr_SetString(PyExc_ValueError, | |||
| 691 | "can only convert an array of size 1 to a Python scalar"); | |||
| 692 | return NULL((void*)0); | |||
| 693 | } | |||
| 694 | } | |||
| 695 | /* Special case of C-order flat indexing... :| */ | |||
| 696 | else if (n == 1 && ndim != 1) { | |||
| 697 | npy_intp *shape = PyArray_SHAPE(self); | |||
| 698 | npy_intp value, size = PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)); | |||
| 699 | ||||
| 700 | value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0])); | |||
| 701 | if (error_converting(value)(((value) == -1) && PyErr_Occurred())) { | |||
| 702 | return NULL((void*)0); | |||
| 703 | } | |||
| 704 | ||||
| 705 | if (check_and_adjust_index(&value, size, -1, NULL((void*)0)) < 0) { | |||
| 706 | return NULL((void*)0); | |||
| 707 | } | |||
| 708 | ||||
| 709 | /* Convert the flat index into a multi-index */ | |||
| 710 | for (idim = ndim-1; idim >= 0; --idim) { | |||
| 711 | multi_index[idim] = value % shape[idim]; | |||
| 712 | value /= shape[idim]; | |||
| 713 | } | |||
| 714 | } | |||
| 715 | /* A multi-index tuple */ | |||
| 716 | else if (n == ndim) { | |||
| 717 | npy_intp value; | |||
| 718 | ||||
| 719 | for (idim = 0; idim < ndim; ++idim) { | |||
| 720 | value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, idim)((((void) (0)), (PyTupleObject *)(args))->ob_item[idim])); | |||
| 721 | if (error_converting(value)(((value) == -1) && PyErr_Occurred())) { | |||
| 722 | return NULL((void*)0); | |||
| 723 | } | |||
| 724 | multi_index[idim] = value; | |||
| 725 | } | |||
| 726 | } | |||
| 727 | else { | |||
| 728 | PyErr_SetString(PyExc_ValueError, | |||
| 729 | "incorrect number of indices for array"); | |||
| 730 | return NULL((void*)0); | |||
| 731 | } | |||
| 732 | ||||
| 733 | return PyArray_MultiIndexGetItem(self, multi_index); | |||
| 734 | } | |||
| 735 | ||||
| 736 | static PyObject * | |||
| 737 | array_setscalar(PyArrayObject *self, PyObject *args) | |||
| 738 | { | |||
| 739 | npy_intp multi_index[NPY_MAXDIMS32]; | |||
| 740 | int n = PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size) - 1; | |||
| 741 | int idim, ndim = PyArray_NDIM(self); | |||
| 742 | PyObject *obj; | |||
| 743 | ||||
| 744 | if (n < 0) { | |||
| 745 | PyErr_SetString(PyExc_ValueError, | |||
| 746 | "itemset must have at least one argument"); | |||
| 747 | return NULL((void*)0); | |||
| 748 | } | |||
| 749 | if (PyArray_FailUnlessWriteable(self, "assignment destination") < 0) { | |||
| 750 | return NULL((void*)0); | |||
| 751 | } | |||
| 752 | ||||
| 753 | obj = PyTuple_GET_ITEM(args, n)((((void) (0)), (PyTupleObject *)(args))->ob_item[n]); | |||
| 754 | ||||
| 755 | /* If there is a tuple as a single argument, treat it as the argument */ | |||
| 756 | if (n == 1 && PyTuple_Check(PyTuple_GET_ITEM(args, 0))((((((PyObject*)(((((void) (0)), (PyTupleObject *)(args))-> ob_item[0])))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
| 757 | args = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 758 | n = PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size); | |||
| 759 | } | |||
| 760 | ||||
| 761 | if (n == 0) { | |||
| 762 | if (PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)) == 1) { | |||
| 763 | for (idim = 0; idim < ndim; ++idim) { | |||
| 764 | multi_index[idim] = 0; | |||
| 765 | } | |||
| 766 | } | |||
| 767 | else { | |||
| 768 | PyErr_SetString(PyExc_ValueError, | |||
| 769 | "can only convert an array of size 1 to a Python scalar"); | |||
| 770 | return NULL((void*)0); | |||
| 771 | } | |||
| 772 | } | |||
| 773 | /* Special case of C-order flat indexing... :| */ | |||
| 774 | else if (n == 1 && ndim != 1) { | |||
| 775 | npy_intp *shape = PyArray_SHAPE(self); | |||
| 776 | npy_intp value, size = PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)); | |||
| 777 | ||||
| 778 | value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0])); | |||
| 779 | if (error_converting(value)(((value) == -1) && PyErr_Occurred())) { | |||
| 780 | return NULL((void*)0); | |||
| 781 | } | |||
| 782 | ||||
| 783 | if (check_and_adjust_index(&value, size, -1, NULL((void*)0)) < 0) { | |||
| 784 | return NULL((void*)0); | |||
| 785 | } | |||
| 786 | ||||
| 787 | /* Convert the flat index into a multi-index */ | |||
| 788 | for (idim = ndim-1; idim >= 0; --idim) { | |||
| 789 | multi_index[idim] = value % shape[idim]; | |||
| 790 | value /= shape[idim]; | |||
| 791 | } | |||
| 792 | } | |||
| 793 | /* A multi-index tuple */ | |||
| 794 | else if (n == ndim) { | |||
| 795 | npy_intp value; | |||
| 796 | ||||
| 797 | for (idim = 0; idim < ndim; ++idim) { | |||
| 798 | value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, idim)((((void) (0)), (PyTupleObject *)(args))->ob_item[idim])); | |||
| 799 | if (error_converting(value)(((value) == -1) && PyErr_Occurred())) { | |||
| 800 | return NULL((void*)0); | |||
| 801 | } | |||
| 802 | multi_index[idim] = value; | |||
| 803 | } | |||
| 804 | } | |||
| 805 | else { | |||
| 806 | PyErr_SetString(PyExc_ValueError, | |||
| 807 | "incorrect number of indices for array"); | |||
| 808 | return NULL((void*)0); | |||
| 809 | } | |||
| 810 | ||||
| 811 | if (PyArray_MultiIndexSetItem(self, multi_index, obj) < 0) { | |||
| 812 | return NULL((void*)0); | |||
| 813 | } | |||
| 814 | else { | |||
| 815 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 816 | } | |||
| 817 | } | |||
| 818 | ||||
| 819 | ||||
| 820 | static PyObject * | |||
| 821 | array_astype(PyArrayObject *self, | |||
| 822 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 823 | { | |||
| 824 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 825 | /* | |||
| 826 | * TODO: UNSAFE default for compatibility, I think | |||
| 827 | * switching to SAME_KIND by default would be good. | |||
| 828 | */ | |||
| 829 | NPY_CASTING casting = NPY_UNSAFE_CASTING; | |||
| 830 | NPY_ORDER order = NPY_KEEPORDER; | |||
| 831 | int forcecopy = 1, subok = 1; | |||
| 832 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 833 | ||||
| 834 | if (npy_parse_arguments("astype", args, len_args, kwnames,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 835 | "dtype", &PyArray_DescrConverter, &dtype,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 836 | "|order", &PyArray_OrderConverter, &order,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 837 | "|casting", &PyArray_CastingConverter, &casting,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 838 | "|subok", &PyArray_PythonPyIntFromInt, &subok,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 839 | "|copy", &PyArray_PythonPyIntFromInt, &forcecopy,_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) | |||
| 840 | NULL, NULL, NULL)_npy_parse_arguments("astype", &__argparse_cache, args, len_args , kwnames, "dtype", &PyArray_DescrConverter, &dtype, "|order" , &PyArray_OrderConverter, &order, "|casting", &PyArray_CastingConverter , &casting, "|subok", &PyArray_PythonPyIntFromInt, & subok, "|copy", &PyArray_PythonPyIntFromInt, &forcecopy , ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 841 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 842 | return NULL((void*)0); | |||
| 843 | } | |||
| 844 | ||||
| 845 | /* If it is not a concrete dtype instance find the best one for the array */ | |||
| 846 | Py_SETREF(dtype, PyArray_AdaptDescriptorToArray(self, (PyObject *)dtype))do { PyObject *_py_tmp = ((PyObject*)(dtype)); (dtype) = (PyArray_AdaptDescriptorToArray (self, (PyObject *)dtype)); _Py_DECREF(((PyObject*)(_py_tmp)) ); } while (0); | |||
| 847 | if (dtype == NULL((void*)0)) { | |||
| 848 | return NULL((void*)0); | |||
| 849 | } | |||
| 850 | ||||
| 851 | /* | |||
| 852 | * If the memory layout matches and, data types are equivalent, | |||
| 853 | * and it's not a subtype if subok is False, then we | |||
| 854 | * can skip the copy. | |||
| 855 | */ | |||
| 856 | if (!forcecopy && (order == NPY_KEEPORDER || | |||
| 857 | (order == NPY_ANYORDER && | |||
| 858 | (PyArray_IS_C_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0001) || | |||
| 859 | PyArray_IS_F_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0002))) || | |||
| 860 | (order == NPY_CORDER && | |||
| 861 | PyArray_IS_C_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0001)) || | |||
| 862 | (order == NPY_FORTRANORDER && | |||
| 863 | PyArray_IS_F_CONTIGUOUS(self)PyArray_CHKFLAGS((self), 0x0002))) && | |||
| 864 | (subok || PyArray_CheckExact(self)(((PyObject*)(self))->ob_type == &PyArray_Type)) && | |||
| 865 | PyArray_EquivTypes(dtype, PyArray_DESCR(self))) { | |||
| 866 | Py_DECREF(dtype)_Py_DECREF(((PyObject*)(dtype))); | |||
| 867 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
| 868 | return (PyObject *)self; | |||
| 869 | } | |||
| 870 | if (!PyArray_CanCastArrayTo(self, dtype, casting)) { | |||
| 871 | PyErr_Clear(); | |||
| 872 | npy_set_invalid_cast_error( | |||
| 873 | PyArray_DESCR(self), dtype, casting, PyArray_NDIM(self) == 0); | |||
| 874 | Py_DECREF(dtype)_Py_DECREF(((PyObject*)(dtype))); | |||
| 875 | return NULL((void*)0); | |||
| 876 | } | |||
| 877 | ||||
| 878 | PyArrayObject *ret; | |||
| 879 | ||||
| 880 | /* This steals the reference to dtype, so no DECREF of dtype */ | |||
| 881 | ret = (PyArrayObject *)PyArray_NewLikeArray( | |||
| 882 | self, order, dtype, subok); | |||
| 883 | if (ret == NULL((void*)0)) { | |||
| 884 | return NULL((void*)0); | |||
| 885 | } | |||
| 886 | /* NumPy 1.20, 2020-10-01 */ | |||
| 887 | if ((PyArray_NDIM(self) != PyArray_NDIM(ret)) && | |||
| 888 | DEPRECATE_FUTUREWARNING(PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 889 | "casting an array to a subarray dtype "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 890 | "will not use broadcasting in the future, but cast each "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 891 | "element to the new dtype and then append the dtype's shape "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 892 | "to the new array. You can opt-in to the new behaviour, by "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 893 | "additional field to the cast: "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 894 | "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n"PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 895 | "This may lead to a different result or to current failures "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 896 | "succeeding. "PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) | |||
| 897 | "(FutureWarning since NumPy 1.20)")PyErr_WarnEx(PyExc_FutureWarning,"casting an array to a subarray dtype " "will not use broadcasting in the future, but cast each " "element to the new dtype and then append the dtype's shape " "to the new array. You can opt-in to the new behaviour, by " "additional field to the cast: " "`arr.astype(np.dtype([('f', dtype)]))['f']`.\n" "This may lead to a different result or to current failures " "succeeding. " "(FutureWarning since NumPy 1.20)",1) < 0) { | |||
| 898 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 899 | return NULL((void*)0); | |||
| 900 | } | |||
| 901 | ||||
| 902 | if (PyArray_CopyInto(ret, self) < 0) { | |||
| 903 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 904 | return NULL((void*)0); | |||
| 905 | } | |||
| 906 | ||||
| 907 | return (PyObject *)ret; | |||
| 908 | } | |||
| 909 | ||||
| 910 | /* default sub-type implementation */ | |||
| 911 | ||||
| 912 | ||||
| 913 | static PyObject * | |||
| 914 | array_wraparray(PyArrayObject *self, PyObject *args) | |||
| 915 | { | |||
| 916 | PyArrayObject *arr; | |||
| 917 | PyObject *obj; | |||
| 918 | ||||
| 919 | if (PyTuple_Size(args) < 1) { | |||
| 920 | PyErr_SetString(PyExc_TypeError, | |||
| 921 | "only accepts 1 argument"); | |||
| 922 | return NULL((void*)0); | |||
| 923 | } | |||
| 924 | obj = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 925 | if (obj == NULL((void*)0)) { | |||
| 926 | return NULL((void*)0); | |||
| 927 | } | |||
| 928 | if (!PyArray_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyArray_Type) || PyType_IsSubtype ((((PyObject*)(obj))->ob_type), (&PyArray_Type)))) { | |||
| 929 | PyErr_SetString(PyExc_TypeError, | |||
| 930 | "can only be called with ndarray object"); | |||
| 931 | return NULL((void*)0); | |||
| 932 | } | |||
| 933 | arr = (PyArrayObject *)obj; | |||
| 934 | ||||
| 935 | if (Py_TYPE(self)(((PyObject*)(self))->ob_type) != Py_TYPE(arr)(((PyObject*)(arr))->ob_type)) { | |||
| 936 | PyArray_Descr *dtype = PyArray_DESCR(arr); | |||
| 937 | Py_INCREF(dtype)_Py_INCREF(((PyObject*)(dtype))); | |||
| 938 | return PyArray_NewFromDescrAndBase( | |||
| 939 | Py_TYPE(self)(((PyObject*)(self))->ob_type), | |||
| 940 | dtype, | |||
| 941 | PyArray_NDIM(arr), | |||
| 942 | PyArray_DIMS(arr), | |||
| 943 | PyArray_STRIDES(arr), PyArray_DATA(arr), | |||
| 944 | PyArray_FLAGS(arr), (PyObject *)self, obj); | |||
| 945 | } else { | |||
| 946 | /*The type was set in __array_prepare__*/ | |||
| 947 | Py_INCREF(arr)_Py_INCREF(((PyObject*)(arr))); | |||
| 948 | return (PyObject *)arr; | |||
| 949 | } | |||
| 950 | } | |||
| 951 | ||||
| 952 | ||||
| 953 | static PyObject * | |||
| 954 | array_preparearray(PyArrayObject *self, PyObject *args) | |||
| 955 | { | |||
| 956 | PyObject *obj; | |||
| 957 | PyArrayObject *arr; | |||
| 958 | PyArray_Descr *dtype; | |||
| 959 | ||||
| 960 | if (PyTuple_Size(args) < 1) { | |||
| 961 | PyErr_SetString(PyExc_TypeError, | |||
| 962 | "only accepts 1 argument"); | |||
| 963 | return NULL((void*)0); | |||
| 964 | } | |||
| 965 | obj = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 966 | if (!PyArray_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyArray_Type) || PyType_IsSubtype ((((PyObject*)(obj))->ob_type), (&PyArray_Type)))) { | |||
| 967 | PyErr_SetString(PyExc_TypeError, | |||
| 968 | "can only be called with ndarray object"); | |||
| 969 | return NULL((void*)0); | |||
| 970 | } | |||
| 971 | arr = (PyArrayObject *)obj; | |||
| 972 | ||||
| 973 | if (Py_TYPE(self)(((PyObject*)(self))->ob_type) == Py_TYPE(arr)(((PyObject*)(arr))->ob_type)) { | |||
| 974 | /* No need to create a new view */ | |||
| 975 | Py_INCREF(arr)_Py_INCREF(((PyObject*)(arr))); | |||
| 976 | return (PyObject *)arr; | |||
| 977 | } | |||
| 978 | ||||
| 979 | dtype = PyArray_DESCR(arr); | |||
| 980 | Py_INCREF(dtype)_Py_INCREF(((PyObject*)(dtype))); | |||
| 981 | return PyArray_NewFromDescrAndBase( | |||
| 982 | Py_TYPE(self)(((PyObject*)(self))->ob_type), dtype, | |||
| 983 | PyArray_NDIM(arr), PyArray_DIMS(arr), PyArray_STRIDES(arr), | |||
| 984 | PyArray_DATA(arr), | |||
| 985 | PyArray_FLAGS(arr), (PyObject *)self, (PyObject *)arr); | |||
| 986 | } | |||
| 987 | ||||
| 988 | ||||
| 989 | static PyObject * | |||
| 990 | array_getarray(PyArrayObject *self, PyObject *args) | |||
| 991 | { | |||
| 992 | PyArray_Descr *newtype = NULL((void*)0); | |||
| 993 | PyObject *ret; | |||
| 994 | ||||
| 995 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O&:__array__", | |||
| 996 | PyArray_DescrConverter, &newtype)) { | |||
| 997 | Py_XDECREF(newtype)_Py_XDECREF(((PyObject*)(newtype))); | |||
| 998 | return NULL((void*)0); | |||
| 999 | } | |||
| 1000 | ||||
| 1001 | /* convert to PyArray_Type */ | |||
| 1002 | if (!PyArray_CheckExact(self)(((PyObject*)(self))->ob_type == &PyArray_Type)) { | |||
| 1003 | PyArrayObject *new; | |||
| 1004 | ||||
| 1005 | Py_INCREF(PyArray_DESCR(self))_Py_INCREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1006 | new = (PyArrayObject *)PyArray_NewFromDescrAndBase( | |||
| 1007 | &PyArray_Type, | |||
| 1008 | PyArray_DESCR(self), | |||
| 1009 | PyArray_NDIM(self), | |||
| 1010 | PyArray_DIMS(self), | |||
| 1011 | PyArray_STRIDES(self), | |||
| 1012 | PyArray_DATA(self), | |||
| 1013 | PyArray_FLAGS(self), | |||
| 1014 | NULL((void*)0), | |||
| 1015 | (PyObject *)self | |||
| 1016 | ); | |||
| 1017 | if (new == NULL((void*)0)) { | |||
| 1018 | return NULL((void*)0); | |||
| 1019 | } | |||
| 1020 | self = new; | |||
| 1021 | } | |||
| 1022 | else { | |||
| 1023 | Py_INCREF(self)_Py_INCREF(((PyObject*)(self))); | |||
| 1024 | } | |||
| 1025 | ||||
| 1026 | if ((newtype == NULL((void*)0)) || PyArray_EquivTypes(PyArray_DESCR(self), newtype)) { | |||
| 1027 | return (PyObject *)self; | |||
| 1028 | } | |||
| 1029 | else { | |||
| 1030 | ret = PyArray_CastToType(self, newtype, 0); | |||
| 1031 | Py_DECREF(self)_Py_DECREF(((PyObject*)(self))); | |||
| 1032 | return ret; | |||
| 1033 | } | |||
| 1034 | } | |||
| 1035 | ||||
| 1036 | /* | |||
| 1037 | * Check whether any of the input and output args have a non-default | |||
| 1038 | * __array_ufunc__ method. Return 1 if so, 0 if not, and -1 on error. | |||
| 1039 | * | |||
| 1040 | * This function primarily exists to help ndarray.__array_ufunc__ determine | |||
| 1041 | * whether it can support a ufunc (which is the case only if none of the | |||
| 1042 | * operands have an override). Thus, unlike in umath/override.c, the | |||
| 1043 | * actual overrides are not needed and one can stop looking once one is found. | |||
| 1044 | */ | |||
| 1045 | static int | |||
| 1046 | any_array_ufunc_overrides(PyObject *args, PyObject *kwds) | |||
| 1047 | { | |||
| 1048 | int i; | |||
| 1049 | int nin, nout; | |||
| 1050 | PyObject *out_kwd_obj; | |||
| 1051 | PyObject *fast; | |||
| 1052 | PyObject **in_objs, **out_objs; | |||
| 1053 | ||||
| 1054 | /* check inputs */ | |||
| 1055 | nin = PyTuple_Size(args); | |||
| 1056 | if (nin < 0) { | |||
| 1057 | return -1; | |||
| 1058 | } | |||
| 1059 | fast = PySequence_Fast(args, "Could not convert object to sequence"); | |||
| 1060 | if (fast == NULL((void*)0)) { | |||
| 1061 | return -1; | |||
| 1062 | } | |||
| 1063 | in_objs = PySequence_Fast_ITEMS(fast)(((((((PyObject*)(fast))->ob_type))->tp_flags & ((1UL << 25))) != 0) ? ((PyListObject *)(fast))->ob_item : ((PyTupleObject *)(fast))->ob_item); | |||
| 1064 | for (i = 0; i < nin; ++i) { | |||
| 1065 | if (PyUFunc_HasOverride(in_objs[i])) { | |||
| 1066 | Py_DECREF(fast)_Py_DECREF(((PyObject*)(fast))); | |||
| 1067 | return 1; | |||
| 1068 | } | |||
| 1069 | } | |||
| 1070 | Py_DECREF(fast)_Py_DECREF(((PyObject*)(fast))); | |||
| 1071 | /* check outputs, if any */ | |||
| 1072 | nout = PyUFuncOverride_GetOutObjects(kwds, &out_kwd_obj, &out_objs); | |||
| 1073 | if (nout < 0) { | |||
| 1074 | return -1; | |||
| 1075 | } | |||
| 1076 | for (i = 0; i < nout; i++) { | |||
| 1077 | if (PyUFunc_HasOverride(out_objs[i])) { | |||
| 1078 | Py_DECREF(out_kwd_obj)_Py_DECREF(((PyObject*)(out_kwd_obj))); | |||
| 1079 | return 1; | |||
| 1080 | } | |||
| 1081 | } | |||
| 1082 | Py_DECREF(out_kwd_obj)_Py_DECREF(((PyObject*)(out_kwd_obj))); | |||
| 1083 | return 0; | |||
| 1084 | } | |||
| 1085 | ||||
| 1086 | ||||
| 1087 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyObject * | |||
| 1088 | array_ufunc(PyArrayObject *NPY_UNUSED(self)(__NPY_UNUSED_TAGGEDself) __attribute__ ((__unused__)), PyObject *args, PyObject *kwds) | |||
| 1089 | { | |||
| 1090 | PyObject *ufunc, *method_name, *normal_args, *ufunc_method; | |||
| 1091 | PyObject *result = NULL((void*)0); | |||
| 1092 | int has_override; | |||
| 1093 | ||||
| 1094 | assert(PyTuple_CheckExact(args))((void) (0)); | |||
| 1095 | assert(kwds == NULL || PyDict_CheckExact(kwds))((void) (0)); | |||
| 1096 | ||||
| 1097 | if (PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size) < 2) { | |||
| 1098 | PyErr_SetString(PyExc_TypeError, | |||
| 1099 | "__array_ufunc__ requires at least 2 arguments"); | |||
| 1100 | return NULL((void*)0); | |||
| 1101 | } | |||
| 1102 | normal_args = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args)(((PyVarObject*)((((void) (0)), (PyTupleObject *)(args))))-> ob_size)); | |||
| 1103 | if (normal_args == NULL((void*)0)) { | |||
| 1104 | return NULL((void*)0); | |||
| 1105 | } | |||
| 1106 | /* ndarray cannot handle overrides itself */ | |||
| 1107 | has_override = any_array_ufunc_overrides(normal_args, kwds); | |||
| 1108 | if (has_override < 0) { | |||
| 1109 | goto cleanup; | |||
| 1110 | } | |||
| 1111 | else if (has_override) { | |||
| 1112 | result = Py_NotImplemented(&_Py_NotImplementedStruct); | |||
| 1113 | Py_INCREF(Py_NotImplemented)_Py_INCREF(((PyObject*)((&_Py_NotImplementedStruct)))); | |||
| 1114 | goto cleanup; | |||
| 1115 | } | |||
| 1116 | ||||
| 1117 | ufunc = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 1118 | method_name = PyTuple_GET_ITEM(args, 1)((((void) (0)), (PyTupleObject *)(args))->ob_item[1]); | |||
| 1119 | /* | |||
| 1120 | * TODO(?): call into UFunc code at a later point, since here arguments are | |||
| 1121 | * already normalized and we do not have to look for __array_ufunc__ again. | |||
| 1122 | */ | |||
| 1123 | ufunc_method = PyObject_GetAttr(ufunc, method_name); | |||
| 1124 | if (ufunc_method == NULL((void*)0)) { | |||
| 1125 | goto cleanup; | |||
| 1126 | } | |||
| 1127 | result = PyObject_Call(ufunc_method, normal_args, kwds); | |||
| 1128 | Py_DECREF(ufunc_method)_Py_DECREF(((PyObject*)(ufunc_method))); | |||
| 1129 | ||||
| 1130 | cleanup: | |||
| 1131 | Py_DECREF(normal_args)_Py_DECREF(((PyObject*)(normal_args))); | |||
| 1132 | /* no need to DECREF borrowed references ufunc and method_name */ | |||
| 1133 | return result; | |||
| 1134 | } | |||
| 1135 | ||||
| 1136 | static PyObject * | |||
| 1137 | array_function(PyArrayObject *NPY_UNUSED(self)(__NPY_UNUSED_TAGGEDself) __attribute__ ((__unused__)), PyObject *c_args, PyObject *c_kwds) | |||
| 1138 | { | |||
| 1139 | PyObject *func, *types, *args, *kwargs, *result; | |||
| 1140 | static char *kwlist[] = {"func", "types", "args", "kwargs", NULL((void*)0)}; | |||
| 1141 | ||||
| 1142 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT( | |||
| 1143 | c_args, c_kwds, "OOOO:__array_function__", kwlist, | |||
| 1144 | &func, &types, &args, &kwargs)) { | |||
| 1145 | return NULL((void*)0); | |||
| 1146 | } | |||
| 1147 | ||||
| 1148 | types = PySequence_Fast( | |||
| 1149 | types, | |||
| 1150 | "types argument to ndarray.__array_function__ must be iterable"); | |||
| 1151 | if (types == NULL((void*)0)) { | |||
| 1152 | return NULL((void*)0); | |||
| 1153 | } | |||
| 1154 | ||||
| 1155 | result = array_function_method_impl(func, types, args, kwargs); | |||
| 1156 | Py_DECREF(types)_Py_DECREF(((PyObject*)(types))); | |||
| 1157 | return result; | |||
| 1158 | } | |||
| 1159 | ||||
| 1160 | static PyObject * | |||
| 1161 | array_copy(PyArrayObject *self, | |||
| 1162 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1163 | { | |||
| 1164 | NPY_ORDER order = NPY_CORDER; | |||
| 1165 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1166 | ||||
| 1167 | if (npy_parse_arguments("copy", args, len_args, kwnames,_npy_parse_arguments("copy", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 1168 | "|order", PyArray_OrderConverter, &order,_npy_parse_arguments("copy", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 1169 | NULL, NULL, NULL)_npy_parse_arguments("copy", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) < 0) { | |||
| 1170 | return NULL((void*)0); | |||
| 1171 | } | |||
| 1172 | ||||
| 1173 | return PyArray_NewCopy(self, order); | |||
| 1174 | } | |||
| 1175 | ||||
| 1176 | /* Separate from array_copy to make __copy__ preserve Fortran contiguity. */ | |||
| 1177 | static PyObject * | |||
| 1178 | array_copy_keeporder(PyArrayObject *self, PyObject *args) | |||
| 1179 | { | |||
| 1180 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, ":__copy__")) { | |||
| 1181 | return NULL((void*)0); | |||
| 1182 | } | |||
| 1183 | return PyArray_NewCopy(self, NPY_KEEPORDER); | |||
| 1184 | } | |||
| 1185 | ||||
| 1186 | #include <stdio.h> | |||
| 1187 | static PyObject * | |||
| 1188 | array_resize(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 1189 | { | |||
| 1190 | static char *kwlist[] = {"refcheck", NULL((void*)0)}; | |||
| 1191 | Py_ssize_t size = PyTuple_Size(args); | |||
| 1192 | int refcheck = 1; | |||
| 1193 | PyArray_Dims newshape; | |||
| 1194 | PyObject *ret, *obj; | |||
| 1195 | ||||
| 1196 | ||||
| 1197 | if (!NpyArg_ParseKeywords(kwds, "|i", kwlist, &refcheck)) { | |||
| 1198 | return NULL((void*)0); | |||
| 1199 | } | |||
| 1200 | ||||
| 1201 | if (size == 0) { | |||
| 1202 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1203 | } | |||
| 1204 | else if (size == 1) { | |||
| 1205 | obj = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 1206 | if (obj == Py_None(&_Py_NoneStruct)) { | |||
| 1207 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1208 | } | |||
| 1209 | args = obj; | |||
| 1210 | } | |||
| 1211 | if (!PyArray_IntpConverter(args, &newshape)) { | |||
| 1212 | if (!PyErr_Occurred()) { | |||
| 1213 | PyErr_SetString(PyExc_TypeError, "invalid shape"); | |||
| 1214 | } | |||
| 1215 | return NULL((void*)0); | |||
| 1216 | } | |||
| 1217 | ||||
| 1218 | ret = PyArray_Resize(self, &newshape, refcheck, NPY_ANYORDER); | |||
| 1219 | npy_free_cache_dim_obj(newshape); | |||
| 1220 | if (ret == NULL((void*)0)) { | |||
| 1221 | return NULL((void*)0); | |||
| 1222 | } | |||
| 1223 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 1224 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1225 | } | |||
| 1226 | ||||
| 1227 | static PyObject * | |||
| 1228 | array_repeat(PyArrayObject *self, PyObject *args, PyObject *kwds) { | |||
| 1229 | PyObject *repeats; | |||
| 1230 | int axis = NPY_MAXDIMS32; | |||
| 1231 | static char *kwlist[] = {"repeats", "axis", NULL((void*)0)}; | |||
| 1232 | ||||
| 1233 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O|O&:repeat", kwlist, | |||
| 1234 | &repeats, | |||
| 1235 | PyArray_AxisConverter, &axis)) { | |||
| 1236 | return NULL((void*)0); | |||
| 1237 | } | |||
| 1238 | return PyArray_Return((PyArrayObject *)PyArray_Repeat(self, repeats, axis)); | |||
| 1239 | } | |||
| 1240 | ||||
| 1241 | static PyObject * | |||
| 1242 | array_choose(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 1243 | { | |||
| 1244 | static char *keywords[] = {"out", "mode", NULL((void*)0)}; | |||
| 1245 | PyObject *choices; | |||
| 1246 | PyArrayObject *out = NULL((void*)0); | |||
| 1247 | NPY_CLIPMODE clipmode = NPY_RAISE; | |||
| 1248 | Py_ssize_t n = PyTuple_Size(args); | |||
| 1249 | ||||
| 1250 | if (n <= 1) { | |||
| 1251 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O:choose", &choices)) { | |||
| 1252 | return NULL((void*)0); | |||
| 1253 | } | |||
| 1254 | } | |||
| 1255 | else { | |||
| 1256 | choices = args; | |||
| 1257 | } | |||
| 1258 | ||||
| 1259 | if (!NpyArg_ParseKeywords(kwds, "|O&O&", keywords, | |||
| 1260 | PyArray_OutputConverter, &out, | |||
| 1261 | PyArray_ClipmodeConverter, &clipmode)) { | |||
| 1262 | return NULL((void*)0); | |||
| 1263 | } | |||
| 1264 | ||||
| 1265 | PyObject *ret = PyArray_Choose(self, choices, out, clipmode); | |||
| 1266 | ||||
| 1267 | /* this matches the unpacking behavior of ufuncs */ | |||
| 1268 | if (out == NULL((void*)0)) { | |||
| 1269 | return PyArray_Return((PyArrayObject *)ret); | |||
| 1270 | } | |||
| 1271 | else { | |||
| 1272 | return ret; | |||
| 1273 | } | |||
| 1274 | } | |||
| 1275 | ||||
| 1276 | static PyObject * | |||
| 1277 | array_sort(PyArrayObject *self, | |||
| 1278 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1279 | { | |||
| 1280 | int axis=-1; | |||
| 1281 | int val; | |||
| 1282 | NPY_SORTKIND sortkind = NPY_QUICKSORT; | |||
| 1283 | PyObject *order = NULL((void*)0); | |||
| 1284 | PyArray_Descr *saved = NULL((void*)0); | |||
| 1285 | PyArray_Descr *newd; | |||
| 1286 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1287 | ||||
| 1288 | if (npy_parse_arguments("sort", args, len_args, kwnames,_npy_parse_arguments("sort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_PythonPyIntFromInt, &axis , "|kind", &PyArray_SortkindConverter, &sortkind, "|order" , ((void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1289 | "|axis", &PyArray_PythonPyIntFromInt, &axis,_npy_parse_arguments("sort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_PythonPyIntFromInt, &axis , "|kind", &PyArray_SortkindConverter, &sortkind, "|order" , ((void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1290 | "|kind", &PyArray_SortkindConverter, &sortkind,_npy_parse_arguments("sort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_PythonPyIntFromInt, &axis , "|kind", &PyArray_SortkindConverter, &sortkind, "|order" , ((void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1291 | "|order", NULL, &order,_npy_parse_arguments("sort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_PythonPyIntFromInt, &axis , "|kind", &PyArray_SortkindConverter, &sortkind, "|order" , ((void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1292 | NULL, NULL, NULL)_npy_parse_arguments("sort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_PythonPyIntFromInt, &axis , "|kind", &PyArray_SortkindConverter, &sortkind, "|order" , ((void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 1293 | return NULL((void*)0); | |||
| 1294 | } | |||
| 1295 | if (order == Py_None(&_Py_NoneStruct)) { | |||
| 1296 | order = NULL((void*)0); | |||
| 1297 | } | |||
| 1298 | if (order != NULL((void*)0)) { | |||
| 1299 | PyObject *new_name; | |||
| 1300 | PyObject *_numpy_internal; | |||
| 1301 | saved = PyArray_DESCR(self); | |||
| 1302 | if (!PyDataType_HASFIELDS(saved)(((PyArray_Descr *)(saved))->names != ((void*)0))) { | |||
| 1303 | PyErr_SetString(PyExc_ValueError, "Cannot specify " \ | |||
| 1304 | "order when the array has no fields."); | |||
| 1305 | return NULL((void*)0); | |||
| 1306 | } | |||
| 1307 | _numpy_internal = PyImport_ImportModule("numpy.core._internal"); | |||
| 1308 | if (_numpy_internal == NULL((void*)0)) { | |||
| 1309 | return NULL((void*)0); | |||
| 1310 | } | |||
| 1311 | new_name = PyObject_CallMethod_PyObject_CallMethod_SizeT(_numpy_internal, "_newnames", | |||
| 1312 | "OO", saved, order); | |||
| 1313 | Py_DECREF(_numpy_internal)_Py_DECREF(((PyObject*)(_numpy_internal))); | |||
| 1314 | if (new_name == NULL((void*)0)) { | |||
| 1315 | return NULL((void*)0); | |||
| 1316 | } | |||
| 1317 | newd = PyArray_DescrNew(saved); | |||
| 1318 | Py_DECREF(newd->names)_Py_DECREF(((PyObject*)(newd->names))); | |||
| 1319 | newd->names = new_name; | |||
| 1320 | ((PyArrayObject_fields *)self)->descr = newd; | |||
| 1321 | } | |||
| 1322 | ||||
| 1323 | val = PyArray_Sort(self, axis, sortkind); | |||
| 1324 | if (order != NULL((void*)0)) { | |||
| 1325 | Py_XDECREF(PyArray_DESCR(self))_Py_XDECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1326 | ((PyArrayObject_fields *)self)->descr = saved; | |||
| 1327 | } | |||
| 1328 | if (val < 0) { | |||
| 1329 | return NULL((void*)0); | |||
| 1330 | } | |||
| 1331 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1332 | } | |||
| 1333 | ||||
| 1334 | static PyObject * | |||
| 1335 | array_partition(PyArrayObject *self, | |||
| 1336 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1337 | { | |||
| 1338 | int axis=-1; | |||
| 1339 | int val; | |||
| 1340 | NPY_SELECTKIND sortkind = NPY_INTROSELECT; | |||
| 1341 | PyObject *order = NULL((void*)0); | |||
| 1342 | PyArray_Descr *saved = NULL((void*)0); | |||
| 1343 | PyArray_Descr *newd; | |||
| 1344 | PyArrayObject * ktharray; | |||
| 1345 | PyObject * kthobj; | |||
| 1346 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1347 | ||||
| 1348 | if (npy_parse_arguments("partition", args, len_args, kwnames,_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1349 | "kth", NULL, &kthobj,_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1350 | "|axis", &PyArray_PythonPyIntFromInt, &axis,_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1351 | "|kind", &PyArray_SelectkindConverter, &sortkind,_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1352 | "|order", NULL, &order,_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1353 | NULL, NULL, NULL)_npy_parse_arguments("partition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_PythonPyIntFromInt, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) < 0) { | |||
| 1354 | return NULL((void*)0); | |||
| 1355 | } | |||
| 1356 | ||||
| 1357 | if (order == Py_None(&_Py_NoneStruct)) { | |||
| 1358 | order = NULL((void*)0); | |||
| 1359 | } | |||
| 1360 | if (order != NULL((void*)0)) { | |||
| 1361 | PyObject *new_name; | |||
| 1362 | PyObject *_numpy_internal; | |||
| 1363 | saved = PyArray_DESCR(self); | |||
| 1364 | if (!PyDataType_HASFIELDS(saved)(((PyArray_Descr *)(saved))->names != ((void*)0))) { | |||
| 1365 | PyErr_SetString(PyExc_ValueError, "Cannot specify " \ | |||
| 1366 | "order when the array has no fields."); | |||
| 1367 | return NULL((void*)0); | |||
| 1368 | } | |||
| 1369 | _numpy_internal = PyImport_ImportModule("numpy.core._internal"); | |||
| 1370 | if (_numpy_internal == NULL((void*)0)) { | |||
| 1371 | return NULL((void*)0); | |||
| 1372 | } | |||
| 1373 | new_name = PyObject_CallMethod_PyObject_CallMethod_SizeT(_numpy_internal, "_newnames", | |||
| 1374 | "OO", saved, order); | |||
| 1375 | Py_DECREF(_numpy_internal)_Py_DECREF(((PyObject*)(_numpy_internal))); | |||
| 1376 | if (new_name == NULL((void*)0)) { | |||
| 1377 | return NULL((void*)0); | |||
| 1378 | } | |||
| 1379 | newd = PyArray_DescrNew(saved); | |||
| 1380 | Py_DECREF(newd->names)_Py_DECREF(((PyObject*)(newd->names))); | |||
| 1381 | newd->names = new_name; | |||
| 1382 | ((PyArrayObject_fields *)self)->descr = newd; | |||
| 1383 | } | |||
| 1384 | ||||
| 1385 | ktharray = (PyArrayObject *)PyArray_FromAny(kthobj, NULL((void*)0), 0, 1, | |||
| 1386 | NPY_ARRAY_DEFAULT((0x0001 | (0x0100 | 0x0400))), NULL((void*)0)); | |||
| 1387 | if (ktharray == NULL((void*)0)) | |||
| 1388 | return NULL((void*)0); | |||
| 1389 | ||||
| 1390 | val = PyArray_Partition(self, ktharray, axis, sortkind); | |||
| 1391 | Py_DECREF(ktharray)_Py_DECREF(((PyObject*)(ktharray))); | |||
| 1392 | ||||
| 1393 | if (order != NULL((void*)0)) { | |||
| 1394 | Py_XDECREF(PyArray_DESCR(self))_Py_XDECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1395 | ((PyArrayObject_fields *)self)->descr = saved; | |||
| 1396 | } | |||
| 1397 | if (val < 0) { | |||
| 1398 | return NULL((void*)0); | |||
| 1399 | } | |||
| 1400 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 1401 | } | |||
| 1402 | ||||
| 1403 | static PyObject * | |||
| 1404 | array_argsort(PyArrayObject *self, | |||
| 1405 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1406 | { | |||
| 1407 | int axis = -1; | |||
| 1408 | NPY_SORTKIND sortkind = NPY_QUICKSORT; | |||
| 1409 | PyObject *order = NULL((void*)0), *res; | |||
| 1410 | PyArray_Descr *newd, *saved=NULL((void*)0); | |||
| 1411 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1412 | ||||
| 1413 | if (npy_parse_arguments("argsort", args, len_args, kwnames,_npy_parse_arguments("argsort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|kind" , &PyArray_SortkindConverter, &sortkind, "|order", (( void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1414 | "|axis", &PyArray_AxisConverter, &axis,_npy_parse_arguments("argsort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|kind" , &PyArray_SortkindConverter, &sortkind, "|order", (( void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1415 | "|kind", &PyArray_SortkindConverter, &sortkind,_npy_parse_arguments("argsort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|kind" , &PyArray_SortkindConverter, &sortkind, "|order", (( void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1416 | "|order", NULL, &order,_npy_parse_arguments("argsort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|kind" , &PyArray_SortkindConverter, &sortkind, "|order", (( void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1417 | NULL, NULL, NULL)_npy_parse_arguments("argsort", &__argparse_cache, args, len_args , kwnames, "|axis", &PyArray_AxisConverter, &axis, "|kind" , &PyArray_SortkindConverter, &sortkind, "|order", (( void*)0), &order, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 1418 | return NULL((void*)0); | |||
| 1419 | } | |||
| 1420 | if (order == Py_None(&_Py_NoneStruct)) { | |||
| 1421 | order = NULL((void*)0); | |||
| 1422 | } | |||
| 1423 | if (order != NULL((void*)0)) { | |||
| 1424 | PyObject *new_name; | |||
| 1425 | PyObject *_numpy_internal; | |||
| 1426 | saved = PyArray_DESCR(self); | |||
| 1427 | if (!PyDataType_HASFIELDS(saved)(((PyArray_Descr *)(saved))->names != ((void*)0))) { | |||
| 1428 | PyErr_SetString(PyExc_ValueError, "Cannot specify " | |||
| 1429 | "order when the array has no fields."); | |||
| 1430 | return NULL((void*)0); | |||
| 1431 | } | |||
| 1432 | _numpy_internal = PyImport_ImportModule("numpy.core._internal"); | |||
| 1433 | if (_numpy_internal == NULL((void*)0)) { | |||
| 1434 | return NULL((void*)0); | |||
| 1435 | } | |||
| 1436 | new_name = PyObject_CallMethod_PyObject_CallMethod_SizeT(_numpy_internal, "_newnames", | |||
| 1437 | "OO", saved, order); | |||
| 1438 | Py_DECREF(_numpy_internal)_Py_DECREF(((PyObject*)(_numpy_internal))); | |||
| 1439 | if (new_name == NULL((void*)0)) { | |||
| 1440 | return NULL((void*)0); | |||
| 1441 | } | |||
| 1442 | newd = PyArray_DescrNew(saved); | |||
| 1443 | Py_DECREF(newd->names)_Py_DECREF(((PyObject*)(newd->names))); | |||
| 1444 | newd->names = new_name; | |||
| 1445 | ((PyArrayObject_fields *)self)->descr = newd; | |||
| 1446 | } | |||
| 1447 | ||||
| 1448 | res = PyArray_ArgSort(self, axis, sortkind); | |||
| 1449 | if (order != NULL((void*)0)) { | |||
| 1450 | Py_XDECREF(PyArray_DESCR(self))_Py_XDECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1451 | ((PyArrayObject_fields *)self)->descr = saved; | |||
| 1452 | } | |||
| 1453 | return PyArray_Return((PyArrayObject *)res); | |||
| 1454 | } | |||
| 1455 | ||||
| 1456 | ||||
| 1457 | static PyObject * | |||
| 1458 | array_argpartition(PyArrayObject *self, | |||
| 1459 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1460 | { | |||
| 1461 | int axis = -1; | |||
| 1462 | NPY_SELECTKIND sortkind = NPY_INTROSELECT; | |||
| 1463 | PyObject *order = NULL((void*)0), *res; | |||
| 1464 | PyArray_Descr *newd, *saved=NULL((void*)0); | |||
| 1465 | PyObject * kthobj; | |||
| 1466 | PyArrayObject * ktharray; | |||
| 1467 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1468 | ||||
| 1469 | if (npy_parse_arguments("argpartition", args, len_args, kwnames,_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1470 | "kth", NULL, &kthobj,_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1471 | "|axis", &PyArray_AxisConverter, &axis,_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1472 | "|kind", &PyArray_SelectkindConverter, &sortkind,_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1473 | "|order", NULL, &order,_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) | |||
| 1474 | NULL, NULL, NULL)_npy_parse_arguments("argpartition", &__argparse_cache, args , len_args, kwnames, "kth", ((void*)0), &kthobj, "|axis", &PyArray_AxisConverter, &axis, "|kind", &PyArray_SelectkindConverter , &sortkind, "|order", ((void*)0), &order, ((void*)0) , ((void*)0), ((void*)0)) < 0) { | |||
| 1475 | return NULL((void*)0); | |||
| 1476 | } | |||
| 1477 | if (order == Py_None(&_Py_NoneStruct)) { | |||
| 1478 | order = NULL((void*)0); | |||
| 1479 | } | |||
| 1480 | if (order != NULL((void*)0)) { | |||
| 1481 | PyObject *new_name; | |||
| 1482 | PyObject *_numpy_internal; | |||
| 1483 | saved = PyArray_DESCR(self); | |||
| 1484 | if (!PyDataType_HASFIELDS(saved)(((PyArray_Descr *)(saved))->names != ((void*)0))) { | |||
| 1485 | PyErr_SetString(PyExc_ValueError, "Cannot specify " | |||
| 1486 | "order when the array has no fields."); | |||
| 1487 | return NULL((void*)0); | |||
| 1488 | } | |||
| 1489 | _numpy_internal = PyImport_ImportModule("numpy.core._internal"); | |||
| 1490 | if (_numpy_internal == NULL((void*)0)) { | |||
| 1491 | return NULL((void*)0); | |||
| 1492 | } | |||
| 1493 | new_name = PyObject_CallMethod_PyObject_CallMethod_SizeT(_numpy_internal, "_newnames", | |||
| 1494 | "OO", saved, order); | |||
| 1495 | Py_DECREF(_numpy_internal)_Py_DECREF(((PyObject*)(_numpy_internal))); | |||
| 1496 | if (new_name == NULL((void*)0)) { | |||
| 1497 | return NULL((void*)0); | |||
| 1498 | } | |||
| 1499 | newd = PyArray_DescrNew(saved); | |||
| 1500 | Py_DECREF(newd->names)_Py_DECREF(((PyObject*)(newd->names))); | |||
| 1501 | newd->names = new_name; | |||
| 1502 | ((PyArrayObject_fields *)self)->descr = newd; | |||
| 1503 | } | |||
| 1504 | ||||
| 1505 | ktharray = (PyArrayObject *)PyArray_FromAny(kthobj, NULL((void*)0), 0, 1, | |||
| 1506 | NPY_ARRAY_DEFAULT((0x0001 | (0x0100 | 0x0400))), NULL((void*)0)); | |||
| 1507 | if (ktharray == NULL((void*)0)) | |||
| 1508 | return NULL((void*)0); | |||
| 1509 | ||||
| 1510 | res = PyArray_ArgPartition(self, ktharray, axis, sortkind); | |||
| 1511 | Py_DECREF(ktharray)_Py_DECREF(((PyObject*)(ktharray))); | |||
| 1512 | ||||
| 1513 | if (order != NULL((void*)0)) { | |||
| 1514 | Py_XDECREF(PyArray_DESCR(self))_Py_XDECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1515 | ((PyArrayObject_fields *)self)->descr = saved; | |||
| 1516 | } | |||
| 1517 | return PyArray_Return((PyArrayObject *)res); | |||
| 1518 | } | |||
| 1519 | ||||
| 1520 | static PyObject * | |||
| 1521 | array_searchsorted(PyArrayObject *self, | |||
| 1522 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 1523 | { | |||
| 1524 | PyObject *keys; | |||
| 1525 | PyObject *sorter; | |||
| 1526 | NPY_SEARCHSIDE side = NPY_SEARCHLEFT; | |||
| 1527 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 1528 | ||||
| 1529 | sorter = NULL((void*)0); | |||
| 1530 | if (npy_parse_arguments("searchsorted", args, len_args, kwnames,_npy_parse_arguments("searchsorted", &__argparse_cache, args , len_args, kwnames, "v", ((void*)0), &keys, "|side", & PyArray_SearchsideConverter, &side, "|sorter", ((void*)0) , &sorter, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1531 | "v", NULL, &keys,_npy_parse_arguments("searchsorted", &__argparse_cache, args , len_args, kwnames, "v", ((void*)0), &keys, "|side", & PyArray_SearchsideConverter, &side, "|sorter", ((void*)0) , &sorter, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1532 | "|side", &PyArray_SearchsideConverter, &side,_npy_parse_arguments("searchsorted", &__argparse_cache, args , len_args, kwnames, "v", ((void*)0), &keys, "|side", & PyArray_SearchsideConverter, &side, "|sorter", ((void*)0) , &sorter, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1533 | "|sorter", NULL, &sorter,_npy_parse_arguments("searchsorted", &__argparse_cache, args , len_args, kwnames, "v", ((void*)0), &keys, "|side", & PyArray_SearchsideConverter, &side, "|sorter", ((void*)0) , &sorter, ((void*)0), ((void*)0), ((void*)0)) | |||
| 1534 | NULL, NULL, NULL)_npy_parse_arguments("searchsorted", &__argparse_cache, args , len_args, kwnames, "v", ((void*)0), &keys, "|side", & PyArray_SearchsideConverter, &side, "|sorter", ((void*)0) , &sorter, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 1535 | return NULL((void*)0); | |||
| 1536 | } | |||
| 1537 | if (sorter == Py_None(&_Py_NoneStruct)) { | |||
| 1538 | sorter = NULL((void*)0); | |||
| 1539 | } | |||
| 1540 | return PyArray_Return((PyArrayObject *)PyArray_SearchSorted(self, keys, side, sorter)); | |||
| 1541 | } | |||
| 1542 | ||||
| 1543 | static void | |||
| 1544 | _deepcopy_call(char *iptr, char *optr, PyArray_Descr *dtype, | |||
| 1545 | PyObject *deepcopy, PyObject *visit) | |||
| 1546 | { | |||
| 1547 | if (!PyDataType_REFCHK(dtype)(((dtype)->flags & (0x01)) == (0x01))) { | |||
| 1548 | return; | |||
| 1549 | } | |||
| 1550 | else if (PyDataType_HASFIELDS(dtype)(((PyArray_Descr *)(dtype))->names != ((void*)0))) { | |||
| 1551 | PyObject *key, *value, *title = NULL((void*)0); | |||
| 1552 | PyArray_Descr *new; | |||
| 1553 | int offset; | |||
| 1554 | Py_ssize_t pos = 0; | |||
| 1555 | while (PyDict_Next(dtype->fields, &pos, &key, &value)) { | |||
| 1556 | if (NPY_TITLE_KEY(key, value)(NPY_TITLE_KEY_check((key), (value)))) { | |||
| 1557 | continue; | |||
| 1558 | } | |||
| 1559 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(value, "Oi|O", &new, &offset, | |||
| 1560 | &title)) { | |||
| 1561 | return; | |||
| 1562 | } | |||
| 1563 | _deepcopy_call(iptr + offset, optr + offset, new, | |||
| 1564 | deepcopy, visit); | |||
| 1565 | } | |||
| 1566 | } | |||
| 1567 | else { | |||
| 1568 | PyObject *itemp, *otemp; | |||
| 1569 | PyObject *res; | |||
| 1570 | memcpy(&itemp, iptr, sizeof(itemp)); | |||
| 1571 | memcpy(&otemp, optr, sizeof(otemp)); | |||
| 1572 | Py_XINCREF(itemp)_Py_XINCREF(((PyObject*)(itemp))); | |||
| 1573 | /* call deepcopy on this argument */ | |||
| 1574 | res = PyObject_CallFunctionObjArgs(deepcopy, itemp, visit, NULL((void*)0)); | |||
| 1575 | Py_XDECREF(itemp)_Py_XDECREF(((PyObject*)(itemp))); | |||
| 1576 | Py_XDECREF(otemp)_Py_XDECREF(((PyObject*)(otemp))); | |||
| 1577 | memcpy(optr, &res, sizeof(res)); | |||
| 1578 | } | |||
| 1579 | ||||
| 1580 | } | |||
| 1581 | ||||
| 1582 | ||||
| 1583 | static PyObject * | |||
| 1584 | array_deepcopy(PyArrayObject *self, PyObject *args) | |||
| 1585 | { | |||
| 1586 | PyArrayObject *copied_array; | |||
| 1587 | PyObject *visit; | |||
| 1588 | NpyIter *iter; | |||
| 1589 | NpyIter_IterNextFunc *iternext; | |||
| 1590 | char *data; | |||
| 1591 | char **dataptr; | |||
| 1592 | npy_intp *strideptr, *innersizeptr; | |||
| 1593 | npy_intp stride, count; | |||
| 1594 | PyObject *copy, *deepcopy; | |||
| 1595 | ||||
| 1596 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O:__deepcopy__", &visit)) { | |||
| 1597 | return NULL((void*)0); | |||
| 1598 | } | |||
| 1599 | copied_array = (PyArrayObject*) PyArray_NewCopy(self, NPY_KEEPORDER); | |||
| 1600 | if (copied_array == NULL((void*)0)) { | |||
| 1601 | return NULL((void*)0); | |||
| 1602 | } | |||
| 1603 | if (PyDataType_REFCHK(PyArray_DESCR(self))(((PyArray_DESCR(self))->flags & (0x01)) == (0x01))) { | |||
| 1604 | copy = PyImport_ImportModule("copy"); | |||
| 1605 | if (copy == NULL((void*)0)) { | |||
| 1606 | Py_DECREF(copied_array)_Py_DECREF(((PyObject*)(copied_array))); | |||
| 1607 | return NULL((void*)0); | |||
| 1608 | } | |||
| 1609 | deepcopy = PyObject_GetAttrString(copy, "deepcopy"); | |||
| 1610 | Py_DECREF(copy)_Py_DECREF(((PyObject*)(copy))); | |||
| 1611 | if (deepcopy == NULL((void*)0)) { | |||
| 1612 | Py_DECREF(copied_array)_Py_DECREF(((PyObject*)(copied_array))); | |||
| 1613 | return NULL((void*)0); | |||
| 1614 | } | |||
| 1615 | iter = NpyIter_New(copied_array, | |||
| 1616 | NPY_ITER_READWRITE0x00010000 | | |||
| 1617 | NPY_ITER_EXTERNAL_LOOP0x00000008 | | |||
| 1618 | NPY_ITER_REFS_OK0x00000020 | | |||
| 1619 | NPY_ITER_ZEROSIZE_OK0x00000040, | |||
| 1620 | NPY_KEEPORDER, NPY_NO_CASTING, | |||
| 1621 | NULL((void*)0)); | |||
| 1622 | if (iter == NULL((void*)0)) { | |||
| 1623 | Py_DECREF(deepcopy)_Py_DECREF(((PyObject*)(deepcopy))); | |||
| 1624 | Py_DECREF(copied_array)_Py_DECREF(((PyObject*)(copied_array))); | |||
| 1625 | return NULL((void*)0); | |||
| 1626 | } | |||
| 1627 | if (NpyIter_GetIterSize(iter) != 0) { | |||
| 1628 | iternext = NpyIter_GetIterNext(iter, NULL((void*)0)); | |||
| 1629 | if (iternext == NULL((void*)0)) { | |||
| 1630 | NpyIter_Deallocate(iter); | |||
| 1631 | Py_DECREF(deepcopy)_Py_DECREF(((PyObject*)(deepcopy))); | |||
| 1632 | Py_DECREF(copied_array)_Py_DECREF(((PyObject*)(copied_array))); | |||
| 1633 | return NULL((void*)0); | |||
| 1634 | } | |||
| 1635 | ||||
| 1636 | dataptr = NpyIter_GetDataPtrArray(iter); | |||
| 1637 | strideptr = NpyIter_GetInnerStrideArray(iter); | |||
| 1638 | innersizeptr = NpyIter_GetInnerLoopSizePtr(iter); | |||
| 1639 | ||||
| 1640 | do { | |||
| 1641 | data = *dataptr; | |||
| 1642 | stride = *strideptr; | |||
| 1643 | count = *innersizeptr; | |||
| 1644 | while (count--) { | |||
| 1645 | _deepcopy_call(data, data, PyArray_DESCR(copied_array), | |||
| 1646 | deepcopy, visit); | |||
| 1647 | data += stride; | |||
| 1648 | } | |||
| 1649 | } while (iternext(iter)); | |||
| 1650 | } | |||
| 1651 | NpyIter_Deallocate(iter); | |||
| 1652 | Py_DECREF(deepcopy)_Py_DECREF(((PyObject*)(deepcopy))); | |||
| 1653 | } | |||
| 1654 | return (PyObject*) copied_array; | |||
| 1655 | } | |||
| 1656 | ||||
| 1657 | /* Convert Array to flat list (using getitem) */ | |||
| 1658 | static PyObject * | |||
| 1659 | _getlist_pkl(PyArrayObject *self) | |||
| 1660 | { | |||
| 1661 | PyObject *theobject; | |||
| 1662 | PyArrayIterObject *iter = NULL((void*)0); | |||
| 1663 | PyObject *list; | |||
| 1664 | PyArray_GetItemFunc *getitem; | |||
| 1665 | ||||
| 1666 | getitem = PyArray_DESCR(self)->f->getitem; | |||
| 1667 | iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); | |||
| 1668 | if (iter == NULL((void*)0)) { | |||
| 1669 | return NULL((void*)0); | |||
| 1670 | } | |||
| 1671 | list = PyList_New(iter->size); | |||
| 1672 | if (list == NULL((void*)0)) { | |||
| 1673 | Py_DECREF(iter)_Py_DECREF(((PyObject*)(iter))); | |||
| 1674 | return NULL((void*)0); | |||
| 1675 | } | |||
| 1676 | while (iter->index < iter->size) { | |||
| 1677 | theobject = getitem(iter->dataptr, self); | |||
| 1678 | PyList_SET_ITEM(list, iter->index, theobject)PyList_SetItem(list, iter->index, theobject); | |||
| 1679 | PyArray_ITER_NEXT(iter)do { ((PyArrayIterObject *)(iter))->index++; if (((PyArrayIterObject *)(iter))->nd_m1 == 0) { do { (((PyArrayIterObject *)(iter )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(iter))))->strides[0]; (((PyArrayIterObject *)(iter)))-> coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(iter))->contiguous) ((PyArrayIterObject *)(iter))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(iter))->ao )->elsize; else if (((PyArrayIterObject *)(iter))->nd_m1 == 1) { do { if ((((PyArrayIterObject *)(iter)))->coordinates [1] < (((PyArrayIterObject *)(iter)))->dims_m1[1]) { (( (PyArrayIterObject *)(iter)))->coordinates[1]++; (((PyArrayIterObject *)(iter)))->dataptr += (((PyArrayIterObject *)(iter)))-> strides[1]; } else { (((PyArrayIterObject *)(iter)))->coordinates [1] = 0; (((PyArrayIterObject *)(iter)))->coordinates[0]++ ; (((PyArrayIterObject *)(iter)))->dataptr += (((PyArrayIterObject *)(iter)))->strides[0] - (((PyArrayIterObject *)(iter)))-> backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i =((PyArrayIterObject *)(iter))->nd_m1; __npy_i >= 0; __npy_i --) { if (((PyArrayIterObject *)(iter))->coordinates[__npy_i ] < ((PyArrayIterObject *)(iter))->dims_m1[__npy_i]) { ( (PyArrayIterObject *)(iter))->coordinates[__npy_i]++; ((PyArrayIterObject *)(iter))->dataptr += ((PyArrayIterObject *)(iter))->strides [__npy_i]; break; } else { ((PyArrayIterObject *)(iter))-> coordinates[__npy_i] = 0; ((PyArrayIterObject *)(iter))->dataptr -= ((PyArrayIterObject *)(iter))->backstrides[__npy_i]; } } } } while (0); | |||
| 1680 | } | |||
| 1681 | Py_DECREF(iter)_Py_DECREF(((PyObject*)(iter))); | |||
| 1682 | return list; | |||
| 1683 | } | |||
| 1684 | ||||
| 1685 | static int | |||
| 1686 | _setlist_pkl(PyArrayObject *self, PyObject *list) | |||
| 1687 | { | |||
| 1688 | PyObject *theobject; | |||
| 1689 | PyArrayIterObject *iter = NULL((void*)0); | |||
| 1690 | PyArray_SetItemFunc *setitem; | |||
| 1691 | ||||
| 1692 | setitem = PyArray_DESCR(self)->f->setitem; | |||
| 1693 | iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); | |||
| 1694 | if (iter == NULL((void*)0)) { | |||
| 1695 | return -1; | |||
| 1696 | } | |||
| 1697 | while(iter->index < iter->size) { | |||
| 1698 | theobject = PyList_GET_ITEM(list, iter->index)(((PyListObject *)(list))->ob_item[iter->index]); | |||
| 1699 | setitem(theobject, iter->dataptr, self); | |||
| 1700 | PyArray_ITER_NEXT(iter)do { ((PyArrayIterObject *)(iter))->index++; if (((PyArrayIterObject *)(iter))->nd_m1 == 0) { do { (((PyArrayIterObject *)(iter )))->dataptr += ((PyArrayIterObject *)(((PyArrayIterObject *)(iter))))->strides[0]; (((PyArrayIterObject *)(iter)))-> coordinates[0]++; } while (0); } else if (((PyArrayIterObject *)(iter))->contiguous) ((PyArrayIterObject *)(iter))-> dataptr += PyArray_DESCR(((PyArrayIterObject *)(iter))->ao )->elsize; else if (((PyArrayIterObject *)(iter))->nd_m1 == 1) { do { if ((((PyArrayIterObject *)(iter)))->coordinates [1] < (((PyArrayIterObject *)(iter)))->dims_m1[1]) { (( (PyArrayIterObject *)(iter)))->coordinates[1]++; (((PyArrayIterObject *)(iter)))->dataptr += (((PyArrayIterObject *)(iter)))-> strides[1]; } else { (((PyArrayIterObject *)(iter)))->coordinates [1] = 0; (((PyArrayIterObject *)(iter)))->coordinates[0]++ ; (((PyArrayIterObject *)(iter)))->dataptr += (((PyArrayIterObject *)(iter)))->strides[0] - (((PyArrayIterObject *)(iter)))-> backstrides[1]; } } while (0); } else { int __npy_i; for (__npy_i =((PyArrayIterObject *)(iter))->nd_m1; __npy_i >= 0; __npy_i --) { if (((PyArrayIterObject *)(iter))->coordinates[__npy_i ] < ((PyArrayIterObject *)(iter))->dims_m1[__npy_i]) { ( (PyArrayIterObject *)(iter))->coordinates[__npy_i]++; ((PyArrayIterObject *)(iter))->dataptr += ((PyArrayIterObject *)(iter))->strides [__npy_i]; break; } else { ((PyArrayIterObject *)(iter))-> coordinates[__npy_i] = 0; ((PyArrayIterObject *)(iter))->dataptr -= ((PyArrayIterObject *)(iter))->backstrides[__npy_i]; } } } } while (0); | |||
| 1701 | } | |||
| 1702 | Py_XDECREF(iter)_Py_XDECREF(((PyObject*)(iter))); | |||
| 1703 | return 0; | |||
| 1704 | } | |||
| 1705 | ||||
| 1706 | ||||
| 1707 | static PyObject * | |||
| 1708 | array_reduce(PyArrayObject *self, PyObject *NPY_UNUSED(args)(__NPY_UNUSED_TAGGEDargs) __attribute__ ((__unused__))) | |||
| 1709 | { | |||
| 1710 | /* version number of this pickle type. Increment if we need to | |||
| 1711 | change the format. Be sure to handle the old versions in | |||
| 1712 | array_setstate. */ | |||
| 1713 | const int version = 1; | |||
| 1714 | PyObject *ret = NULL((void*)0), *state = NULL((void*)0), *obj = NULL((void*)0), *mod = NULL((void*)0); | |||
| 1715 | PyObject *mybool, *thestr = NULL((void*)0); | |||
| 1716 | PyArray_Descr *descr; | |||
| 1717 | ||||
| 1718 | /* Return a tuple of (callable object, arguments, object's state) */ | |||
| 1719 | /* We will put everything in the object's state, so that on UnPickle | |||
| 1720 | it can use the string object as memory without a copy */ | |||
| 1721 | ||||
| 1722 | ret = PyTuple_New(3); | |||
| 1723 | if (ret == NULL((void*)0)) { | |||
| 1724 | return NULL((void*)0); | |||
| 1725 | } | |||
| 1726 | mod = PyImport_ImportModule("numpy.core._multiarray_umath"); | |||
| 1727 | if (mod == NULL((void*)0)) { | |||
| 1728 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 1729 | return NULL((void*)0); | |||
| 1730 | } | |||
| 1731 | obj = PyObject_GetAttrString(mod, "_reconstruct"); | |||
| 1732 | Py_DECREF(mod)_Py_DECREF(((PyObject*)(mod))); | |||
| 1733 | PyTuple_SET_ITEM(ret, 0, obj)PyTuple_SetItem(ret, 0, obj); | |||
| 1734 | PyTuple_SET_ITEM(ret, 1,PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1735 | Py_BuildValue("ONc",PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1736 | (PyObject *)Py_TYPE(self),PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1737 | Py_BuildValue("(N)",PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1738 | PyLong_FromLong(0)),PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1739 | /* dummy data-type */PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')) | |||
| 1740 | 'b'))PyTuple_SetItem(ret, 1, _Py_BuildValue_SizeT("ONc", (PyObject *)(((PyObject*)(self))->ob_type), _Py_BuildValue_SizeT("(N)" , PyLong_FromLong(0)), 'b')); | |||
| 1741 | ||||
| 1742 | /* Now fill in object's state. This is a tuple with | |||
| 1743 | 5 arguments | |||
| 1744 | ||||
| 1745 | 1) an integer with the pickle version. | |||
| 1746 | 2) a Tuple giving the shape | |||
| 1747 | 3) a PyArray_Descr Object (with correct bytorder set) | |||
| 1748 | 4) a npy_bool stating if Fortran or not | |||
| 1749 | 5) a Python object representing the data (a string, or | |||
| 1750 | a list or any user-defined object). | |||
| 1751 | ||||
| 1752 | Notice because Python does not describe a mechanism to write | |||
| 1753 | raw data to the pickle, this performs a copy to a string first | |||
| 1754 | This issue is now addressed in protocol 5, where a buffer is serialized | |||
| 1755 | instead of a string, | |||
| 1756 | */ | |||
| 1757 | ||||
| 1758 | state = PyTuple_New(5); | |||
| 1759 | if (state == NULL((void*)0)) { | |||
| 1760 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 1761 | return NULL((void*)0); | |||
| 1762 | } | |||
| 1763 | PyTuple_SET_ITEM(state, 0, PyLong_FromLong(version))PyTuple_SetItem(state, 0, PyLong_FromLong(version)); | |||
| 1764 | PyTuple_SET_ITEM(state, 1, PyObject_GetAttrString((PyObject *)self,PyTuple_SetItem(state, 1, PyObject_GetAttrString((PyObject *) self, "shape")) | |||
| 1765 | "shape"))PyTuple_SetItem(state, 1, PyObject_GetAttrString((PyObject *) self, "shape")); | |||
| 1766 | descr = PyArray_DESCR(self); | |||
| 1767 | Py_INCREF(descr)_Py_INCREF(((PyObject*)(descr))); | |||
| 1768 | PyTuple_SET_ITEM(state, 2, (PyObject *)descr)PyTuple_SetItem(state, 2, (PyObject *)descr); | |||
| 1769 | mybool = (PyArray_ISFORTRAN(self)(PyArray_CHKFLAGS(self, 0x0002) && (!PyArray_CHKFLAGS (self, 0x0001))) ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct)); | |||
| 1770 | Py_INCREF(mybool)_Py_INCREF(((PyObject*)(mybool))); | |||
| 1771 | PyTuple_SET_ITEM(state, 3, mybool)PyTuple_SetItem(state, 3, mybool); | |||
| 1772 | if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_LIST_PICKLE)(((PyArray_DESCR(self))->flags & (0x02)) == (0x02))) { | |||
| 1773 | thestr = _getlist_pkl(self); | |||
| 1774 | } | |||
| 1775 | else { | |||
| 1776 | thestr = PyArray_ToString(self, NPY_ANYORDER); | |||
| 1777 | } | |||
| 1778 | if (thestr == NULL((void*)0)) { | |||
| 1779 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 1780 | Py_DECREF(state)_Py_DECREF(((PyObject*)(state))); | |||
| 1781 | return NULL((void*)0); | |||
| 1782 | } | |||
| 1783 | PyTuple_SET_ITEM(state, 4, thestr)PyTuple_SetItem(state, 4, thestr); | |||
| 1784 | PyTuple_SET_ITEM(ret, 2, state)PyTuple_SetItem(ret, 2, state); | |||
| 1785 | return ret; | |||
| 1786 | } | |||
| 1787 | ||||
| 1788 | static PyObject * | |||
| 1789 | array_reduce_ex_regular(PyArrayObject *self, int NPY_UNUSED(protocol)(__NPY_UNUSED_TAGGEDprotocol) __attribute__ ((__unused__))) | |||
| 1790 | { | |||
| 1791 | PyObject *subclass_array_reduce = NULL((void*)0); | |||
| 1792 | PyObject *ret; | |||
| 1793 | ||||
| 1794 | /* We do not call array_reduce directly but instead lookup and call | |||
| 1795 | * the __reduce__ method to make sure that it's possible to customize | |||
| 1796 | * pickling in sub-classes. */ | |||
| 1797 | subclass_array_reduce = PyObject_GetAttrString((PyObject *)self, | |||
| 1798 | "__reduce__"); | |||
| 1799 | if (subclass_array_reduce == NULL((void*)0)) { | |||
| 1800 | return NULL((void*)0); | |||
| 1801 | } | |||
| 1802 | ret = PyObject_CallObject(subclass_array_reduce, NULL((void*)0)); | |||
| 1803 | Py_DECREF(subclass_array_reduce)_Py_DECREF(((PyObject*)(subclass_array_reduce))); | |||
| 1804 | return ret; | |||
| 1805 | } | |||
| 1806 | ||||
| 1807 | static PyObject * | |||
| 1808 | array_reduce_ex_picklebuffer(PyArrayObject *self, int protocol) | |||
| 1809 | { | |||
| 1810 | PyObject *numeric_mod = NULL((void*)0), *from_buffer_func = NULL((void*)0); | |||
| 1811 | PyObject *pickle_module = NULL((void*)0), *picklebuf_class = NULL((void*)0); | |||
| 1812 | PyObject *picklebuf_args = NULL((void*)0); | |||
| 1813 | PyObject *buffer = NULL((void*)0), *transposed_array = NULL((void*)0); | |||
| 1814 | PyArray_Descr *descr = NULL((void*)0); | |||
| 1815 | char order; | |||
| 1816 | ||||
| 1817 | descr = PyArray_DESCR(self); | |||
| 1818 | ||||
| 1819 | /* if the python version is below 3.8, the pickle module does not provide | |||
| 1820 | * built-in support for protocol 5. We try importing the pickle5 | |||
| 1821 | * backport instead */ | |||
| 1822 | #if PY_VERSION_HEX((3 << 24) | (8 << 16) | (5 << 8) | (0xF << 4) | (0 << 0)) >= 0x03080000 | |||
| 1823 | /* we expect protocol 5 to be available in Python 3.8 */ | |||
| 1824 | pickle_module = PyImport_ImportModule("pickle"); | |||
| 1825 | #else | |||
| 1826 | pickle_module = PyImport_ImportModule("pickle5"); | |||
| 1827 | if (pickle_module == NULL((void*)0)) { | |||
| 1828 | /* for protocol 5, raise a clear ImportError if pickle5 is not found | |||
| 1829 | */ | |||
| 1830 | PyErr_SetString(PyExc_ImportError, "Using pickle protocol 5 " | |||
| 1831 | "requires the pickle5 module for Python >=3.6 and <3.8"); | |||
| 1832 | return NULL((void*)0); | |||
| 1833 | } | |||
| 1834 | #endif | |||
| 1835 | if (pickle_module == NULL((void*)0)){ | |||
| 1836 | return NULL((void*)0); | |||
| 1837 | } | |||
| 1838 | picklebuf_class = PyObject_GetAttrString(pickle_module, "PickleBuffer"); | |||
| 1839 | Py_DECREF(pickle_module)_Py_DECREF(((PyObject*)(pickle_module))); | |||
| 1840 | if (picklebuf_class == NULL((void*)0)) { | |||
| 1841 | return NULL((void*)0); | |||
| 1842 | } | |||
| 1843 | ||||
| 1844 | /* Construct a PickleBuffer of the array */ | |||
| 1845 | ||||
| 1846 | if (!PyArray_IS_C_CONTIGUOUS((PyArrayObject*) self)PyArray_CHKFLAGS(((PyArrayObject*) self), 0x0001) && | |||
| 1847 | PyArray_IS_F_CONTIGUOUS((PyArrayObject*) self)PyArray_CHKFLAGS(((PyArrayObject*) self), 0x0002)) { | |||
| 1848 | /* if the array if Fortran-contiguous and not C-contiguous, | |||
| 1849 | * the PickleBuffer instance will hold a view on the transpose | |||
| 1850 | * of the initial array, that is C-contiguous. */ | |||
| 1851 | order = 'F'; | |||
| 1852 | transposed_array = PyArray_Transpose((PyArrayObject*)self, NULL((void*)0)); | |||
| 1853 | picklebuf_args = Py_BuildValue_Py_BuildValue_SizeT("(N)", transposed_array); | |||
| 1854 | } | |||
| 1855 | else { | |||
| 1856 | order = 'C'; | |||
| 1857 | picklebuf_args = Py_BuildValue_Py_BuildValue_SizeT("(O)", self); | |||
| 1858 | } | |||
| 1859 | if (picklebuf_args == NULL((void*)0)) { | |||
| 1860 | Py_DECREF(picklebuf_class)_Py_DECREF(((PyObject*)(picklebuf_class))); | |||
| 1861 | return NULL((void*)0); | |||
| 1862 | } | |||
| 1863 | ||||
| 1864 | buffer = PyObject_CallObject(picklebuf_class, picklebuf_args); | |||
| 1865 | Py_DECREF(picklebuf_class)_Py_DECREF(((PyObject*)(picklebuf_class))); | |||
| 1866 | Py_DECREF(picklebuf_args)_Py_DECREF(((PyObject*)(picklebuf_args))); | |||
| 1867 | if (buffer == NULL((void*)0)) { | |||
| 1868 | /* Some arrays may refuse to export a buffer, in which case | |||
| 1869 | * just fall back on regular __reduce_ex__ implementation | |||
| 1870 | * (gh-12745). | |||
| 1871 | */ | |||
| 1872 | PyErr_Clear(); | |||
| 1873 | return array_reduce_ex_regular(self, protocol); | |||
| 1874 | } | |||
| 1875 | ||||
| 1876 | /* Get the _frombuffer() function for reconstruction */ | |||
| 1877 | ||||
| 1878 | numeric_mod = PyImport_ImportModule("numpy.core.numeric"); | |||
| 1879 | if (numeric_mod == NULL((void*)0)) { | |||
| 1880 | Py_DECREF(buffer)_Py_DECREF(((PyObject*)(buffer))); | |||
| 1881 | return NULL((void*)0); | |||
| 1882 | } | |||
| 1883 | from_buffer_func = PyObject_GetAttrString(numeric_mod, | |||
| 1884 | "_frombuffer"); | |||
| 1885 | Py_DECREF(numeric_mod)_Py_DECREF(((PyObject*)(numeric_mod))); | |||
| 1886 | if (from_buffer_func == NULL((void*)0)) { | |||
| 1887 | Py_DECREF(buffer)_Py_DECREF(((PyObject*)(buffer))); | |||
| 1888 | return NULL((void*)0); | |||
| 1889 | } | |||
| 1890 | ||||
| 1891 | return Py_BuildValue_Py_BuildValue_SizeT("N(NONN)", | |||
| 1892 | from_buffer_func, buffer, (PyObject *)descr, | |||
| 1893 | PyObject_GetAttrString((PyObject *)self, "shape"), | |||
| 1894 | PyUnicode_FromStringAndSize(&order, 1)); | |||
| 1895 | } | |||
| 1896 | ||||
| 1897 | static PyObject * | |||
| 1898 | array_reduce_ex(PyArrayObject *self, PyObject *args) | |||
| 1899 | { | |||
| 1900 | int protocol; | |||
| 1901 | PyArray_Descr *descr = NULL((void*)0); | |||
| 1902 | ||||
| 1903 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "i", &protocol)) { | |||
| 1904 | return NULL((void*)0); | |||
| 1905 | } | |||
| 1906 | ||||
| 1907 | descr = PyArray_DESCR(self); | |||
| 1908 | if ((protocol < 5) || | |||
| 1909 | (!PyArray_IS_C_CONTIGUOUS((PyArrayObject*)self)PyArray_CHKFLAGS(((PyArrayObject*)self), 0x0001) && | |||
| 1910 | !PyArray_IS_F_CONTIGUOUS((PyArrayObject*)self)PyArray_CHKFLAGS(((PyArrayObject*)self), 0x0002)) || | |||
| 1911 | PyDataType_FLAGCHK(descr, NPY_ITEM_HASOBJECT)(((descr)->flags & (0x01)) == (0x01)) || | |||
| 1912 | (PyType_IsSubtype(((PyObject*)self)->ob_type, &PyArray_Type) && | |||
| 1913 | ((PyObject*)self)->ob_type != &PyArray_Type) || | |||
| 1914 | descr->elsize == 0) { | |||
| 1915 | /* The PickleBuffer class from version 5 of the pickle protocol | |||
| 1916 | * can only be used for arrays backed by a contiguous data buffer. | |||
| 1917 | * For all other cases we fallback to the generic array_reduce | |||
| 1918 | * method that involves using a temporary bytes allocation. */ | |||
| 1919 | return array_reduce_ex_regular(self, protocol); | |||
| 1920 | } | |||
| 1921 | else { | |||
| 1922 | return array_reduce_ex_picklebuffer(self, protocol); | |||
| 1923 | } | |||
| 1924 | } | |||
| 1925 | ||||
| 1926 | static PyObject * | |||
| 1927 | array_setstate(PyArrayObject *self, PyObject *args) | |||
| 1928 | { | |||
| 1929 | PyObject *shape; | |||
| 1930 | PyArray_Descr *typecode; | |||
| 1931 | int version = 1; | |||
| 1932 | int is_f_order; | |||
| 1933 | PyObject *rawdata = NULL((void*)0); | |||
| 1934 | char *datastr; | |||
| 1935 | Py_ssize_t len; | |||
| 1936 | npy_intp size, dimensions[NPY_MAXDIMS32]; | |||
| 1937 | int nd; | |||
| 1938 | npy_intp nbytes; | |||
| 1939 | int overflowed; | |||
| 1940 | ||||
| 1941 | PyArrayObject_fields *fa = (PyArrayObject_fields *)self; | |||
| 1942 | ||||
| 1943 | /* This will free any memory associated with a and | |||
| 1944 | use the string in setstate as the (writeable) memory. | |||
| 1945 | */ | |||
| 1946 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "(iO!O!iO):__setstate__", | |||
| ||||
| 1947 | &version, | |||
| 1948 | &PyTuple_Type, &shape, | |||
| 1949 | &PyArrayDescr_Type(*(PyTypeObject *)(&PyArrayDescr_TypeFull)), &typecode, | |||
| 1950 | &is_f_order, | |||
| 1951 | &rawdata)) { | |||
| 1952 | PyErr_Clear(); | |||
| 1953 | version = 0; | |||
| 1954 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "(O!O!iO):__setstate__", | |||
| 1955 | &PyTuple_Type, &shape, | |||
| 1956 | &PyArrayDescr_Type(*(PyTypeObject *)(&PyArrayDescr_TypeFull)), &typecode, | |||
| 1957 | &is_f_order, | |||
| 1958 | &rawdata)) { | |||
| 1959 | return NULL((void*)0); | |||
| 1960 | } | |||
| 1961 | } | |||
| 1962 | ||||
| 1963 | /* If we ever need another pickle format, increment the version | |||
| 1964 | number. But we should still be able to handle the old versions. | |||
| 1965 | We've only got one right now. */ | |||
| 1966 | if (version != 1 && version != 0) { | |||
| 1967 | PyErr_Format(PyExc_ValueError, | |||
| 1968 | "can't handle version %d of numpy.ndarray pickle", | |||
| 1969 | version); | |||
| 1970 | return NULL((void*)0); | |||
| 1971 | } | |||
| 1972 | ||||
| 1973 | Py_XDECREF(PyArray_DESCR(self))_Py_XDECREF(((PyObject*)(PyArray_DESCR(self)))); | |||
| 1974 | fa->descr = typecode; | |||
| 1975 | Py_INCREF(typecode)_Py_INCREF(((PyObject*)(typecode))); | |||
| 1976 | nd = PyArray_IntpFromSequence(shape, dimensions, NPY_MAXDIMS32); | |||
| 1977 | if (nd < 0) { | |||
| 1978 | return NULL((void*)0); | |||
| 1979 | } | |||
| 1980 | size = PyArray_MultiplyList(dimensions, nd); | |||
| 1981 | if (size < 0) { | |||
| 1982 | /* More items than are addressable */ | |||
| 1983 | return PyErr_NoMemory(); | |||
| 1984 | } | |||
| 1985 | overflowed = npy_mul_with_overflow_intp( | |||
| 1986 | &nbytes, size, PyArray_DESCR(self)->elsize); | |||
| 1987 | if (overflowed) { | |||
| 1988 | /* More bytes than are addressable */ | |||
| 1989 | return PyErr_NoMemory(); | |||
| 1990 | } | |||
| 1991 | ||||
| 1992 | if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)(((typecode)->flags & (0x02)) == (0x02))) { | |||
| 1993 | if (!PyList_Check(rawdata)((((((PyObject*)(rawdata))->ob_type))->tp_flags & ( (1UL << 25))) != 0)) { | |||
| 1994 | PyErr_SetString(PyExc_TypeError, | |||
| 1995 | "object pickle not returning list"); | |||
| 1996 | return NULL((void*)0); | |||
| 1997 | } | |||
| 1998 | } | |||
| 1999 | else { | |||
| 2000 | Py_INCREF(rawdata)_Py_INCREF(((PyObject*)(rawdata))); | |||
| 2001 | ||||
| 2002 | /* Backward compatibility with Python 2 NumPy pickles */ | |||
| 2003 | if (PyUnicode_Check(rawdata)((((((PyObject*)(rawdata))->ob_type))->tp_flags & ( (1UL << 28))) != 0)) { | |||
| 2004 | PyObject *tmp; | |||
| 2005 | tmp = PyUnicode_AsLatin1String(rawdata); | |||
| ||||
| 2006 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2007 | rawdata = tmp; | |||
| 2008 | if (tmp == NULL((void*)0)) { | |||
| 2009 | /* More informative error message */ | |||
| 2010 | PyErr_SetString(PyExc_ValueError, | |||
| 2011 | ("Failed to encode latin1 string when unpickling a Numpy array. " | |||
| 2012 | "pickle.load(a, encoding='latin1') is assumed.")); | |||
| 2013 | return NULL((void*)0); | |||
| 2014 | } | |||
| 2015 | } | |||
| 2016 | ||||
| 2017 | if (!PyBytes_Check(rawdata)((((((PyObject*)(rawdata))->ob_type))->tp_flags & ( (1UL << 27))) != 0)) { | |||
| 2018 | PyErr_SetString(PyExc_TypeError, | |||
| 2019 | "pickle not returning string"); | |||
| 2020 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2021 | return NULL((void*)0); | |||
| 2022 | } | |||
| 2023 | ||||
| 2024 | if (PyBytes_AsStringAndSize(rawdata, &datastr, &len) < 0) { | |||
| 2025 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2026 | return NULL((void*)0); | |||
| 2027 | } | |||
| 2028 | ||||
| 2029 | if (len != nbytes) { | |||
| 2030 | PyErr_SetString(PyExc_ValueError, | |||
| 2031 | "buffer size does not" \ | |||
| 2032 | " match array size"); | |||
| 2033 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2034 | return NULL((void*)0); | |||
| 2035 | } | |||
| 2036 | } | |||
| 2037 | ||||
| 2038 | if ((PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA0x0004)) { | |||
| 2039 | PyDataMem_FREE(PyArray_DATA(self)); | |||
| 2040 | PyArray_CLEARFLAGS(self, NPY_ARRAY_OWNDATA0x0004); | |||
| 2041 | } | |||
| 2042 | Py_XDECREF(PyArray_BASE(self))_Py_XDECREF(((PyObject*)(PyArray_BASE(self)))); | |||
| 2043 | fa->base = NULL((void*)0); | |||
| 2044 | ||||
| 2045 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WRITEBACKIFCOPY0x2000); | |||
| 2046 | PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY0x1000); | |||
| 2047 | ||||
| 2048 | if (PyArray_DIMS(self) != NULL((void*)0)) { | |||
| 2049 | npy_free_cache_dim_array(self); | |||
| 2050 | fa->dimensions = NULL((void*)0); | |||
| 2051 | } | |||
| 2052 | ||||
| 2053 | fa->flags = NPY_ARRAY_DEFAULT((0x0001 | (0x0100 | 0x0400))); | |||
| 2054 | ||||
| 2055 | fa->nd = nd; | |||
| 2056 | ||||
| 2057 | if (nd > 0) { | |||
| 2058 | fa->dimensions = npy_alloc_cache_dim(2 * nd); | |||
| 2059 | if (fa->dimensions == NULL((void*)0)) { | |||
| 2060 | return PyErr_NoMemory(); | |||
| 2061 | } | |||
| 2062 | fa->strides = PyArray_DIMS(self) + nd; | |||
| 2063 | if (nd) { | |||
| 2064 | memcpy(PyArray_DIMS(self), dimensions, sizeof(npy_intp)*nd); | |||
| 2065 | } | |||
| 2066 | _array_fill_strides(PyArray_STRIDES(self), dimensions, nd, | |||
| 2067 | PyArray_DESCR(self)->elsize, | |||
| 2068 | (is_f_order ? NPY_ARRAY_F_CONTIGUOUS0x0002 : | |||
| 2069 | NPY_ARRAY_C_CONTIGUOUS0x0001), | |||
| 2070 | &(fa->flags)); | |||
| 2071 | } | |||
| 2072 | ||||
| 2073 | if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)(((typecode)->flags & (0x02)) == (0x02))) { | |||
| 2074 | int swap = PyArray_ISBYTESWAPPED(self)(!((PyArray_DESCR(self)->byteorder) != '>')); | |||
| 2075 | fa->data = datastr; | |||
| 2076 | /* Bytes should always be considered immutable, but we just grab the | |||
| 2077 | * pointer if they are large, to save memory. */ | |||
| 2078 | if (!IsAligned(self) || swap || (len <= 1000)) { | |||
| 2079 | npy_intp num = PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self))); | |||
| 2080 | if (num == 0) { | |||
| 2081 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2082 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 2083 | } | |||
| 2084 | fa->data = PyDataMem_NEW(num); | |||
| 2085 | if (PyArray_DATA(self) == NULL((void*)0)) { | |||
| 2086 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2087 | return PyErr_NoMemory(); | |||
| 2088 | } | |||
| 2089 | if (swap) { | |||
| 2090 | /* byte-swap on pickle-read */ | |||
| 2091 | npy_intp numels = PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)); | |||
| 2092 | PyArray_DESCR(self)->f->copyswapn(PyArray_DATA(self), | |||
| 2093 | PyArray_DESCR(self)->elsize, | |||
| 2094 | datastr, PyArray_DESCR(self)->elsize, | |||
| 2095 | numels, 1, self); | |||
| 2096 | if (!(PyArray_ISEXTENDED(self)((((PyArray_TYPE(self)) >=NPY_STRING) && ((PyArray_TYPE (self)) <=NPY_VOID)) || (((PyArray_TYPE(self)) >= NPY_USERDEF ) && ((PyArray_TYPE(self)) < NPY_USERDEF+ NPY_NUMUSERTYPES ))) || | |||
| 2097 | PyArray_DESCR(self)->metadata || | |||
| 2098 | PyArray_DESCR(self)->c_metadata)) { | |||
| 2099 | fa->descr = PyArray_DescrFromType( | |||
| 2100 | PyArray_DESCR(self)->type_num); | |||
| 2101 | } | |||
| 2102 | else { | |||
| 2103 | fa->descr = PyArray_DescrNew(typecode); | |||
| 2104 | if (PyArray_DESCR(self)->byteorder == NPY_BIG'>') { | |||
| 2105 | PyArray_DESCR(self)->byteorder = NPY_LITTLE'<'; | |||
| 2106 | } | |||
| 2107 | else if (PyArray_DESCR(self)->byteorder == NPY_LITTLE'<') { | |||
| 2108 | PyArray_DESCR(self)->byteorder = NPY_BIG'>'; | |||
| 2109 | } | |||
| 2110 | } | |||
| 2111 | Py_DECREF(typecode)_Py_DECREF(((PyObject*)(typecode))); | |||
| 2112 | } | |||
| 2113 | else { | |||
| 2114 | memcpy(PyArray_DATA(self), datastr, num); | |||
| 2115 | } | |||
| 2116 | PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA0x0004); | |||
| 2117 | fa->base = NULL((void*)0); | |||
| 2118 | Py_DECREF(rawdata)_Py_DECREF(((PyObject*)(rawdata))); | |||
| 2119 | } | |||
| 2120 | else { | |||
| 2121 | if (PyArray_SetBaseObject(self, rawdata) < 0) { | |||
| 2122 | return NULL((void*)0); | |||
| 2123 | } | |||
| 2124 | } | |||
| 2125 | } | |||
| 2126 | else { | |||
| 2127 | npy_intp num = PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self))); | |||
| 2128 | int elsize = PyArray_DESCR(self)->elsize; | |||
| 2129 | if (num == 0 || elsize == 0) { | |||
| 2130 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 2131 | } | |||
| 2132 | fa->data = PyDataMem_NEW(num); | |||
| 2133 | if (PyArray_DATA(self) == NULL((void*)0)) { | |||
| 2134 | return PyErr_NoMemory(); | |||
| 2135 | } | |||
| 2136 | if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_NEEDS_INIT)(((PyArray_DESCR(self))->flags & (0x08)) == (0x08))) { | |||
| 2137 | memset(PyArray_DATA(self), 0, PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self)))); | |||
| 2138 | } | |||
| 2139 | PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA0x0004); | |||
| 2140 | fa->base = NULL((void*)0); | |||
| 2141 | if (_setlist_pkl(self, rawdata) < 0) { | |||
| 2142 | return NULL((void*)0); | |||
| 2143 | } | |||
| 2144 | } | |||
| 2145 | ||||
| 2146 | PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL(0x0001 | 0x0002 | 0x0100)); | |||
| 2147 | ||||
| 2148 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 2149 | } | |||
| 2150 | ||||
| 2151 | /*NUMPY_API*/ | |||
| 2152 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) int | |||
| 2153 | PyArray_Dump(PyObject *self, PyObject *file, int protocol) | |||
| 2154 | { | |||
| 2155 | static PyObject *method = NULL((void*)0); | |||
| 2156 | PyObject *ret; | |||
| 2157 | npy_cache_import("numpy.core._methods", "_dump", &method); | |||
| 2158 | if (method == NULL((void*)0)) { | |||
| 2159 | return -1; | |||
| 2160 | } | |||
| 2161 | if (protocol < 0) { | |||
| 2162 | ret = PyObject_CallFunction_PyObject_CallFunction_SizeT(method, "OO", self, file); | |||
| 2163 | } | |||
| 2164 | else { | |||
| 2165 | ret = PyObject_CallFunction_PyObject_CallFunction_SizeT(method, "OOi", self, file, protocol); | |||
| 2166 | } | |||
| 2167 | if (ret == NULL((void*)0)) { | |||
| 2168 | return -1; | |||
| 2169 | } | |||
| 2170 | Py_DECREF(ret)_Py_DECREF(((PyObject*)(ret))); | |||
| 2171 | return 0; | |||
| 2172 | } | |||
| 2173 | ||||
| 2174 | /*NUMPY_API*/ | |||
| 2175 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyObject * | |||
| 2176 | PyArray_Dumps(PyObject *self, int protocol) | |||
| 2177 | { | |||
| 2178 | static PyObject *method = NULL((void*)0); | |||
| 2179 | npy_cache_import("numpy.core._methods", "_dumps", &method); | |||
| 2180 | if (method == NULL((void*)0)) { | |||
| 2181 | return NULL((void*)0); | |||
| 2182 | } | |||
| 2183 | if (protocol < 0) { | |||
| 2184 | return PyObject_CallFunction_PyObject_CallFunction_SizeT(method, "O", self); | |||
| 2185 | } | |||
| 2186 | else { | |||
| 2187 | return PyObject_CallFunction_PyObject_CallFunction_SizeT(method, "Oi", self, protocol); | |||
| 2188 | } | |||
| 2189 | } | |||
| 2190 | ||||
| 2191 | ||||
| 2192 | static PyObject * | |||
| 2193 | array_dump(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2194 | { | |||
| 2195 | NPY_FORWARD_NDARRAY_METHOD("_dump")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_dump", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | ||||
| 2199 | static PyObject * | |||
| 2200 | array_dumps(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2201 | { | |||
| 2202 | NPY_FORWARD_NDARRAY_METHOD("_dumps")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_dumps", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2203 | } | |||
| 2204 | ||||
| 2205 | ||||
| 2206 | static PyObject * | |||
| 2207 | array_sizeof(PyArrayObject *self) | |||
| 2208 | { | |||
| 2209 | /* object + dimension and strides */ | |||
| 2210 | Py_ssize_t nbytes = Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_basicsize + | |||
| 2211 | PyArray_NDIM(self) * sizeof(npy_intp) * 2; | |||
| 2212 | if (PyArray_CHKFLAGS(self, NPY_ARRAY_OWNDATA0x0004)) { | |||
| 2213 | nbytes += PyArray_NBYTES(self)(PyArray_ITEMSIZE(self) * PyArray_MultiplyList(PyArray_DIMS(self ), PyArray_NDIM(self))); | |||
| 2214 | } | |||
| 2215 | return PyLong_FromSsize_t(nbytes); | |||
| 2216 | } | |||
| 2217 | ||||
| 2218 | ||||
| 2219 | static PyObject * | |||
| 2220 | array_transpose(PyArrayObject *self, PyObject *args) | |||
| 2221 | { | |||
| 2222 | PyObject *shape = Py_None(&_Py_NoneStruct); | |||
| 2223 | Py_ssize_t n = PyTuple_Size(args); | |||
| 2224 | PyArray_Dims permute; | |||
| 2225 | PyObject *ret; | |||
| 2226 | ||||
| 2227 | if (n > 1) { | |||
| 2228 | shape = args; | |||
| 2229 | } | |||
| 2230 | else if (n == 1) { | |||
| 2231 | shape = PyTuple_GET_ITEM(args, 0)((((void) (0)), (PyTupleObject *)(args))->ob_item[0]); | |||
| 2232 | } | |||
| 2233 | ||||
| 2234 | if (shape == Py_None(&_Py_NoneStruct)) { | |||
| 2235 | ret = PyArray_Transpose(self, NULL((void*)0)); | |||
| 2236 | } | |||
| 2237 | else { | |||
| 2238 | if (!PyArray_IntpConverter(shape, &permute)) { | |||
| 2239 | return NULL((void*)0); | |||
| 2240 | } | |||
| 2241 | ret = PyArray_Transpose(self, &permute); | |||
| 2242 | npy_free_cache_dim_obj(permute); | |||
| 2243 | } | |||
| 2244 | ||||
| 2245 | return ret; | |||
| 2246 | } | |||
| 2247 | ||||
| 2248 | #define _CHKTYPENUM(typ) ((typ) ? (typ)->type_num : NPY_NOTYPE) | |||
| 2249 | ||||
| 2250 | static PyObject * | |||
| 2251 | array_mean(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2252 | { | |||
| 2253 | NPY_FORWARD_NDARRAY_METHOD("_mean")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_mean", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2254 | } | |||
| 2255 | ||||
| 2256 | static PyObject * | |||
| 2257 | array_sum(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2258 | { | |||
| 2259 | NPY_FORWARD_NDARRAY_METHOD("_sum")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_sum", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2260 | } | |||
| 2261 | ||||
| 2262 | ||||
| 2263 | static PyObject * | |||
| 2264 | array_cumsum(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2265 | { | |||
| 2266 | int axis = NPY_MAXDIMS32; | |||
| 2267 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 2268 | PyArrayObject *out = NULL((void*)0); | |||
| 2269 | int rtype; | |||
| 2270 | static char *kwlist[] = {"axis", "dtype", "out", NULL((void*)0)}; | |||
| 2271 | ||||
| 2272 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|O&O&O&:cumsum", kwlist, | |||
| 2273 | PyArray_AxisConverter, &axis, | |||
| 2274 | PyArray_DescrConverter2, &dtype, | |||
| 2275 | PyArray_OutputConverter, &out)) { | |||
| 2276 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2277 | return NULL((void*)0); | |||
| 2278 | } | |||
| 2279 | ||||
| 2280 | rtype = _CHKTYPENUM(dtype); | |||
| 2281 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2282 | return PyArray_CumSum(self, axis, rtype, out); | |||
| 2283 | } | |||
| 2284 | ||||
| 2285 | static PyObject * | |||
| 2286 | array_prod(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2287 | { | |||
| 2288 | NPY_FORWARD_NDARRAY_METHOD("_prod")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_prod", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2289 | } | |||
| 2290 | ||||
| 2291 | static PyObject * | |||
| 2292 | array_cumprod(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2293 | { | |||
| 2294 | int axis = NPY_MAXDIMS32; | |||
| 2295 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 2296 | PyArrayObject *out = NULL((void*)0); | |||
| 2297 | int rtype; | |||
| 2298 | static char *kwlist[] = {"axis", "dtype", "out", NULL((void*)0)}; | |||
| 2299 | ||||
| 2300 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|O&O&O&:cumprod", kwlist, | |||
| 2301 | PyArray_AxisConverter, &axis, | |||
| 2302 | PyArray_DescrConverter2, &dtype, | |||
| 2303 | PyArray_OutputConverter, &out)) { | |||
| 2304 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2305 | return NULL((void*)0); | |||
| 2306 | } | |||
| 2307 | ||||
| 2308 | rtype = _CHKTYPENUM(dtype); | |||
| 2309 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2310 | return PyArray_CumProd(self, axis, rtype, out); | |||
| 2311 | } | |||
| 2312 | ||||
| 2313 | ||||
| 2314 | static PyObject * | |||
| 2315 | array_dot(PyArrayObject *self, | |||
| 2316 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 2317 | { | |||
| 2318 | PyObject *a = (PyObject *)self, *b, *o = NULL((void*)0); | |||
| 2319 | PyArrayObject *ret; | |||
| 2320 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 2321 | ||||
| 2322 | if (npy_parse_arguments("dot", args, len_args, kwnames,_npy_parse_arguments("dot", &__argparse_cache, args, len_args , kwnames, "b", ((void*)0), &b, "|out", ((void*)0), & o, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2323 | "b", NULL, &b,_npy_parse_arguments("dot", &__argparse_cache, args, len_args , kwnames, "b", ((void*)0), &b, "|out", ((void*)0), & o, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2324 | "|out", NULL, &o,_npy_parse_arguments("dot", &__argparse_cache, args, len_args , kwnames, "b", ((void*)0), &b, "|out", ((void*)0), & o, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2325 | NULL, NULL, NULL)_npy_parse_arguments("dot", &__argparse_cache, args, len_args , kwnames, "b", ((void*)0), &b, "|out", ((void*)0), & o, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 2326 | return NULL((void*)0); | |||
| 2327 | } | |||
| 2328 | ||||
| 2329 | if (o != NULL((void*)0)) { | |||
| 2330 | if (o == Py_None(&_Py_NoneStruct)) { | |||
| 2331 | o = NULL((void*)0); | |||
| 2332 | } | |||
| 2333 | else if (!PyArray_Check(o)((((PyObject*)(o))->ob_type) == (&PyArray_Type) || PyType_IsSubtype ((((PyObject*)(o))->ob_type), (&PyArray_Type)))) { | |||
| 2334 | PyErr_SetString(PyExc_TypeError, | |||
| 2335 | "'out' must be an array"); | |||
| 2336 | return NULL((void*)0); | |||
| 2337 | } | |||
| 2338 | } | |||
| 2339 | ret = (PyArrayObject *)PyArray_MatrixProduct2(a, b, (PyArrayObject *)o); | |||
| 2340 | return PyArray_Return(ret); | |||
| 2341 | } | |||
| 2342 | ||||
| 2343 | ||||
| 2344 | static PyObject * | |||
| 2345 | array_any(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2346 | { | |||
| 2347 | NPY_FORWARD_NDARRAY_METHOD("_any")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_any", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2348 | } | |||
| 2349 | ||||
| 2350 | ||||
| 2351 | static PyObject * | |||
| 2352 | array_all(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2353 | { | |||
| 2354 | NPY_FORWARD_NDARRAY_METHOD("_all")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_all", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2355 | } | |||
| 2356 | ||||
| 2357 | static PyObject * | |||
| 2358 | array_stddev(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2359 | { | |||
| 2360 | NPY_FORWARD_NDARRAY_METHOD("_std")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_std", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2361 | } | |||
| 2362 | ||||
| 2363 | static PyObject * | |||
| 2364 | array_variance(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2365 | { | |||
| 2366 | NPY_FORWARD_NDARRAY_METHOD("_var")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_var", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2367 | } | |||
| 2368 | ||||
| 2369 | static PyObject * | |||
| 2370 | array_compress(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2371 | { | |||
| 2372 | int axis = NPY_MAXDIMS32; | |||
| 2373 | PyObject *condition; | |||
| 2374 | PyArrayObject *out = NULL((void*)0); | |||
| 2375 | static char *kwlist[] = {"condition", "axis", "out", NULL((void*)0)}; | |||
| 2376 | ||||
| 2377 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "O|O&O&:compress", kwlist, | |||
| 2378 | &condition, | |||
| 2379 | PyArray_AxisConverter, &axis, | |||
| 2380 | PyArray_OutputConverter, &out)) { | |||
| 2381 | return NULL((void*)0); | |||
| 2382 | } | |||
| 2383 | ||||
| 2384 | PyObject *ret = PyArray_Compress(self, condition, axis, out); | |||
| 2385 | ||||
| 2386 | /* this matches the unpacking behavior of ufuncs */ | |||
| 2387 | if (out == NULL((void*)0)) { | |||
| 2388 | return PyArray_Return((PyArrayObject *)ret); | |||
| 2389 | } | |||
| 2390 | else { | |||
| 2391 | return ret; | |||
| 2392 | } | |||
| 2393 | } | |||
| 2394 | ||||
| 2395 | ||||
| 2396 | static PyObject * | |||
| 2397 | array_nonzero(PyArrayObject *self, PyObject *args) | |||
| 2398 | { | |||
| 2399 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "")) { | |||
| 2400 | return NULL((void*)0); | |||
| 2401 | } | |||
| 2402 | return PyArray_Nonzero(self); | |||
| 2403 | } | |||
| 2404 | ||||
| 2405 | ||||
| 2406 | static PyObject * | |||
| 2407 | array_trace(PyArrayObject *self, | |||
| 2408 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 2409 | { | |||
| 2410 | int axis1 = 0, axis2 = 1, offset = 0; | |||
| 2411 | PyArray_Descr *dtype = NULL((void*)0); | |||
| 2412 | PyArrayObject *out = NULL((void*)0); | |||
| 2413 | int rtype; | |||
| 2414 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 2415 | ||||
| 2416 | if (npy_parse_arguments("trace", args, len_args, kwnames,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2417 | "|offset", &PyArray_PythonPyIntFromInt, &offset,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2418 | "|axis1", &PyArray_PythonPyIntFromInt, &axis1,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2419 | "|axis2", &PyArray_PythonPyIntFromInt, &axis2,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2420 | "|dtype", &PyArray_DescrConverter2, &dtype,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2421 | "|out", &PyArray_OutputConverter, &out,_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) | |||
| 2422 | NULL, NULL, NULL)_npy_parse_arguments("trace", &__argparse_cache, args, len_args , kwnames, "|offset", &PyArray_PythonPyIntFromInt, &offset , "|axis1", &PyArray_PythonPyIntFromInt, &axis1, "|axis2" , &PyArray_PythonPyIntFromInt, &axis2, "|dtype", & PyArray_DescrConverter2, &dtype, "|out", &PyArray_OutputConverter , &out, ((void*)0), ((void*)0), ((void*)0)) < 0) { | |||
| 2423 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2424 | return NULL((void*)0); | |||
| 2425 | } | |||
| 2426 | ||||
| 2427 | rtype = _CHKTYPENUM(dtype); | |||
| 2428 | Py_XDECREF(dtype)_Py_XDECREF(((PyObject*)(dtype))); | |||
| 2429 | PyObject *ret = PyArray_Trace(self, offset, axis1, axis2, rtype, out); | |||
| 2430 | ||||
| 2431 | /* this matches the unpacking behavior of ufuncs */ | |||
| 2432 | if (out == NULL((void*)0)) { | |||
| 2433 | return PyArray_Return((PyArrayObject *)ret); | |||
| 2434 | } | |||
| 2435 | else { | |||
| 2436 | return ret; | |||
| 2437 | } | |||
| 2438 | } | |||
| 2439 | ||||
| 2440 | #undef _CHKTYPENUM | |||
| 2441 | ||||
| 2442 | ||||
| 2443 | static PyObject * | |||
| 2444 | array_clip(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2445 | { | |||
| 2446 | NPY_FORWARD_NDARRAY_METHOD("_clip")static PyObject *callable = ((void*)0); npy_cache_import("numpy.core._methods" , "_clip", &callable); if (callable == ((void*)0)) { return ((void*)0); } return forward_ndarray_method(self, args, kwds , callable); | |||
| 2447 | } | |||
| 2448 | ||||
| 2449 | ||||
| 2450 | static PyObject * | |||
| 2451 | array_conjugate(PyArrayObject *self, PyObject *args) | |||
| 2452 | { | |||
| 2453 | PyArrayObject *out = NULL((void*)0); | |||
| 2454 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O&:conjugate", | |||
| 2455 | PyArray_OutputConverter, | |||
| 2456 | &out)) { | |||
| 2457 | return NULL((void*)0); | |||
| 2458 | } | |||
| 2459 | return PyArray_Conjugate(self, out); | |||
| 2460 | } | |||
| 2461 | ||||
| 2462 | ||||
| 2463 | static PyObject * | |||
| 2464 | array_diagonal(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2465 | { | |||
| 2466 | int axis1 = 0, axis2 = 1, offset = 0; | |||
| 2467 | static char *kwlist[] = {"offset", "axis1", "axis2", NULL((void*)0)}; | |||
| 2468 | PyArrayObject *ret; | |||
| 2469 | ||||
| 2470 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|iii:diagonal", kwlist, | |||
| 2471 | &offset, | |||
| 2472 | &axis1, | |||
| 2473 | &axis2)) { | |||
| 2474 | return NULL((void*)0); | |||
| 2475 | } | |||
| 2476 | ||||
| 2477 | ret = (PyArrayObject *)PyArray_Diagonal(self, offset, axis1, axis2); | |||
| 2478 | return PyArray_Return(ret); | |||
| 2479 | } | |||
| 2480 | ||||
| 2481 | ||||
| 2482 | static PyObject * | |||
| 2483 | array_flatten(PyArrayObject *self, | |||
| 2484 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 2485 | { | |||
| 2486 | NPY_ORDER order = NPY_CORDER; | |||
| 2487 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 2488 | ||||
| 2489 | if (npy_parse_arguments("flatten", args, len_args, kwnames,_npy_parse_arguments("flatten", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 2490 | "|order", PyArray_OrderConverter, &order,_npy_parse_arguments("flatten", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 2491 | NULL, NULL, NULL)_npy_parse_arguments("flatten", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) < 0) { | |||
| 2492 | return NULL((void*)0); | |||
| 2493 | } | |||
| 2494 | return PyArray_Flatten(self, order); | |||
| 2495 | } | |||
| 2496 | ||||
| 2497 | ||||
| 2498 | static PyObject * | |||
| 2499 | array_ravel(PyArrayObject *self, | |||
| 2500 | PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames) | |||
| 2501 | { | |||
| 2502 | NPY_ORDER order = NPY_CORDER; | |||
| 2503 | NPY_PREPARE_ARGPARSERstatic _NpyArgParserCache __argparse_cache = {-1}; | |||
| 2504 | ||||
| 2505 | if (npy_parse_arguments("ravel", args, len_args, kwnames,_npy_parse_arguments("ravel", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 2506 | "|order", PyArray_OrderConverter, &order,_npy_parse_arguments("ravel", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) | |||
| 2507 | NULL, NULL, NULL)_npy_parse_arguments("ravel", &__argparse_cache, args, len_args , kwnames, "|order", PyArray_OrderConverter, &order, ((void *)0), ((void*)0), ((void*)0)) < 0) { | |||
| 2508 | return NULL((void*)0); | |||
| 2509 | } | |||
| 2510 | return PyArray_Ravel(self, order); | |||
| 2511 | } | |||
| 2512 | ||||
| 2513 | ||||
| 2514 | static PyObject * | |||
| 2515 | array_round(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2516 | { | |||
| 2517 | int decimals = 0; | |||
| 2518 | PyArrayObject *out = NULL((void*)0); | |||
| 2519 | static char *kwlist[] = {"decimals", "out", NULL((void*)0)}; | |||
| 2520 | ||||
| 2521 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|iO&:round", kwlist, | |||
| 2522 | &decimals, | |||
| 2523 | PyArray_OutputConverter, &out)) { | |||
| 2524 | return NULL((void*)0); | |||
| 2525 | } | |||
| 2526 | ||||
| 2527 | PyObject *ret = PyArray_Round(self, decimals, out); | |||
| 2528 | ||||
| 2529 | /* this matches the unpacking behavior of ufuncs */ | |||
| 2530 | if (out == NULL((void*)0)) { | |||
| 2531 | return PyArray_Return((PyArrayObject *)ret); | |||
| 2532 | } | |||
| 2533 | else { | |||
| 2534 | return ret; | |||
| 2535 | } | |||
| 2536 | } | |||
| 2537 | ||||
| 2538 | ||||
| 2539 | ||||
| 2540 | static PyObject * | |||
| 2541 | array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) | |||
| 2542 | { | |||
| 2543 | static char *kwlist[] = {"write", "align", "uic", NULL((void*)0)}; | |||
| 2544 | PyObject *write_flag = Py_None(&_Py_NoneStruct); | |||
| 2545 | PyObject *align_flag = Py_None(&_Py_NoneStruct); | |||
| 2546 | PyObject *uic = Py_None(&_Py_NoneStruct); | |||
| 2547 | int flagback = PyArray_FLAGS(self); | |||
| 2548 | ||||
| 2549 | PyArrayObject_fields *fa = (PyArrayObject_fields *)self; | |||
| 2550 | ||||
| 2551 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|OOO:setflags", kwlist, | |||
| 2552 | &write_flag, | |||
| 2553 | &align_flag, | |||
| 2554 | &uic)) | |||
| 2555 | return NULL((void*)0); | |||
| 2556 | ||||
| 2557 | if (align_flag != Py_None(&_Py_NoneStruct)) { | |||
| 2558 | if (PyObject_Not(align_flag)) { | |||
| 2559 | PyArray_CLEARFLAGS(self, NPY_ARRAY_ALIGNED0x0100); | |||
| 2560 | } | |||
| 2561 | else if (IsAligned(self)) { | |||
| 2562 | PyArray_ENABLEFLAGS(self, NPY_ARRAY_ALIGNED0x0100); | |||
| 2563 | } | |||
| 2564 | else { | |||
| 2565 | PyErr_SetString(PyExc_ValueError, | |||
| 2566 | "cannot set aligned flag of mis-" | |||
| 2567 | "aligned array to True"); | |||
| 2568 | return NULL((void*)0); | |||
| 2569 | } | |||
| 2570 | } | |||
| 2571 | ||||
| 2572 | if (uic != Py_None(&_Py_NoneStruct)) { | |||
| 2573 | if (PyObject_IsTrue(uic)) { | |||
| 2574 | fa->flags = flagback; | |||
| 2575 | PyErr_SetString(PyExc_ValueError, | |||
| 2576 | "cannot set WRITEBACKIFCOPY " | |||
| 2577 | "flag to True"); | |||
| 2578 | return NULL((void*)0); | |||
| 2579 | } | |||
| 2580 | else { | |||
| 2581 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WRITEBACKIFCOPY0x2000); | |||
| 2582 | PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY0x1000); | |||
| 2583 | Py_XDECREF(fa->base)_Py_XDECREF(((PyObject*)(fa->base))); | |||
| 2584 | fa->base = NULL((void*)0); | |||
| 2585 | } | |||
| 2586 | } | |||
| 2587 | ||||
| 2588 | if (write_flag != Py_None(&_Py_NoneStruct)) { | |||
| 2589 | if (PyObject_IsTrue(write_flag)) { | |||
| 2590 | if (_IsWriteable(self)) { | |||
| 2591 | /* | |||
| 2592 | * _IsWritable (and PyArray_UpdateFlags) allows flipping this, | |||
| 2593 | * although the C-Api user who created the array may have | |||
| 2594 | * chosen to make it non-writable for a good reason, so | |||
| 2595 | * deprecate. | |||
| 2596 | */ | |||
| 2597 | if ((PyArray_BASE(self) == NULL((void*)0)) && | |||
| 2598 | !PyArray_CHKFLAGS(self, NPY_ARRAY_OWNDATA0x0004) && | |||
| 2599 | !PyArray_CHKFLAGS(self, NPY_ARRAY_WRITEABLE0x0400)) { | |||
| 2600 | /* 2017-05-03, NumPy 1.17.0 */ | |||
| 2601 | if (DEPRECATE("making a non-writeable array writeable "PyErr_WarnEx(PyExc_DeprecationWarning,"making a non-writeable array writeable " "is deprecated for arrays without a base " "which do not own their data." ,1) | |||
| 2602 | "is deprecated for arrays without a base "PyErr_WarnEx(PyExc_DeprecationWarning,"making a non-writeable array writeable " "is deprecated for arrays without a base " "which do not own their data." ,1) | |||
| 2603 | "which do not own their data.")PyErr_WarnEx(PyExc_DeprecationWarning,"making a non-writeable array writeable " "is deprecated for arrays without a base " "which do not own their data." ,1) < 0) { | |||
| 2604 | return NULL((void*)0); | |||
| 2605 | } | |||
| 2606 | } | |||
| 2607 | PyArray_ENABLEFLAGS(self, NPY_ARRAY_WRITEABLE0x0400); | |||
| 2608 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WARN_ON_WRITE); | |||
| 2609 | } | |||
| 2610 | else { | |||
| 2611 | fa->flags = flagback; | |||
| 2612 | PyErr_SetString(PyExc_ValueError, | |||
| 2613 | "cannot set WRITEABLE " | |||
| 2614 | "flag to True of this " | |||
| 2615 | "array"); | |||
| 2616 | return NULL((void*)0); | |||
| 2617 | } | |||
| 2618 | } | |||
| 2619 | else { | |||
| 2620 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WRITEABLE0x0400); | |||
| 2621 | PyArray_CLEARFLAGS(self, NPY_ARRAY_WARN_ON_WRITE); | |||
| 2622 | } | |||
| 2623 | } | |||
| 2624 | Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct); | |||
| 2625 | } | |||
| 2626 | ||||
| 2627 | ||||
| 2628 | static PyObject * | |||
| 2629 | array_newbyteorder(PyArrayObject *self, PyObject *args) | |||
| 2630 | { | |||
| 2631 | char endian = NPY_SWAP's'; | |||
| 2632 | PyArray_Descr *new; | |||
| 2633 | ||||
| 2634 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O&:newbyteorder", PyArray_ByteorderConverter, | |||
| 2635 | &endian)) { | |||
| 2636 | return NULL((void*)0); | |||
| 2637 | } | |||
| 2638 | new = PyArray_DescrNewByteorder(PyArray_DESCR(self), endian); | |||
| 2639 | if (!new) { | |||
| 2640 | return NULL((void*)0); | |||
| 2641 | } | |||
| 2642 | return PyArray_View(self, new, NULL((void*)0)); | |||
| 2643 | ||||
| 2644 | } | |||
| 2645 | ||||
| 2646 | static PyObject * | |||
| 2647 | array_complex(PyArrayObject *self, PyObject *NPY_UNUSED(args)(__NPY_UNUSED_TAGGEDargs) __attribute__ ((__unused__))) | |||
| 2648 | { | |||
| 2649 | PyArrayObject *arr; | |||
| 2650 | PyArray_Descr *dtype; | |||
| 2651 | PyObject *c; | |||
| 2652 | ||||
| 2653 | if (PyArray_SIZE(self)PyArray_MultiplyList(PyArray_DIMS(self), PyArray_NDIM(self)) != 1) { | |||
| 2654 | PyErr_SetString(PyExc_TypeError, | |||
| 2655 | "only length-1 arrays can be converted to Python scalars"); | |||
| 2656 | return NULL((void*)0); | |||
| 2657 | } | |||
| 2658 | ||||
| 2659 | dtype = PyArray_DescrFromType(NPY_CDOUBLE); | |||
| 2660 | if (dtype == NULL((void*)0)) { | |||
| 2661 | return NULL((void*)0); | |||
| 2662 | } | |||
| 2663 | ||||
| 2664 | if (!PyArray_CanCastArrayTo(self, dtype, NPY_SAME_KIND_CASTING) && | |||
| 2665 | !(PyArray_TYPE(self) == NPY_OBJECT)) { | |||
| 2666 | PyObject *descr = (PyObject*)PyArray_DESCR(self); | |||
| 2667 | ||||
| 2668 | Py_DECREF(dtype)_Py_DECREF(((PyObject*)(dtype))); | |||
| 2669 | PyErr_Format(PyExc_TypeError, | |||
| 2670 | "Unable to convert %R to complex", descr); | |||
| 2671 | return NULL((void*)0); | |||
| 2672 | } | |||
| 2673 | ||||
| 2674 | if (PyArray_TYPE(self) == NPY_OBJECT) { | |||
| 2675 | /* let python try calling __complex__ on the object. */ | |||
| 2676 | PyObject *args, *res; | |||
| 2677 | ||||
| 2678 | Py_DECREF(dtype)_Py_DECREF(((PyObject*)(dtype))); | |||
| 2679 | args = Py_BuildValue_Py_BuildValue_SizeT("(O)", *((PyObject**)PyArray_DATA(self))); | |||
| 2680 | if (args == NULL((void*)0)) { | |||
| 2681 | return NULL((void*)0); | |||
| 2682 | } | |||
| 2683 | res = PyComplex_Type.tp_new(&PyComplex_Type, args, NULL((void*)0)); | |||
| 2684 | Py_DECREF(args)_Py_DECREF(((PyObject*)(args))); | |||
| 2685 | return res; | |||
| 2686 | } | |||
| 2687 | ||||
| 2688 | arr = (PyArrayObject *)PyArray_CastToType(self, dtype, 0); | |||
| 2689 | if (arr == NULL((void*)0)) { | |||
| 2690 | return NULL((void*)0); | |||
| 2691 | } | |||
| 2692 | c = PyComplex_FromCComplex(*((Py_complex*)PyArray_DATA(arr))); | |||
| 2693 | Py_DECREF(arr)_Py_DECREF(((PyObject*)(arr))); | |||
| 2694 | return c; | |||
| 2695 | } | |||
| 2696 | ||||
| 2697 | NPY_NO_EXPORT__attribute__((visibility("hidden"))) PyMethodDef array_methods[] = { | |||
| 2698 | ||||
| 2699 | /* for subtypes */ | |||
| 2700 | {"__array__", | |||
| 2701 | (PyCFunction)array_getarray, | |||
| 2702 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2703 | {"__array_prepare__", | |||
| 2704 | (PyCFunction)array_preparearray, | |||
| 2705 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2706 | {"__array_wrap__", | |||
| 2707 | (PyCFunction)array_wraparray, | |||
| 2708 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2709 | {"__array_ufunc__", | |||
| 2710 | (PyCFunction)array_ufunc, | |||
| 2711 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2712 | {"__array_function__", | |||
| 2713 | (PyCFunction)array_function, | |||
| 2714 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2715 | ||||
| 2716 | /* for the sys module */ | |||
| 2717 | {"__sizeof__", | |||
| 2718 | (PyCFunction) array_sizeof, | |||
| 2719 | METH_NOARGS0x0004, NULL((void*)0)}, | |||
| 2720 | ||||
| 2721 | /* for the copy module */ | |||
| 2722 | {"__copy__", | |||
| 2723 | (PyCFunction)array_copy_keeporder, | |||
| 2724 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2725 | {"__deepcopy__", | |||
| 2726 | (PyCFunction)array_deepcopy, | |||
| 2727 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2728 | ||||
| 2729 | /* for Pickling */ | |||
| 2730 | {"__reduce__", | |||
| 2731 | (PyCFunction) array_reduce, | |||
| 2732 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2733 | {"__reduce_ex__", | |||
| 2734 | (PyCFunction) array_reduce_ex, | |||
| 2735 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2736 | {"__setstate__", | |||
| 2737 | (PyCFunction) array_setstate, | |||
| 2738 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2739 | {"dumps", | |||
| 2740 | (PyCFunction) array_dumps, | |||
| 2741 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2742 | {"dump", | |||
| 2743 | (PyCFunction) array_dump, | |||
| 2744 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2745 | ||||
| 2746 | {"__complex__", | |||
| 2747 | (PyCFunction) array_complex, | |||
| 2748 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2749 | ||||
| 2750 | {"__format__", | |||
| 2751 | (PyCFunction) array_format, | |||
| 2752 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2753 | ||||
| 2754 | /* Original and Extended methods added 2005 */ | |||
| 2755 | {"all", | |||
| 2756 | (PyCFunction)array_all, | |||
| 2757 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2758 | {"any", | |||
| 2759 | (PyCFunction)array_any, | |||
| 2760 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2761 | {"argmax", | |||
| 2762 | (PyCFunction)array_argmax, | |||
| 2763 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2764 | {"argmin", | |||
| 2765 | (PyCFunction)array_argmin, | |||
| 2766 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2767 | {"argpartition", | |||
| 2768 | (PyCFunction)array_argpartition, | |||
| 2769 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2770 | {"argsort", | |||
| 2771 | (PyCFunction)array_argsort, | |||
| 2772 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2773 | {"astype", | |||
| 2774 | (PyCFunction)array_astype, | |||
| 2775 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2776 | {"byteswap", | |||
| 2777 | (PyCFunction)array_byteswap, | |||
| 2778 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2779 | {"choose", | |||
| 2780 | (PyCFunction)array_choose, | |||
| 2781 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2782 | {"clip", | |||
| 2783 | (PyCFunction)array_clip, | |||
| 2784 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2785 | {"compress", | |||
| 2786 | (PyCFunction)array_compress, | |||
| 2787 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2788 | {"conj", | |||
| 2789 | (PyCFunction)array_conjugate, | |||
| 2790 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2791 | {"conjugate", | |||
| 2792 | (PyCFunction)array_conjugate, | |||
| 2793 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2794 | {"copy", | |||
| 2795 | (PyCFunction)array_copy, | |||
| 2796 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2797 | {"cumprod", | |||
| 2798 | (PyCFunction)array_cumprod, | |||
| 2799 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2800 | {"cumsum", | |||
| 2801 | (PyCFunction)array_cumsum, | |||
| 2802 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2803 | {"diagonal", | |||
| 2804 | (PyCFunction)array_diagonal, | |||
| 2805 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2806 | {"dot", | |||
| 2807 | (PyCFunction)array_dot, | |||
| 2808 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2809 | {"fill", | |||
| 2810 | (PyCFunction)array_fill, | |||
| 2811 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2812 | {"flatten", | |||
| 2813 | (PyCFunction)array_flatten, | |||
| 2814 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2815 | {"getfield", | |||
| 2816 | (PyCFunction)array_getfield, | |||
| 2817 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2818 | {"item", | |||
| 2819 | (PyCFunction)array_toscalar, | |||
| 2820 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2821 | {"itemset", | |||
| 2822 | (PyCFunction) array_setscalar, | |||
| 2823 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2824 | {"max", | |||
| 2825 | (PyCFunction)array_max, | |||
| 2826 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2827 | {"mean", | |||
| 2828 | (PyCFunction)array_mean, | |||
| 2829 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2830 | {"min", | |||
| 2831 | (PyCFunction)array_min, | |||
| 2832 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2833 | {"newbyteorder", | |||
| 2834 | (PyCFunction)array_newbyteorder, | |||
| 2835 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2836 | {"nonzero", | |||
| 2837 | (PyCFunction)array_nonzero, | |||
| 2838 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2839 | {"partition", | |||
| 2840 | (PyCFunction)array_partition, | |||
| 2841 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2842 | {"prod", | |||
| 2843 | (PyCFunction)array_prod, | |||
| 2844 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2845 | {"ptp", | |||
| 2846 | (PyCFunction)array_ptp, | |||
| 2847 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2848 | {"put", | |||
| 2849 | (PyCFunction)array_put, | |||
| 2850 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2851 | {"ravel", | |||
| 2852 | (PyCFunction)array_ravel, | |||
| 2853 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2854 | {"repeat", | |||
| 2855 | (PyCFunction)array_repeat, | |||
| 2856 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2857 | {"reshape", | |||
| 2858 | (PyCFunction)array_reshape, | |||
| 2859 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2860 | {"resize", | |||
| 2861 | (PyCFunction)array_resize, | |||
| 2862 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2863 | {"round", | |||
| 2864 | (PyCFunction)array_round, | |||
| 2865 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2866 | {"searchsorted", | |||
| 2867 | (PyCFunction)array_searchsorted, | |||
| 2868 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2869 | {"setfield", | |||
| 2870 | (PyCFunction)array_setfield, | |||
| 2871 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2872 | {"setflags", | |||
| 2873 | (PyCFunction)array_setflags, | |||
| 2874 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2875 | {"sort", | |||
| 2876 | (PyCFunction)array_sort, | |||
| 2877 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2878 | {"squeeze", | |||
| 2879 | (PyCFunction)array_squeeze, | |||
| 2880 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2881 | {"std", | |||
| 2882 | (PyCFunction)array_stddev, | |||
| 2883 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2884 | {"sum", | |||
| 2885 | (PyCFunction)array_sum, | |||
| 2886 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2887 | {"swapaxes", | |||
| 2888 | (PyCFunction)array_swapaxes, | |||
| 2889 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2890 | {"take", | |||
| 2891 | (PyCFunction)array_take, | |||
| 2892 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2893 | {"tobytes", | |||
| 2894 | (PyCFunction)array_tobytes, | |||
| 2895 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2896 | {"tofile", | |||
| 2897 | (PyCFunction)array_tofile, | |||
| 2898 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2899 | {"tolist", | |||
| 2900 | (PyCFunction)array_tolist, | |||
| 2901 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2902 | {"tostring", | |||
| 2903 | (PyCFunction)array_tostring, | |||
| 2904 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2905 | {"trace", | |||
| 2906 | (PyCFunction)array_trace, | |||
| 2907 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2908 | {"transpose", | |||
| 2909 | (PyCFunction)array_transpose, | |||
| 2910 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
| 2911 | {"var", | |||
| 2912 | (PyCFunction)array_variance, | |||
| 2913 | METH_VARARGS0x0001 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2914 | {"view", | |||
| 2915 | (PyCFunction)array_view, | |||
| 2916 | METH_FASTCALL0x0080 | METH_KEYWORDS0x0002, NULL((void*)0)}, | |||
| 2917 | {NULL((void*)0), NULL((void*)0), 0, NULL((void*)0)} /* sentinel */ | |||
| 2918 | }; |
| 1 | #ifndef PyUnicode_AsLatin1String |
| 2 | struct _object; |
| 3 | typedef struct _object PyObject; |
| 4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
| 5 | PyObject* PyUnicode_AsLatin1String(PyObject *unicode) { |
| 6 | return clang_analyzer_PyObject_New_Reference(); |
| 7 | } |
| 8 | #else |
| 9 | #warning "API PyUnicode_AsLatin1String is defined as a macro." |
| 10 | #endif |