@@ -210,6 +210,62 @@ stringdtype_compatible_na(PyObject *na1, PyObject *na2, PyObject **out_na) { |
210 | 210 | return 0; |
211 | 211 | } |
212 | 212 |
|
| 213 | +// Combine the na_object and coerce attributes of the string descriptors in |
| 214 | +// descrs, ignoring descriptors of other dtypes. The na_objects must be |
| 215 | +// compatible, and coercion is only enabled if it is enabled for all string |
| 216 | +// descriptors. Returns -1 with an error set if two descriptors have |
| 217 | +// incompatible na_objects. out_na_object (a borrowed reference) and |
| 218 | +// out_coerce may be NULL if only the compatibility check is needed. |
| 219 | +NPY_NO_EXPORT int |
| 220 | +stringdtype_common_na_coerce(int ndescrs, PyArray_Descr *const descrs[], |
| 221 | + PyObject **out_na_object, int *out_coerce) |
| 222 | +{ |
| 223 | + PyObject *na_object = NULL; |
| 224 | + int coerce = 1; |
| 225 | + for (int i = 0; i < ndescrs; i++) { |
| 226 | + if (NPY_DTYPE(descrs[i]) != &PyArray_StringDType) { |
| 227 | + continue; |
| 228 | + } |
| 229 | + PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)descrs[i]; |
| 230 | + if (stringdtype_compatible_na(na_object, descr->na_object, &na_object) == -1) { |
| 231 | + return -1; |
| 232 | + } |
| 233 | + coerce = coerce && descr->coerce; |
| 234 | + } |
| 235 | + if (out_na_object != NULL) { |
| 236 | + *out_na_object = na_object; |
| 237 | + } |
| 238 | + if (out_coerce != NULL) { |
| 239 | + *out_coerce = coerce; |
| 240 | + } |
| 241 | + return 0; |
| 242 | +} |
| 243 | + |
| 244 | +// Select the descriptor that determines how nulls are handled in an |
| 245 | +// operation accepting several string operands whose descriptors may differ, |
| 246 | +// ignoring descriptors of other dtypes. The caller has already checked that |
| 247 | +// the na_objects are compatible, so the first string descriptor with a set |
| 248 | +// na_object determines the null-handling behavior for the whole operation. |
| 249 | +// Returns the first string descriptor if none has an na_object set. |
| 250 | +NPY_NO_EXPORT PyArray_StringDTypeObject * |
| 251 | +stringdtype_effective_na_descr(int ndescrs, PyArray_Descr *const descrs[]) |
| 252 | +{ |
| 253 | + PyArray_StringDTypeObject *first = NULL; |
| 254 | + for (int i = 0; i < ndescrs; i++) { |
| 255 | + if (NPY_DTYPE(descrs[i]) != &PyArray_StringDType) { |
| 256 | + continue; |
| 257 | + } |
| 258 | + PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)descrs[i]; |
| 259 | + if (descr->na_object != NULL) { |
| 260 | + return descr; |
| 261 | + } |
| 262 | + if (first == NULL) { |
| 263 | + first = descr; |
| 264 | + } |
| 265 | + } |
| 266 | + return first; |
| 267 | +} |
| 268 | + |
213 | 269 | /* |
214 | 270 | * This is used to determine the correct dtype to return when dealing |
215 | 271 | * with a mix of different dtypes (for example when creating an array |
@@ -218,18 +274,19 @@ stringdtype_compatible_na(PyObject *na1, PyObject *na2, PyObject **out_na) { |
218 | 274 | static PyArray_StringDTypeObject * |
219 | 275 | common_instance(PyArray_StringDTypeObject *dtype1, PyArray_StringDTypeObject *dtype2) |
220 | 276 | { |
| 277 | + PyArray_Descr *descrs[2] = {(PyArray_Descr *)dtype1, (PyArray_Descr *)dtype2}; |
221 | 278 | PyObject *out_na_object = NULL; |
| 279 | + int out_coerce = 1; |
222 | 280 |
|
223 | | - if (stringdtype_compatible_na( |
224 | | - dtype1->na_object, dtype2->na_object, &out_na_object) == -1) { |
| 281 | + if (stringdtype_common_na_coerce(2, descrs, &out_na_object, &out_coerce) == -1) { |
225 | 282 | PyErr_Format(PyExc_TypeError, |
226 | 283 | "Cannot find common instance for incompatible dtypes " |
227 | 284 | "'%R' and '%R'", (PyObject *)dtype1, (PyObject *)dtype2); |
228 | 285 | return NULL; |
229 | 286 | } |
230 | 287 |
|
231 | 288 | return (PyArray_StringDTypeObject *)new_stringdtype_instance( |
232 | | - out_na_object, dtype1->coerce && dtype2->coerce); |
| 289 | + out_na_object, out_coerce); |
233 | 290 | } |
234 | 291 |
|
235 | 292 | /* |
@@ -467,17 +524,12 @@ _compare(void *a, void *b, PyArray_StringDTypeObject *descr_a, |
467 | 524 | { |
468 | 525 | npy_string_allocator *allocator_a = descr_a->allocator; |
469 | 526 | npy_string_allocator *allocator_b = descr_b->allocator; |
470 | | - // descr_a and descr_b are either the same object or objects |
471 | | - // that are equal, so we can safely refer only to descr_a. |
472 | | - // This is enforced in the resolve_descriptors for comparisons |
473 | | - // |
474 | | - // Note that even though the default_string isn't checked in comparisons, |
475 | | - // it will still be the same for both descrs because the value of |
476 | | - // default_string is always the empty string unless na_object is a string. |
477 | | - int has_null = descr_a->na_object != NULL; |
478 | | - int has_string_na = descr_a->has_string_na; |
479 | | - int has_nan_na = descr_a->has_nan_na; |
480 | | - npy_static_string *default_string = &descr_a->default_string; |
| 527 | + PyArray_Descr *descrs[2] = {(PyArray_Descr *)descr_a, (PyArray_Descr *)descr_b}; |
| 528 | + PyArray_StringDTypeObject *nadescr = stringdtype_effective_na_descr(2, descrs); |
| 529 | + int has_null = nadescr->na_object != NULL; |
| 530 | + int has_string_na = nadescr->has_string_na; |
| 531 | + int has_nan_na = nadescr->has_nan_na; |
| 532 | + npy_static_string *default_string = &nadescr->default_string; |
481 | 533 | const npy_packed_static_string *ps_a = (npy_packed_static_string *)a; |
482 | 534 | npy_static_string s_a = {0, NULL}; |
483 | 535 | int a_is_null = NpyString_load(allocator_a, ps_a, &s_a); |
|