Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 131c3b5

Browse filesBrowse files
authored
Merge pull request #27543 from charris/backport-27534
BUG: Fix user dtype can-cast with python scalar during promotion
2 parents a39cced + 6756b4e commit 131c3b5
Copy full SHA for 131c3b5

File tree

Expand file treeCollapse file tree

1 file changed

+16
-5
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-5
lines changed

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

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/convert_datatype.c
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -897,18 +897,29 @@ can_cast_pyscalar_scalar_to(
897897
}
898898

899899
/*
900-
* For all other cases we use the default dtype.
900+
* For all other cases we need to make a bit of a dance to find the cast
901+
* safety. We do so by finding the descriptor for the "scalar" (without
902+
* a value; for parametric user dtypes a value may be needed eventually).
901903
*/
902-
PyArray_Descr *from;
904+
PyArray_DTypeMeta *from_DType;
905+
PyArray_Descr *default_dtype;
903906
if (flags & NPY_ARRAY_WAS_PYTHON_INT) {
904-
from = PyArray_DescrFromType(NPY_LONG);
907+
default_dtype = PyArray_DescrNewFromType(NPY_INTP);
908+
from_DType = &PyArray_PyLongDType;
905909
}
906910
else if (flags & NPY_ARRAY_WAS_PYTHON_FLOAT) {
907-
from = PyArray_DescrFromType(NPY_DOUBLE);
911+
default_dtype = PyArray_DescrNewFromType(NPY_FLOAT64);
912+
from_DType = &PyArray_PyFloatDType;
908913
}
909914
else {
910-
from = PyArray_DescrFromType(NPY_CDOUBLE);
915+
default_dtype = PyArray_DescrNewFromType(NPY_COMPLEX128);
916+
from_DType = &PyArray_PyComplexDType;
911917
}
918+
919+
PyArray_Descr *from = npy_find_descr_for_scalar(
920+
NULL, default_dtype, from_DType, NPY_DTYPE(to));
921+
Py_DECREF(default_dtype);
922+
912923
int res = PyArray_CanCastTypeTo(from, to, casting);
913924
Py_DECREF(from);
914925
return res;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.