@@ -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 ) {
0 commit comments