Skip to content

Commit

Permalink
Merge pull request #20466 from charris/backport-20365
Browse files Browse the repository at this point in the history
BUG: Fix failure to create aligned, empty structured dtype
  • Loading branch information
charris committed Nov 26, 2021
2 parents 87d6f96 + 75fd6a7 commit 157ee4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/descriptor.c
Expand Up @@ -1325,7 +1325,7 @@ _convert_from_dict(PyObject *obj, int align)
goto fail;
}
/* If align is set, make sure the alignment divides into the size */
if (align && itemsize % new->alignment != 0) {
if (align && new->alignment > 0 && itemsize % new->alignment != 0) {
PyErr_Format(PyExc_ValueError,
"NumPy dtype descriptor requires alignment of %d bytes, "
"which is not divisible into the specified itemsize %d",
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_dtype.py
Expand Up @@ -621,6 +621,12 @@ def test_alignment(self):
t2 = np.dtype('2i4', align=True)
assert_equal(t1.alignment, t2.alignment)

def test_aligned_empty(self):
# Mainly regression test for gh-19696: construction failed completely
dt = np.dtype([], align=True)
assert dt == np.dtype([])
dt = np.dtype({"names": [], "formats": [], "itemsize": 0}, align=True)
assert dt == np.dtype([])

def iter_struct_object_dtypes():
"""
Expand Down

0 comments on commit 157ee4f

Please sign in to comment.