Permalink
Browse files

Allow only axis=0 and axis=None for 0-d arrays, and disallow axis>MAX…

…_DIMS (addresses #1286)

These changes should catch errors earlier by raising exceptions, instead
of resulting to unexpected behavior.
  • Loading branch information...
1 parent ca35f53 commit faea0c8820fc773a100a4875afb42fe8f1944b3d @pv pv committed Nov 7, 2009
Showing with 20 additions and 2 deletions.
  1. +7 −2 numpy/core/src/multiarray/ctors.c
  2. +13 −0 numpy/core/tests/test_multiarray.py
View
9 numpy/core/src/multiarray/ctors.c
@@ -2424,21 +2424,26 @@ PyArray_CopyInto(PyArrayObject *dest, PyArrayObject *src)
/*NUMPY_API
PyArray_CheckAxis
+
+ check that axis is valid
+ convert 0-d arrays to 1-d arrays
*/
NPY_NO_EXPORT PyObject *
PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags)
{
PyObject *temp1, *temp2;
int n = arr->nd;
- if ((*axis >= MAX_DIMS) || (n==0)) {
+ if (*axis == MAX_DIMS || n == 0) {
if (n != 1) {
temp1 = PyArray_Ravel(arr,0);
if (temp1 == NULL) {
*axis = 0;
return NULL;
}
- *axis = PyArray_NDIM(temp1)-1;
+ if (*axis == MAX_DIMS) {
+ *axis = PyArray_NDIM(temp1)-1;
+ }
}
else {
temp1 = (PyObject *)arr;
View
13 numpy/core/tests/test_multiarray.py
@@ -644,6 +644,19 @@ def test_all(self):
axes.remove(i)
assert all(amax == aargmax.choose(*a.transpose(i,*axes)))
+class TestMinMax(TestCase):
+ def test_scalar(self):
+ assert_raises(ValueError, np.amax, 1, 1)
+ assert_raises(ValueError, np.amin, 1, 1)
+
+ assert_equal(np.amax(1, axis=0), 1)
+ assert_equal(np.amin(1, axis=0), 1)
+ assert_equal(np.amax(1, axis=None), 1)
+ assert_equal(np.amin(1, axis=None), 1)
+
+ def test_axis(self):
+ assert_raises(ValueError, np.amax, [1,2,3], 1000)
+ assert_equal(np.amax([[1,2,3]], axis=1), 3)
class TestNewaxis(TestCase):
def test_basic(self):

0 comments on commit faea0c8

Please sign in to comment.