[Python-checkins] bpo-39877: Py_Initialize() pass tstate to PyEval_InitThreads() (GH-18884)

Victor Stinner webhook-mailer at python.org
Mon Mar 9 16:24:24 EDT 2020


https://github.com/python/cpython/commit/111e4ee52a1739e7c7221adde2fc364ef4954af2
commit: 111e4ee52a1739e7c7221adde2fc364ef4954af2
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-03-09T21:24:14+01:00
summary:

bpo-39877: Py_Initialize() pass tstate to PyEval_InitThreads() (GH-18884)

files:
M Include/internal/pycore_ceval.h
M Python/ceval.c
M Python/pylifecycle.c

diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h
index 2a7c235cfc264..b20e85ccb0aa3 100644
--- a/Include/internal/pycore_ceval.h
+++ b/Include/internal/pycore_ceval.h
@@ -53,6 +53,8 @@ extern PyObject *_PyEval_EvalCode(
     PyObject *kwdefs, PyObject *closure,
     PyObject *name, PyObject *qualname);
 
+extern PyStatus _PyEval_InitThreads(PyThreadState *tstate);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Python/ceval.c b/Python/ceval.c
index 26cefeadcde68..208fdab802c65 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -13,6 +13,7 @@
 #include "pycore_call.h"
 #include "pycore_ceval.h"
 #include "pycore_code.h"
+#include "pycore_initconfig.h"
 #include "pycore_object.h"
 #include "pycore_pyerrors.h"
 #include "pycore_pylifecycle.h"
@@ -214,26 +215,39 @@ PyEval_ThreadsInitialized(void)
     return gil_created(&runtime->ceval.gil);
 }
 
-void
-PyEval_InitThreads(void)
+PyStatus
+_PyEval_InitThreads(PyThreadState *tstate)
 {
-    _PyRuntimeState *runtime = &_PyRuntime;
-    struct _ceval_runtime_state *ceval = &runtime->ceval;
+    if (tstate == NULL) {
+        return _PyStatus_ERR("tstate is NULL");
+    }
+
+    struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
     struct _gil_runtime_state *gil = &ceval->gil;
     if (gil_created(gil)) {
-        return;
+        return _PyStatus_OK();
     }
 
     PyThread_init_thread();
     create_gil(gil);
-    PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
-    ensure_tstate_not_null(__func__, tstate);
     take_gil(ceval, tstate);
 
     struct _pending_calls *pending = &ceval->pending;
     pending->lock = PyThread_allocate_lock();
     if (pending->lock == NULL) {
-        Py_FatalError("Can't initialize threads for pending calls");
+        return _PyStatus_NO_MEMORY();
+    }
+    return _PyStatus_OK();
+}
+
+void
+PyEval_InitThreads(void)
+{
+    PyThreadState *tstate = _PyThreadState_GET();
+
+    PyStatus status = _PyEval_InitThreads(tstate);
+    if (_PyStatus_EXCEPTION(status)) {
+        Py_ExitStatusException(status);
     }
 }
 
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index eaae5fdbb6692..c99c3673d739c 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -554,7 +554,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
     _PyGILState_Init(tstate);
 
     /* Create the GIL */
-    PyEval_InitThreads();
+    status = _PyEval_InitThreads(tstate);
+    if (_PyStatus_EXCEPTION(status)) {
+        return status;
+    }
 
     *tstate_p = tstate;
     return _PyStatus_OK();



More information about the Python-checkins mailing list