PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
_PyPathConfig *config,
- const _PyMainInterpreterConfig *main_config);
+ const _PyCoreConfig *core_config);
PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
PyAPI_FUNC(_PyInitError) _Py_InitializeCore(const _PyCoreConfig *);
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
-PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *);
-PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *);
+PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadEnv(_PyCoreConfig *);
+PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *);
+PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
+
+PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *, _PyCoreConfig *);
PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *);
PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *);
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
#ifdef Py_BUILD_CORE
-PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(
- const _PyMainInterpreterConfig *main_config);
+PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
#endif
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
int dump_refs; /* PYTHONDUMPREFS */
int malloc_stats; /* PYTHONMALLOCSTATS */
int utf8_mode; /* -X utf8 or PYTHONUTF8 environment variable */
+
+ wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
+ wchar_t *home; /* PYTHONHOME environment variable,
+ see also Py_SetPythonHome(). */
+ wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
} _PyCoreConfig;
#define _PyCoreConfig_INIT (_PyCoreConfig){.use_hash_seed = -1}
*/
typedef struct {
int install_signal_handlers;
- /* PYTHONPATH environment variable */
- wchar_t *module_search_path_env;
- /* PYTHONHOME environment variable, see also Py_SetPythonHome(). */
- wchar_t *home;
- /* Program name, see also Py_GetProgramName() */
- wchar_t *program_name;
+ PyObject *argv; /* sys.argv list, can be NULL */
+ PyObject *module_search_path; /* sys.path list */
} _PyMainInterpreterConfig;
#define _PyMainInterpreterConfig_INIT \
bytes long.
*/
static int
-search_for_prefix(const _PyMainInterpreterConfig *main_config,
+search_for_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *prefix)
{
size_t n;
wchar_t *vpath;
/* If PYTHONHOME is set, we believe it unconditionally */
- if (main_config->home) {
- wcsncpy(prefix, main_config->home, MAXPATHLEN);
+ if (core_config->home) {
+ wcsncpy(prefix, core_config->home, MAXPATHLEN);
prefix[MAXPATHLEN] = L'\0';
wchar_t *delim = wcschr(prefix, DELIM);
if (delim) {
static void
-calculate_prefix(const _PyMainInterpreterConfig *main_config,
+calculate_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *prefix)
{
- calculate->prefix_found = search_for_prefix(main_config, calculate, prefix);
+ calculate->prefix_found = search_for_prefix(core_config, calculate, prefix);
if (!calculate->prefix_found) {
if (!Py_FrozenFlag) {
fprintf(stderr,
MAXPATHLEN bytes long.
*/
static int
-search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
+search_for_exec_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *exec_prefix)
{
size_t n;
/* If PYTHONHOME is set, we believe it unconditionally */
- if (main_config->home) {
- wchar_t *delim = wcschr(main_config->home, DELIM);
+ if (core_config->home) {
+ wchar_t *delim = wcschr(core_config->home, DELIM);
if (delim) {
wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
}
else {
- wcsncpy(exec_prefix, main_config->home, MAXPATHLEN);
+ wcsncpy(exec_prefix, core_config->home, MAXPATHLEN);
}
exec_prefix[MAXPATHLEN] = L'\0';
joinpath(exec_prefix, calculate->lib_python);
static void
-calculate_exec_prefix(const _PyMainInterpreterConfig *main_config,
+calculate_exec_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *exec_prefix)
{
- calculate->exec_prefix_found = search_for_exec_prefix(main_config,
+ calculate->exec_prefix_found = search_for_exec_prefix(core_config,
calculate,
exec_prefix);
if (!calculate->exec_prefix_found) {
static _PyInitError
-calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
+calculate_program_full_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
wchar_t program_full_path[MAXPATHLEN+1];
* other way to find a directory to start the search from. If
* $PATH isn't exported, you lose.
*/
- if (wcschr(main_config->program_name, SEP)) {
- wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
+ if (wcschr(core_config->program_name, SEP)) {
+ wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
}
#ifdef __APPLE__
/* On Mac OS X, if a script uses an interpreter of the form
wcsncpy(program_full_path, path, MAXPATHLEN);
}
- joinpath(program_full_path, main_config->program_name);
+ joinpath(program_full_path, core_config->program_name);
if (isxfile(program_full_path)) {
break;
}
static _PyInitError
-calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
+calculate_module_search_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate,
const wchar_t *prefix, const wchar_t *exec_prefix,
_PyPathConfig *config)
{
/* Calculate size of return buffer */
size_t bufsz = 0;
- if (main_config->module_search_path_env != NULL) {
- bufsz += wcslen(main_config->module_search_path_env) + 1;
+ if (core_config->module_search_path_env != NULL) {
+ bufsz += wcslen(core_config->module_search_path_env) + 1;
}
wchar_t *defpath = calculate->pythonpath;
buf[0] = '\0';
/* Run-time value of $PYTHONPATH goes first */
- if (main_config->module_search_path_env) {
- wcscpy(buf, main_config->module_search_path_env);
+ if (core_config->module_search_path_env) {
+ wcscpy(buf, core_config->module_search_path_env);
wcscat(buf, delimiter);
}
static _PyInitError
calculate_init(PyCalculatePath *calculate,
- const _PyMainInterpreterConfig *main_config)
+ const _PyCoreConfig *core_config)
{
size_t len;
const char *path = getenv("PATH");
static _PyInitError
-calculate_path_impl(const _PyMainInterpreterConfig *main_config,
+calculate_path_impl(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
_PyInitError err;
- err = calculate_program_full_path(main_config, calculate, config);
+ err = calculate_program_full_path(core_config, calculate, config);
if (_Py_INIT_FAILED(err)) {
return err;
}
wchar_t prefix[MAXPATHLEN+1];
memset(prefix, 0, sizeof(prefix));
- calculate_prefix(main_config, calculate, prefix);
+ calculate_prefix(core_config, calculate, prefix);
calculate_zip_path(calculate, prefix);
wchar_t exec_prefix[MAXPATHLEN+1];
memset(exec_prefix, 0, sizeof(exec_prefix));
- calculate_exec_prefix(main_config, calculate, exec_prefix);
+ calculate_exec_prefix(core_config, calculate, exec_prefix);
if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
!Py_FrozenFlag)
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
}
- err = calculate_module_search_path(main_config, calculate,
+ err = calculate_module_search_path(core_config, calculate,
prefix, exec_prefix, config);
if (_Py_INIT_FAILED(err)) {
return err;
_PyInitError
-_PyPathConfig_Calculate(_PyPathConfig *config,
- const _PyMainInterpreterConfig *main_config)
+_PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config)
{
PyCalculatePath calculate;
memset(&calculate, 0, sizeof(calculate));
- _PyInitError err = calculate_init(&calculate, main_config);
+ _PyInitError err = calculate_init(&calculate, core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
- err = calculate_path_impl(main_config, &calculate, config);
+ err = calculate_path_impl(core_config, &calculate, config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
#endif
} _Py_CommandLineDetails;
-/* FIXME: temporary structure until sys module configuration can be moved
- into _PyMainInterpreterConfig */
-typedef struct {
- PyObject *argv; /* sys.argv list */
- PyObject *path0; /* path0: if set, it is prepended to sys.path */
-} _PySysConfig;
-
/* Structure used by Py_Main() to pass data to subfunctions */
typedef struct {
/* Exit status ("exit code") */
int stdin_is_interactive;
_PyCoreConfig core_config;
_PyMainInterpreterConfig config;
- _PySysConfig sys_config;
_Py_CommandLineDetails cmdline;
PyObject *main_importer_path;
/* non-zero if filename, command (-c) or module (-m) is set
_Py_OptList env_warning_options;
int argc;
wchar_t **argv;
+
+ int sys_argc;
+ wchar_t **sys_argv;
} _PyMain;
/* .cmdline is initialized to zeros */
{
_PyPathConfig_Clear(&_Py_path_config);
_PyImport_Fini2();
- _PyMainInterpreterConfig_Clear(&pymain->config);
+ _PyCoreConfig_Clear(&pymain->core_config);
#ifdef __INSURE__
/* Insure++ is a memory analysis tool that aids in discovering
pymain_optlist_clear(&cmdline->xoptions);
PyMem_RawFree(cmdline->command);
+ PyMem_RawFree(pymain->sys_argv);
pymain_optlist_clear(&pymain->env_warning_options);
+}
+
+
+/* Clear Python ojects */
+static void
+pymain_free_python(_PyMain *pymain)
+{
Py_CLEAR(pymain->main_importer_path);
- Py_CLEAR(pymain->sys_config.argv);
- Py_CLEAR(pymain->sys_config.path0);
+ _PyMainInterpreterConfig_Clear(&pymain->config);
}
+
static void
pymain_free(_PyMain *pymain)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
+ pymain_free_python(pymain);
pymain_free_pymain(pymain);
pymain_free_globals(pymain);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
+
static int
pymain_run_main_from_importer(_PyMain *pymain)
{
/* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__
environment variables on macOS if available. */
static _PyInitError
-config_get_program_name(_PyMainInterpreterConfig *config)
+config_get_program_name(_PyCoreConfig *config)
{
assert(config->program_name == NULL);
static int
pymain_get_program_name(_PyMain *pymain)
{
- if (pymain->config.program_name == NULL) {
+ if (pymain->core_config.program_name == NULL) {
/* Use argv[0] by default */
- pymain->config.program_name = pymain_wstrdup(pymain, pymain->argv[0]);
- if (pymain->config.program_name == NULL) {
+ pymain->core_config.program_name = pymain_wstrdup(pymain, pymain->argv[0]);
+ if (pymain->core_config.program_name == NULL) {
return -1;
}
}
}
-static PyObject *
-pymain_create_argv_list(int argc, wchar_t **argv)
+static int
+pymain_create_argv_list(_PyMain *pymain)
{
- if (argc <= 0 || argv == NULL) {
+ int argc = pymain->sys_argc;
+ wchar_t** argv = pymain->sys_argv;
+
+ if (argc <= 0 || pymain->sys_argv == NULL) {
/* Ensure at least one (empty) argument is seen */
static wchar_t *empty_argv[1] = {L""};
argv = empty_argv;
argc = 1;
}
- PyObject *av = PyList_New(argc);
- if (av == NULL) {
- return NULL;
+ PyObject *list = PyList_New(argc);
+ if (list == NULL) {
+ return -1;
}
for (int i = 0; i < argc; i++) {
PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
if (v == NULL) {
- Py_DECREF(av);
- return NULL;
+ Py_DECREF(list);
+ return -1;
}
- PyList_SET_ITEM(av, i, v);
+ PyList_SET_ITEM(list, i, v);
}
- return av;
+
+ pymain->config.argv = list;
+ return 0;
}
-/* Create sys.argv list and maybe also path0 */
static int
-pymain_compute_argv(_PyMain *pymain)
+pymain_init_sys_argv(_PyMain *pymain)
{
_Py_CommandLineDetails *cmdline = &pymain->cmdline;
}
/* Copy argv to be able to modify it (to force -c/-m) */
- int argc2 = pymain->argc - _PyOS_optind;
- size_t size = argc2 * sizeof(pymain->argv[0]);
- wchar_t **argv2 = PyMem_RawMalloc(size);
- if (argv2 == NULL) {
+ pymain->sys_argc = pymain->argc - _PyOS_optind;
+ size_t size = pymain->sys_argc * sizeof(pymain->argv[0]);
+ pymain->sys_argv = PyMem_RawMalloc(size);
+ if (pymain->sys_argv == NULL) {
pymain->err = _Py_INIT_NO_MEMORY();
return -1;
}
- memcpy(argv2, &pymain->argv[_PyOS_optind], size);
+ memcpy(pymain->sys_argv, &pymain->argv[_PyOS_optind], size);
- /* TODO: Move this to _Py_InitializeMainInterpreter */
if (cmdline->command != NULL) {
/* Force sys.argv[0] = '-c' */
- argv2[0] = L"-c";
+ pymain->sys_argv[0] = L"-c";
}
else if (cmdline->module != NULL) {
/* Force sys.argv[0] = '-m'*/
- argv2[0] = L"-m";
+ pymain->sys_argv[0] = L"-m";
}
+ return 0;
+}
- /* Create sys.argv list */
- pymain->sys_config.argv = pymain_create_argv_list(argc2, argv2);
- if (pymain->sys_config.argv == NULL) {
- pymain->err = _Py_INIT_ERR("failed to create sys.argv");
- goto error;
- }
- /* Need to update sys.path[0]? */
- int update_path;
+static int
+pymain_update_sys_path(_PyMain *pymain)
+{
if (pymain->main_importer_path != NULL) {
/* Let pymain_run_main_from_importer() adjust sys.path[0] later */
- update_path = 0;
- } else {
- /* Use config settings to decide whether or not to update sys.path[0] */
- update_path = (Py_IsolatedFlag == 0);
- }
-
- /* If '-c' and '-m' options are not used in the command line
- and update_path is non-zero, prepend argv[0] to sys.path. If argv[0] is
- a symlink, use the real path. */
- if (update_path) {
- pymain->sys_config.path0 = _PyPathConfig_ComputeArgv0(argc2, argv2);
- if (pymain->sys_config.path0 == NULL) {
- pymain->err = _Py_INIT_NO_MEMORY();
- goto error;
- }
+ return 0;
}
- PyMem_RawFree(argv2);
- return 0;
-error:
- return -1;
-}
+ if (Py_IsolatedFlag) {
+ return 0;
+ }
-static int
-pymain_set_sys_argv(_PyMain *pymain)
-{
- /* Set sys.argv */
- if (PySys_SetObject("argv", pymain->sys_config.argv) != 0) {
- pymain->err = _Py_INIT_ERR("can't assign sys.argv");
+ /* Prepend argv[0] to sys.path.
+ If argv[0] is a symlink, use the real path. */
+ PyObject *sys_path = PySys_GetObject("path");
+ if (sys_path == NULL) {
+ pymain->err = _Py_INIT_ERR("can't get sys.path");
return -1;
}
- Py_CLEAR(pymain->sys_config.argv);
- if (pymain->sys_config.path0 != NULL) {
- /* Prepend path0 to sys.path */
- PyObject *sys_path = PySys_GetObject("path");
- if (sys_path == NULL) {
- pymain->err = _Py_INIT_ERR("can't get sys.path");
- return -1;
- }
+ PyObject *path0 = _PyPathConfig_ComputeArgv0(pymain->sys_argc, pymain->sys_argv);
+ if (path0 == NULL) {
+ pymain->err = _Py_INIT_NO_MEMORY();
+ return -1;
+ }
- if (PyList_Insert(sys_path, 0, pymain->sys_config.path0) < 0) {
- pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
- return -1;
- }
- Py_CLEAR(pymain->sys_config.path0);
+ /* Prepend path0 to sys.path */
+ if (PyList_Insert(sys_path, 0, path0) < 0) {
+ Py_DECREF(path0);
+ pymain->err = _Py_INIT_ERR("sys.path.insert(0, path0) failed");
+ return -1;
}
+ Py_DECREF(path0);
return 0;
}
static _PyInitError
-config_init_pythonpath(_PyMainInterpreterConfig *config)
+config_init_pythonpath(_PyCoreConfig *config)
{
wchar_t *path;
int res = config_get_env_var_dup(&path, L"PYTHONPATH", "PYTHONPATH");
static _PyInitError
-config_init_home(_PyMainInterpreterConfig *config)
+config_init_home(_PyCoreConfig *config)
{
wchar_t *home;
_PyInitError
-_PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config)
+_PyCoreConfig_ReadEnv(_PyCoreConfig *config)
{
_PyInitError err = config_init_home(config);
if (_Py_INIT_FAILED(err)) {
return -1;
}
- _PyInitError err = _PyMainInterpreterConfig_ReadEnv(&pymain->config);
+ _PyInitError err = _PyCoreConfig_ReadEnv(&pymain->core_config);
if (_Py_INIT_FAILED(pymain->err)) {
pymain->err = err;
return -1;
}
/* FIXME: if utf8_mode value changed, parse again cmdline */
- _PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config);
+ if (pymain_init_sys_argv(pymain) < 0) {
+ return -1;
+ }
+
+ _PyInitError err = _PyCoreConfig_Read(&pymain->core_config);
if (_Py_INIT_FAILED(err)) {
pymain->err = err;
return -1;
}
-
return 0;
}
return res;
}
-static int
-pymain_init_python(_PyMain *pymain)
+
+static PyObject *
+config_create_path_list(const wchar_t *path, wchar_t delim)
{
+ int i, n;
+ const wchar_t *p;
+ PyObject *v;
+
+ n = 1;
+ p = path;
+ while ((p = wcschr(p, delim)) != NULL) {
+ n++;
+ p++;
+ }
+ v = PyList_New(n);
+ if (v == NULL) {
+ return NULL;
+ }
+ for (i = 0; ; i++) {
+ p = wcschr(path, delim);
+ if (p == NULL) {
+ p = path + wcslen(path); /* End of string */
+ }
+ PyObject *w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
+ if (w == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyList_SET_ITEM(v, i, w);
+ if (*p == '\0') {
+ break;
+ }
+ path = p+1;
+ }
+ return v;
+}
+
+
+static _PyInitError
+config_init_module_search_path(_PyMainInterpreterConfig *config, _PyCoreConfig *core_config)
+{
+ _PyInitError err = _PyPathConfig_Init(core_config);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+ wchar_t *sys_path = Py_GetPath();
+
+ config->module_search_path = config_create_path_list(sys_path, DELIM);
+ if (config->module_search_path == NULL) {
+ return _Py_INIT_NO_MEMORY();
+ }
+ return _Py_INIT_OK();
+}
+
+
+_PyInitError
+_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config, _PyCoreConfig *core_config)
+{
+ /* Signal handlers are installed by default */
+ if (config->install_signal_handlers < 0) {
+ config->install_signal_handlers = 1;
+ }
+
+ if (config->module_search_path == NULL &&
+ !core_config->_disable_importlib)
+ {
+ _PyInitError err = config_init_module_search_path(config, core_config);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
+ }
+ return _Py_INIT_OK();
+}
+
+
+static int
+pymain_init_python_core(_PyMain *pymain)
+{
pymain_set_global_config(pymain);
pymain_init_stdio(pymain);
if (_Py_INIT_FAILED(pymain->err)) {
return -1;
}
+ return 0;
+}
+
+static int
+pymain_init_python_main(_PyMain *pymain)
+{
if (pymain_add_xoptions(pymain)) {
return -1;
}
return -1;
}
+ /* Create sys.argv list */
+ if (pymain_create_argv_list(pymain) < 0) {
+ pymain->err = _Py_INIT_ERR("failed to create sys.argv");
+ return -1;
+ }
+
+ _PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config,
+ &pymain->core_config);
+ if (_Py_INIT_FAILED(err)) {
+ pymain->err = err;
+ return -1;
+ }
+
if (pymain_init_main_interpreter(pymain)) {
return -1;
}
pymain->main_importer_path = pymain_get_importer(pymain->cmdline.filename);
}
- /* FIXME: put argv into _PyMainInterpreterConfig.
- Currently, PySys_SetArgvEx() can still modify sys.path and so must be
- called after _Py_InitializeMainInterpreter() which calls
- _PyPathConfig_Init(). */
- if (pymain_compute_argv(pymain) < 0) {
- return -1;
- }
- if (pymain_set_sys_argv(pymain) < 0) {
+ if (pymain_update_sys_path(pymain) < 0) {
return -1;
}
return 0;
return 0;
}
- res = pymain_init_python(pymain);
+ res = pymain_init_python_core(pymain);
+ if (res < 0) {
+ return -1;
+ }
+
+ res = pymain_init_python_main(pymain);
if (res < 0) {
return -1;
}
pymain_run_python(pymain);
+ pymain_free_python(pymain);
+
if (Py_FinalizeEx() < 0) {
/* Value unlikely to be confused with a non-error exit status or
other special meaning */
static _PyInitError
-get_program_full_path(const _PyMainInterpreterConfig *main_config,
+get_program_full_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
wchar_t program_full_path[MAXPATHLEN+1];
* $PATH isn't exported, you lose.
*/
#ifdef ALTSEP
- if (wcschr(main_config->program_name, SEP) ||
- wcschr(main_config->program_name, ALTSEP))
+ if (wcschr(core_config->program_name, SEP) ||
+ wcschr(core_config->program_name, ALTSEP))
#else
- if (wcschr(main_config->program_name, SEP))
+ if (wcschr(core_config->program_name, SEP))
#endif
{
- wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
+ wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
}
else if (calculate->path_env) {
const wchar_t *path = calculate->path_env;
}
/* join() is safe for MAXPATHLEN+1 size buffer */
- join(program_full_path, main_config->program_name);
+ join(program_full_path, core_config->program_name);
if (exists(program_full_path)) {
break;
}
static void
calculate_init(PyCalculatePath *calculate,
- const _PyMainInterpreterConfig *main_config)
+ const _PyCoreConfig *core_config)
{
- calculate->home = main_config->home;
+ calculate->home = core_config->home;
calculate->path_env = _wgetenv(L"PATH");
}
static _PyInitError
-calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
+calculate_module_search_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config,
wchar_t *prefix)
{
#endif
/* We only use the default relative PYTHONPATH if we haven't
anything better to use! */
- int skipdefault = (main_config->module_search_path_env != NULL ||
+ int skipdefault = (core_config->module_search_path_env != NULL ||
calculate->home != NULL ||
calculate->machine_path != NULL ||
calculate->user_path != NULL);
bufsz += wcslen(calculate->machine_path) + 1;
}
bufsz += wcslen(calculate->zip_path) + 1;
- if (main_config->module_search_path_env != NULL) {
- bufsz += wcslen(main_config->module_search_path_env) + 1;
+ if (core_config->module_search_path_env != NULL) {
+ bufsz += wcslen(core_config->module_search_path_env) + 1;
}
wchar_t *buf, *start_buf;
if (buf == NULL) {
/* We can't exit, so print a warning and limp along */
fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
- if (main_config->module_search_path_env) {
+ if (core_config->module_search_path_env) {
fprintf(stderr, "Using environment $PYTHONPATH.\n");
- config->module_search_path = main_config->module_search_path_env;
+ config->module_search_path = core_config->module_search_path_env;
}
else {
fprintf(stderr, "Using default static path.\n");
}
start_buf = buf;
- if (main_config->module_search_path_env) {
+ if (core_config->module_search_path_env) {
if (wcscpy_s(buf, bufsz - (buf - start_buf),
- main_config->module_search_path_env)) {
+ core_config->module_search_path_env)) {
return INIT_ERR_BUFFER_OVERFLOW();
}
buf = wcschr(buf, L'\0');
static _PyInitError
-calculate_path_impl(const _PyMainInterpreterConfig *main_config,
+calculate_path_impl(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
_PyInitError err;
return err;
}
- err = get_program_full_path(main_config, calculate, config);
+ err = get_program_full_path(core_config, calculate, config);
if (_Py_INIT_FAILED(err)) {
return err;
}
calculate_home_prefix(calculate, prefix);
- err = calculate_module_search_path(main_config, calculate, config, prefix);
+ err = calculate_module_search_path(core_config, calculate, config, prefix);
if (_Py_INIT_FAILED(err)) {
return err;
}
_PyInitError
-_PyPathConfig_Calculate(_PyPathConfig *config,
- const _PyMainInterpreterConfig *main_config)
+_PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config)
{
PyCalculatePath calculate;
memset(&calculate, 0, sizeof(calculate));
- calculate_init(&calculate, main_config);
+ calculate_init(&calculate, core_config);
- _PyInitError err = calculate_path_impl(main_config, &calculate, config);
+ _PyInitError err = calculate_path_impl(core_config, &calculate, config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
and Py_GetProgramFullPath() */
_PyInitError
-_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
+_PyPathConfig_Init(const _PyCoreConfig *core_config)
{
if (_Py_path_config.module_search_path) {
/* Already initialized */
/* Calculate program_full_path, prefix, exec_prefix (Unix)
or dll_path (Windows), and module_search_path */
- err = _PyPathConfig_Calculate(&new_config, main_config);
+ err = _PyPathConfig_Calculate(&new_config, core_config);
if (_Py_INIT_FAILED(err)) {
_PyPathConfig_Clear(&new_config);
goto done;
}
- /* Copy home and program_name from main_config */
- if (main_config->home != NULL) {
- new_config.home = _PyMem_RawWcsdup(main_config->home);
+ /* Copy home and program_name from core_config */
+ if (core_config->home != NULL) {
+ new_config.home = _PyMem_RawWcsdup(core_config->home);
if (new_config.home == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
new_config.home = NULL;
}
- new_config.program_name = _PyMem_RawWcsdup(main_config->program_name);
+ new_config.program_name = _PyMem_RawWcsdup(core_config->program_name);
if (new_config.program_name == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
}
_PyInitError err;
- _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
+ _PyCoreConfig config = _PyCoreConfig_INIT;
- err = _PyMainInterpreterConfig_ReadEnv(&config);
+ err = _PyCoreConfig_ReadEnv(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}
- err = _PyMainInterpreterConfig_Read(&config);
+ err = _PyCoreConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}
goto error;
}
- _PyMainInterpreterConfig_Clear(&config);
+ _PyCoreConfig_Clear(&config);
return;
error:
- _PyMainInterpreterConfig_Clear(&config);
+ _PyCoreConfig_Clear(&config);
_Py_FatalInitError(err);
}
*/
_PyInitError
-_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config)
+_PyCoreConfig_Read(_PyCoreConfig *config)
{
- /* Signal handlers are installed by default */
- if (config->install_signal_handlers < 0) {
- config->install_signal_handlers = 1;
- }
-
if (config->program_name == NULL) {
#ifdef MS_WINDOWS
const wchar_t *program_name = L"python";
return _Py_INIT_OK();
}
-
void
-_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
+_PyCoreConfig_Clear(_PyCoreConfig *config)
{
#define CLEAR(ATTR) \
do { \
}
+void
+_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
+{
+ Py_CLEAR(config->argv);
+ Py_CLEAR(config->module_search_path);
+}
+
+
/* Update interpreter state based on supplied configuration settings
*
* After calling this function, most of the restrictions on the interpreter
return _Py_INIT_ERR("can't initialize time");
}
- /* GetPath may initialize state that _PySys_EndInit locks
- in, and so has to be called first. */
- err = _PyPathConfig_Init(&interp->config);
- if (_Py_INIT_FAILED(err)) {
- return err;
+ assert(interp->config.module_search_path != NULL);
+ if (PySys_SetObject("path", interp->config.module_search_path) != 0) {
+ return _Py_INIT_ERR("can't assign sys.path");
}
- wchar_t *sys_path = Py_GetPath();
- /* Finish setting up the sys module and import system */
- PySys_SetPath(sys_path);
if (_PySys_EndInit(interp->sysdict) < 0)
return _Py_INIT_ERR("can't finish initializing sys");
}
}
+ if (interp->config.argv != NULL) {
+ if (PySys_SetObject("argv", interp->config.argv) != 0) {
+ return _Py_INIT_ERR("can't assign sys.argv");
+ }
+ }
+
return _Py_INIT_OK();
}
goto done;
}
- err = _PyMainInterpreterConfig_ReadEnv(&config);
+ err = _PyCoreConfig_ReadEnv(&core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
- err = _PyMainInterpreterConfig_Read(&config);
+ err = _PyMainInterpreterConfig_Read(&config, &core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
err = _Py_INIT_OK();
done:
+ _PyCoreConfig_Clear(&core_config);
_PyMainInterpreterConfig_Clear(&config);
return err;
}
interp->config = main_interp->config;
}
- err = _PyPathConfig_Init(&interp->config);
+ err = _PyPathConfig_Init(&interp->core_config);
if (_Py_INIT_FAILED(err)) {
return err;
}