Skip to content

Commit 32bb744

Browse files
committed
Try to fix PyInterpreterState_Get() on Windows with Python 3.9
1 parent 3d92fbf commit 32bb744

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎pythoncapi_compat.h‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
357357
#if ((PY_VERSION_HEX < 0x030900A5 \
358358
|| (defined(PYPY_VERSION) && PY_VERSION_HEX < 0x030C0000)) \
359359
&& !(defined(Py_LIMITED_API) && PY_VERSION_HEX < 0x030A0000))
360+
361+
#if !(defined(Py_LIMITED_API) && PY_VERSION_HEX < 0x030A0000 && defined(MS_WINDOWS))
360362
static inline PyInterpreterState* PyInterpreterState_Get(void)
361363
{
362364
PyThreadState *tstate;
@@ -372,10 +374,33 @@ static inline PyInterpreterState* PyInterpreterState_Get(void)
372374
}
373375
return interp;
374376
}
377+
#else
378+
// PyInterpreterState_Get() is in the limited C API 3.9, but not in the 3.9
379+
// stable ABI on Windows. It was added to stable ABI on Windows in Python 3.10.
380+
// Use a macro to override C API function.
381+
static inline PyInterpreterState*
382+
PythonCAPI_Compat_PyInterpreterState_Get(void)
383+
{
384+
PyThreadState *tstate;
385+
PyInterpreterState *interp;
386+
387+
tstate = PyThreadState_GET();
388+
if (tstate == _Py_NULL) {
389+
Py_FatalError("GIL released (tstate is NULL)");
390+
}
391+
interp = tstate->interp;
392+
if (interp == _Py_NULL) {
393+
Py_FatalError("no current interpreter");
394+
}
395+
return interp;
396+
}
397+
#define PyInterpreterState_Get() PythonCAPI_Compat_PyInterpreterState_Get()
398+
#endif
399+
375400
#endif
376401

377402

378-
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
403+
// bpo-39947 added PyThreadState_GetID() to Python 3.9.0a6
379404
#if (0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 \
380405
&& !defined(PYPY_VERSION) && !defined(Py_LIMITED_API))
381406
static inline uint64_t PyThreadState_GetID(PyThreadState *tstate)

0 commit comments

Comments
 (0)