@@ -50,19 +50,18 @@ Numeric decoder derived from TCL library
50
50
#include "date_conversions.h"
51
51
#include "datetime.h"
52
52
53
- static PyTypeObject * type_decimal ;
54
- static PyTypeObject * cls_dataframe ;
55
- static PyTypeObject * cls_series ;
56
- static PyTypeObject * cls_index ;
57
- static PyTypeObject * cls_nat ;
58
- static PyTypeObject * cls_na ;
59
- PyObject * cls_timedelta ;
60
-
61
53
npy_int64 get_nat (void ) { return NPY_MIN_INT64 ; }
62
54
63
55
typedef char * (* PFN_PyTypeToUTF8 )(JSOBJ obj , JSONTypeContext * ti ,
64
56
size_t * _outLen );
65
57
58
+ int object_is_decimal_type (PyObject * obj );
59
+ int object_is_dataframe_type (PyObject * obj );
60
+ int object_is_series_type (PyObject * obj );
61
+ int object_is_index_type (PyObject * obj );
62
+ int object_is_nat_type (PyObject * obj );
63
+ int object_is_na_type (PyObject * obj );
64
+
66
65
typedef struct __NpyArrContext {
67
66
PyObject * array ;
68
67
char * dataptr ;
@@ -146,44 +145,6 @@ enum PANDAS_FORMAT { SPLIT, RECORDS, INDEX, COLUMNS, VALUES };
146
145
147
146
int PdBlock_iterNext (JSOBJ , JSONTypeContext * );
148
147
149
- void * initObjToJSON (void ) {
150
- PyObject * mod_pandas ;
151
- PyObject * mod_nattype ;
152
- PyObject * mod_natype ;
153
- PyObject * mod_decimal = PyImport_ImportModule ("decimal" );
154
- type_decimal =
155
- (PyTypeObject * )PyObject_GetAttrString (mod_decimal , "Decimal" );
156
- Py_DECREF (mod_decimal );
157
-
158
- PyDateTime_IMPORT ;
159
-
160
- mod_pandas = PyImport_ImportModule ("pandas" );
161
- if (mod_pandas ) {
162
- cls_dataframe =
163
- (PyTypeObject * )PyObject_GetAttrString (mod_pandas , "DataFrame" );
164
- cls_index = (PyTypeObject * )PyObject_GetAttrString (mod_pandas , "Index" );
165
- cls_series =
166
- (PyTypeObject * )PyObject_GetAttrString (mod_pandas , "Series" );
167
- Py_DECREF (mod_pandas );
168
- }
169
-
170
- mod_nattype = PyImport_ImportModule ("pandas._libs.tslibs.nattype" );
171
- if (mod_nattype ) {
172
- cls_nat =
173
- (PyTypeObject * )PyObject_GetAttrString (mod_nattype , "NaTType" );
174
- Py_DECREF (mod_nattype );
175
- }
176
-
177
- mod_natype = PyImport_ImportModule ("pandas._libs.missing" );
178
- if (mod_natype ) {
179
- cls_na = (PyTypeObject * )PyObject_GetAttrString (mod_natype , "NAType" );
180
- Py_DECREF (mod_natype );
181
- }
182
-
183
- // GH 31463
184
- return NULL ;
185
- }
186
-
187
148
static TypeContext * createTypeContext (void ) {
188
149
TypeContext * pc ;
189
150
@@ -216,8 +177,7 @@ static TypeContext *createTypeContext(void) {
216
177
static PyObject * get_values (PyObject * obj ) {
217
178
PyObject * values = NULL ;
218
179
219
- if (PyObject_TypeCheck (obj , cls_index ) ||
220
- PyObject_TypeCheck (obj , cls_series )) {
180
+ if (object_is_index_type (obj ) || object_is_series_type (obj )) {
221
181
// The special cases to worry about are dt64tz and category[dt64tz].
222
182
// In both cases we want the UTC-localized datetime64 ndarray,
223
183
// without going through and object array of Timestamps.
@@ -1510,12 +1470,12 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1510
1470
pc -> PyTypeToUTF8 = PyUnicodeToUTF8 ;
1511
1471
tc -> type = JT_UTF8 ;
1512
1472
return ;
1513
- } else if (PyObject_TypeCheck (obj , type_decimal )) {
1473
+ } else if (object_is_decimal_type (obj )) {
1514
1474
GET_TC (tc )-> doubleValue = PyFloat_AsDouble (obj );
1515
1475
tc -> type = JT_DOUBLE ;
1516
1476
return ;
1517
1477
} else if (PyDateTime_Check (obj ) || PyDate_Check (obj )) {
1518
- if (PyObject_TypeCheck (obj , cls_nat )) {
1478
+ if (object_is_nat_type (obj )) {
1519
1479
tc -> type = JT_NULL ;
1520
1480
return ;
1521
1481
}
@@ -1606,14 +1566,14 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1606
1566
"%R (0d array) is not JSON serializable at the moment" ,
1607
1567
obj );
1608
1568
goto INVALID ;
1609
- } else if (PyObject_TypeCheck (obj , cls_na )) {
1569
+ } else if (object_is_na_type (obj )) {
1610
1570
tc -> type = JT_NULL ;
1611
1571
return ;
1612
1572
}
1613
1573
1614
1574
ISITERABLE :
1615
1575
1616
- if (PyObject_TypeCheck (obj , cls_index )) {
1576
+ if (object_is_index_type (obj )) {
1617
1577
if (enc -> outputFormat == SPLIT ) {
1618
1578
tc -> type = JT_OBJECT ;
1619
1579
pc -> iterBegin = Index_iterBegin ;
@@ -1637,7 +1597,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1637
1597
}
1638
1598
1639
1599
return ;
1640
- } else if (PyObject_TypeCheck (obj , cls_series )) {
1600
+ } else if (object_is_series_type (obj )) {
1641
1601
if (enc -> outputFormat == SPLIT ) {
1642
1602
tc -> type = JT_OBJECT ;
1643
1603
pc -> iterBegin = Series_iterBegin ;
@@ -1701,7 +1661,7 @@ void Object_beginTypeContext(JSOBJ _obj, JSONTypeContext *tc) {
1701
1661
pc -> iterGetValue = NpyArr_iterGetValue ;
1702
1662
pc -> iterGetName = NpyArr_iterGetName ;
1703
1663
return ;
1704
- } else if (PyObject_TypeCheck (obj , cls_dataframe )) {
1664
+ } else if (object_is_dataframe_type (obj )) {
1705
1665
if (enc -> blkCtxtPassthru ) {
1706
1666
pc -> pdblock = enc -> blkCtxtPassthru ;
1707
1667
tc -> type =
@@ -1969,6 +1929,11 @@ char *Object_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen) {
1969
1929
1970
1930
PyObject * objToJSON (PyObject * Py_UNUSED (self ), PyObject * args ,
1971
1931
PyObject * kwargs ) {
1932
+ PyDateTime_IMPORT ;
1933
+ if (PyDateTimeAPI == NULL ) {
1934
+ return NULL ;
1935
+ }
1936
+
1972
1937
static char * kwlist [] = {"obj" ,
1973
1938
"ensure_ascii" ,
1974
1939
"double_precision" ,
0 commit comments