Skip to content

Commit 7af3741

Browse files
committed
refactor to avoid repeating resolve_descriptors implementations
1 parent 79bfa3b commit 7af3741

File tree

1 file changed

+37
-87
lines changed

1 file changed

+37
-87
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 37 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@ gil_error(PyObject *type, const char *msg)
1212
PyGILState_Release(gstate);
1313
}
1414

15+
#define ANY_TO_STRING_RESOLVE_DESCRIPTORS(safety) \
16+
static NPY_CASTING any_to_string_##safety##_resolve_descriptors( \
17+
PyObject *NPY_UNUSED(self), PyArray_DTypeMeta *dtypes[2], \
18+
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2], \
19+
npy_intp *NPY_UNUSED(view_offset)) \
20+
{ \
21+
if (given_descrs[1] == NULL) { \
22+
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance( \
23+
(PyTypeObject *)dtypes[1]); \
24+
if (new == NULL) { \
25+
return (NPY_CASTING)-1; \
26+
} \
27+
loop_descrs[1] = new; \
28+
} \
29+
else { \
30+
Py_INCREF(given_descrs[1]); \
31+
loop_descrs[1] = given_descrs[1]; \
32+
} \
33+
\
34+
Py_INCREF(given_descrs[0]); \
35+
loop_descrs[0] = given_descrs[0]; \
36+
\
37+
return NPY_##safety##_CASTING; \
38+
}
39+
40+
ANY_TO_STRING_RESOLVE_DESCRIPTORS(SAFE)
41+
ANY_TO_STRING_RESOLVE_DESCRIPTORS(UNSAFE)
42+
1543
// string to string
1644

1745
static NPY_CASTING
@@ -81,32 +109,6 @@ static char *p2s_name = "cast_PandasStringDType_to_StringDType";
81109

82110
// unicode to string
83111

84-
static NPY_CASTING
85-
unicode_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
86-
PyArray_DTypeMeta *dtypes[2],
87-
PyArray_Descr *given_descrs[2],
88-
PyArray_Descr *loop_descrs[2],
89-
npy_intp *NPY_UNUSED(view_offset))
90-
{
91-
if (given_descrs[1] == NULL) {
92-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
93-
(PyTypeObject *)dtypes[1]);
94-
if (new == NULL) {
95-
return (NPY_CASTING)-1;
96-
}
97-
loop_descrs[1] = new;
98-
}
99-
else {
100-
Py_INCREF(given_descrs[1]);
101-
loop_descrs[1] = given_descrs[1];
102-
}
103-
104-
Py_INCREF(given_descrs[0]);
105-
loop_descrs[0] = given_descrs[0];
106-
107-
return NPY_SAFE_CASTING;
108-
}
109-
110112
// Find the number of bytes, *utf8_bytes*, needed to store the string
111113
// represented by *codepoints* in UTF-8. The array of *codepoints* is
112114
// *max_length* long, but may be padded with null codepoints. *num_codepoints*
@@ -252,10 +254,10 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
252254
return 0;
253255
}
254256

255-
static PyType_Slot u2s_slots[] = {
256-
{NPY_METH_resolve_descriptors, &unicode_to_string_resolve_descriptors},
257-
{NPY_METH_strided_loop, &unicode_to_string},
258-
{0, NULL}};
257+
static PyType_Slot u2s_slots[] = {{NPY_METH_resolve_descriptors,
258+
&any_to_string_SAFE_resolve_descriptors},
259+
{NPY_METH_strided_loop, &unicode_to_string},
260+
{0, NULL}};
259261

260262
static char *u2s_name = "cast_Unicode_to_StringDType";
261263

@@ -451,32 +453,6 @@ static char *s2b_name = "cast_StringDType_to_Bool";
451453

452454
// bool to string
453455

454-
static NPY_CASTING
455-
bool_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
456-
PyArray_DTypeMeta *dtypes[2],
457-
PyArray_Descr *given_descrs[2],
458-
PyArray_Descr *loop_descrs[2],
459-
npy_intp *NPY_UNUSED(view_offset))
460-
{
461-
if (given_descrs[1] == NULL) {
462-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
463-
(PyTypeObject *)dtypes[1]);
464-
if (new == NULL) {
465-
return (NPY_CASTING)-1;
466-
}
467-
loop_descrs[1] = new;
468-
}
469-
else {
470-
Py_INCREF(given_descrs[1]);
471-
loop_descrs[1] = given_descrs[1];
472-
}
473-
474-
Py_INCREF(given_descrs[0]);
475-
loop_descrs[0] = given_descrs[0];
476-
477-
return NPY_SAFE_CASTING;
478-
}
479-
480456
static int
481457
bool_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
482458
npy_intp const dimensions[], npy_intp const strides[],
@@ -516,10 +492,10 @@ bool_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
516492
return 0;
517493
}
518494

519-
static PyType_Slot b2s_slots[] = {
520-
{NPY_METH_resolve_descriptors, &bool_to_string_resolve_descriptors},
521-
{NPY_METH_strided_loop, &bool_to_string},
522-
{0, NULL}};
495+
static PyType_Slot b2s_slots[] = {{NPY_METH_resolve_descriptors,
496+
&any_to_string_SAFE_resolve_descriptors},
497+
{NPY_METH_strided_loop, &bool_to_string},
498+
{0, NULL}};
523499

524500
static char *b2s_name = "cast_Bool_to_StringDType";
525501

@@ -605,32 +581,6 @@ uint_to_string(unsigned long long in, char *out)
605581
return pylong_to_string(pylong_val, out);
606582
}
607583

608-
static NPY_CASTING
609-
int_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
610-
PyArray_DTypeMeta *dtypes[2],
611-
PyArray_Descr *given_descrs[2],
612-
PyArray_Descr *loop_descrs[2],
613-
npy_intp *NPY_UNUSED(view_offset))
614-
{
615-
if (given_descrs[1] == NULL) {
616-
PyArray_Descr *new = (PyArray_Descr *)new_stringdtype_instance(
617-
(PyTypeObject *)dtypes[1]);
618-
if (new == NULL) {
619-
return (NPY_CASTING)-1;
620-
}
621-
loop_descrs[1] = new;
622-
}
623-
else {
624-
Py_INCREF(given_descrs[1]);
625-
loop_descrs[1] = given_descrs[1];
626-
}
627-
628-
Py_INCREF(given_descrs[0]);
629-
loop_descrs[0] = given_descrs[0];
630-
631-
return NPY_UNSAFE_CASTING;
632-
}
633-
634584
#define STRING_TO_INT(typename, typekind, shortname, numpy_tag, printf_code, \
635585
npy_longtype) \
636586
static NPY_CASTING string_to_##typename##_resolve_descriptors( \
@@ -721,7 +671,7 @@ int_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
721671
\
722672
static PyType_Slot shortname##2s_slots [] = { \
723673
{NPY_METH_resolve_descriptors, \
724-
&int_to_string_resolve_descriptors}, \
674+
&any_to_string_UNSAFE_resolve_descriptors}, \
725675
{NPY_METH_strided_loop, &typename##_to_string}, \
726676
{0, NULL}}; \
727677
\

0 commit comments

Comments
 (0)