Skip to content

Commit

Permalink
Merge pull request #25121 from charris/backport-25042
Browse files Browse the repository at this point in the history
BUG: ensure passing ``np.dtype`` to itself doesn't crash
  • Loading branch information
charris committed Nov 12, 2023
2 parents cefdd34 + 766d5a8 commit d81f0ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions numpy/core/src/multiarray/descriptor.c
Expand Up @@ -1478,6 +1478,11 @@ PyArray_DTypeOrDescrConverterRequired(PyObject *obj, npy_dtype_info *dt_info)
dt_info->descr = NULL;

if (PyObject_TypeCheck(obj, &PyArrayDTypeMeta_Type)) {
if (obj == (PyObject *)&PyArrayDescr_Type) {
PyErr_SetString(PyExc_TypeError,
"Cannot convert np.dtype into a dtype.");
return NPY_FAIL;
}
Py_INCREF(obj);
dt_info->dtype = (PyArray_DTypeMeta *)obj;
dt_info->descr = NULL;
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_dtype.py
Expand Up @@ -1898,3 +1898,9 @@ def test_result_type_integers_and_unitless_timedelta64():
td = np.timedelta64(4)
result = np.result_type(0, td)
assert_dtype_equal(result, td.dtype)


def test_creating_dtype_with_dtype_class_errors():
# Regression test for #25031, calling `np.dtype` with itself segfaulted.
with pytest.raises(TypeError, match="Cannot convert np.dtype into a"):
np.array(np.ones(10), dtype=np.dtype)

0 comments on commit d81f0ae

Please sign in to comment.