Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e427c6

Browse files
authored
bpo-39947: Add _PyThreadState_GetDict() function (GH-19160)
1 parent 302e5a8 commit 0e427c6

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

Include/cpython/pystate.h

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
149149
* if it is NULL. */
150150
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
151151

152+
PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
153+
152154
/* PyGILState */
153155

154156
/* Helper/diagnostic function - return 1 if the current thread

Modules/_asynciomodule.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,13 @@ get_running_loop(PyObject **loop)
236236
rl = cached_running_holder; // borrowed
237237
}
238238
else {
239-
if (ts->dict == NULL) {
239+
PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed
240+
if (ts_dict == NULL) {
240241
goto not_found;
241242
}
242243

243244
rl = _PyDict_GetItemIdWithError(
244-
ts->dict, &PyId___asyncio_running_event_loop__); // borrowed
245+
ts_dict, &PyId___asyncio_running_event_loop__); // borrowed
245246
if (rl == NULL) {
246247
if (PyErr_Occurred()) {
247248
goto error;

Python/pystate.c

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#include "Python.h"
55
#include "pycore_ceval.h"
66
#include "pycore_initconfig.h"
7+
#include "pycore_pyerrors.h"
8+
#include "pycore_pylifecycle.h"
79
#include "pycore_pymem.h"
810
#include "pycore_pystate.h"
9-
#include "pycore_pylifecycle.h"
1011

1112
/* --------------------------------------------------------------------------
1213
CAUTION
@@ -979,20 +980,28 @@ PyThreadState_Swap(PyThreadState *newts)
979980
PyThreadState_GetDict() returns NULL, an exception has *not* been raised
980981
and the caller should assume no per-thread state is available. */
981982

983+
PyObject *
984+
_PyThreadState_GetDict(PyThreadState *tstate)
985+
{
986+
assert(tstate != NULL);
987+
if (tstate->dict == NULL) {
988+
tstate->dict = PyDict_New();
989+
if (tstate->dict == NULL) {
990+
_PyErr_Clear(tstate);
991+
}
992+
}
993+
return tstate->dict;
994+
}
995+
996+
982997
PyObject *
983998
PyThreadState_GetDict(void)
984999
{
9851000
PyThreadState *tstate = _PyThreadState_GET();
986-
if (tstate == NULL)
1001+
if (tstate == NULL) {
9871002
return NULL;
988-
989-
if (tstate->dict == NULL) {
990-
PyObject *d;
991-
tstate->dict = d = PyDict_New();
992-
if (d == NULL)
993-
PyErr_Clear();
9941003
}
995-
return tstate->dict;
1004+
return _PyThreadState_GetDict(tstate);
9961005
}
9971006

9981007

0 commit comments

Comments
 (0)