Skip to content

Commit a5cb327

Browse files
authored
Merge pull request numpy#27534 from seberg/issue-27389
BUG: Fix user dtype can-cast with python scalar during promotion
2 parents d1724e3 + 8b7fb0e commit a5cb327

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

numpy/_core/src/multiarray/convert_datatype.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -716,18 +716,29 @@ can_cast_pyscalar_scalar_to(
716716
}
717717

718718
/*
719-
* For all other cases we use the default dtype.
719+
* For all other cases we need to make a bit of a dance to find the cast
720+
* safety. We do so by finding the descriptor for the "scalar" (without
721+
* a value; for parametric user dtypes a value may be needed eventually).
720722
*/
721-
PyArray_Descr *from;
723+
PyArray_DTypeMeta *from_DType;
724+
PyArray_Descr *default_dtype;
722725
if (flags & NPY_ARRAY_WAS_PYTHON_INT) {
723-
from = PyArray_DescrFromType(NPY_LONG);
726+
default_dtype = PyArray_DescrNewFromType(NPY_INTP);
727+
from_DType = &PyArray_PyLongDType;
724728
}
725729
else if (flags & NPY_ARRAY_WAS_PYTHON_FLOAT) {
726-
from = PyArray_DescrFromType(NPY_DOUBLE);
730+
default_dtype = PyArray_DescrNewFromType(NPY_FLOAT64);
731+
from_DType = &PyArray_PyFloatDType;
727732
}
728733
else {
729-
from = PyArray_DescrFromType(NPY_CDOUBLE);
734+
default_dtype = PyArray_DescrNewFromType(NPY_COMPLEX128);
735+
from_DType = &PyArray_PyComplexDType;
730736
}
737+
738+
PyArray_Descr *from = npy_find_descr_for_scalar(
739+
NULL, default_dtype, from_DType, NPY_DTYPE(to));
740+
Py_DECREF(default_dtype);
741+
731742
int res = PyArray_CanCastTypeTo(from, to, casting);
732743
Py_DECREF(from);
733744
return res;

0 commit comments

Comments
 (0)