10000 BUG: ensure passing `np.dtype` to itself doesn't crash · numpy/numpy@ac8f1f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac8f1f1

Browse files
committed
BUG: ensure passing np.dtype to itself doesn't crash
1 parent 2f34979 commit ac8f1f1

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,9 @@ PyArray_CastDescrToDType(PyArray_Descr *descr, PyArray_DTypeMeta *given_DType)
10991099
Py_INCREF(descr);
11001100
return descr;
11011101
}
1102+
if (given_DType == (PyArray_DTypeMeta *)&PyArrayDescr_Type) {
1103+
goto error;
1104+
}
11021105
if (!NPY_DT_is_parametric(given_DType)) {
11031106
/*
11041107
* Don't actually do anything, the default is always the result

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="float64 to <class 'numpy.dtype'>"):
1939+
np.array(np.ones(10), dtype=np.dtype)

0 commit comments

Comments
 (0)
0