@@ -12,6 +12,34 @@ gil_error(PyObject *type, const char *msg)
12
12
PyGILState_Release (gstate );
13
13
}
14
14
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
+
15
43
// string to string
16
44
17
45
static NPY_CASTING
@@ -81,32 +109,6 @@ static char *p2s_name = "cast_PandasStringDType_to_StringDType";
81
109
82
110
// unicode to string
83
111
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
-
110
112
// Find the number of bytes, *utf8_bytes*, needed to store the string
111
113
// represented by *codepoints* in UTF-8. The array of *codepoints* is
112
114
// *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[],
252
254
return 0 ;
253
255
}
254
256
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 }};
259
261
260
262
static char * u2s_name = "cast_Unicode_to_StringDType" ;
261
263
@@ -451,32 +453,6 @@ static char *s2b_name = "cast_StringDType_to_Bool";
451
453
452
454
// bool to string
453
455
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
-
480
456
static int
481
457
bool_to_string (PyArrayMethod_Context * NPY_UNUSED (context ), char * const data [],
482
458
npy_intp const dimensions [], npy_intp const strides [],
@@ -516,10 +492,10 @@ bool_to_string(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
516
492
return 0 ;
517
493
}
518
494
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 }};
523
499
524
500
static char * b2s_name = "cast_Bool_to_StringDType" ;
525
501
@@ -605,32 +581,6 @@ uint_to_string(unsigned long long in, char *out)
605
581
return pylong_to_string (pylong_val , out );
606
582
}
607
583
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
-
634
584
#define STRING_TO_INT (typename , typekind , shortname , numpy_tag , printf_code , \
635
585
npy_longtype ) \
636
586
static NPY_CASTING string_to_##typename##_resolve_descriptors( \
@@ -721,7 +671,7 @@ int_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
721
671
\
722
672
static PyType_Slot shortname##2s_slots [] = { \
723
673
{NPY_METH_resolve_descriptors, \
724
- &int_to_string_resolve_descriptors}, \
674
+ &any_to_string_UNSAFE_resolve_descriptors}, \
725
675
{NPY_METH_strided_loop, &typename##_to_string}, \
726
676
{0, NULL}}; \
727
677
\
0 commit comments