Skip to content

Commit

Permalink
fix: replacement for the _PyThreadState_GetDict(tstate) API in Pyth…
Browse files Browse the repository at this point in the history
…on 3.13
  • Loading branch information
Xmader committed Sep 20, 2024
1 parent 92798a8 commit ec20bdf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/PyEventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ PyEventLoop PyEventLoop::_getLoopOnThread(PyThreadState *tstate) {
// However, simply replacing it with `PyThreadState_GetDict()` does not work,
// since the public `PyThreadState_GetDict()` API can only get from the current thread.
// We need to somehow get the thread dictionary on the main thread instead of the current thread.
if (tstate == NULL) {
return _loopNotFound();
if (!tstate->dict) {
// Modified from https://github.com/python/cpython/blob/v3.13.0rc1/Python/pystate.c#L1934-L1951
tstate->dict = PyDict_New();
if (tstate->dict == NULL) { // when it's still null, no per-thread state is available, and an exception should not be raised
PyObject *old_exc = tstate->current_exception;
tstate->current_exception = NULL; // _PyErr_Clear(tstate)
Py_XDECREF(old_exc);
return _loopNotFound();
}
}
PyObject *ts_dict = tstate->dict;
#elif PY_VERSION_HEX >= 0x03090000 // Python version is greater than 3.9
Expand Down

0 comments on commit ec20bdf

Please sign in to comment.