MCPcopy Create free account
hub / github.com/numpy/numpy / prepare_ufunc_output

Function prepare_ufunc_output

numpy/core/src/umath/ufunc_object.c:1180–1242  ·  view source on GitHub ↗

* Calls the given __array_prepare__ function on the operand *op, * substituting it in place if a new array is returned and matches * the old one. * * This requires that the dimensions, strides and data type remain * exactly the same, which may be more strict than before. */

Source from the content-addressed store, hash-verified

1178 * exactly the same, which may be more strict than before.
1179 */
1180static int
1181prepare_ufunc_output(PyUFuncObject *ufunc,
1182 PyArrayObject **op,
1183 PyObject *arr_prep,
1184 ufunc_full_args full_args,
1185 int i)
1186{
1187 if (arr_prep != NULL && arr_prep != Py_None) {
1188 PyObject *res;
1189 PyArrayObject *arr;
1190 PyObject *args_tup;
1191
1192 /* Call with the context argument */
1193 args_tup = _get_wrap_prepare_args(full_args);
1194 if (args_tup == NULL) {
1195 return -1;
1196 }
1197 res = PyObject_CallFunction(
1198 arr_prep, "O(OOi)", *op, ufunc, args_tup, i);
1199 Py_DECREF(args_tup);
1200
1201 if (res == NULL) {
1202 return -1;
1203 }
1204 else if (!PyArray_Check(res)) {
1205 PyErr_SetString(PyExc_TypeError,
1206 "__array_prepare__ must return an "
1207 "ndarray or subclass thereof");
1208 Py_DECREF(res);
1209 return -1;
1210 }
1211 arr = (PyArrayObject *)res;
1212
1213 /* If the same object was returned, nothing to do */
1214 if (arr == *op) {
1215 Py_DECREF(arr);
1216 }
1217 /* If the result doesn't match, throw an error */
1218 else if (PyArray_NDIM(arr) != PyArray_NDIM(*op) ||
1219 !PyArray_CompareLists(PyArray_DIMS(arr),
1220 PyArray_DIMS(*op),
1221 PyArray_NDIM(arr)) ||
1222 !PyArray_CompareLists(PyArray_STRIDES(arr),
1223 PyArray_STRIDES(*op),
1224 PyArray_NDIM(arr)) ||
1225 !PyArray_EquivTypes(PyArray_DESCR(arr),
1226 PyArray_DESCR(*op))) {
1227 PyErr_SetString(PyExc_TypeError,
1228 "__array_prepare__ must return an "
1229 "ndarray or subclass thereof which is "
1230 "otherwise identical to its input");
1231 Py_DECREF(arr);
1232 return -1;
1233 }
1234 /* Replace the op value */
1235 else {
1236 Py_DECREF(*op);
1237 *op = arr;

Callers 2

execute_ufunc_loopFunction · 0.85

Calls 7

_get_wrap_prepare_argsFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_CompareListsFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_EquivTypesFunction · 0.85
PyArray_DESCRFunction · 0.85

Tested by

no test coverage detected