@@ -765,15 +765,16 @@ static inline int
765
765
AK_TM_transfer_object (TriMapObject * tm ,
766
766
bool from_src ,
767
767
PyArrayObject * array_from ,
768
- PyArrayObject * array_to
768
+ PyArrayObject * array_to ,
769
+ bool clear_target
769
770
) {
770
771
Py_ssize_t one_count = from_src ? tm -> src_one_count : tm -> dst_one_count ;
771
772
TriMapOne * one_pairs = from_src ? tm -> src_one : tm -> dst_one ;
772
773
773
774
// NOTE: could use PyArray_Scalar instead of PyArray_GETITEM if we wanted to store scalars instead of Python objects; however, that is pretty uncommon for object arrays to store PyArray_Scalars
774
775
bool f_is_obj = PyArray_TYPE (array_from ) == NPY_OBJECT ;
775
776
776
- // the passed in object array is assumed to be contiguous and have NULL (not None) in each position
777
+ // the passed in object array is contiguous and have NULL (not None) in each position
777
778
PyObject * * array_to_data = (PyObject * * )PyArray_DATA (array_to );
778
779
PyObject * pyo ;
779
780
void * f ;
@@ -788,6 +789,9 @@ AK_TM_transfer_object(TriMapObject* tm,
788
789
else { // will convert any value to an object
789
790
pyo = PyArray_GETITEM (array_from , f );
790
791
}
792
+ if (clear_target ) {
793
+ Py_XDECREF (array_to_data [o -> to ]);
794
+ }
791
795
array_to_data [o -> to ] = pyo ;
792
796
}
793
797
PyObject * * t ;
@@ -810,9 +814,12 @@ AK_TM_transfer_object(TriMapObject* tm,
810
814
}
811
815
while (t < t_end ) {
812
816
Py_INCREF (pyo ); // one more than we need
817
+ if (clear_target ) {
818
+ Py_XDECREF (* t );
819
+ }
813
820
* t ++ = pyo ;
814
821
}
815
- Py_DECREF (pyo ); // remove the extra one
822
+ Py_DECREF (pyo ); // remove the extra ref
816
823
}
817
824
else { // from_dst, dst is an array
818
825
dst_pos = 0 ;
@@ -827,6 +834,9 @@ AK_TM_transfer_object(TriMapObject* tm,
827
834
else {
828
835
pyo = PyArray_GETITEM (array_from , f );
829
836
}
837
+ if (clear_target ) {
838
+ Py_XDECREF (* t );
839
+ }
830
840
* t ++ = pyo ;
831
841
dst_pos ++ ;
832
842
}
@@ -998,7 +1008,7 @@ AK_TM_map_no_fill(TriMapObject* tm,
998
1008
}
999
1009
// transfer values
1000
1010
if (dtype_is_obj ) {
1001
- if (AK_TM_transfer_object (tm , from_src , array_from , array_to )) {
1011
+ if (AK_TM_transfer_object (tm , from_src , array_from , array_to , false )) {
1002
1012
Py_DECREF ((PyObject * )array_to );
1003
1013
return NULL ;
1004
1014
}
@@ -1123,10 +1133,16 @@ TriMap_map_merge(TriMapObject *tm, PyObject *args)
1123
1133
bool transfer_from_dst = PyArray_SIZE ((PyArrayObject * )tm -> final_src_fill ) != 0 ;
1124
1134
1125
1135
if (dtype_is_obj ) {
1126
- if (AK_TM_transfer_object (tm , true, array_src , array_to )) {
1136
+ if (AK_TM_transfer_object (tm , true, array_src , array_to , false )) {
1127
1137
Py_DECREF ((PyObject * )array_to );
1128
1138
return NULL ;
1129
1139
}
1140
+ if (transfer_from_dst ) {
1141
+ if (AK_TM_transfer_object (tm , false, array_dst , array_to , true)) {
1142
+ Py_DECREF ((PyObject * )array_to );
1143
+ return NULL ;
1144
+ }
1145
+ }
1130
1146
}
1131
1147
else if (dtype_is_unicode ) {
1132
1148
AK_TM_TRANSFER_FLEXIBLE (Py_UCS4 , true, array_src , array_to );
@@ -1152,34 +1168,9 @@ TriMap_map_merge(TriMapObject *tm, PyObject *args)
1152
1168
}
1153
1169
}
1154
1170
}
1155
-
1156
1171
return (PyObject * )array_to ;
1157
-
1158
- // // transfer values
1159
- // if (dtype_is_obj) {
1160
- // if (AK_TM_transfer_object(tm, from_src, array_from, array_to)) {
1161
- // Py_DECREF((PyObject*)array_to);
1162
- // return NULL;
1163
- // }
1164
- // }
1165
- // else if (dtype_is_unicode) {
1166
- // AK_TM_TRANSFER_FLEXIBLE(Py_UCS4);
1167
- // }
1168
- // else if (dtype_is_string) {
1169
- // AK_TM_TRANSFER_FLEXIBLE(char);
1170
- // }
1171
- // else {
1172
- // if (AK_TM_transfer_scalar(tm, from_src, array_from, array_to)) {
1173
- // Py_DECREF((PyObject*)array_to);
1174
- // return NULL;
1175
- // }
1176
- // }
1177
- // PyArray_CLEARFLAGS(array_to, NPY_ARRAY_WRITEABLE);
1178
- // return (PyObject*)array_to;
1179
1172
}
1180
1173
1181
-
1182
-
1183
1174
// Returns NULL on error.
1184
1175
static inline PyObject *
1185
1176
AK_TM_map_fill (TriMapObject * tm ,
@@ -1232,7 +1223,7 @@ AK_TM_map_fill(TriMapObject* tm,
1232
1223
}
1233
1224
// array_from, array_to inc refed and dec refed on error
1234
1225
if (dtype_is_obj ) {
1235
- if (AK_TM_transfer_object (tm , from_src , array_from , array_to )) {
1226
+ if (AK_TM_transfer_object (tm , from_src , array_from , array_to , false )) {
1236
1227
goto error ;
1237
1228
}
1238
1229
if (AK_TM_fill_object (tm , from_src , array_to , fill_value )) {
0 commit comments