File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -146,8 +146,15 @@ PyEventLoop PyEventLoop::_getLoopOnThread(PyThreadState *tstate) {
146
146
// However, simply replacing it with `PyThreadState_GetDict()` does not work,
147
147
// since the public `PyThreadState_GetDict()` API can only get from the current thread.
148
148
// 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
+ }
151
158
}
152
159
PyObject *ts_dict = tstate->dict ;
153
160
#elif PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9
You can’t perform that action at this time.
0 commit comments