Skip to content

Commit bfab79f

Browse files
committed
PyCapsule_Import() every time for subinterp
1 parent f9d8aeb commit bfab79f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Include/datetime.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,24 @@ typedef struct {
194194
* */
195195
#ifndef _PY_DATETIME_IMPL
196196
/* Define global variable for the C API and a macro for setting it. */
197-
static PyDateTime_CAPI *PyDateTimeAPI = NULL;
197+
static PyDateTime_CAPI *_pydatetimeapi_main = NULL;
198198

199-
#define PyDateTime_IMPORT \
200-
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
199+
static inline void _import_pydatetime(void) {
200+
if (PyInterpreterState_Get() == PyInterpreterState_Main()) {
201+
_pydatetimeapi_main = PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0);
202+
}
203+
}
204+
#define PyDateTime_IMPORT _import_pydatetime()
205+
206+
static inline PyDateTime_CAPI *_get_pydatetime_api(void) {
207+
if (PyInterpreterState_Get() == PyInterpreterState_Main()) {
208+
return _pydatetimeapi_main;
209+
}
210+
else {
211+
return PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0);
212+
}
213+
}
214+
#define PyDateTimeAPI _get_pydatetime_api()
201215

202216
/* Macro for access to the UTC singleton */
203217
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC

0 commit comments

Comments
 (0)