diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-07-20 17:16:22 -0700 |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2018-07-21 02:16:22 +0200 |
commit | 03ec4df67d6b4ce93a2da21db7c84dff8259953f (patch) | |
tree | c47087d89707148936e251a888cacb21642c864a /Python/pylifecycle.c | |
parent | 49abd307d222e6fe85b5175eed6b6f87fc656a8d (diff) | |
download | cpython-git-03ec4df67d6b4ce93a2da21db7c84dff8259953f.tar.gz |
bpo-34008: Allow to call Py_Main() after Py_Initialize() (GH-8043) (GH-8352)
Py_Main() can again be called after Py_Initialize(), as in Python
3.6. The new configuration is ignored, except of
_PyMainInterpreterConfig.argv which is used to update sys.argv.
(cherry picked from commit fb47bca9ee2d07ce96df94b4e4abafd11826eb01)
Co-authored-by: Victor Stinner <vstinner@redhat.com>
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index fdb759f480..219a465588 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -775,6 +775,22 @@ _Py_InitializeCore(const _PyCoreConfig *core_config) return _Py_INIT_OK(); } +/* Py_Initialize() has already been called: update the main interpreter + configuration. Example of bpo-34008: Py_Main() called after + Py_Initialize(). */ +static _PyInitError +_Py_ReconfigureMainInterpreter(PyInterpreterState *interp, + const _PyMainInterpreterConfig *config) +{ + if (config->argv != NULL) { + int res = PyDict_SetItemString(interp->sysdict, "argv", config->argv); + if (res < 0) { + return _Py_INIT_ERR("fail to set sys.argv"); + } + } + return _Py_INIT_OK(); +} + /* Update interpreter state based on supplied configuration settings * * After calling this function, most of the restrictions on the interpreter @@ -796,9 +812,6 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) if (!_PyRuntime.core_initialized) { return _Py_INIT_ERR("runtime core not initialized"); } - if (_PyRuntime.initialized) { - return _Py_INIT_ERR("main interpreter already initialized"); - } /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); @@ -813,6 +826,10 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) return _Py_INIT_ERR("failed to copy main interpreter config"); } + if (_PyRuntime.initialized) { + return _Py_ReconfigureMainInterpreter(interp, config); + } + if (interp->core_config._disable_importlib) { /* Special mode for freeze_importlib: run with no import system * |