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