NUMPY_API * Nonzero * * TODO: In NumPy 2.0, should make the iteration order a parameter. */
| 2767 | * TODO: In NumPy 2.0, should make the iteration order a parameter. |
| 2768 | */ |
| 2769 | NPY_NO_EXPORT PyObject * |
| 2770 | PyArray_Nonzero(PyArrayObject *self) |
| 2771 | { |
| 2772 | int i, ndim = PyArray_NDIM(self); |
| 2773 | PyArrayObject *ret = NULL; |
| 2774 | PyObject *ret_tuple; |
| 2775 | npy_intp ret_dims[2]; |
| 2776 | |
| 2777 | PyArray_NonzeroFunc *nonzero; |
| 2778 | PyArray_Descr *dtype; |
| 2779 | |
| 2780 | npy_intp nonzero_count; |
| 2781 | npy_intp added_count = 0; |
| 2782 | int needs_api; |
| 2783 | int is_bool; |
| 2784 | |
| 2785 | NpyIter *iter; |
| 2786 | NpyIter_IterNextFunc *iternext; |
| 2787 | NpyIter_GetMultiIndexFunc *get_multi_index; |
| 2788 | char **dataptr; |
| 2789 | |
| 2790 | dtype = PyArray_DESCR(self); |
| 2791 | nonzero = dtype->f->nonzero; |
| 2792 | needs_api = PyDataType_FLAGCHK(dtype, NPY_NEEDS_PYAPI); |
| 2793 | |
| 2794 | /* Special case - nonzero(zero_d) is nonzero(atleast_1d(zero_d)) */ |
| 2795 | if (ndim == 0) { |
| 2796 | char const* msg; |
| 2797 | if (PyArray_ISBOOL(self)) { |
| 2798 | msg = |
| 2799 | "Calling nonzero on 0d arrays is deprecated, as it behaves " |
| 2800 | "surprisingly. Use `atleast_1d(cond).nonzero()` if the old " |
| 2801 | "behavior was intended. If the context of this warning is of " |
| 2802 | "the form `arr[nonzero(cond)]`, just use `arr[cond]`."; |
| 2803 | } |
| 2804 | else { |
| 2805 | msg = |
| 2806 | "Calling nonzero on 0d arrays is deprecated, as it behaves " |
| 2807 | "surprisingly. Use `atleast_1d(arr).nonzero()` if the old " |
| 2808 | "behavior was intended."; |
| 2809 | } |
| 2810 | if (DEPRECATE(msg) < 0) { |
| 2811 | return NULL; |
| 2812 | } |
| 2813 | |
| 2814 | static npy_intp const zero_dim_shape[1] = {1}; |
| 2815 | static npy_intp const zero_dim_strides[1] = {0}; |
| 2816 | |
| 2817 | Py_INCREF(PyArray_DESCR(self)); /* array creation steals reference */ |
| 2818 | PyArrayObject *self_1d = (PyArrayObject *)PyArray_NewFromDescrAndBase( |
| 2819 | Py_TYPE(self), PyArray_DESCR(self), |
| 2820 | 1, zero_dim_shape, zero_dim_strides, PyArray_BYTES(self), |
| 2821 | PyArray_FLAGS(self), (PyObject *)self, (PyObject *)self); |
| 2822 | if (self_1d == NULL) { |
| 2823 | return NULL; |
| 2824 | } |
| 2825 | ret_tuple = PyArray_Nonzero(self_1d); |
| 2826 | Py_DECREF(self_1d); |
no test coverage detected