From 04b89c63e531ee29b8a0cdb3f2c5ce410f6169b2 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 20 Feb 2013 15:04:33 +0100 Subject: [PATCH] BUG: Fix regression of bad error/random behavior in item method Appearently .item() for arrays with size != 1 correctly returned an error in 1.6., but failed to raise the error (due to missing return) in 1.7. --- numpy/core/src/multiarray/methods.c | 1 + numpy/core/tests/test_regression.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 7f0e3861b9ad..902384b0d21a 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -633,6 +633,7 @@ array_toscalar(PyArrayObject *self, PyObject *args) else { PyErr_SetString(PyExc_ValueError, "can only convert an array of size 1 to a Python scalar"); + return NULL; } } /* Special case of C-order flat indexing... :| */ diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 1cf2e6e85988..9a193b3c1485 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1068,6 +1068,11 @@ def test_recarray_tolist(self, level=rlevel): assert_( a[0].tolist() == b[0]) assert_( a[1].tolist() == b[1]) + def test_nonscalar_item_method(self): + # Make sure that .item() fails graciously when it should + a = np.arange(5) + assert_raises(ValueError, a.item) + def test_char_array_creation(self, level=rlevel): a = np.array('123', dtype='c') b = np.array(asbytes_nested(['1','2','3']))