diff options
author | Victor Stinner <vstinner@python.org> | 2021-04-02 13:28:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-02 13:28:13 (GMT) |
commit | 442ad74fc2928b095760eb89aba93c28eab17f9b (patch) | |
tree | 29f784c0860f41cd6d2c16d34e8ced47ebf5e005 /Python/pylifecycle.c | |
parent | 58384c6ab01bbc35cc14cdeb716f6c45a3df426b (diff) | |
download | cpython-442ad74fc2928b095760eb89aba93c28eab17f9b.zip cpython-442ad74fc2928b095760eb89aba93c28eab17f9b.tar.gz cpython-442ad74fc2928b095760eb89aba93c28eab17f9b.tar.bz2 |
bpo-43687: Py_Initialize() creates singletons earlier (GH-25147)
Reorganize pycore_interp_init() to initialize singletons before the
the first PyType_Ready() call. Fix an issue when Python is configured
using --without-doc-strings.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 116 |
1 files changed, 72 insertions, 44 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8309477..64723ce 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -628,43 +628,67 @@ pycore_create_interpreter(_PyRuntimeState *runtime, static PyStatus -pycore_init_types(PyInterpreterState *interp) +pycore_init_singletons(PyInterpreterState *interp) { PyStatus status; - int is_main_interp = _Py_IsMainInterpreter(interp); - status = _PyGC_Init(interp); + if (_PyLong_Init(interp) < 0) { + return _PyStatus_ERR("can't init longs"); + } + + if (_Py_IsMainInterpreter(interp)) { + _PyFloat_Init(); + } + + status = _PyBytes_Init(interp); + if (_PyStatus_EXCEPTION(status)) { + return status; + } + + status = _PyUnicode_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } - // Create the empty tuple singleton. It must be created before the first - // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases - // for example. status = _PyTuple_Init(interp); if (_PyStatus_EXCEPTION(status)) { return status; } + return _PyStatus_OK(); +} + + +static PyStatus +pycore_init_types(PyInterpreterState *interp) +{ + PyStatus status; + int is_main_interp = _Py_IsMainInterpreter(interp); + if (is_main_interp) { + if (_PyStructSequence_Init() < 0) { + return _PyStatus_ERR("can't initialize structseq"); + } + status = _PyTypes_Init(); if (_PyStatus_EXCEPTION(status)) { return status; } - } - if (!_PyLong_Init(interp)) { - return _PyStatus_ERR("can't init longs"); - } + if (_PyLong_InitTypes() < 0) { + return _PyStatus_ERR("can't init int type"); + } - status = _PyUnicode_Init(interp); - if (_PyStatus_EXCEPTION(status)) { - return status; + status = _PyUnicode_InitTypes(); + if (_PyStatus_EXCEPTION(status)) { + return status; + } } - status = _PyBytes_Init(interp); - if (_PyStatus_EXCEPTION(status)) { - return status; + if (is_main_interp) { + if (_PyFloat_InitTypes() < 0) { + return _PyStatus_ERR("can't init float"); + } } status = _PyExc_Init(interp); @@ -672,17 +696,7 @@ pycore_init_types(PyInterpreterState *interp) return status; } - if (is_main_interp) { - if (!_PyFloat_Init()) { - return _PyStatus_ERR("can't init float"); - } - - if (_PyStructSequence_Init() < 0) { - return _PyStatus_ERR("can't initialize structseq"); - } - } - - status = _PyErr_Init(); + status = _PyErr_InitTypes(); if (_PyStatus_EXCEPTION(status)) { return status; } @@ -693,22 +707,15 @@ pycore_init_types(PyInterpreterState *interp) } } - if (_PyWarnings_InitState(interp) < 0) { - return _PyStatus_ERR("can't initialize warnings"); - } - - status = _PyAtExit_Init(interp); - if (_PyStatus_EXCEPTION(status)) { - return status; - } - return _PyStatus_OK(); } static PyStatus -pycore_init_builtins(PyInterpreterState *interp) +pycore_init_builtins(PyThreadState *tstate) { + PyInterpreterState *interp = tstate->interp; + PyObject *bimod = _PyBuiltin_Init(interp); if (bimod == NULL) { goto error; @@ -744,6 +751,7 @@ pycore_init_builtins(PyInterpreterState *interp) } interp->import_func = Py_NewRef(import_func); + assert(!_PyErr_Occurred(tstate)); return _PyStatus_OK(); error: @@ -755,29 +763,49 @@ error: static PyStatus pycore_interp_init(PyThreadState *tstate) { + PyInterpreterState *interp = tstate->interp; PyStatus status; PyObject *sysmod = NULL; - status = pycore_init_types(tstate->interp); + // Create singletons before the first PyType_Ready() call, since + // PyType_Ready() uses singletons like the Unicode empty string (tp_doc) + // and the empty tuple singletons (tp_bases). + status = pycore_init_singletons(interp); if (_PyStatus_EXCEPTION(status)) { - goto done; + return status; } - status = _PySys_Create(tstate, &sysmod); + // The GC must be initialized before the first GC collection. + status = _PyGC_Init(interp); + if (_PyStatus_EXCEPTION(status)) { + return status; + } + + status = pycore_init_types(interp); if (_PyStatus_EXCEPTION(status)) { goto done; } - assert(!_PyErr_Occurred(tstate)); + if (_PyWarnings_InitState(interp) < 0) { + return _PyStatus_ERR("can't initialize warnings"); + } + + status = _PyAtExit_Init(interp); + if (_PyStatus_EXCEPTION(status)) { + return status; + } - status = pycore_init_builtins(tstate->interp); + status = _PySys_Create(tstate, &sysmod); if (_PyStatus_EXCEPTION(status)) { goto done; } - assert(!_PyErr_Occurred(tstate)); + status = pycore_init_builtins(tstate); + if (_PyStatus_EXCEPTION(status)) { + goto done; + } - const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); + const PyConfig *config = _PyInterpreterState_GetConfig(interp); if (config->_install_importlib) { /* This call sets up builtin and frozen import support */ if (init_importlib(tstate, sysmod) < 0) { |