* Returns -1 and sets an exception if *axis is an invalid axis for * an array of dimension ndim, otherwise adjusts it in place to be * 0 <= *axis < ndim, and returns 0. * * msg_prefix: borrowed reference, a string to prepend to the message */
| 135 | * msg_prefix: borrowed reference, a string to prepend to the message |
| 136 | */ |
| 137 | static inline int |
| 138 | check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix) |
| 139 | { |
| 140 | /* Check that index is valid, taking into account negative indices */ |
| 141 | if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) { |
| 142 | /* |
| 143 | * Load the exception type, if we don't already have it. Unfortunately |
| 144 | * we don't have access to npy_cache_import here |
| 145 | */ |
| 146 | static PyObject *AxisError_cls = NULL; |
| 147 | PyObject *exc; |
| 148 | |
| 149 | npy_cache_import("numpy.exceptions", "AxisError", &AxisError_cls); |
| 150 | if (AxisError_cls == NULL) { |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | /* Invoke the AxisError constructor */ |
| 155 | exc = PyObject_CallFunction(AxisError_cls, "iiO", |
| 156 | *axis, ndim, msg_prefix); |
| 157 | if (exc == NULL) { |
| 158 | return -1; |
| 159 | } |
| 160 | PyErr_SetObject(AxisError_cls, exc); |
| 161 | Py_DECREF(exc); |
| 162 | |
| 163 | return -1; |
| 164 | } |
| 165 | /* adjust negative indices */ |
| 166 | if (*axis < 0) { |
| 167 | *axis += ndim; |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | static inline int |
| 172 | check_and_adjust_axis(int *axis, int ndim) |
| 173 | { |
no test coverage detected