The path configuration is now computed in the "main" initialization.
The core initialization no longer computes it.
* Add _PyConfig_Read() function to read the configuration without
computing the path configuration.
* pyinit_core() no longer computes the path configuration: it is now
computed by init_interp_main().
* The path configuration output members of PyConfig are now optional:
* executable
* base_executable
* prefix
* base_prefix
* exec_prefix
* base_exec_prefix
* _PySys_UpdateConfig() now skips NULL strings in PyConfig.
* _testembed: Rename test_set_config() to test_init_set_config() for
consistency with other tests.
PyConfig *config,
const PyConfig *config2);
extern PyStatus _PyConfig_InitPathConfig(PyConfig *config);
+extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
extern PyStatus _PyConfig_Write(const PyConfig *config,
struct pyruntimestate *runtime);
extern PyStatus _PyConfig_SetPyArgv(
'check_hash_pycs_mode',
'program_name',
'platlibdir',
- 'executable',
- 'base_executable',
- 'prefix',
- 'base_prefix',
- 'exec_prefix',
- 'base_exec_prefix',
# optional wstr:
# 'pythonpath_env'
- # 'home',
+ # 'home'
# 'pycache_prefix'
# 'run_command'
# 'run_module'
# 'run_filename'
+ # 'executable'
+ # 'prefix'
+ # 'exec_prefix'
+ # 'base_executable'
+ # 'base_prefix'
+ # 'base_exec_prefix'
):
value_tests.append((key, invalid_wstr))
type_tests.append((key, b'bytes'))
self.set_config(base_executable="base_executable")
self.assertEqual(sys._base_executable, "base_executable")
+ # When base_xxx is NULL, value is copied from xxxx
+ self.set_config(
+ executable='executable',
+ prefix="prefix",
+ exec_prefix="exec_prefix",
+ base_executable=None,
+ base_prefix=None,
+ base_exec_prefix=None)
+ self.assertEqual(sys._base_executable, "executable")
+ self.assertEqual(sys.base_prefix, "prefix")
+ self.assertEqual(sys.base_exec_prefix, "exec_prefix")
+
def test_path(self):
self.set_config(module_search_paths_set=1,
module_search_paths=['a', 'b', 'c'])
}
-static int test_set_config(void)
+static int test_init_set_config(void)
{
// Initialize core
PyConfig config;
{"test_init_setpath_config", test_init_setpath_config},
{"test_init_setpythonhome", test_init_setpythonhome},
{"test_init_warnoptions", test_init_warnoptions},
- {"test_init_set_config", test_set_config},
+ {"test_init_set_config", test_init_set_config},
{"test_run_main", test_run_main},
{"test_get_argc_argv", test_get_argc_argv},
assert(_PyWideStringList_CheckConsistency(&config->warnoptions));
assert(_PyWideStringList_CheckConsistency(&config->module_search_paths));
assert(config->module_search_paths_set >= 0);
- if (config->_install_importlib) {
- /* don't check config->module_search_paths */
- assert(config->executable != NULL);
- assert(config->base_executable != NULL);
- assert(config->prefix != NULL);
- assert(config->base_prefix != NULL);
- assert(config->exec_prefix != NULL);
- assert(config->base_exec_prefix != NULL);
- }
assert(config->platlibdir != NULL);
assert(config->filesystem_encoding != NULL);
assert(config->filesystem_errors != NULL);
GET_WSTR_OPT(home);
GET_WSTR(platlibdir);
+ // Path configuration output
GET_UINT(module_search_paths_set);
GET_WSTRLIST(module_search_paths);
- if (config->_install_importlib) {
- GET_WSTR(executable);
- GET_WSTR(base_executable);
- GET_WSTR(prefix);
- GET_WSTR(base_prefix);
- GET_WSTR(exec_prefix);
- GET_WSTR(base_exec_prefix);
- }
- else {
- GET_WSTR_OPT(executable);
- GET_WSTR_OPT(base_executable);
- GET_WSTR_OPT(prefix);
- GET_WSTR_OPT(base_prefix);
- GET_WSTR_OPT(exec_prefix);
- GET_WSTR_OPT(base_exec_prefix);
- }
+ GET_WSTR_OPT(executable);
+ GET_WSTR_OPT(base_executable);
+ GET_WSTR_OPT(prefix);
+ GET_WSTR_OPT(base_prefix);
+ GET_WSTR_OPT(exec_prefix);
+ GET_WSTR_OPT(base_exec_prefix);
GET_UINT(skip_source_first_line);
GET_WSTR_OPT(run_command);
static PyStatus
-config_read(PyConfig *config)
+config_read(PyConfig *config, int compute_path_config)
{
PyStatus status;
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
}
}
- if (config->_install_importlib) {
+ if (compute_path_config && config->_install_importlib) {
status = _PyConfig_InitPathConfig(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
The only side effects are to modify config and to call _Py_SetArgcArgv(). */
PyStatus
-PyConfig_Read(PyConfig *config)
+_PyConfig_Read(PyConfig *config, int compute_path_config)
{
PyStatus status;
goto done;
}
- status = config_read(config);
+ status = config_read(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
}
+PyStatus
+PyConfig_Read(PyConfig *config)
+{
+ return _PyConfig_Read(config, 1);
+}
+
+
PyObject*
_Py_GetConfigsAsDict(void)
{
static int
-interpreter_set_config(const PyConfig *config)
+interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
{
- PyThreadState *tstate = PyThreadState_Get();
+ const PyConfig *config = &tstate->interp->config;
- PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
- if (_PyStatus_EXCEPTION(status)) {
- _PyErr_SetFromPyStatus(status);
- return -1;
- }
-
- status = _PyConfig_Copy(&tstate->interp->config, config);
- if (_PyStatus_EXCEPTION(status)) {
- _PyErr_SetFromPyStatus(status);
- return -1;
+ if (!only_update_path_config) {
+ PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
+ if (_PyStatus_EXCEPTION(status)) {
+ _PyErr_SetFromPyStatus(status);
+ return -1;
+ }
}
- config = &tstate->interp->config;
- if (config->_install_importlib && _Py_IsMainInterpreter(tstate)) {
- status = _PyConfig_WritePathConfig(config);
+ if (_Py_IsMainInterpreter(tstate)) {
+ PyStatus status = _PyConfig_WritePathConfig(config);
if (_PyStatus_EXCEPTION(status)) {
_PyErr_SetFromPyStatus(status);
return -1;
int
_PyInterpreterState_SetConfig(const PyConfig *src_config)
{
+ PyThreadState *tstate = PyThreadState_Get();
int res = -1;
PyConfig config;
goto done;
}
- res = interpreter_set_config(&config);
+ status = _PyConfig_Copy(&tstate->interp->config, &config);
+ if (_PyStatus_EXCEPTION(status)) {
+ _PyErr_SetFromPyStatus(status);
+ goto done;
+ }
+
+ res = interpreter_update_config(tstate, 0);
done:
PyConfig_Clear(&config);
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->_install_importlib) {
- if (_Py_IsMainInterpreter(tstate)) {
- status = _PyConfig_WritePathConfig(config);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
- }
-
/* This call sets up builtin and frozen import support */
status = init_importlib(tstate, sysmod);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
- status = PyConfig_Read(&config);
+ // Read the configuration, but don't compute the path configuration
+ // (it is computed in the main init).
+ status = _PyConfig_Read(&config, 0);
if (_PyStatus_EXCEPTION(status)) {
goto done;
}
static PyStatus
pyinit_main_reconfigure(PyThreadState *tstate)
{
- if (_PySys_UpdateConfig(tstate) < 0) {
- return _PyStatus_ERR("fail to update sys for the new conf");
+ if (interpreter_update_config(tstate, 0) < 0) {
+ return _PyStatus_ERR("fail to reconfigure Python");
}
return _PyStatus_OK();
}
return _PyStatus_OK();
}
+ // Compute the path configuration
+ status = _PyConfig_InitPathConfig(&interp->config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
if (is_main_interp) {
if (_PyTime_Init() < 0) {
return _PyStatus_ERR("can't initialize time");
}
}
- if (_PySys_UpdateConfig(tstate) < 0) {
- return _PyStatus_ERR("can't finish initializing sys");
+ if (interpreter_update_config(tstate, 1) < 0) {
+ return _PyStatus_ERR("failed to update the Python config");
}
status = init_importlib_external(tstate);
#define SET_SYS_FROM_WSTR(KEY, VALUE) \
SET_SYS(KEY, PyUnicode_FromWideChar(VALUE, -1));
+#define COPY_WSTR(SYS_ATTR, WSTR) \
+ if (WSTR != NULL) { \
+ SET_SYS_FROM_WSTR(SYS_ATTR, WSTR); \
+ }
+
if (config->module_search_paths_set) {
COPY_LIST("path", config->module_search_paths);
}
- SET_SYS_FROM_WSTR("executable", config->executable);
- SET_SYS_FROM_WSTR("_base_executable", config->base_executable);
- SET_SYS_FROM_WSTR("prefix", config->prefix);
- SET_SYS_FROM_WSTR("base_prefix", config->base_prefix);
- SET_SYS_FROM_WSTR("exec_prefix", config->exec_prefix);
- SET_SYS_FROM_WSTR("base_exec_prefix", config->base_exec_prefix);
- SET_SYS_FROM_WSTR("platlibdir", config->platlibdir);
+ COPY_WSTR("executable", config->executable);
+ COPY_WSTR("_base_executable", config->base_executable);
+ COPY_WSTR("prefix", config->prefix);
+ COPY_WSTR("base_prefix", config->base_prefix);
+ COPY_WSTR("exec_prefix", config->exec_prefix);
+ COPY_WSTR("base_exec_prefix", config->base_exec_prefix);
+ COPY_WSTR("platlibdir", config->platlibdir);
if (config->pycache_prefix != NULL) {
SET_SYS_FROM_WSTR("pycache_prefix", config->pycache_prefix);
SET_SYS("_xoptions", sys_create_xoptions_dict(config));
-#undef COPY_LIST
#undef SET_SYS_FROM_WSTR
+#undef COPY_LIST
+#undef COPY_WSTR
// sys.flags
PyObject *flags = _PySys_GetObject(tstate, "flags"); // borrowed ref