Skip to content

Commit 083bc1a

Browse files
committed
BUG: Disallow flexible dtypes in compound types
Fixes numpy#8969
1 parent 67f8fe7 commit 083bc1a

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎numpy/core/src/multiarray/descriptor.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ _convert_from_array_descr(PyObject *obj, int align)
510510
if (ret == NPY_FAIL) {
511511
goto fail;
512512
}
513+
if (PyDataType_ISUNSIZED(conv)) {
514+
PyErr_SetString(PyExc_ValueError,
515+
"Flexible dtypes within compound dtypes must have a size");
516+
goto fail;
517+
}
513518
if ((PyDict_GetItem(fields, name) != NULL)
514519
|| (title
515520
#if defined(NPY_PY3K)
@@ -655,6 +660,13 @@ _convert_from_list(PyObject *obj, int align)
655660
Py_DECREF(key);
656661
goto fail;
657662
}
663+
if (PyDataType_ISUNSIZED(conv)) {
664+
PyErr_SetString(PyExc_ValueError,
665+
"Flexible dtypes within compound dtypes must have a size");
666+
Py_DECREF(tup);
667+
Py_DECREF(key);
668+
goto fail;
669+
}
658670
dtypeflags |= (conv->flags & NPY_FROM_FIELDS);
659671
PyTuple_SET_ITEM(tup, 0, (PyObject *)conv);
660672
if (align) {

‎numpy/core/tests/test_api.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_array_array():
100100

101101
# test array_struct interface
102102
a = np.array([(1, 4.0, 'Hello'), (2, 6.0, 'World')],
103-
dtype=[('f0', int), ('f1', float), ('f2', str)])
103+
dtype=[('f0', int), ('f1', float), ('f2', str, 5)])
104104
o = type("o", (object,),
105105
dict(__array_struct__=a.__array_struct__))
106106
## wasn't what I expected... is np.array(o) supposed to equal a ?

‎numpy/core/tests/test_records.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def test_zero_width_strings(self):
286286
assert_equal(rec['f0'], ['test', 'test', 'test'])
287287
assert_equal(rec['f1'], ['', '', ''])
288288

289-
dt = np.dtype([('f0', '|S4'), ('f1', '|S')])
289+
dt = np.dtype([('f0', '|S4'), ('f1', '|S0')])
290290
rec = np.rec.fromarrays(cols, dtype=dt)
291291
assert_equal(rec.itemsize, 4)
292292
assert_equal(rec['f0'], [b'test', b'test', b'test'])

0 commit comments

Comments
 (0)