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

Function check_and_adjust_axis_msg

numpy/core/src/multiarray/common.h:137–170  ·  view source on GitHub ↗

* 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 */

Source from the content-addressed store, hash-verified

135 * msg_prefix: borrowed reference, a string to prepend to the message
136 */
137static inline int
138check_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}
171static inline int
172check_and_adjust_axis(int *axis, int ndim)
173{

Callers 4

PyArray_DiagonalFunction · 0.85
PyArray_SwapAxesFunction · 0.85
normalize_axis_indexFunction · 0.85
check_and_adjust_axisFunction · 0.85

Calls 1

npy_cache_importFunction · 0.85

Tested by

no test coverage detected