GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: centralized helper for output coerce and na_object in stringdtyp… · ngoldbaum/numpy@993cb9a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/_core
    • src
      • multiarray/stringdtype
        • dtype.c
        • dtype.h
      • umath
        • stringdtype_ufuncs.cpp
    • tests
      • test_stringdtype.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/_core/src/multiarray/stringdtype/dtype.c‎

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/stringdtype/dtype.c
+66-14Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,62 @@ stringdtype_compatible_na(PyObject *na1, PyObject *na2, PyObject **out_na) {
210210
return 0;
211211
}
212212

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+
213269
/*
214270
* This is used to determine the correct dtype to return when dealing
215271
* 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) {
218274
static PyArray_StringDTypeObject *
219275
common_instance(PyArray_StringDTypeObject *dtype1, PyArray_StringDTypeObject *dtype2)
220276
{
277+
PyArray_Descr *descrs[2] = {(PyArray_Descr *)dtype1, (PyArray_Descr *)dtype2};
221278
PyObject *out_na_object = NULL;
279+
int out_coerce = 1;
222280

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) {
225282
PyErr_Format(PyExc_TypeError,
226283
"Cannot find common instance for incompatible dtypes "
227284
"'%R' and '%R'", (PyObject *)dtype1, (PyObject *)dtype2);
228285
return NULL;
229286
}
230287

231288
return (PyArray_StringDTypeObject *)new_stringdtype_instance(
232-
out_na_object, dtype1->coerce && dtype2->coerce);
289+
out_na_object, out_coerce);
233290
}
234291

235292
/*
@@ -467,17 +524,12 @@ _compare(void *a, void *b, PyArray_StringDTypeObject *descr_a,
467524
{
468525
npy_string_allocator *allocator_a = descr_a->allocator;
469526
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;
481533
const npy_packed_static_string *ps_a = (npy_packed_static_string *)a;
482534
npy_static_string s_a = {0, NULL};
483535
int a_is_null = NpyString_load(allocator_a, ps_a, &s_a);
Collapse file

‎numpy/_core/src/multiarray/stringdtype/dtype.h‎

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/stringdtype/dtype.h
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ _eq_comparison(int scoerce, int ocoerce, PyObject *sna, PyObject *ona);
5656
NPY_NO_EXPORT int
5757
stringdtype_compatible_na(PyObject *na1, PyObject *na2, PyObject **out_na);
5858

59+
NPY_NO_EXPORT int
60+
stringdtype_common_na_coerce(int ndescrs, PyArray_Descr *const descrs[],
61+
PyObject **out_na_object, int *out_coerce);
62+
63+
NPY_NO_EXPORT PyArray_StringDTypeObject *
64+
stringdtype_effective_na_descr(int ndescrs, PyArray_Descr *const descrs[]);
65+
5966
NPY_NO_EXPORT int
6067
na_eq_cmp(PyObject *a, PyObject *b);
6168

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.