Skip to content

Commit 8d2f1a1

Browse files
committed
WIP: _PyThreadState_GetDict(tstate) API gets removed in Python 3.13
1 parent 9fd5412 commit 8d2f1a1

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

include/PyEventLoop.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,4 @@ private:
331331
static inline std::vector<AsyncHandle> _timeoutIdMap;
332332
};
333333

334-
// See https://github.com/python/cpython/blob/v3.13.0rc1/Python/pystate.c#L1940-L1951
335-
// Python 3.13 removed it from the public API. Re-exporting here.
336-
extern PyObject *_PyThreadState_GetDict(PyThreadState *tstate);
337-
338334
#endif

src/PyEventLoop.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ PyEventLoop PyEventLoop::_loopNotFound() {
140140
/* static */
141141
PyEventLoop PyEventLoop::_getLoopOnThread(PyThreadState *tstate) {
142142
// Modified from Python 3.9 `get_running_loop` https://github.com/python/cpython/blob/7cb3a44/Modules/_asynciomodule.c#L241-L278
143-
#if PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9
143+
144+
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
145+
// The private `_PyThreadState_GetDict(tstate)` API gets removed in Python 3.13.
146+
// However, the fix below cannot be a perfect replacement since the public `PyThreadState_GetDict()` API can only get from the current thread.
147+
// We need to somehow get the thread dictionary on the main thread instead of the current thread.
148+
PyObject *ts_dict = PyThreadState_GetDict();
149+
#elif PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9
144150
PyObject *ts_dict = _PyThreadState_GetDict(tstate); // borrowed reference
145151
#else // Python 3.8
146152
PyObject *ts_dict = tstate->dict; // see https://github.com/python/cpython/blob/v3.8.17/Modules/_asynciomodule.c#L244-L245

0 commit comments

Comments
 (0)