Skip to content

Commit 941bc42

Browse files
authored
BUG: reject structured DTypes that contain StringDType fields (numpy#32027)
1 parent e0befa7 commit 941bc42

4 files changed

Lines changed: 79 additions & 3 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* Structured dtypes and subarray dtypes containing ``StringDType`` now
2+
consistently raise ``TypeError``. Formerly some structured dtpes
3+
containing ``StringDType`` could be created, but this could lead to
4+
data corruption or crashes on array data accesses.

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ _convert_from_tuple(PyObject *obj, int align)
308308
return type;
309309
}
310310

311+
/*
312+
* A subarray dtype is never attached to an array, so a base with
313+
* per-instance state (a finalize slot, e.g. StringDType) could
314+
* never be finalized and anything using the dtype would misbehave.
315+
*/
316+
if (NPY_DT_SLOTS(NPY_DTYPE(type))->finalize_descr != NULL) {
317+
PyErr_Format(PyExc_TypeError,
318+
"%s is not currently supported within subarray dtypes.",
319+
((PyTypeObject *)NPY_DTYPE(type))->tp_name);
320+
goto fail;
321+
}
322+
311323
/* validate and set shape */
312324
for (int i=0; i < shape.len; i++) {
313325
if (shape.ptr[i] < 0) {
@@ -388,6 +400,25 @@ _convert_from_tuple(PyObject *obj, int align)
388400
}
389401
}
390402

403+
/*
404+
* DTypes with a finalize slot (e.g. StringDType) carry per-instance state
405+
* that array creation must finalize, which the structured dtype machinery
406+
* does not do. Reject them as field dtypes; they cannot be wrapped in
407+
* subarray dtypes either, so subarray bases need not be checked. Returns
408+
* -1 with an exception set if rejected, 0 otherwise.
409+
*/
410+
static int
411+
_reject_unsupported_field_dtype(PyArray_Descr *descr)
412+
{
413+
if (NPY_DT_SLOTS(NPY_DTYPE(descr))->finalize_descr != NULL) {
414+
PyErr_Format(PyExc_TypeError,
415+
"%s is not currently supported for structured dtype "
416+
"fields.", ((PyTypeObject *)NPY_DTYPE(descr))->tp_name);
417+
return -1;
418+
}
419+
return 0;
420+
}
421+
391422
/*
392423
* obj is a list. Each item is a tuple with
393424
*
@@ -495,9 +526,8 @@ _convert_from_array_descr(PyObject *obj, int align)
495526
"Field elements must be tuples with at most 3 elements, got '%R'", item);
496527
goto fail;
497528
}
498-
if (PyObject_IsInstance((PyObject *)conv, (PyObject *)&PyArray_StringDType)) {
499-
PyErr_Format(PyExc_TypeError,
500-
"StringDType is not currently supported for structured dtype fields.");
529+
if (_reject_unsupported_field_dtype(conv) < 0) {
530+
Py_DECREF(conv);
501531
goto fail;
502532
}
503533
if ((PyDict_GetItemWithError(fields, name) != NULL) // noqa: borrowed-ref OK
@@ -640,6 +670,10 @@ _convert_from_list(PyObject *obj, int align)
640670
if (conv == NULL) {
641671
goto fail;
642672
}
673+
if (_reject_unsupported_field_dtype(conv) < 0) {
674+
Py_DECREF(conv);
675+
goto fail;
676+
}
643677
dtypeflags |= (conv->flags & NPY_FROM_FIELDS);
644678
if (align) {
645679
int _align = conv->alignment;
@@ -1133,6 +1167,12 @@ _convert_from_dict(PyObject *obj, int align)
11331167
Py_DECREF(ind);
11341168
goto fail;
11351169
}
1170+
if (_reject_unsupported_field_dtype(newdescr) < 0) {
1171+
Py_DECREF(newdescr);
1172+
Py_DECREF(tup);
1173+
Py_DECREF(ind);
1174+
goto fail;
1175+
}
11361176
PyTuple_SET_ITEM(tup, 0, (PyObject *)newdescr);
11371177
int _align = 1;
11381178
if (align) {

‎numpy/_core/tests/test_custom_dtypes.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,13 @@ def test_type_pickle():
483483

484484
def test_is_numeric():
485485
assert SF._is_numeric
486+
487+
488+
def test_structured_field_allowed():
489+
# dtypes without per-instance state (no finalize slot) are allowed as
490+
# structured dtype fields and as subarray dtype bases
491+
dt = np.dtype([("a", SF(2.))])
492+
arr = np.zeros(2, dt)
493+
arr["a"] = [1., 2.]
494+
assert_array_equal(arr["a"].astype(np.float64), [1., 2.])
495+
assert np.dtype((SF(2.), 3)).subdtype[0] == SF(2.)

‎numpy/_core/tests/test_stringdtype.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ def test_dtype_creation():
113113
assert len(hashes) == 4
114114

115115

116+
@pytest.mark.parametrize("dtype_spec", [
117+
[("a", StringDType())],
118+
[("a", StringDType(), 10)],
119+
[("a", (StringDType(), 10))],
120+
{"names": ["a"], "formats": [StringDType()]},
121+
{"names": ["a"], "formats": [(StringDType(), 2)]},
122+
{"a": (StringDType(), 0)},
123+
"T,i4",
124+
])
125+
def test_structured_dtype_creation_rejected(dtype_spec):
126+
with pytest.raises(TypeError, match="not currently supported"):
127+
np.dtype(dtype_spec)
128+
129+
130+
def test_subarray_dtype_rejected():
131+
with pytest.raises(TypeError,
132+
match="not currently supported within subarray"):
133+
np.dtype((StringDType(), 2))
134+
# (dtype, ()) is equivalent to the dtype itself and remains allowed
135+
assert np.dtype((StringDType(), ())) == StringDType()
136+
137+
116138
def test_dtype_equality(dtype):
117139
assert dtype == dtype
118140
for ch in "SU":

0 commit comments

Comments
 (0)