Skip to content

Commit

Permalink
Merge pull request numpy#10923 from ihormelnyk/master
Browse files Browse the repository at this point in the history
Fixes numpy#663: fixed dtype alignment for array of structs in case of converting from tuple descr
  • Loading branch information
ahaldane authored Apr 18, 2018
2 parents 23bc50d + ab9953b commit 8be088a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ def test_aligned_size(self):
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
Expand Down

0 comments on commit 8be088a

Please sign in to comment.