Skip to content

Commit 111e4ee

Browse files
authored
bpo-39877: Py_Initialize() pass tstate to PyEval_InitThreads() (GH-18884)
1 parent 3225b9f commit 111e4ee

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Include/internal/pycore_ceval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern PyObject *_PyEval_EvalCode(
5353
PyObject *kwdefs, PyObject *closure,
5454
PyObject *name, PyObject *qualname);
5555

56+
extern PyStatus _PyEval_InitThreads(PyThreadState *tstate);
57+
5658
#ifdef __cplusplus
5759
}
5860
#endif

Python/ceval.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_call.h"
1414
#include "pycore_ceval.h"
1515
#include "pycore_code.h"
16+
#include "pycore_initconfig.h"
1617
#include "pycore_object.h"
1718
#include "pycore_pyerrors.h"
1819
#include "pycore_pylifecycle.h"
@@ -214,26 +215,39 @@ PyEval_ThreadsInitialized(void)
214215
return gil_created(&runtime->ceval.gil);
215216
}
216217

217-
void
218-
PyEval_InitThreads(void)
218+
PyStatus
219+
_PyEval_InitThreads(PyThreadState *tstate)
219220
{
220-
_PyRuntimeState *runtime = &_PyRuntime;
221-
struct _ceval_runtime_state *ceval = &runtime->ceval;
221+
if (tstate == NULL) {
222+
return _PyStatus_ERR("tstate is NULL");
223+
}
224+
225+
struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
222226
struct _gil_runtime_state *gil = &ceval->gil;
223227
if (gil_created(gil)) {
224-
return;
228+
return _PyStatus_OK();
225229
}
226230

227231
PyThread_init_thread();
228232
create_gil(gil);
229-
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
230-
ensure_tstate_not_null(__func__, tstate);
231233
take_gil(ceval, tstate);
232234

233235
struct _pending_calls *pending = &ceval->pending;
234236
pending->lock = PyThread_allocate_lock();
235237
if (pending->lock == NULL) {
236-
Py_FatalError("Can't initialize threads for pending calls");
238+
return _PyStatus_NO_MEMORY();
239+
}
240+
return _PyStatus_OK();
241+
}
242+
243+
void
244+
PyEval_InitThreads(void)
245+
{
246+
PyThreadState *tstate = _PyThreadState_GET();
247+
248+
PyStatus status = _PyEval_InitThreads(tstate);
249+
if (_PyStatus_EXCEPTION(status)) {
250+
Py_ExitStatusException(status);
237251
}
238252
}
239253

Python/pylifecycle.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
554554
_PyGILState_Init(tstate);
555555

556556
/* Create the GIL */
557-
PyEval_InitThreads();
557+
status = _PyEval_InitThreads(tstate);
558+
if (_PyStatus_EXCEPTION(status)) {
559+
return status;
560+
}
558561

559562
*tstate_p = tstate;
560563
return _PyStatus_OK();

0 commit comments

Comments
 (0)