8000 gh-108634: init_interpreter() returns Py_Status · vstinner/cpython@6e8ba06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e8ba06

Browse files
committed
pythongh-108634: init_interpreter() returns Py_Status
* Replace Py_FatalError() with PyStatus in init_interpreter(). * init_runtime() cannot be called with _initialized=1: add an assertion in the caller.
1 parent 3edcf74 commit 6e8ba06

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

Include/internal/pycore_initconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct pyruntimestate;
2222
#endif
2323

2424
#define _PyStatus_OK() \
25-
(PyStatus){._type = _PyStatus_TYPE_OK,}
25+
(PyStatus){._type = _PyStatus_TYPE_OK}
2626
/* other fields are set to 0 */
2727
#define _PyStatus_ERR(ERR_MSG) \
2828
(PyStatus){ \

Include/internal/pycore_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
182182

183183
extern void _PyType_InitCache(PyInterpreterState *interp);
184184

185-
extern void _PyObject_InitState(PyInterpreterState *interp);
185+
extern PyStatus _PyObject_InitState(PyInterpreterState *interp);
186186
extern void _PyObject_FiniState(PyInterpreterState *interp);
187187
extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj);
188188

Objects/object.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ PyObject _Py_NotImplementedStruct = {
20342034
};
20352035

20362036

2037-
void
2037+
PyStatus
20382038
_PyObject_InitState(PyInterpreterState *interp)
20392039
{
20402040
#ifdef Py_TRACE_REFS
@@ -2048,9 +2048,10 @@ _PyObject_InitState(PyInterpreterState *interp)
20482048
_Py_hashtable_hash_ptr, _Py_hashtable_compare_direct,
20492049
NULL, NULL, &alloc);
20502050
if (REFCHAIN(interp) == NULL) {
2051-
Py_FatalError("_PyObject_InitState() memory allocation failure");
2051+
return _PyStatus_NO_MEMORY();
20522052
}
20532053
#endif
2054+
return _PyStatus_OK();
20542055
}
20552056

20562057
void

Python/pystate.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,11 @@ init_runtime(_PyRuntimeState *runtime,
426426
Py_ssize_t unicode_next_index,
427427
PyThread_type_lock locks[NUMLOCKS])
428428
{
429-
if (runtime->_initialized) {
430-
Py_FatalError("runtime already initialized");
431-
}
432-
assert(!runtime->preinitializing &&
433-
!runtime->preinitialized &&
434-
!runtime->core_initialized &&
435-
!runtime->initialized);
429+
assert(!runtime->preinitializing);
430+
assert(!runtime->preinitialized);
431+
assert(!runtime->core_initialized);
432+
assert(!runtime->initialized);
433+
assert(!runtime->_initialized);
436434

437435
runtime->open_code_hook = open_code_hook;
438436
runtime->open_code_userdata = open_code_userdata;
@@ -476,6 +474,7 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
476474
// Py_Initialize() must be running again.
477475
// Reset to _PyRuntimeState_INIT.
478476
memcpy(runtime, &initial, sizeof(*runtime));
477+
assert(!runtime->_initialized);
479478
}
480479

481480
if (gilstate_tss_init(runtime) != 0) {
@@ -647,14 +646,14 @@ free_interpreter(PyInterpreterState *interp)
647646
main interpreter. We fix those fields here, in addition
648647
to the other dynamically initialized fields.
649648
*/
650-
static void
649+
static PyStatus
651650
init_interpreter(PyInterpreterState *interp,
652651
_PyRuntimeState *runtime, int64_t id,
653652
PyInterpreterState *next,
654653
PyThread_type_lock pending_lock)
655654
{
656655
if (interp->_initialized) {
657-
Py_FatalError("interpreter already initialized");
656+
return _PyStatus_ERR("interpreter already initialized");
658657
}
659658

660659
assert(runtime != NULL);
@@ -675,7 +674,10 @@ init_interpreter(PyInterpreterState *interp,
675674
memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
676675
}
677676

678-
_PyObject_InitState(interp);
677+
PyStatus status = _PyObject_InitState(interp);
678+
if (_PyStatus_EXCEPTION(status)) {
679+
return status;
680+
}
679681

680682
_PyEval_InitState(interp, pending_lock);
681683
_PyGC_InitState(&interp->gc);
@@ -701,6 +703,7 @@ init_interpreter(PyInterpreterState *interp,
701703
}
702704
interp->f_opcode_trace_set = false;
703705
interp->_initialized = 1;
706+
return _PyStatus_OK();
704707
}
705708

706709
PyInterpreterState *
@@ -772,7 +775,15 @@ PyInterpreterState_New(void)
772775
}
773776
interpreters->head = interp;
774777

775-
init_interpreter(interp, runtime, id, old_head, pending_lock);
778+
PyStatus status = init_interpreter(interp, runtime,
779+
id, old_head, pending_lock);
780+
if (_PyStatus_EXCEPTION(status)) {
781+
const char *err_msg = status.err_msg;
782+
if (!err_msg) {
783+
err_msg = "failed to initialize the interpreter";
784+
}
785+
Py_FatalError(err_msg);
786+
}
776787

777788
HEAD_UNLOCK(runtime);
778789
return interp;

0 commit comments

Comments
 (0)
0