mirror of
https://github.com/python/cpython.git
synced 2025-07-17 00:05:20 +00:00
bpo-34170: Add _PyCoreConfig.isolated (GH-8417)
* _PyCoreConfig: add isolated and site_import attributes * Replace Py_IgnoreEnvironment with config->ignore_environment when reading the current configuration * _PyCoreConfig_Read() now sets ignore_environment, utf8_mode, isolated and site_import from Py_IgnoreEnvironment, Py_UTF8Mode, Py_IsolatedFlag and Py_NoSiteFlag * _Py_InitializeCore() now sets Py_xxx flags from the configuration * pymain_read_conf() now uses _PyCoreConfig_Copy() to save/restore the configuration. * Rename _disable_importlib of _PyCoreConfig to _install_importlib * _PyCoreConfig_SetGlobalConfig() now also set Py_HashRandomizationFlag * Replace !Py_NoSiteFlag with core_config->site_import
This commit is contained in:
parent
ac0b3c2f4d
commit
d19d8d5279
7 changed files with 411 additions and 353 deletions
|
@ -283,8 +283,7 @@ core_config_init_module_search_paths(_PyCoreConfig *config,
|
|||
|
||||
|
||||
_PyInitError
|
||||
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config,
|
||||
int *isolated, int *no_site_import)
|
||||
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
|
||||
{
|
||||
_PyPathConfig path_config = _PyPathConfig_INIT;
|
||||
_PyInitError err;
|
||||
|
@ -345,11 +344,11 @@ _PyCoreConfig_InitPathConfig(_PyCoreConfig *config,
|
|||
}
|
||||
}
|
||||
|
||||
if (path_config.isolated != -1 && isolated != NULL) {
|
||||
*isolated = path_config.isolated;
|
||||
if (path_config.isolated != -1) {
|
||||
config->isolated = path_config.isolated;
|
||||
}
|
||||
if (path_config.no_site_import != -1 && no_site_import != NULL) {
|
||||
*no_site_import = path_config.no_site_import;
|
||||
if (path_config.site_import != -1) {
|
||||
config->site_import = path_config.site_import;
|
||||
}
|
||||
|
||||
_PyPathConfig_Clear(&path_config);
|
||||
|
@ -375,10 +374,7 @@ pathconfig_global_init(void)
|
|||
_PyInitError err;
|
||||
_PyCoreConfig config = _PyCoreConfig_INIT;
|
||||
|
||||
/* Py_IsolatedFlag and Py_NoSiteFlag are left unchanged: pass NULL.
|
||||
_PyCoreConfig_InitPathConfig() will be called later and will set
|
||||
these flags. */
|
||||
err = _PyCoreConfig_Read(&config, NULL, NULL);
|
||||
err = _PyCoreConfig_Read(&config);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
goto error;
|
||||
}
|
||||
|
|
|
@ -599,12 +599,9 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
{
|
||||
assert(core_config != NULL);
|
||||
|
||||
PyInterpreterState *interp;
|
||||
PyThreadState *tstate;
|
||||
PyObject *bimod, *sysmod, *pstderr;
|
||||
_PyInitError err;
|
||||
_PyCoreConfig_SetGlobalConfig(core_config);
|
||||
|
||||
err = _PyRuntime_Initialize();
|
||||
_PyInitError err = _PyRuntime_Initialize();
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
}
|
||||
|
@ -646,17 +643,12 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
return err;
|
||||
}
|
||||
|
||||
if (!core_config->use_hash_seed || core_config->hash_seed) {
|
||||
/* Random or non-zero hash seed */
|
||||
Py_HashRandomizationFlag = 1;
|
||||
}
|
||||
|
||||
err = _PyInterpreterState_Enable(&_PyRuntime);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
}
|
||||
|
||||
interp = PyInterpreterState_New();
|
||||
PyInterpreterState *interp = PyInterpreterState_New();
|
||||
if (interp == NULL) {
|
||||
return _Py_INIT_ERR("can't make main interpreter");
|
||||
}
|
||||
|
@ -664,8 +656,9 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
|
||||
return _Py_INIT_ERR("failed to copy core config");
|
||||
}
|
||||
core_config = &interp->core_config;
|
||||
|
||||
tstate = PyThreadState_New(interp);
|
||||
PyThreadState *tstate = PyThreadState_New(interp);
|
||||
if (tstate == NULL)
|
||||
return _Py_INIT_ERR("can't make first thread");
|
||||
(void) PyThreadState_Swap(tstate);
|
||||
|
@ -699,6 +692,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
return _Py_INIT_ERR("can't make modules dictionary");
|
||||
interp->modules = modules;
|
||||
|
||||
PyObject *sysmod;
|
||||
err = _PySys_BeginInit(&sysmod);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
|
@ -720,7 +714,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
if (_PyStructSequence_Init() < 0)
|
||||
return _Py_INIT_ERR("can't initialize structseq");
|
||||
|
||||
bimod = _PyBuiltin_Init();
|
||||
PyObject *bimod = _PyBuiltin_Init();
|
||||
if (bimod == NULL)
|
||||
return _Py_INIT_ERR("can't initialize builtins modules");
|
||||
_PyImport_FixupBuiltin(bimod, "builtins", modules);
|
||||
|
@ -734,7 +728,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
|
||||
/* Set up a preliminary stderr printer until we have enough
|
||||
infrastructure for the io module in place. */
|
||||
pstderr = PyFile_NewStdPrinter(fileno(stderr));
|
||||
PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr));
|
||||
if (pstderr == NULL)
|
||||
return _Py_INIT_ERR("can't set preliminary stderr");
|
||||
_PySys_SetObjectId(&PyId_stderr, pstderr);
|
||||
|
@ -759,7 +753,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
if (!_PyContext_Init())
|
||||
return _Py_INIT_ERR("can't init context");
|
||||
|
||||
if (!core_config->_disable_importlib) {
|
||||
if (core_config->_install_importlib) {
|
||||
err = _PyCoreConfig_SetPathConfig(core_config);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
|
@ -767,7 +761,7 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
|
|||
}
|
||||
|
||||
/* This call sets up builtin and frozen import support */
|
||||
if (!interp->core_config._disable_importlib) {
|
||||
if (core_config->_install_importlib) {
|
||||
err = initimport(interp, sysmod);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
|
@ -809,21 +803,20 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp,
|
|||
_PyInitError
|
||||
_Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
|
||||
{
|
||||
PyInterpreterState *interp;
|
||||
PyThreadState *tstate;
|
||||
_PyInitError err;
|
||||
|
||||
if (!_PyRuntime.core_initialized) {
|
||||
return _Py_INIT_ERR("runtime core not initialized");
|
||||
}
|
||||
|
||||
/* Get current thread state and interpreter pointer */
|
||||
tstate = PyThreadState_GET();
|
||||
if (!tstate)
|
||||
PyThreadState *tstate = PyThreadState_GET();
|
||||
if (!tstate) {
|
||||
return _Py_INIT_ERR("failed to read thread state");
|
||||
interp = tstate->interp;
|
||||
if (!interp)
|
||||
}
|
||||
PyInterpreterState *interp = tstate->interp;
|
||||
if (!interp) {
|
||||
return _Py_INIT_ERR("failed to get interpreter");
|
||||
}
|
||||
_PyCoreConfig *core_config = &interp->core_config;
|
||||
|
||||
/* Now finish configuring the main interpreter */
|
||||
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
|
||||
|
@ -834,7 +827,7 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
|
|||
return _Py_ReconfigureMainInterpreter(interp, config);
|
||||
}
|
||||
|
||||
if (interp->core_config._disable_importlib) {
|
||||
if (!core_config->_install_importlib) {
|
||||
/* Special mode for freeze_importlib: run with no import system
|
||||
*
|
||||
* This means anything which needs support from extension modules
|
||||
|
@ -852,13 +845,13 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
|
|||
return _Py_INIT_ERR("can't finish initializing sys");
|
||||
}
|
||||
|
||||
err = initexternalimport(interp);
|
||||
_PyInitError err = initexternalimport(interp);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* initialize the faulthandler module */
|
||||
err = _PyFaulthandler_Init(interp->core_config.faulthandler);
|
||||
err = _PyFaulthandler_Init(core_config->faulthandler);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
}
|
||||
|
@ -875,8 +868,9 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
|
|||
}
|
||||
}
|
||||
|
||||
if (_PyTraceMalloc_Init(interp->core_config.tracemalloc) < 0)
|
||||
if (_PyTraceMalloc_Init(core_config->tracemalloc) < 0) {
|
||||
return _Py_INIT_ERR("can't initialize tracemalloc");
|
||||
}
|
||||
|
||||
err = add_main_module(interp);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
|
@ -902,7 +896,7 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
|
|||
|
||||
_PyRuntime.initialized = 1;
|
||||
|
||||
if (!Py_NoSiteFlag) {
|
||||
if (core_config->site_import) {
|
||||
err = initsite(); /* Module site */
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
|
@ -924,11 +918,10 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
|
|||
_PyCoreConfig config = _PyCoreConfig_INIT;
|
||||
_PyInitError err;
|
||||
|
||||
config.ignore_environment = Py_IgnoreEnvironmentFlag;
|
||||
config._disable_importlib = !install_importlib;
|
||||
config._install_importlib = install_importlib;
|
||||
config.install_signal_handlers = install_sigs;
|
||||
|
||||
err = _PyCoreConfig_Read(&config, &Py_IsolatedFlag, &Py_NoSiteFlag);
|
||||
err = _PyCoreConfig_Read(&config);
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
goto done;
|
||||
}
|
||||
|
@ -1320,6 +1313,7 @@ new_interpreter(PyThreadState **tstate_p)
|
|||
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
|
||||
return _Py_INIT_ERR("failed to copy core config");
|
||||
}
|
||||
core_config = &interp->core_config;
|
||||
if (_PyMainInterpreterConfig_Copy(&interp->config, config) < 0) {
|
||||
return _Py_INIT_ERR("failed to copy main interpreter config");
|
||||
}
|
||||
|
@ -1395,7 +1389,7 @@ new_interpreter(PyThreadState **tstate_p)
|
|||
return err;
|
||||
}
|
||||
|
||||
if (!Py_NoSiteFlag) {
|
||||
if (core_config->site_import) {
|
||||
err = initsite();
|
||||
if (_Py_INIT_FAILED(err)) {
|
||||
return err;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue