]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39877: Py_Initialize() pass tstate to PyEval_InitThreads() (GH-18884)
authorVictor Stinner <vstinner@python.org>
Mon, 9 Mar 2020 20:24:14 +0000 (21:24 +0100)
committerGitHub <noreply@github.com>
Mon, 9 Mar 2020 20:24:14 +0000 (21:24 +0100)
Include/internal/pycore_ceval.h
Python/ceval.c
Python/pylifecycle.c

index 2a7c235cfc264c5463db9db532c55597598f90f7..b20e85ccb0aa3a886d5921e8d2bf17d3cb44ee28 100644 (file)
@@ -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
index 26cefeadcde681b72dd63ec741f2e22d4678d4a0..208fdab802c6574d74ad51493a8d84274dae1c7f 100644 (file)
@@ -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);
     }
 }
 
index eaae5fdbb6692b34aafe15c51f63b7593f1494c0..c99c3673d739c26d377ed4393abe639a5df04f5c 100644 (file)
@@ -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();