diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-08-12 17:27:57 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-08-12 17:27:57 -0700 |
commit | dd2717f3b1a524c58b7bf79f234b87a6a9f68b2e (patch) | |
tree | f84aaf4c589ba1376eabdc6d099845c495c9bc4c /numpy/core | |
parent | 6cae18513cf244ad919ad1f9994446c1d126f35a (diff) | |
parent | 8c0c77549355a0b6d9d1194467f819893d27eb51 (diff) | |
download | numpy-dd2717f3b1a524c58b7bf79f234b87a6a9f68b2e.tar.gz |
Merge pull request #3609 from mwiebe/fill_struct_from_tuple
Bugfix: Regression when filling struct from tuple
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/convert.c | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c index e3dd78b9f..62b9034c2 100644 --- a/numpy/core/src/multiarray/convert.c +++ b/numpy/core/src/multiarray/convert.c @@ -413,7 +413,14 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) else { PyArrayObject *src_arr; - src_arr = (PyArrayObject *)PyArray_FromAny(obj, NULL, 0, 0, 0, NULL); + /** + * The dtype of the destination is used when converting + * from the pyobject, so that for example a tuple gets + * recognized as a struct scalar of the required type. + */ + Py_INCREF(PyArray_DTYPE(arr)); + src_arr = (PyArrayObject *)PyArray_FromAny(obj, + PyArray_DTYPE(arr), 0, 0, 0, NULL); if (src_arr == NULL) { return -1; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 19806bd76..b7235d05f 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -164,9 +164,17 @@ class TestAttributes(TestCase): y[...] = 1 assert_equal(x,y) + def test_fill_struct_array(self): + # Filling from a scalar x = array([(0,0.0), (1,1.0)], dtype='i4,f8') x.fill(x[0]) assert_equal(x['f1'][1], x['f1'][0]) + # Filling from a tuple that can be converted + # to a scalar + x = np.zeros(2, dtype=[('a', 'f8'), ('b', 'i4')]) + x.fill((3.5, -2)) + assert_array_equal(x['a'], [3.5, 3.5]) + assert_array_equal(x['b'], [-2, -2]) class TestAssignment(TestCase): def test_assignment_broadcasting(self): |