diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-09-15 13:43:14 -0500 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2020-09-15 13:43:14 -0500 |
commit | fc0297e3311de5f47a46767f6d6c5181c9a7b07d (patch) | |
tree | 8b7f0438d9aedaabf5243d4d76983c6c531279a4 | |
parent | 4e9c37d0555fe4d58632d47fb5de98a40a427050 (diff) | |
download | numpy-fc0297e3311de5f47a46767f6d6c5181c9a7b07d.tar.gz |
MAINT: Relax check that a correct type is already set
This allows running `xpress` on current master. `xpress` copies the
type from `np.dtype(object)` rather than using `&PyArrayDescr_Type`.
That seems overall fine, we can just replace it.
The only reason for this check is to ensure that the user does not
override our updated `type(user_dtype)` and thus corrupting it.
-rw-r--r-- | numpy/core/src/multiarray/dtypemeta.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/dtypemeta.c b/numpy/core/src/multiarray/dtypemeta.c index c483f9937..ae1dfdd81 100644 --- a/numpy/core/src/multiarray/dtypemeta.c +++ b/numpy/core/src/multiarray/dtypemeta.c @@ -312,14 +312,28 @@ string_known_scalar_types( NPY_NO_EXPORT int dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr) { - if (Py_TYPE(descr) != &PyArrayDescr_Type) { + int has_type_set = Py_TYPE(descr) == &PyArrayDescr_Type; + + if (!has_type_set) { + /* Accept if the type was filled in from an existing builtin dtype */ + for (int i = 0; i < NPY_NTYPES; i++) { + PyArray_Descr *builtin = PyArray_DescrFromType(i); + has_type_set = Py_TYPE(descr) == Py_TYPE(builtin); + Py_DECREF(builtin); + if (has_type_set) { + break; + } + } + } + if (!has_type_set) { PyErr_Format(PyExc_RuntimeError, "During creation/wrapping of legacy DType, the original class " "was not of PyArrayDescr_Type (it is replaced in this step). " "The extension creating a custom DType for type %S must be " - "modified to ensure `Py_TYPE(descr) == &PyArrayDescr_Type` at " - "registration time.", - descr->typeobj); + "modified to ensure `Py_TYPE(descr) == &PyArrayDescr_Type` or " + "that of an existing dtype (with the assumption it is just " + "copied over and can be replaced).", + descr->typeobj, Py_TYPE(descr)); return -1; } |