diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-08-05 00:08:13 +0200 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-08-05 00:08:13 +0200 |
commit | 6cafbfd66c51874bdee55f56419b68cabe741646 (patch) | |
tree | abe99a6687e697240d396cc1bfb2a1cf2785ef38 | |
parent | b1d63619443d9f0a9018db53339bd6143cecf862 (diff) | |
parent | 3f0cb83f128f3bc61ff9eae2ff5bdc882c922a93 (diff) | |
download | numpy-6cafbfd66c51874bdee55f56419b68cabe741646.tar.gz |
Merge pull request #4926 from juliantaylor/concatenate-error
ENH: better error message for invalid axis and concatenate inputs
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 41 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 6 |
2 files changed, 36 insertions, 11 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index b84dff864..d32fcabd2 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -16,6 +16,11 @@ #include "conversion_utils.h" +static int +PyArray_PyIntAsInt_ErrMsg(PyObject *o, const char * msg) NPY_GCC_NONNULL(2); +static npy_intp +PyArray_PyIntAsIntp_ErrMsg(PyObject *o, const char * msg) NPY_GCC_NONNULL(2); + /**************************************************************** * Useful function for conversion when used with PyArg_ParseTuple ****************************************************************/ @@ -215,8 +220,9 @@ PyArray_AxisConverter(PyObject *obj, int *axis) *axis = NPY_MAXDIMS; } else { - *axis = PyArray_PyIntAsInt(obj); - if (PyErr_Occurred()) { + *axis = PyArray_PyIntAsInt_ErrMsg(obj, + "an integer is required for the axis"); + if (error_converting(*axis)) { return NPY_FAIL; } } @@ -251,7 +257,8 @@ PyArray_ConvertMultiAxis(PyObject *axis_in, int ndim, npy_bool *out_axis_flags) } for (i = 0; i < naxes; ++i) { PyObject *tmp = PyTuple_GET_ITEM(axis_in, i); - int axis = PyArray_PyIntAsInt(tmp); + int axis = PyArray_PyIntAsInt_ErrMsg(tmp, + "integers are required for the axis tuple elements"); int axis_orig = axis; if (error_converting(axis)) { return NPY_FAIL; @@ -281,7 +288,8 @@ PyArray_ConvertMultiAxis(PyObject *axis_in, int ndim, npy_bool *out_axis_flags) memset(out_axis_flags, 0, ndim); - axis = PyArray_PyIntAsInt(axis_in); + axis = PyArray_PyIntAsInt_ErrMsg(axis_in, + "an integer is required for the axis"); axis_orig = axis; if (error_converting(axis)) { @@ -736,13 +744,12 @@ PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting) * Other conversion functions *****************************/ -/*NUMPY_API*/ -NPY_NO_EXPORT int -PyArray_PyIntAsInt(PyObject *o) +static int +PyArray_PyIntAsInt_ErrMsg(PyObject *o, const char * msg) { npy_intp long_value; /* This assumes that NPY_SIZEOF_INTP >= NPY_SIZEOF_INT */ - long_value = PyArray_PyIntAsIntp(o); + long_value = PyArray_PyIntAsIntp_ErrMsg(o, msg); #if (NPY_SIZEOF_INTP > NPY_SIZEOF_INT) if ((long_value < INT_MIN) || (long_value > INT_MAX)) { @@ -754,8 +761,14 @@ PyArray_PyIntAsInt(PyObject *o) } /*NUMPY_API*/ -NPY_NO_EXPORT npy_intp -PyArray_PyIntAsIntp(PyObject *o) +NPY_NO_EXPORT int +PyArray_PyIntAsInt(PyObject *o) +{ + return PyArray_PyIntAsInt_ErrMsg(o, "an integer is required"); +} + +static npy_intp +PyArray_PyIntAsIntp_ErrMsg(PyObject *o, const char * msg) { #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP) long long long_value = -1; @@ -763,7 +776,6 @@ PyArray_PyIntAsIntp(PyObject *o) long long_value = -1; #endif PyObject *obj, *err; - static char *msg = "an integer is required"; if (!o) { PyErr_SetString(PyExc_TypeError, msg); @@ -909,6 +921,13 @@ PyArray_PyIntAsIntp(PyObject *o) return long_value; } +/*NUMPY_API*/ +NPY_NO_EXPORT npy_intp +PyArray_PyIntAsIntp(PyObject *o) +{ + return PyArray_PyIntAsIntp_ErrMsg(o, "an integer is required"); +} + /* * PyArray_IntpFromIndexSequence diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 7fade0832..bd189af0c 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -576,6 +576,12 @@ PyArray_Concatenate(PyObject *op, int axis) PyArrayObject **arrays; PyArrayObject *ret; + if (!PySequence_Check(op)) { + PyErr_SetString(PyExc_TypeError, + "The first input argument needs to be a sequence"); + return NULL; + } + /* Convert the input list into arrays */ narrays = PySequence_Size(op); if (narrays < 0) { |