Skip to content

Commit 6a85517

Browse files
committed
BUG: Ensure nditer always adds necessary casts (and tiny simplification)
1 parent 72da798 commit 6a85517

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

numpy/_core/src/multiarray/mapping.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,6 @@ array_assign_subscript(PyArrayObject *self, PyObject *ind, PyObject *op)
20352035
goto fail;
20362036
}
20372037

2038-
int allocated_array = 0;
20392038
if (tmp_arr == NULL) {
20402039
/* Fill extra op, need to swap first */
20412040
tmp_arr = mit->extra_op;
@@ -2049,7 +2048,11 @@ array_assign_subscript(PyArrayObject *self, PyObject *ind, PyObject *op)
20492048
if (PyArray_CopyObject(tmp_arr, op) < 0) {
20502049
goto fail;
20512050
}
2052-
allocated_array = 1;
2051+
/*
2052+
* In this branch we copy directly from a newly allocated array which
2053+
* may have a new descr:
2054+
*/
2055+
descr = PyArray_DESCR(tmp_arr);
20532056
}
20542057

20552058
if (PyArray_MapIterCheckIndices(mit) < 0) {
@@ -2097,8 +2100,7 @@ array_assign_subscript(PyArrayObject *self, PyObject *ind, PyObject *op)
20972100
// for non-REFCHK user DTypes. See gh-27057 for the prior discussion about this.
20982101
if (PyArray_GetDTypeTransferFunction(
20992102
1, itemsize, itemsize,
2100-
allocated_array ? PyArray_DESCR(mit->extra_op) : PyArray_DESCR(self),
2101-
PyArray_DESCR(self),
2103+
descr, PyArray_DESCR(self),
21022104
0, &cast_info, &transfer_flags) != NPY_SUCCEED) {
21032105
goto fail;
21042106
}

numpy/_core/src/multiarray/nditer_constr.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,8 +1315,10 @@ npyiter_check_casting(int nop, PyArrayObject **op,
13151315
printf("\n");
13161316
#endif
13171317
/* If the types aren't equivalent, a cast is necessary */
1318-
if (op[iop] != NULL && !PyArray_EquivTypes(PyArray_DESCR(op[iop]),
1319-
op_dtype[iop])) {
1318+
npy_intp view_offset = NPY_MIN_INTP;
1319+
if (op[iop] != NULL && !(PyArray_SafeCast(
1320+
PyArray_DESCR(op[iop]), op_dtype[iop], &view_offset,
1321+
NPY_NO_CASTING, 1) && view_offset == 0)) {
13201322
/* Check read (op -> temp) casting */
13211323
if ((op_itflags[iop] & NPY_OP_ITFLAG_READ) &&
13221324
!PyArray_CanCastArrayTo(op[iop],

0 commit comments

Comments
 (0)