Skip to content

Commit 20185fd

Browse files
authored
Merge pull request numpy#26105 from ngoldbaum/all-string-multiply-promoter
MNT: install all-string promoter for multiply
2 parents 6bac54e + 3ac9316 commit 20185fd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

numpy/_core/src/umath/stringdtype_ufuncs.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,18 @@ init_stringdtype_ufuncs(PyObject *umath)
23392339
INIT_MULTIPLY(Int64, int64);
23402340
INIT_MULTIPLY(UInt64, uint64);
23412341

2342+
// This is needed so the generic promoters defined after this don't match
2343+
// for np.multiply(string_array, string_array)
2344+
2345+
PyArray_DTypeMeta *hdtypes[] = {
2346+
&PyArray_StringDType,
2347+
&PyArray_StringDType,
2348+
&PyArray_StringDType};
2349+
2350+
if (add_promoter(umath, "multiply", hdtypes, 3, string_multiply_promoter) < 0) {
2351+
return -1;
2352+
}
2353+
23422354
// all other integer dtypes are handled with a generic promoter
23432355

23442356
PyArray_DTypeMeta *rdtypes[] = {

numpy/_core/tests/test_stringdtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ def test_multiply_reduce():
765765
assert res == val * np.prod(repeats)
766766

767767

768+
def test_multiply_two_string_raises():
769+
arr = np.array(["hello", "world"])
770+
with pytest.raises(np._core._exceptions._UFuncNoLoopError):
771+
np.multiply(arr, arr)
772+
773+
768774
@pytest.mark.parametrize("use_out", [True, False])
769775
@pytest.mark.parametrize("other", [2, [2, 1, 3, 4, 1, 3]])
770776
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)