diff options
author | Victor Stinner <vstinner@python.org> | 2020-11-04 16:34:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-04 16:34:34 (GMT) |
commit | af1d64d9f7a7cf673279725fdbaf4adcca51d41f (patch) | |
tree | 03baffe8b044cbbb2c73542bccddad0d9a97c82e /Python/pylifecycle.c | |
parent | 58ca33b4674f39189b03c9a39fa7b676b43b3d08 (diff) | |
download | cpython-af1d64d9f7a7cf673279725fdbaf4adcca51d41f.zip cpython-af1d64d9f7a7cf673279725fdbaf4adcca51d41f.tar.gz cpython-af1d64d9f7a7cf673279725fdbaf4adcca51d41f.tar.bz2 |
bpo-42260: Main init modify sys.flags in-place (GH-23150)
When Py_Initialize() is called twice, the second call now updates
more sys attributes for the configuration, rather than only sys.argv.
* Rename _PySys_InitMain() to _PySys_UpdateConfig().
* _PySys_UpdateConfig() now modifies sys.flags in-place, instead of
creating a new flags object.
* Remove old commented sys.flags flags (unbuffered and skip_first).
* Add private _PySys_GetObject() function.
* When Py_Initialize(), Py_InitializeFromConfig() and
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index cad0fa7..1f826d7 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -949,19 +949,10 @@ done: configuration. Example of bpo-34008: Py_Main() called after Py_Initialize(). */ static PyStatus -_Py_ReconfigureMainInterpreter(PyThreadState *tstate) +pyinit_main_reconfigure(PyThreadState *tstate) { - const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp); - - PyObject *argv = _PyWideStringList_AsList(&config->argv); - if (argv == NULL) { - return _PyStatus_NO_MEMORY(); \ - } - - int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv); - Py_DECREF(argv); - if (res < 0) { - return _PyStatus_ERR("fail to set sys.argv"); + if (_PySys_UpdateConfig(tstate) < 0) { + return _PyStatus_ERR("fail to update sys for the new conf"); } return _PyStatus_OK(); } @@ -995,7 +986,7 @@ init_interp_main(PyThreadState *tstate) } } - if (_PySys_InitMain(tstate) < 0) { + if (_PySys_UpdateConfig(tstate) < 0) { return _PyStatus_ERR("can't finish initializing sys"); } @@ -1100,7 +1091,7 @@ pyinit_main(PyThreadState *tstate) } if (interp->runtime->initialized) { - return _Py_ReconfigureMainInterpreter(tstate); + return pyinit_main_reconfigure(tstate); } PyStatus status = init_interp_main(tstate); @@ -1112,19 +1103,6 @@ pyinit_main(PyThreadState *tstate) PyStatus -_Py_InitializeMain(void) -{ - PyStatus status = _PyRuntime_Initialize(); - if (_PyStatus_EXCEPTION(status)) { - return status; - } - _PyRuntimeState *runtime = &_PyRuntime; - PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); - return pyinit_main(tstate); -} - - -PyStatus Py_InitializeFromConfig(const PyConfig *config) { if (config == NULL) { @@ -1191,6 +1169,19 @@ Py_Initialize(void) } +PyStatus +_Py_InitializeMain(void) +{ + PyStatus status = _PyRuntime_Initialize(); + if (_PyStatus_EXCEPTION(status)) { + return status; + } + _PyRuntimeState *runtime = &_PyRuntime; + PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime); + return pyinit_main(tstate); +} + + static void finalize_modules_delete_special(PyThreadState *tstate, int verbose) { |