@@ -1046,6 +1046,7 @@ string_lrstrip_chars_strided_loop(
1046
1046
PyArray_StringDTypeObject *s1descr = (PyArray_StringDTypeObject *)context->descriptors [0 ];
1047
1047
int has_null = s1descr->na_object != NULL ;
1048
1048
int has_string_na = s1descr->has_string_na ;
1049
+ int has_nan_na = s1descr->has_nan_na ;
1049
1050
1050
1051
const npy_static_string *default_string = &s1descr->default_string ;
1051
1052
npy_intp N = dimensions[0 ];
@@ -1072,28 +1073,47 @@ string_lrstrip_chars_strided_loop(
1072
1073
s2 = *default_string;
1073
1074
}
1074
1075
}
1076
+ else if (has_nan_na) {
1077
+ if (s2_isnull) {
1078
+ npy_gil_error (PyExc_ValueError,
1079
+ " Cannot use a null string that is not a "
1080
+ " string as the %s delimiter" , ufunc_name);
1081
+ }
1082
+ if (s1_isnull) {
1083
+ if (NpyString_pack_null (oallocator, ops) < 0 ) {
1084
+ npy_gil_error (PyExc_MemoryError,
1085
+ " Failed to deallocate string in %s" ,
1086
+ ufunc_name);
1087
+ goto fail;
1088
+ }
1089
+ goto next_step;
1090
+ }
1091
+ }
1075
1092
else {
1076
1093
npy_gil_error (PyExc_ValueError,
1077
- " Cannot strip null values that are not strings" );
1094
+ " Can only strip null values that are strings "
1095
+ " or NaN-like values" );
1078
1096
goto fail;
1079
1097
}
1080
1098
}
1099
+ {
1100
+ char *new_buf = (char *)PyMem_RawCalloc (s1.size , 1 );
1101
+ Buffer<ENCODING::UTF8> buf1 ((char *)s1.buf , s1.size );
1102
+ Buffer<ENCODING::UTF8> buf2 ((char *)s2.buf , s2.size );
1103
+ Buffer<ENCODING::UTF8> outbuf (new_buf, s1.size );
1104
+ size_t new_buf_size = string_lrstrip_chars
1105
+ (buf1, buf2, outbuf, striptype);
1081
1106
1107
+ if (NpyString_pack (oallocator, ops, new_buf, new_buf_size) < 0 ) {
1108
+ npy_gil_error (PyExc_MemoryError, " Failed to pack string in %s" ,
1109
+ ufunc_name);
1110
+ PyMem_RawFree (new_buf);
1111
+ goto fail;
1112
+ }
1082
1113
1083
- char *new_buf = (char *)PyMem_RawCalloc (s1.size , 1 );
1084
- Buffer<ENCODING::UTF8> buf1 ((char *)s1.buf , s1.size );
1085
- Buffer<ENCODING::UTF8> buf2 ((char *)s2.buf , s2.size );
1086
- Buffer<ENCODING::UTF8> outbuf (new_buf, s1.size );
1087
- size_t new_buf_size = string_lrstrip_chars
1088
- (buf1, buf2, outbuf, striptype);
1089
-
1090
- if (NpyString_pack (oallocator, ops, new_buf, new_buf_size) < 0 ) {
1091
- npy_gil_error (PyExc_MemoryError, " Failed to pack string in %s" ,
1092
- ufunc_name);
1093
- goto fail;
1114
+ PyMem_RawFree (new_buf);
1094
1115
}
1095
-
1096
- PyMem_RawFree (new_buf);
1116
+ next_step:
1097
1117
1098
1118
in1 += strides[0 ];
1099
1119
in2 += strides[1 ];
@@ -1150,8 +1170,9 @@ string_lrstrip_whitespace_strided_loop(
1150
1170
const char *ufunc_name = ((PyUFuncObject *)context->caller )->name ;
1151
1171
STRIPTYPE striptype = *(STRIPTYPE *)context->method ->static_data ;
1152
1172
PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)context->descriptors [0 ];
1153
- int has_string_na = descr->has_string_na ;
1154
1173
int has_null = descr->na_object != NULL ;
1174
+ int has_string_na = descr->has_string_na ;
1175
+ int has_nan_na = descr->has_nan_na ;
1155
1176
const npy_static_string *default_string = &descr->default_string ;
1156
1177
1157
1178
npy_string_allocator *allocators[2 ] = {};
@@ -1181,26 +1202,39 @@ string_lrstrip_whitespace_strided_loop(
1181
1202
if (has_string_na || !has_null) {
1182
1203
s = *default_string;
1183
1204
}
1205
+ else if (has_nan_na) {
1206
+ if (NpyString_pack_null (oallocator, ops) < 0 ) {
1207
+ npy_gil_error (PyExc_MemoryError,
1208
+ " Failed to deallocate string in %s" ,
1209
+ ufunc_name);
1210
+ goto fail;
1211
+ }
1212
+ goto next_step;
1213
+ }
1184
1214
else {
1185
1215
npy_gil_error (PyExc_ValueError,
1186
- " Cannot strip null values that are not strings" );
1216
+ " Can only strip null values that are strings or "
1217
+ " NaN-like values" );
1187
1218
goto fail;
1188
1219
}
1189
1220
}
1221
+ {
1222
+ char *new_buf = (char *)PyMem_RawCalloc (s.size , 1 );
1223
+ Buffer<ENCODING::UTF8> buf ((char *)s.buf , s.size );
1224
+ Buffer<ENCODING::UTF8> outbuf (new_buf, s.size );
1225
+ size_t new_buf_size = string_lrstrip_whitespace (
1226
+ buf, outbuf, striptype);
1190
1227
1191
- char *new_buf = ( char *) PyMem_RawCalloc (s. size , 1 );
1192
- Buffer<ENCODING::UTF8> buf (( char *)s. buf , s. size );
1193
- Buffer<ENCODING::UTF8> outbuf (new_buf, s. size );
1194
- size_t new_buf_size = string_lrstrip_whitespace (
1195
- buf, outbuf, striptype);
1228
+ if ( NpyString_pack (oallocator, ops, new_buf, new_buf_size) < 0 ) {
1229
+ npy_gil_error (PyExc_MemoryError, " Failed to pack string in %s " ,
1230
+ ufunc_name );
1231
+ goto fail;
1232
+ }
1196
1233
1197
- if (NpyString_pack (oallocator, ops, new_buf, new_buf_size) < 0 ) {
1198
- npy_gil_error (PyExc_MemoryError, " Failed to pack string in %s" ,
1199
- ufunc_name);
1200
- goto fail;
1234
+ PyMem_RawFree (new_buf);
1201
1235
}
1202
1236
1203
- PyMem_RawFree (new_buf);
1237
+ next_step:
1204
1238
1205
1239
in += strides[0 ];
1206
1240
out += strides[1 ];
0 commit comments