Skip to content

Commit 6ed4500

Browse files
authored
Merge pull request numpy#27057 from ngoldbaum/fix-array-assign-subscript
2 parents f9dcb45 + 6a0d05c commit 6ed4500

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

numpy/_core/src/multiarray/mapping.c

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

2037+
int allocated_array = 0;
20372038
if (tmp_arr == NULL) {
20382039
/* Fill extra op, need to swap first */
20392040
tmp_arr = mit->extra_op;
@@ -2047,6 +2048,7 @@ array_assign_subscript(PyArrayObject *self, PyObject *ind, PyObject *op)
20472048
if (PyArray_CopyObject(tmp_arr, op) < 0) {
20482049
goto fail;
20492050
}
2051+
allocated_array = 1;
20502052
}
20512053

20522054
if (PyArray_MapIterCheckIndices(mit) < 0) {
@@ -2090,10 +2092,12 @@ array_assign_subscript(PyArrayObject *self, PyObject *ind, PyObject *op)
20902092
/* May need a generic copy function (only for refs and odd sizes) */
20912093
NPY_ARRAYMETHOD_FLAGS transfer_flags;
20922094
npy_intp itemsize = PyArray_ITEMSIZE(self);
2093-
2094-
if (PyArray_GetDTypeTransferFunction(1,
2095-
itemsize, itemsize,
2096-
PyArray_DESCR(self), PyArray_DESCR(self),
2095+
// TODO: the heuristic used here to determine the src_dtype might be subtly wrong
2096+
// for non-REFCHK user DTypes. See gh-27057 for the prior discussion about this.
2097+
if (PyArray_GetDTypeTransferFunction(
2098+
1, itemsize, itemsize,
2099+
allocated_array ? PyArray_DESCR(mit->extra_op) : PyArray_DESCR(self),
2100+
PyArray_DESCR(self),
20972101
0, &cast_info, &transfer_flags) != NPY_SUCCEED) {
20982102
goto fail;
20992103
}

numpy/_core/tests/test_stringdtype.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,15 @@ def test_fancy_indexing(string_list):
495495
sarr = np.array(string_list, dtype="T")
496496
assert_array_equal(sarr, sarr[np.arange(sarr.shape[0])])
497497

498-
# see gh-27003
499-
for ind in [[0, 1], ...]:
500-
a = np.array(['a'*16, 'b'*16], dtype="T")
501-
b = np.array(['d'*16, 'e'*16], dtype="T")
502-
a[ind] = b
503-
assert_array_equal(a, b)
504-
assert a[0] == 'd'*16
498+
# see gh-27003 and gh-27053
499+
for ind in [[True, True], [0, 1], ...]:
500+
for lop in [['a'*16, 'b'*16], ['', '']]:
501+
a = np.array(lop, dtype="T")
502+
rop = ['d'*16, 'e'*16]
503+
for b in [rop, np.array(rop, dtype="T")]:
504+
a[ind] = b
505+
assert_array_equal(a, b)
506+
assert a[0] == 'd'*16
505507

506508

507509
def test_creation_functions():

0 commit comments

Comments
 (0)