bpo-39947: Add _PyThreadState_GetDict() function (GH-19160)

This commit is contained in:
Victor Stinner 2020-03-25 21:22:55 +01:00 committed by GitHub
parent 302e5a8f79
commit 0e427c6d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View File

@ -149,6 +149,8 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
* if it is NULL. */ * if it is NULL. */
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void); PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
/* PyGILState */ /* PyGILState */
/* Helper/diagnostic function - return 1 if the current thread /* Helper/diagnostic function - return 1 if the current thread

View File

@ -236,12 +236,13 @@ get_running_loop(PyObject **loop)
rl = cached_running_holder; // borrowed rl = cached_running_holder; // borrowed
} }
else { else {
if (ts->dict == NULL) { PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed
if (ts_dict == NULL) {
goto not_found; goto not_found;
} }
rl = _PyDict_GetItemIdWithError( rl = _PyDict_GetItemIdWithError(
ts->dict, &PyId___asyncio_running_event_loop__); // borrowed ts_dict, &PyId___asyncio_running_event_loop__); // borrowed
if (rl == NULL) { if (rl == NULL) {
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
goto error; goto error;

View File

@ -4,9 +4,10 @@
#include "Python.h" #include "Python.h"
#include "pycore_ceval.h" #include "pycore_ceval.h"
#include "pycore_initconfig.h" #include "pycore_initconfig.h"
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h" #include "pycore_pymem.h"
#include "pycore_pystate.h" #include "pycore_pystate.h"
#include "pycore_pylifecycle.h"
/* -------------------------------------------------------------------------- /* --------------------------------------------------------------------------
CAUTION CAUTION
@ -979,20 +980,28 @@ PyThreadState_Swap(PyThreadState *newts)
PyThreadState_GetDict() returns NULL, an exception has *not* been raised PyThreadState_GetDict() returns NULL, an exception has *not* been raised
and the caller should assume no per-thread state is available. */ and the caller should assume no per-thread state is available. */
PyObject *
_PyThreadState_GetDict(PyThreadState *tstate)
{
assert(tstate != NULL);
if (tstate->dict == NULL) {
tstate->dict = PyDict_New();
if (tstate->dict == NULL) {
_PyErr_Clear(tstate);
}
}
return tstate->dict;
}
PyObject * PyObject *
PyThreadState_GetDict(void) PyThreadState_GetDict(void)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) if (tstate == NULL) {
return NULL; return NULL;
if (tstate->dict == NULL) {
PyObject *d;
tstate->dict = d = PyDict_New();
if (d == NULL)
PyErr_Clear();
} }
return tstate->dict; return _PyThreadState_GetDict(tstate);
} }