File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed
numpy/_core/src/multiarray Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -716,18 +716,29 @@ can_cast_pyscalar_scalar_to(
716
716
}
717
717
718
718
/*
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).
720
722
*/
721
- PyArray_Descr * from ;
723
+ PyArray_DTypeMeta * from_DType ;
724
+ PyArray_Descr * default_dtype ;
722
725
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 ;
724
728
}
725
729
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 ;
727
732
}
728
733
else {
729
- from = PyArray_DescrFromType (NPY_CDOUBLE );
734
+ default_dtype = PyArray_DescrNewFromType (NPY_COMPLEX128 );
735
+ from_DType = & PyArray_PyComplexDType ;
730
736
}
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
+
731
742
int res = PyArray_CanCastTypeTo (from , to , casting );
732
743
Py_DECREF (from );
733
744
return res ;
You can’t perform that action at this time.
0 commit comments