* Apply the __array_wrap__ function with the given array and content. * * Interprets wrap=None and wrap=NULL as intended by _find_array_wrap * * Steals a reference to obj and wrap. * Pass context=NULL to indicate there is no context. */
| 480 | * Pass context=NULL to indicate there is no context. |
| 481 | */ |
| 482 | static PyObject * |
| 483 | _apply_array_wrap( |
| 484 | PyObject *wrap, PyArrayObject *obj, _ufunc_context const *context) { |
| 485 | if (wrap == NULL) { |
| 486 | /* default behavior */ |
| 487 | return PyArray_Return(obj); |
| 488 | } |
| 489 | else if (wrap == Py_None) { |
| 490 | Py_DECREF(wrap); |
| 491 | return (PyObject *)obj; |
| 492 | } |
| 493 | else { |
| 494 | PyObject *res; |
| 495 | PyObject *py_context = NULL; |
| 496 | |
| 497 | /* Convert the context object to a tuple, if present */ |
| 498 | if (context == NULL) { |
| 499 | py_context = Py_None; |
| 500 | Py_INCREF(py_context); |
| 501 | } |
| 502 | else { |
| 503 | PyObject *args_tup; |
| 504 | /* Call the method with appropriate context */ |
| 505 | args_tup = _get_wrap_prepare_args(context->args); |
| 506 | if (args_tup == NULL) { |
| 507 | goto fail; |
| 508 | } |
| 509 | py_context = Py_BuildValue("OOi", |
| 510 | context->ufunc, args_tup, context->out_i); |
| 511 | Py_DECREF(args_tup); |
| 512 | if (py_context == NULL) { |
| 513 | goto fail; |
| 514 | } |
| 515 | } |
| 516 | /* try __array_wrap__(obj, context) */ |
| 517 | res = PyObject_CallFunctionObjArgs(wrap, obj, py_context, NULL); |
| 518 | Py_DECREF(py_context); |
| 519 | |
| 520 | /* try __array_wrap__(obj) if the context argument is not accepted */ |
| 521 | if (res == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 522 | PyErr_Clear(); |
| 523 | res = PyObject_CallFunctionObjArgs(wrap, obj, NULL); |
| 524 | } |
| 525 | Py_DECREF(wrap); |
| 526 | Py_DECREF(obj); |
| 527 | return res; |
| 528 | fail: |
| 529 | Py_DECREF(wrap); |
| 530 | Py_DECREF(obj); |
| 531 | return NULL; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /*UFUNC_API |
no test coverage detected