Skip to content

Commit 92798a8

Browse files
committed
fix: _PyThreadState_GetDict(tstate) API gets removed in Python 3.13
1 parent 3706a47 commit 92798a8

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/PyEventLoop.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ PyEventLoop PyEventLoop::_getLoopOnThread(PyThreadState *tstate) {
143143

144144
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
145145
// 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.
146+
// However, simply replacing it with `PyThreadState_GetDict()` does not work,
147+
// since the public `PyThreadState_GetDict()` API can only get from the current thread.
147148
// We need to somehow get the thread dictionary on the main thread instead of the current thread.
148-
PyObject *ts_dict = PyThreadState_GetDict();
149+
if (tstate == NULL) {
150+
return _loopNotFound();
151+
}
152+
PyObject *ts_dict = tstate->dict;
149153
#elif PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9
150154
PyObject *ts_dict = _PyThreadState_GetDict(tstate); // borrowed reference
151155
#else // Python 3.8

0 commit comments

Comments
 (0)