Skip to content

Commit 65bb1da

Browse files
ngoldbaumcharris
authored andcommitted
BUG: fix crash in ufunc.resolve_dtypes with a Python scalar type (#32496)
1 parent 294956e commit 65bb1da

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎numpy/_core/src/multiarray/abstractdtypes.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,12 @@ npy_update_operand_for_scalar(
410410
* in all other cases, we don't technically consider equivalent.
411411
* NOTE(seberg): I don't think we should be beholden to this logic.
412412
*/
413+
const char *name = (scalar != NULL) ? Py_TYPE(scalar)->tp_name :
414+
((PyArray_FLAGS(*operand) & NPY_ARRAY_WAS_PYTHON_INT) ? "int" :
415+
((PyArray_FLAGS(*operand) & NPY_ARRAY_WAS_PYTHON_FLOAT) ? "float" : "complex"));
413416
PyErr_Format(PyExc_TypeError,
414417
"cannot cast Python %s to %S under the casting rule 'equiv'",
415-
Py_TYPE(scalar)->tp_name, descr);
418+
name, descr);
416419
return -1;
417420
}
418421

‎numpy/_core/tests/test_ufunc.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,13 @@ def test_resolve_dtypes_basic(self):
32563256
r = np.add.resolve_dtypes((f4, int, None))
32573257
assert r == (f4, f4, f4)
32583258

3259+
msg = r"cannot cast Python.*under the casting rule 'equiv'"
3260+
for pytype, dtype in [(int, "uint8"), (float, "float32"),
3261+
(complex, "complex64")]:
3262+
with pytest.raises(TypeError, match=msg):
3263+
np.add.resolve_dtypes((np.dtype(dtype), pytype, None),
3264+
casting="equiv")
3265+
32593266
with pytest.raises(TypeError):
32603267
np.add.resolve_dtypes((i4, f4, None), casting="no")
32613268

0 commit comments

Comments
 (0)