8000 Merge pull request #25042 from ngoldbaum/fix-dtype-autophagia-segfault · numpy/numpy@95ca101 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95ca101

Browse files
authored
Merge pull request #25042 from ngoldbaum/fix-dtype-autophagia-segfault
BUG: ensure passing `np.dtype` to itself doesn't crash
2 parents 42c33f3 + b046293 commit 95ca101

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
@@ -1479,6 +1479,11 @@ PyArray_DTypeOrDescrConverterRequired(PyObject *obj, npy_dtype_info *dt_info)
14791479
dt_info->descr = NULL;
14801480

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

numpy/_core/tests/test_dtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,3 +1931,9 @@ def test_result_type_integers_and_unitless_timedelta64():
19311931
td = np.timedelta64(4)
19321932
result = np.result_type(0, td)
19331933
assert_dtype_equal(result, td.dtype)
1934+
1935+
1936+
def test_creating_dtype_with_dtype_class_errors():
1937+
# Regression test for #25031, calling `np.dtype` with itself segfaulted.
1938+
with pytest.raises(TypeError, match="Cannot convert np.dtype into a"):
1939+
np.array(np.ones(10), dtype=np.dtype)

0 commit comments

Comments
 (0)
0