Skip to content

Commit d81f0ae

Browse files
authored
Merge pull request numpy#25121 from charris/backport-25042
BUG: ensure passing ``np.dtype`` to itself doesn't crash
2 parents cefdd34 + 766d5a8 commit d81f0ae

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

numpy/core/src/multiarray/descriptor.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,11 @@ PyArray_DTypeOrDescrConverterRequired(PyObject *obj, npy_dtype_info *dt_info)
14781478
dt_info->descr = NULL;
14791479

14801480
if (PyObject_TypeCheck(obj, &PyArrayDTypeMeta_Type)) {
1481+
if (obj == (PyObject *)&PyArrayDescr_Type) {
1482+
PyErr_SetString(PyExc_TypeError,
1483+
"Cannot convert np.dtype into a dtype.");
1484+
return NPY_FAIL;
1485+
}
14811486
Py_INCREF(obj);
14821487
dt_info->dtype = (PyArray_DTypeMeta *)obj;
14831488
dt_info->descr = NULL;

numpy/core/tests/test_dtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,3 +1898,9 @@ def test_result_type_integers_and_unitless_timedelta64():
18981898
td = np.timedelta64(4)
18991899
result = np.result_type(0, td)
19001900
assert_dtype_equal(result, td.dtype)
1901+
1902+
1903+
def test_creating_dtype_with_dtype_class_errors():
1904+
# Regression test for #25031, calling `np.dtype` with itself segfaulted.
1905+
with pytest.raises(TypeError, match="Cannot convert np.dtype into a"):
1906+
np.array(np.ones(10), dtype=np.dtype)

0 commit comments

Comments
 (0)