diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2018-02-04 19:03:41 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-04 19:03:41 -0700 |
| commit | 92d1759b95a882facafd0d3545ca353232944b79 (patch) | |
| tree | 5d3452c5145d1d84f4f453f64b92a4a74530d667 /numpy/core/src | |
| parent | cccb15512aa7305f9d1dfd50219bc5cc7a5614cf (diff) | |
| parent | 74e8690a2ee1b4b5c5ca4b0c505cbf44298b7fe8 (diff) | |
| download | numpy-92d1759b95a882facafd0d3545ca353232944b79.tar.gz | |
Merge pull request #10502 from eric-wieser/swapaxes-AxisError
MAINT: Use AxisError in swapaxes, unique, and diagonal
Diffstat (limited to 'numpy/core/src')
| -rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 18 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 6 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.h | 2 | ||||
| -rw-r--r-- | numpy/core/src/multiarray/shape.c | 15 |
4 files changed, 15 insertions, 26 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index 486eb43ce..eb9ef5915 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -14,6 +14,7 @@ #include "npy_pycompat.h" +#include "multiarraymodule.h" #include "common.h" #include "arrayobject.h" #include "ctors.h" @@ -1818,26 +1819,17 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) } /* Handle negative axes with standard Python indexing rules */ - if (axis1 < 0) { - axis1 += ndim; + if (check_and_adjust_axis_msg(&axis1, ndim, npy_ma_str_axis1) < 0) { + return NULL; } - if (axis2 < 0) { - axis2 += ndim; + if (check_and_adjust_axis_msg(&axis2, ndim, npy_ma_str_axis2) < 0) { + return NULL; } - - /* Error check the two axes */ if (axis1 == axis2) { PyErr_SetString(PyExc_ValueError, "axis1 and axis2 cannot be the same"); return NULL; } - else if (axis1 < 0 || axis1 >= ndim || axis2 < 0 || axis2 >= ndim) { - PyErr_Format(PyExc_ValueError, - "axis1(=%d) and axis2(=%d) " - "must be within range (ndim=%d)", - axis1, axis2, ndim); - return NULL; - } /* Get the shape and strides of the two axes */ shape = PyArray_SHAPE(self); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 00ea958ef..3e322c7e2 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4690,6 +4690,8 @@ NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_order = NULL; NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_copy = NULL; NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_dtype = NULL; NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ndmin = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis1 = NULL; +NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis2 = NULL; static int intern_strings(void) @@ -4704,12 +4706,14 @@ intern_strings(void) npy_ma_str_copy = PyUString_InternFromString("copy"); npy_ma_str_dtype = PyUString_InternFromString("dtype"); npy_ma_str_ndmin = PyUString_InternFromString("ndmin"); + npy_ma_str_axis1 = PyUString_InternFromString("axis1"); + npy_ma_str_axis2 = PyUString_InternFromString("axis2"); return npy_ma_str_array && npy_ma_str_array_prepare && npy_ma_str_array_wrap && npy_ma_str_array_finalize && npy_ma_str_buffer && npy_ma_str_ufunc && npy_ma_str_order && npy_ma_str_copy && npy_ma_str_dtype && - npy_ma_str_ndmin; + npy_ma_str_ndmin && npy_ma_str_axis1 && npy_ma_str_axis2; } diff --git a/numpy/core/src/multiarray/multiarraymodule.h b/numpy/core/src/multiarray/multiarraymodule.h index 82ae24845..3de68c549 100644 --- a/numpy/core/src/multiarray/multiarraymodule.h +++ b/numpy/core/src/multiarray/multiarraymodule.h @@ -11,5 +11,7 @@ NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_order; NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_copy; NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_dtype; NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ndmin; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis1; +NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis2; #endif diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index 61908e95e..29c122bd3 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -17,6 +17,7 @@ #include "shape.h" +#include "multiarraymodule.h" /* for interned strings */ #include "templ_common.h" /* for npy_mul_with_overflow_intp */ #include "common.h" /* for convert_shape_to_string */ #include "alloc.h" @@ -648,20 +649,10 @@ PyArray_SwapAxes(PyArrayObject *ap, int a1, int a2) int n = PyArray_NDIM(ap); int i; - if (a1 < 0) { - a1 += n; - } - if (a2 < 0) { - a2 += n; - } - if ((a1 < 0) || (a1 >= n)) { - PyErr_SetString(PyExc_ValueError, - "bad axis1 argument to swapaxes"); + if (check_and_adjust_axis_msg(&a1, n, npy_ma_str_axis1) < 0) { return NULL; } - if ((a2 < 0) || (a2 >= n)) { - PyErr_SetString(PyExc_ValueError, - "bad axis2 argument to swapaxes"); + if (check_and_adjust_axis_msg(&a2, n, npy_ma_str_axis2) < 0) { return NULL; } |
