Commit d19d8d52 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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
parent ac0b3c2f
......@@ -52,17 +52,17 @@ typedef struct _PyPathConfig {
wchar_t *program_name;
/* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
wchar_t *home;
/* isolated and no_site_import are used to set Py_IsolatedFlag and
/* isolated and site_import are used to set Py_IsolatedFlag and
Py_NoSiteFlag flags on Windows in read_pth_file(). These fields
are ignored when their value are equal to -1 (unset). */
int isolated;
int no_site_import;
int site_import;
} _PyPathConfig;
#define _PyPathConfig_INIT \
{.module_search_path = NULL, \
.isolated = -1, \
.no_site_import = -1}
.site_import = -1}
/* Note: _PyPathConfig_INIT sets other fields to 0/NULL */
PyAPI_DATA(_PyPathConfig) _Py_path_config;
......
......@@ -54,20 +54,16 @@ PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
PyAPI_FUNC(_PyInitError) _Py_InitializeCore(const _PyCoreConfig *);
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(
_PyCoreConfig *config,
int *isolated,
int *no_site_import);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(int) _PyCoreConfig_Copy(
_PyCoreConfig *config,
const _PyCoreConfig *config2);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(
_PyCoreConfig *config,
int *isolated,
int *no_site_import);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
const _PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(
_PyMainInterpreterConfig *config,
......
......@@ -28,7 +28,7 @@ typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
typedef struct {
int install_signal_handlers; /* Install signal handlers? -1 means unset */
int ignore_environment; /* -E, Py_IgnoreEnvironmentFlag */
int ignore_environment; /* -E, Py_IgnoreEnvironmentFlag, -1 means unset */
int use_hash_seed; /* PYTHONHASHSEED=x */
unsigned long hash_seed;
const char *allocator; /* Memory allocator: _PyMem_SetupAllocators() */
......@@ -75,18 +75,42 @@ typedef struct {
wchar_t *dll_path; /* Windows DLL path */
#endif
/* Private fields */
int _disable_importlib; /* Needed by freeze_importlib */
/* If greater than 0, enable isolated mode: sys.path contains
neither the script's directory nor the user's site-packages directory.
Set to 1 by the -I command line option. If set to -1 (default), inherit
Py_IsolatedFlag value. */
int isolated;
/* If equal to zero, disable the import of the module site and the
site-dependent manipulations of sys.path that it entails. Also disable
these manipulations if site is explicitly imported later (call
site.main() if you want them to be triggered).
Set to 0 by the -S command line option. If set to -1 (default), set to
the negative value of Py_NoSiteFlag. */
int site_import;
/* --- Private fields -------- */
/* Install importlib? If set to 0, importlib is not initialized at all.
Needed by freeze_importlib: see install_importlib argument of
_Py_InitializeEx_Private(). */
int _install_importlib;
} _PyCoreConfig;
#define _PyCoreConfig_INIT \
(_PyCoreConfig){ \
.install_signal_handlers = -1, \
.ignore_environment = -1, \
.use_hash_seed = -1, \
.coerce_c_locale = -1, \
.utf8_mode = -1, \
.argc = -1, \
.nmodule_search_path = -1}
.nmodule_search_path = -1, \
.isolated = -1, \
.site_import = -1, \
._install_importlib = 1}
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
/* Placeholders while working on the new configuration API
......
This diff is collapsed.
......@@ -563,7 +563,7 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
wcscpy_s(prefix, MAXPATHLEN+1, path);
reduce(prefix);
config->isolated = 1;
config->no_site_import = 1;
config->site_import = 0;
size_t bufsiz = MAXPATHLEN;
size_t prefixlen = wcslen(prefix);
......@@ -588,7 +588,7 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
}
if (strcmp(line, "import site") == 0) {
config->no_site_import = 0;
config->site_import = 1;
continue;
}
else if (strncmp(line, "import ", 7) == 0) {
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment