@@ -74,6 +74,7 @@ typedef struct {
74
74
PyObject * type_index ;
75
75
PyObject * type_nat ;
76
76
PyObject * type_na ;
77
+ PyObject * type_offset ;
77
78
} modulestate ;
78
79
79
80
#define modulestate (o ) ((modulestate *)PyModule_GetState(o))
@@ -211,6 +212,26 @@ int object_is_na_type(PyObject *obj) {
211
212
}
212
213
return result ;
213
214
}
215
+
216
+ int object_is_offset_type (PyObject * obj ) {
217
+ PyObject * module = PyState_FindModule (& moduledef );
218
+ if (module == NULL )
219
+ return 0 ;
220
+ modulestate * state = modulestate (module );
221
+ if (state == NULL )
222
+ return 0 ;
223
+ PyObject * type_offset = state -> type_offset ;
224
+ if (type_offset == NULL ) {
225
+ PyErr_Clear ();
226
+ return 0 ;
227
+ }
228
+ int result = PyObject_IsInstance (obj , type_offset );
229
+ if (result == -1 ) {
230
+ PyErr_Clear ();
231
+ return 0 ;
232
+ }
233
+ return result ;
234
+ }
214
235
#else
215
236
/* Used in objToJSON.c */
216
237
int object_is_decimal_type (PyObject * obj ) {
@@ -345,6 +366,27 @@ int object_is_na_type(PyObject *obj) {
345
366
return result ;
346
367
}
347
368
369
+ int object_is_offset_type (PyObject * obj ) {
370
+ PyObject * module = PyImport_ImportModule ("pandas._libs.tslibs.offsets" );
371
+ if (module == NULL ) {
372
+ PyErr_Clear ();
373
+ return 0 ;
374
+ }
375
+ PyObject * type_offset = PyObject_GetAttrString (module , "BaseOffset" );
376
+ if (type_offset == NULL ) {
377
+ Py_DECREF (module );
378
+ PyErr_Clear ();
379
+ return 0 ;
380
+ }
381
+ int result = PyObject_IsInstance (obj , type_offset );
382
+ if (result == -1 ) {
383
+ Py_DECREF (module );
384
+ Py_DECREF (type_offset );
385
+ PyErr_Clear ();
386
+ return 0 ;
387
+ }
388
+ return result ;
389
+ }
348
390
#endif
349
391
350
392
static int module_traverse (PyObject * m , visitproc visit , void * arg ) {
@@ -354,6 +396,7 @@ static int module_traverse(PyObject *m, visitproc visit, void *arg) {
354
396
Py_VISIT (modulestate (m )-> type_index );
355
397
Py_VISIT (modulestate (m )-> type_nat );
356
398
Py_VISIT (modulestate (m )-> type_na );
399
+ Py_VISIT (modulestate (m )-> type_offset );
357
400
return 0 ;
358
401
}
359
402
@@ -364,6 +407,7 @@ static int module_clear(PyObject *m) {
364
407
Py_CLEAR (modulestate (m )-> type_index );
365
408
Py_CLEAR (modulestate (m )-> type_nat );
366
409
Py_CLEAR (modulestate (m )-> type_na );
410
+ Py_CLEAR (modulestate (m )-> type_offset );
367
411
return 0 ;
368
412
}
369
413
@@ -434,6 +478,16 @@ PyMODINIT_FUNC PyInit_json(void) {
434
478
} else {
435
479
PyErr_Clear ();
436
480
}
481
+
482
+ PyObject * mod_offset = PyImport_ImportModule ("pandas._libs.tslibs.offsets" );
483
+ if (mod_offset ) {
484
+ PyObject * type_offset = PyObject_GetAttrString (mod_offset , "BaseOffset" );
485
+ assert (type_offset != NULL );
486
+ modulestate (module )-> type_offset = type_offset ;
487
+
488
+ Py_DECREF (mod_offset );
489
+ }
490
+
437
491
#endif
438
492
439
493
/* Not vendored for now
0 commit comments