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

Function _array_nonzero

numpy/core/src/multiarray/number.c:825–860  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

823
824
825static int
826_array_nonzero(PyArrayObject *mp)
827{
828 npy_intp n;
829
830 n = PyArray_SIZE(mp);
831 if (n == 1) {
832 int res;
833 if (Py_EnterRecursiveCall(" while converting array to bool")) {
834 return -1;
835 }
836 res = PyArray_DESCR(mp)->f->nonzero(PyArray_DATA(mp), mp);
837 /* nonzero has no way to indicate an error, but one can occur */
838 if (PyErr_Occurred()) {
839 res = -1;
840 }
841 Py_LeaveRecursiveCall();
842 return res;
843 }
844 else if (n == 0) {
845 /* 2017-09-25, 1.14 */
846 if (DEPRECATE("The truth value of an empty array is ambiguous. "
847 "Returning False, but in future this will result in an error. "
848 "Use `array.size > 0` to check that an array is not empty.") < 0) {
849 return -1;
850 }
851 return 0;
852 }
853 else {
854 PyErr_SetString(PyExc_ValueError,
855 "The truth value of an array "
856 "with more than one element is ambiguous. "
857 "Use a.any() or a.all()");
858 return -1;
859 }
860}
861
862/*
863 * Convert the array to a scalar if allowed, and apply the builtin function

Callers

nothing calls this directly

Calls 3

PyArray_DESCRFunction · 0.85
PyArray_DATAFunction · 0.85
nonzeroMethod · 0.80

Tested by

no test coverage detected