]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39947: Add _PyThreadState_GetDict() function (GH-19160)
authorVictor Stinner <vstinner@python.org>
Wed, 25 Mar 2020 20:22:55 +0000 (21:22 +0100)
committerGitHub <noreply@github.com>
Wed, 25 Mar 2020 20:22:55 +0000 (21:22 +0100)
Include/cpython/pystate.h
Modules/_asynciomodule.c
Python/pystate.c

index 4ea509dc6eeff103d5e7e6f6b6197973b193d066..27097270a0cb917a89bf9009bde6c3c363a9c30b 100644 (file)
@@ -149,6 +149,8 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
  * if it is NULL. */
 PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
 
+PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
+
 /* PyGILState */
 
 /* Helper/diagnostic function - return 1 if the current thread
index fa94a65f1cea8f72bdaafafbb0ed833ff0a41fe7..1092b08411474e15416a8352cb2c56a07787c97d 100644 (file)
@@ -236,12 +236,13 @@ get_running_loop(PyObject **loop)
         rl = cached_running_holder;  // borrowed
     }
     else {
-        if (ts->dict == NULL) {
+        PyObject *ts_dict = _PyThreadState_GetDict(ts);  // borrowed
+        if (ts_dict == NULL) {
             goto not_found;
         }
 
         rl = _PyDict_GetItemIdWithError(
-            ts->dict, &PyId___asyncio_running_event_loop__);  // borrowed
+            ts_dict, &PyId___asyncio_running_event_loop__);  // borrowed
         if (rl == NULL) {
             if (PyErr_Occurred()) {
                 goto error;
index c7154ea7060a2d56cedba87f9f13d6f36eaf2f8a..66a1d3b492d4bb11184da763795d254c9b80b1f3 100644 (file)
@@ -4,9 +4,10 @@
 #include "Python.h"
 #include "pycore_ceval.h"
 #include "pycore_initconfig.h"
+#include "pycore_pyerrors.h"
+#include "pycore_pylifecycle.h"
 #include "pycore_pymem.h"
 #include "pycore_pystate.h"
-#include "pycore_pylifecycle.h"
 
 /* --------------------------------------------------------------------------
 CAUTION
@@ -979,20 +980,28 @@ PyThreadState_Swap(PyThreadState *newts)
    PyThreadState_GetDict() returns NULL, an exception has *not* been raised
    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 *
 PyThreadState_GetDict(void)
 {
     PyThreadState *tstate = _PyThreadState_GET();
-    if (tstate == NULL)
+    if (tstate == 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);
 }