gh-108634: PyInterpreterState_New() no longer calls Py_FatalError() (#108748)

pycore_create_interpreter() now returns a status, rather than
calling Py_FatalError().

* PyInterpreterState_New() now calls Py_ExitStatusException() instead
  of calling Py_FatalError() directly.
* Replace Py_FatalError() with PyStatus in init_interpreter() and
  _PyObject_InitState().
* _PyErr_SetFromPyStatus() now raises RuntimeError, instead of
  ValueError. It can now call PyErr_NoMemory(), raise MemoryError,
  if it detects _PyStatus_NO_MEMORY() error message.
This commit is contained in:
Victor Stinner 2023-09-01 12:43:30 +02:00 committed by GitHub
parent 844f4c2e12
commit b936cf4fe0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 53 deletions

View file

@ -426,13 +426,11 @@ init_runtime(_PyRuntimeState *runtime,
Py_ssize_t unicode_next_index,
PyThread_type_lock locks[NUMLOCKS])
{
if (runtime->_initialized) {
Py_FatalError("runtime already initialized");
}
assert(!runtime->preinitializing &&
!runtime->preinitialized &&
!runtime->core_initialized &&
!runtime->initialized);
assert(!runtime->preinitializing);
assert(!runtime->preinitialized);
assert(!runtime->core_initialized);
assert(!runtime->initialized);
assert(!runtime->_initialized);
runtime->open_code_hook = open_code_hook;
runtime->open_code_userdata = open_code_userdata;
@ -476,6 +474,7 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
// Py_Initialize() must be running again.
// Reset to _PyRuntimeState_INIT.
memcpy(runtime, &initial, sizeof(*runtime));
assert(!runtime->_initialized);
}
if (gilstate_tss_init(runtime) != 0) {
@ -647,14 +646,14 @@ free_interpreter(PyInterpreterState *interp)
main interpreter. We fix those fields here, in addition
to the other dynamically initialized fields.
*/
static void
static PyStatus
init_interpreter(PyInterpreterState *interp,
_PyRuntimeState *runtime, int64_t id,
PyInterpreterState *next,
PyThread_type_lock pending_lock)
{
if (interp->_initialized) {
Py_FatalError("interpreter already initialized");
return _PyStatus_ERR("interpreter already initialized");
}
assert(runtime != NULL);
@ -675,7 +674,10 @@ init_interpreter(PyInterpreterState *interp,
memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
}
_PyObject_InitState(interp);
PyStatus status = _PyObject_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
_PyEval_InitState(interp, pending_lock);
_PyGC_InitState(&interp->gc);
@ -701,42 +703,44 @@ init_interpreter(PyInterpreterState *interp,
}
interp->f_opcode_trace_set = false;
interp->_initialized = 1;
return _PyStatus_OK();
}
PyInterpreterState *
PyInterpreterState_New(void)
{
PyInterpreterState *interp;
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_fast_get(runtime);
/* tstate is NULL when Py_InitializeFromConfig() calls
PyInterpreterState_New() to create the main interpreter. */
if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
return NULL;
PyStatus
_PyInterpreterState_New(PyThreadState *tstate, PyInterpreterState **pinterp)
{
*pinterp = NULL;
// Don't get runtime from tstate since tstate can be NULL
_PyRuntimeState *runtime = &_PyRuntime;
// tstate is NULL when pycore_create_interpreter() calls
// _PyInterpreterState_New() to create the main interpreter.
if (tstate != NULL) {
if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
return _PyStatus_ERR("sys.audit failed");
}
}
PyThread_type_lock pending_lock = PyThread_allocate_lock();
if (pending_lock == NULL) {
if (tstate != NULL) {
_PyErr_NoMemory(tstate);
}
return NULL;
return _PyStatus_NO_MEMORY();
}
/* Don't get runtime from tstate since tstate can be NULL. */
struct pyinterpreters *interpreters = &runtime->interpreters;
/* We completely serialize creation of multiple interpreters, since
it simplifies things here and blocking concurrent calls isn't a problem.
Regardless, we must fully block subinterpreter creation until
after the main interpreter is created. */
HEAD_LOCK(runtime);
struct pyinterpreters *interpreters = &runtime->interpreters;
int64_t id = interpreters->next_id;
interpreters->next_id += 1;
// Allocate the interpreter and add it to the runtime state.
PyInterpreterState *interp;
PyStatus status;
PyInterpreterState *old_head = interpreters->head;
if (old_head == NULL) {
// We are creating the main interpreter.
@ -755,36 +759,59 @@ PyInterpreterState_New(void)
interp = alloc_interpreter();
if (interp == NULL) {
status = _PyStatus_NO_MEMORY();
goto error;
}
// Set to _PyInterpreterState_INIT.
memcpy(interp, &initial._main_interpreter,
sizeof(*interp));
memcpy(interp, &initial._main_interpreter, sizeof(*interp));
if (id < 0) {
/* overflow or Py_Initialize() not called yet! */
if (tstate != NULL) {
_PyErr_SetString(tstate, PyExc_RuntimeError,
"failed to get an interpreter ID");
}
status = _PyStatus_ERR("failed to get an interpreter ID");
goto error;
}
}
interpreters->head = interp;
init_interpreter(interp, runtime, id, old_head, pending_lock);
status = init_interpreter(interp, runtime,
id, old_head, pending_lock);
if (_PyStatus_EXCEPTION(status)) {
goto error;
}
pending_lock = NULL;
HEAD_UNLOCK(runtime);
return interp;
assert(interp != NULL);
*pinterp = interp;
return _PyStatus_OK();
error:
HEAD_UNLOCK(runtime);
PyThread_free_lock(pending_lock);
if (pending_lock != NULL) {
PyThread_free_lock(pending_lock);
}
if (interp != NULL) {
free_interpreter(interp);
}
return NULL;
return status;
}
PyInterpreterState *
PyInterpreterState_New(void)
{
// tstate can be NULL
PyThreadState *tstate = current_fast_get(&_PyRuntime);
PyInterpreterState *interp;
PyStatus status = _PyInterpreterState_New(tstate, &interp);
if (_PyStatus_EXCEPTION(status)) {
Py_ExitStatusException(status);
}
assert(interp != NULL);
return interp;
}