Skip to content

Commit 20a2a70

Browse files
authored
Merge pull request numpy#20365 from seberg/issue-19696
BUG: Fix failure to create aligned, empty structured dtype
2 parents c0b7c6d + b5f1b3e commit 20a2a70

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

numpy/core/src/multiarray/descriptor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ _convert_from_dict(PyObject *obj, int align)
13261326
goto fail;
13271327
}
13281328
/* If align is set, make sure the alignment divides into the size */
1329-
if (align && itemsize % new->alignment != 0) {
1329+
if (align && new->alignment > 0 && itemsize % new->alignment != 0) {
13301330
PyErr_Format(PyExc_ValueError,
13311331
"NumPy dtype descriptor requires alignment of %d bytes, "
13321332
"which is not divisible into the specified itemsize %d",

numpy/core/tests/test_dtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ def test_alignment(self):
627627
t2 = np.dtype('2i4', align=True)
628628
assert_equal(t1.alignment, t2.alignment)
629629

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

631637
def iter_struct_object_dtypes():
632638
"""

0 commit comments

Comments
 (0)