diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-05 23:36:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-05 23:36:56 (GMT) |
commit | 7d2ef3ef5042356aaeaf832ad4204b7dad2e1b8c (patch) | |
tree | b38d3476031398b85e041b8ac45f85d07a043b38 /Python/pylifecycle.c | |
parent | a9df651eb4c18a07ec309df190419613e95cba7b (diff) | |
download | cpython-7d2ef3ef5042356aaeaf832ad4204b7dad2e1b8c.zip cpython-7d2ef3ef5042356aaeaf832ad4204b7dad2e1b8c.tar.gz cpython-7d2ef3ef5042356aaeaf832ad4204b7dad2e1b8c.tar.bz2 |
bpo-36142: _PyPreConfig_Write() sets the allocator (GH-12186)
* _PyPreConfig_Write() now sets the memory allocator.
* _PyPreConfig_Write() gets a return type: _PyInitError.
* _Py_InitializeCore() now reads and writes the pre-configuration
(set the memory allocator, configure the locale) before reading and
writing the core configuration.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r-- | Python/pylifecycle.c | 103 |
1 files changed, 64 insertions, 39 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index c955a1d..f72af86 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -480,16 +480,6 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p, } *interp_p = interp; - /* bpo-34008: For backward compatibility reasons, calling Py_Main() after - Py_Initialize() ignores the new configuration. */ - if (core_config->preconfig.allocator != NULL) { - const char *allocator = _PyMem_GetAllocatorsName(); - if (allocator == NULL || strcmp(core_config->preconfig.allocator, allocator) != 0) { - return _Py_INIT_USER_ERR("cannot modify memory allocator " - "after first Py_Initialize()"); - } - } - _PyCoreConfig_SetGlobalConfig(core_config); if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) { @@ -521,12 +511,6 @@ pycore_init_runtime(const _PyCoreConfig *core_config) return err; } - if (core_config->preconfig.allocator != NULL) { - if (_PyMem_SetupAllocators(core_config->preconfig.allocator) < 0) { - return _Py_INIT_USER_ERR("Unknown PYTHONMALLOC allocator"); - } - } - /* Py_Finalize leaves _Py_Finalizing set in order to help daemon * threads behave a little more gracefully at interpreter shutdown. * We clobber it here so the new interpreter can start with a clean @@ -728,6 +712,65 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p, return _Py_INIT_OK(); } + +static _PyInitError +pyinit_preconfig(_PyPreConfig *preconfig, const _PyPreConfig *src_preconfig) +{ + _PyInitError err; + PyMemAllocatorEx old_alloc; + + /* Set LC_CTYPE to the user preferred locale */ + _Py_SetLocaleFromEnv(LC_CTYPE); + + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + if (_PyPreConfig_Copy(preconfig, src_preconfig) >= 0) { + err = _PyPreConfig_Read(preconfig); + } + else { + err = _Py_INIT_ERR("failed to copy pre config"); + } + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + if (_Py_INIT_FAILED(err)) { + return err; + } + + return _PyPreConfig_Write(preconfig); +} + + +static _PyInitError +pyinit_coreconfig(_PyCoreConfig *config, const _PyCoreConfig *src_config, + PyInterpreterState **interp_p) +{ + PyMemAllocatorEx old_alloc; + _PyInitError err; + + /* Set LC_CTYPE to the user preferred locale */ + _Py_SetLocaleFromEnv(LC_CTYPE); + + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + if (_PyCoreConfig_Copy(config, src_config) >= 0) { + err = _PyCoreConfig_Read(config, NULL); + } + else { + err = _Py_INIT_ERR("failed to copy core config"); + } + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + if (_Py_INIT_FAILED(err)) { + return err; + } + + if (!_PyRuntime.core_initialized) { + return _Py_InitializeCore_impl(interp_p, config); + } + else { + return _Py_Initialize_ReconfigureCore(interp_p, config); + } +} + + /* Begin interpreter initialization * * On return, the first thread and interpreter state have been created, @@ -749,41 +792,23 @@ _PyInitError _Py_InitializeCore(PyInterpreterState **interp_p, const _PyCoreConfig *src_config) { - assert(src_config != NULL); - PyMemAllocatorEx old_alloc; _PyInitError err; - /* Copy the configuration, since _PyCoreConfig_Read() modifies it - (and the input configuration is read only). */ - _PyCoreConfig config = _PyCoreConfig_INIT; - - /* Set LC_CTYPE to the user preferred locale */ - _Py_SetLocaleFromEnv(LC_CTYPE); + assert(src_config != NULL); - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - if (_PyCoreConfig_Copy(&config, src_config) >= 0) { - err = _PyCoreConfig_Read(&config, NULL); - } - else { - err = _Py_INIT_ERR("failed to copy core config"); - } - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + _PyCoreConfig local_config = _PyCoreConfig_INIT; + err = pyinit_preconfig(&local_config.preconfig, &src_config->preconfig); if (_Py_INIT_FAILED(err)) { goto done; } - if (!_PyRuntime.core_initialized) { - err = _Py_InitializeCore_impl(interp_p, &config); - } - else { - err = _Py_Initialize_ReconfigureCore(interp_p, &config); - } + err = pyinit_coreconfig(&local_config, src_config, interp_p); done: _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - _PyCoreConfig_Clear(&config); + _PyCoreConfig_Clear(&local_config); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); return err; |