diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2013-01-13 01:19:13 +0100 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-01-13 01:20:23 +0100 |
commit | 8362e081291e792be304cbdb8ece2e00845baf39 (patch) | |
tree | 55a1884305f36296780392308406b98f53c955c5 /numpy | |
parent | 00a8c0ca278280d04d32fc84386c29800e3b5a00 (diff) | |
download | numpy-8362e081291e792be304cbdb8ece2e00845baf39.tar.gz |
BUG: Convert non-array rhs for boolean assignment with correct dtype
Enforcing the left hand side datatype for a non-array right hand side
argument in index assignments was the behavior before 1.7. and is the
general behaviour here. (note this means a non-array right hand side
checks for NaN, etc. if the left hand side is integer, but an array
right hand side does not)
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/mapping.c | 14 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 10 |
2 files changed, 21 insertions, 3 deletions
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index 001ccba08..6cc00edab 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -1306,9 +1306,17 @@ array_ass_sub(PyArrayObject *self, PyObject *ind, PyObject *op) PyArrayObject *op_arr; PyArray_Descr *dtype = NULL; - op_arr = (PyArrayObject *)PyArray_FromAny(op, dtype, 0, 0, 0, NULL); - if (op_arr == NULL) { - return -1; + if (!PyArray_Check(op)) { + dtype = PyArray_DTYPE(self); + Py_INCREF(dtype); + op_arr = (PyArrayObject *)PyArray_FromAny(op, dtype, 0, 0, 0, NULL); + if (op_arr == NULL) { + return -1; + } + } + else { + op_arr = op; + Py_INCREF(op_arr); } if (PyArray_NDIM(op_arr) < 2) { diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index d5e59cdc0..f8566caf3 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -304,6 +304,16 @@ class TestRegression(TestCase): self.assertRaises(ValueError, bfa) self.assertRaises(ValueError, bfb) + def test_nonarray_assignment(self): + # See also Issue gh-2870, test for nonarray assignment + # and equivalent unsafe casted array assignment + a = np.arange(10) + b = np.ones(10, dtype=bool) + def assign(a, b, c): + a[b] = c + assert_raises(ValueError, assign, a, b, np.nan) + a[b] = np.array(np.nan) # but not this. + def test_unpickle_dtype_with_object(self,level=rlevel): """Implemented in r2840""" dt = np.dtype([('x',int),('y',np.object_),('z','O')]) |