diff options
author | Ihor Melnyk <ihor.melnyk@eleks.com> | 2018-04-17 17:28:45 +0300 |
---|---|---|
committer | Ihor Melnyk <ihor.melnyk@eleks.com> | 2018-04-18 17:40:21 +0300 |
commit | ab9953baf29ace44ef1f014f3713f1ee919f79bb (patch) | |
tree | fbc8b12a13b5c077b4466e30283c480165c23390 /numpy/core | |
parent | 8323be1bc44c2811fc36f5b99c1a30ebcee8edbd (diff) | |
download | numpy-ab9953baf29ace44ef1f014f3713f1ee919f79bb.tar.gz |
Fixes #663: fixed dtype alignment for array of structs in case of converting from tuple descr
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 8 |
2 files changed, 22 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 8d983ffc9..c1c1ce568 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -243,7 +243,7 @@ is_datetime_typestr(char *type, Py_ssize_t len) } static PyArray_Descr * -_convert_from_tuple(PyObject *obj) +_convert_from_tuple(PyObject *obj, int align) { PyArray_Descr *type, *res; PyObject *val; @@ -252,9 +252,16 @@ _convert_from_tuple(PyObject *obj) if (PyTuple_GET_SIZE(obj) != 2) { return NULL; } - if (!PyArray_DescrConverter(PyTuple_GET_ITEM(obj,0), &type)) { - return NULL; + if (align) { + if (!PyArray_DescrAlignConverter(PyTuple_GET_ITEM(obj, 0), &type)) { + return NULL; + } } + else { + if (!PyArray_DescrConverter(PyTuple_GET_ITEM(obj, 0), &type)) { + return NULL; + } + } val = PyTuple_GET_ITEM(obj,1); /* try to interpret next item as a type */ res = _use_inherit(type, val, &errflag); @@ -1547,7 +1554,7 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) } else if (PyTuple_Check(obj)) { /* or a tuple */ - *at = _convert_from_tuple(obj); + *at = _convert_from_tuple(obj, 0); if (*at == NULL){ if (PyErr_Occurred()) { return NPY_FAIL; @@ -2928,6 +2935,9 @@ PyArray_DescrAlignConverter(PyObject *obj, PyArray_Descr **at) *at = _convert_from_commastring(tmp, 1); Py_DECREF(tmp); } + else if (PyTuple_Check(obj)) { + *at = _convert_from_tuple(obj, 1); + } else if (PyList_Check(obj)) { *at = _convert_from_array_descr(obj, 1); } diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 35b8f6868..27fbb10d5 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -205,6 +205,14 @@ class TestRecord(object): assert_equal(dt3.itemsize, 11) assert_equal(dt1, dt2) assert_equal(dt2, dt3) + # Array of subtype should preserve alignment + dt1 = np.dtype([('a', '|i1'), + ('b', [('f0', '<i2'), + ('f1', '<f4')], 2)], align=True) + assert_equal(dt1.descr, [('a', '|i1'), ('', '|V3'), + ('b', [('f0', '<i2'), ('', '|V2'), + ('f1', '<f4')], (2,))]) + def test_union_struct(self): # Should be able to create union dtypes |