Skip to content

Commit

Permalink
Allow only axis=0 and axis=None for 0-d arrays, and disallow axis>MAX…
Browse files Browse the repository at this point in the history
…_DIMS (addresses #1286)

These changes should catch errors earlier by raising exceptions, instead
of resulting to unexpected behavior.
  • Loading branch information
pv committed Nov 7, 2009
1 parent ca35f53 commit faea0c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions numpy/core/src/multiarray/ctors.c
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -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):
Expand Down

0 comments on commit faea0c8

Please sign in to comment.