Skip to content

Commit ec20bdf

Browse files
committed
fix: replacement for the _PyThreadState_GetDict(tstate) API in Python 3.13
1 parent 92798a8 commit ec20bdf

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/PyEventLoop.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,15 @@ PyEventLoop PyEventLoop::_getLoopOnThread(PyThreadState *tstate) {
146146
// However, simply replacing it with `PyThreadState_GetDict()` does not work,
147147
// since the public `PyThreadState_GetDict()` API can only get from the current thread.
148148
// We need to somehow get the thread dictionary on the main thread instead of the current thread.
149-
if (tstate == NULL) {
150-
return _loopNotFound();
149+
if (!tstate->dict) {
150+
// Modified from https://github.com/python/cpython/blob/v3.13.0rc1/Python/pystate.c#L1934-L1951
151+
tstate->dict = PyDict_New();
152+
if (tstate->dict == NULL) { // when it's still null, no per-thread state is available, and an exception should not be raised
153+
PyObject *old_exc = tstate->current_exception;
154+
tstate->current_exception = NULL; // _PyErr_Clear(tstate)
155+
Py_XDECREF(old_exc);
156+
return _loopNotFound();
157+
}
151158
}
152159
PyObject *ts_dict = tstate->dict;
153160
#elif PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9

0 commit comments

Comments
 (0)