@@ -246,20 +246,9 @@ binary_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
246
246
{
247
247
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0 ];
248
248
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
249
+ PyArray_StringDTypeObject *common_descr = common_instance (descr1, descr2);
249
250
250
- // _eq_comparison has a short-circuit pointer comparison fast path,
251
- // so no need to check here
252
- int eq_res = _eq_comparison (descr1->coerce , descr2->coerce ,
253
- descr1->na_object , descr2->na_object );
254
-
255
- if (eq_res < 0 ) {
256
- return (NPY_CASTING)-1 ;
257
- }
258
-
259
- if (eq_res != 1 ) {
260
- PyErr_SetString (PyExc_TypeError,
261
- " Can only do binary operations with equal StringDType "
262
- " instances." );
251
+ if (common_descr == NULL ) {
263
252
return (NPY_CASTING)-1 ;
264
253
}
265
254
@@ -272,8 +261,7 @@ binary_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
272
261
273
262
if (given_descrs[2 ] == NULL ) {
274
263
out_descr = (PyArray_Descr *)new_stringdtype_instance (
275
- ((PyArray_StringDTypeObject *)given_descrs[1 ])->na_object ,
276
- ((PyArray_StringDTypeObject *)given_descrs[1 ])->coerce );
264
+ common_descr->na_object , common_descr->coerce );
277
265
278
266
if (out_descr == NULL ) {
279
267
return (NPY_CASTING)-1 ;
@@ -562,6 +550,14 @@ string_comparison_resolve_descriptors(
562
550
PyArray_Descr *const given_descrs[],
563
551
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
564
552
{
553
+ PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0 ];
554
+ PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
555
+ PyArray_StringDTypeObject *common_descr = common_instance (descr1, descr2);
556
+
557
+ if (common_descr == NULL ) {
558
+ return (NPY_CASTING)-1 ;
559
+ }
560
+
565
561
Py_INCREF (given_descrs[0 ]);
566
562
loop_descrs[0 ] = given_descrs[0 ];
567
563
Py_INCREF (given_descrs[1 ]);
@@ -788,20 +784,9 @@ string_findlike_resolve_descriptors(
788
784
{
789
785
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0 ];
790
786
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
787
+ PyArray_StringDTypeObject *common_descr = common_instance (descr1, descr2);
791
788
792
- // _eq_comparison has a short-circuit pointer comparison fast path,
793
- // so no need to check here
794
- int eq_res = _eq_comparison (descr1->coerce , descr2->coerce ,
795
- descr1->na_object , descr2->na_object );
796
-
797
- if (eq_res < 0 ) {
798
- return (NPY_CASTING)-1 ;
799
- }
800
-
801
- if (eq_res != 1 ) {
802
- PyErr_SetString (PyExc_TypeError,
803
- " Can only do binary operations with equal StringDType "
804
- " instances." );
789
+ if (common_descr == NULL ) {
805
790
return (NPY_CASTING)-1 ;
806
791
}
807
792
@@ -849,20 +834,9 @@ string_startswith_endswith_resolve_descriptors(
849
834
{
850
835
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0 ];
851
836
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
837
+ PyArray_StringDTypeObject *common_descr = common_instance (descr1, descr2);
852
838
853
- // _eq_comparison has a short-circuit pointer comparison fast path, so
854
- // no need to do it here
855
- int eq_res = _eq_comparison (descr1->coerce , descr2->coerce ,
856
- descr1->na_object , descr2->na_object );
857
-
858
- if (eq_res < 0 ) {
859
- return (NPY_CASTING)-1 ;
860
- }
861
-
862
- if (eq_res != 1 ) {
863
- PyErr_SetString (PyExc_TypeError,
864
- " Can only do binary operations with equal StringDType "
865
- " instances." );
839
+ if (common_descr == NULL ) {
866
840
return (NPY_CASTING)-1 ;
867
841
}
868
842
@@ -1061,46 +1035,6 @@ all_strings_promoter(PyObject *NPY_UNUSED(ufunc),
1061
1035
return 0 ;
1062
1036
}
1063
1037
1064
- static NPY_CASTING
1065
- strip_chars_resolve_descriptors (
1066
- struct PyArrayMethodObject_tag *NPY_UNUSED (method),
1067
- PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
1068
- PyArray_Descr *const given_descrs[],
1069
- PyArray_Descr *loop_descrs[],
1070
- npy_intp *NPY_UNUSED(view_offset))
1071
- {
1072
- Py_INCREF (given_descrs[0 ]);
1073
- loop_descrs[0 ] = given_descrs[0 ];
1074
-
1075
- // we don't actually care about the null behavior of the second argument,
1076
- // so no need to check if the first two descrs are equal like in
1077
- // binary_resolve_descriptors
1078
-
1079
- Py_INCREF (given_descrs[1 ]);
1080
- loop_descrs[1 ] = given_descrs[1 ];
1081
-
1082
- PyArray_Descr *out_descr = NULL ;
1083
-
1084
- if (given_descrs[2 ] == NULL ) {
1085
- out_descr = (PyArray_Descr *)new_stringdtype_instance (
1086
- ((PyArray_StringDTypeObject *)given_descrs[0 ])->na_object ,
1087
- ((PyArray_StringDTypeObject *)given_descrs[0 ])->coerce );
1088
-
1089
- if (out_descr == NULL ) {
1090
- return (NPY_CASTING)-1 ;
1091
- }
1092
- }
1093
- else {
1094
- Py_INCREF (given_descrs[2 ]);
1095
- out_descr = given_descrs[2 ];
1096
- }
1097
-
1098
- loop_descrs[2 ] = out_descr;
1099
-
1100
- return NPY_NO_CASTING;
1101
- }
1102
-
1103
-
1104
1038
NPY_NO_EXPORT int
1105
1039
string_lrstrip_chars_strided_loop (
1106
1040
PyArrayMethod_Context *context, char *const data[],
@@ -1309,21 +1243,10 @@ replace_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
1309
1243
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
1310
1244
PyArray_StringDTypeObject *descr3 = (PyArray_StringDTypeObject *)given_descrs[2 ];
1311
1245
1312
- // _eq_comparison has a short-circuit pointer comparison fast path, so
1313
- // no need to do it here
1314
- int eq_res = (_eq_comparison (descr1->coerce , descr2->coerce ,
1315
- descr1->na_object , descr2->na_object ) &&
1316
- _eq_comparison (descr1->coerce , descr3->coerce ,
1317
- descr1->na_object , descr3->na_object ));
1318
-
1319
- if (eq_res < 0 ) {
1320
- return (NPY_CASTING)-1 ;
1321
- }
1246
+ PyArray_StringDTypeObject *common_descr = common_instance (
1247
+ common_instance (descr1, descr2), descr3);
1322
1248
1323
- if (eq_res != 1 ) {
1324
- PyErr_SetString (PyExc_TypeError,
1325
- " String replace is only supported with equal StringDType "
1326
- " instances." );
1249
+ if (common_descr == NULL ) {
1327
1250
return (NPY_CASTING)-1 ;
1328
1251
}
1329
1252
@@ -1340,8 +1263,7 @@ replace_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
1340
1263
1341
1264
if (given_descrs[4 ] == NULL ) {
1342
1265
out_descr = (PyArray_Descr *)new_stringdtype_instance (
1343
- ((PyArray_StringDTypeObject *)given_descrs[0 ])->na_object ,
1344
- ((PyArray_StringDTypeObject *)given_descrs[0 ])->coerce );
1266
+ common_descr->na_object , common_descr->coerce );
1345
1267
1346
1268
if (out_descr == NULL ) {
1347
1269
return (NPY_CASTING)-1 ;
@@ -1588,18 +1510,9 @@ center_ljust_rjust_resolve_descriptors(
1588
1510
{
1589
1511
PyArray_StringDTypeObject *input_descr = (PyArray_StringDTypeObject *)given_descrs[0 ];
1590
1512
PyArray_StringDTypeObject *fill_descr = (PyArray_StringDTypeObject *)given_descrs[2 ];
1513
+ PyArray_StringDTypeObject *common_descr = common_instance (input_descr, fill_descr);
1591
1514
1592
- int eq_res = _eq_comparison (input_descr->coerce , fill_descr->coerce ,
1593
- input_descr->na_object , fill_descr->na_object );
1594
-
1595
- if (eq_res < 0 ) {
1596
- return (NPY_CASTING)-1 ;
1597
- }
1598
-
1599
- if (eq_res != 1 ) {
1600
- PyErr_SetString (PyExc_TypeError,
1601
- " Can only do text justification operations with equal"
1602
- " StringDType instances." );
1515
+ if (common_descr == NULL ) {
1603
1516
return (NPY_CASTING)-1 ;
1604
1517
}
1605
1518
@@ -1614,8 +1527,7 @@ center_ljust_rjust_resolve_descriptors(
1614
1527
1615
1528
if (given_descrs[3 ] == NULL ) {
1616
1529
out_descr = (PyArray_Descr *)new_stringdtype_instance (
1617
- ((PyArray_StringDTypeObject *)given_descrs[1 ])->na_object ,
1618
- ((PyArray_StringDTypeObject *)given_descrs[1 ])->coerce );
1530
+ common_descr->na_object , common_descr->coerce );
1619
1531
1620
1532
if (out_descr == NULL ) {
1621
1533
return (NPY_CASTING)-1 ;
@@ -1888,6 +1800,7 @@ zfill_strided_loop(PyArrayMethod_Context *context,
1888
1800
return -1 ;
1889
1801
}
1890
1802
1803
+
1891
1804
static NPY_CASTING
1892
1805
string_partition_resolve_descriptors (
1893
1806
PyArrayMethodObject *self,
@@ -1901,14 +1814,23 @@ string_partition_resolve_descriptors(
1901
1814
" currently support the 'out' keyword" , self->name );
1902
1815
return (NPY_CASTING)-1 ;
1903
1816
}
1904
- for (int i=0 ; i<2 ; i++) {
1905
- Py_INCREF (given_descrs[i]);
1906
- loop_descrs[i] = given_descrs[i];
1817
+
1818
+ PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0 ];
1819
+ PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1 ];
1820
+ PyArray_StringDTypeObject *common_descr = common_instance (descr1, descr2);
1821
+
1822
+ if (common_descr == NULL ) {
1823
+ return (NPY_CASTING)-1 ;
1907
1824
}
1908
- PyArray_StringDTypeObject *adescr = (PyArray_StringDTypeObject *)given_descrs[0 ];
1825
+
1826
+ Py_INCREF (given_descrs[0 ]);
1827
+ loop_descrs[0 ] = given_descrs[0 ];
1828
+ Py_INCREF (given_descrs[1 ]);
1829
+ loop_descrs[1 ] = given_descrs[1 ];
1830
+
1909
1831
for (int i=2 ; i<5 ; i++) {
1910
1832
loop_descrs[i] = (PyArray_Descr *)new_stringdtype_instance (
1911
- adescr ->na_object , adescr ->coerce );
1833
+ common_descr ->na_object , common_descr ->coerce );
1912
1834
if (loop_descrs[i] == NULL ) {
1913
1835
return (NPY_CASTING)-1 ;
1914
1836
}
@@ -2655,7 +2577,7 @@ init_stringdtype_ufuncs(PyObject *umath)
2655
2577
2656
2578
for (int i=0 ; i<3 ; i++) {
2657
2579
if (init_ufunc (umath, strip_chars_names[i], strip_chars_dtypes,
2658
- &strip_chars_resolve_descriptors ,
2580
+ &binary_resolve_descriptors ,
2659
2581
&string_lrstrip_chars_strided_loop,
2660
2582
2 , 1 , NPY_NO_CASTING, (NPY_ARRAYMETHOD_FLAGS) 0 ,
2661
2583
&strip_types[i]) < 0 ) {
0 commit comments