2017-12-04 08:39:15 -04:00
|
|
|
/* Path configuration like module_search_path (sys.path) */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include "osdefs.h"
|
2019-02-28 22:44:13 -04:00
|
|
|
#include "pycore_coreconfig.h"
|
2018-11-06 19:44:03 -04:00
|
|
|
#include "pycore_fileutils.h"
|
2018-10-31 23:15:58 -03:00
|
|
|
#include "pycore_pathconfig.h"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pymem.h"
|
|
|
|
#include "pycore_pystate.h"
|
2018-03-25 10:03:10 -03:00
|
|
|
#include <wchar.h>
|
2017-12-04 08:39:15 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
_PyPathConfig _Py_path_config = _PyPathConfig_INIT;
|
|
|
|
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
static int
|
|
|
|
copy_wstr(wchar_t **dst, const wchar_t *src)
|
|
|
|
{
|
|
|
|
if (src != NULL) {
|
|
|
|
*dst = _PyMem_RawWcsdup(src);
|
|
|
|
if (*dst == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*dst = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-12-04 08:39:15 -04:00
|
|
|
_PyPathConfig_Clear(_PyPathConfig *config)
|
|
|
|
{
|
|
|
|
/* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator,
|
|
|
|
since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be
|
|
|
|
called before Py_Initialize() which can changes the memory allocator. */
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
#define CLEAR(ATTR) \
|
|
|
|
do { \
|
|
|
|
PyMem_RawFree(ATTR); \
|
|
|
|
ATTR = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CLEAR(config->prefix);
|
|
|
|
CLEAR(config->program_full_path);
|
2018-11-18 00:41:48 -04:00
|
|
|
CLEAR(config->exec_prefix);
|
2017-12-04 08:39:15 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
CLEAR(config->dll_path);
|
|
|
|
#endif
|
|
|
|
CLEAR(config->module_search_path);
|
|
|
|
CLEAR(config->home);
|
|
|
|
CLEAR(config->program_name);
|
|
|
|
#undef CLEAR
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
/* Calculate the path configuration: initialize path_config from core_config */
|
|
|
|
static _PyInitError
|
|
|
|
_PyPathConfig_Calculate(_PyPathConfig *path_config,
|
|
|
|
const _PyCoreConfig *core_config)
|
2017-12-04 08:39:15 -04:00
|
|
|
{
|
|
|
|
_PyInitError err;
|
|
|
|
_PyPathConfig new_config = _PyPathConfig_INIT;
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2018-11-18 00:41:48 -04:00
|
|
|
/* Calculate program_full_path, prefix, exec_prefix,
|
|
|
|
dll_path (Windows), and module_search_path */
|
2018-07-20 21:06:16 -03:00
|
|
|
err = _PyPathConfig_Calculate_impl(&new_config, core_config);
|
2017-12-04 08:39:15 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
2018-07-20 21:06:16 -03:00
|
|
|
goto err;
|
2017-12-04 08:39:15 -04:00
|
|
|
}
|
|
|
|
|
2017-12-13 21:20:52 -04:00
|
|
|
/* Copy home and program_name from core_config */
|
2018-07-20 21:06:16 -03:00
|
|
|
if (copy_wstr(&new_config.home, core_config->home) < 0) {
|
|
|
|
err = _Py_INIT_NO_MEMORY();
|
|
|
|
goto err;
|
2017-12-04 08:39:15 -04:00
|
|
|
}
|
2018-07-20 21:06:16 -03:00
|
|
|
if (copy_wstr(&new_config.program_name, core_config->program_name) < 0) {
|
2017-12-04 08:39:15 -04:00
|
|
|
err = _Py_INIT_NO_MEMORY();
|
2018-07-20 21:06:16 -03:00
|
|
|
goto err;
|
2017-12-04 08:39:15 -04:00
|
|
|
}
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
_PyPathConfig_Clear(path_config);
|
|
|
|
*path_config = new_config;
|
|
|
|
|
|
|
|
err = _Py_INIT_OK();
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
_PyPathConfig_Clear(&new_config);
|
|
|
|
|
|
|
|
done:
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_PyInitError
|
|
|
|
_PyPathConfig_SetGlobal(const _PyPathConfig *config)
|
|
|
|
{
|
|
|
|
_PyInitError err;
|
|
|
|
_PyPathConfig new_config = _PyPathConfig_INIT;
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
do { \
|
|
|
|
if (copy_wstr(&new_config.ATTR, config->ATTR) < 0) { \
|
|
|
|
_PyPathConfig_Clear(&new_config); \
|
|
|
|
err = _Py_INIT_NO_MEMORY(); \
|
|
|
|
goto done; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
COPY_ATTR(program_full_path);
|
|
|
|
COPY_ATTR(prefix);
|
2018-11-18 00:41:48 -04:00
|
|
|
COPY_ATTR(exec_prefix);
|
2018-07-20 21:06:16 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_ATTR(dll_path);
|
|
|
|
#endif
|
|
|
|
COPY_ATTR(module_search_path);
|
|
|
|
COPY_ATTR(program_name);
|
|
|
|
COPY_ATTR(home);
|
|
|
|
|
2017-12-04 08:39:15 -04:00
|
|
|
_PyPathConfig_Clear(&_Py_path_config);
|
2018-07-20 21:06:16 -03:00
|
|
|
/* Steal new_config strings; don't clear new_config */
|
2017-12-04 08:39:15 -04:00
|
|
|
_Py_path_config = new_config;
|
|
|
|
|
|
|
|
err = _Py_INIT_OK();
|
|
|
|
|
|
|
|
done:
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
void
|
|
|
|
_PyPathConfig_ClearGlobal(void)
|
|
|
|
{
|
2019-03-18 18:24:28 -03:00
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
_PyPathConfig_Clear(&_Py_path_config);
|
2019-03-18 18:24:28 -03:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
2018-07-20 21:06:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static wchar_t*
|
2019-03-15 11:08:05 -03:00
|
|
|
_PyWstrList_Join(const _PyWstrList *list, wchar_t sep)
|
2018-07-20 21:06:16 -03:00
|
|
|
{
|
|
|
|
size_t len = 1; /* NUL terminator */
|
2019-03-15 11:08:05 -03:00
|
|
|
for (Py_ssize_t i=0; i < list->length; i++) {
|
2018-07-20 21:06:16 -03:00
|
|
|
if (i != 0) {
|
|
|
|
len++;
|
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
len += wcslen(list->items[i]);
|
2018-07-20 21:06:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *text = PyMem_RawMalloc(len * sizeof(wchar_t));
|
|
|
|
if (text == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wchar_t *str = text;
|
2019-03-15 11:08:05 -03:00
|
|
|
for (Py_ssize_t i=0; i < list->length; i++) {
|
|
|
|
wchar_t *path = list->items[i];
|
2018-07-20 21:06:16 -03:00
|
|
|
if (i != 0) {
|
|
|
|
*str++ = SEP;
|
|
|
|
}
|
|
|
|
len = wcslen(path);
|
|
|
|
memcpy(str, path, len * sizeof(wchar_t));
|
|
|
|
str += len;
|
|
|
|
}
|
|
|
|
*str = L'\0';
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the global path configuration from core_config. */
|
|
|
|
_PyInitError
|
|
|
|
_PyCoreConfig_SetPathConfig(const _PyCoreConfig *core_config)
|
|
|
|
{
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
_PyInitError err;
|
|
|
|
_PyPathConfig path_config = _PyPathConfig_INIT;
|
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
path_config.module_search_path = _PyWstrList_Join(&core_config->module_search_paths, DELIM);
|
2018-07-20 21:06:16 -03:00
|
|
|
if (path_config.module_search_path == NULL) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_wstr(&path_config.program_full_path, core_config->executable) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
if (copy_wstr(&path_config.prefix, core_config->prefix) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
2018-11-18 00:41:48 -04:00
|
|
|
if (copy_wstr(&path_config.exec_prefix, core_config->exec_prefix) < 0) {
|
2018-07-20 21:06:16 -03:00
|
|
|
goto no_memory;
|
|
|
|
}
|
2018-11-18 00:41:48 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (copy_wstr(&path_config.dll_path, core_config->dll_path) < 0) {
|
2018-07-20 21:06:16 -03:00
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (copy_wstr(&path_config.program_name, core_config->program_name) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
if (copy_wstr(&path_config.home, core_config->home) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = _PyPathConfig_SetGlobal(&path_config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = _Py_INIT_OK();
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
err = _Py_INIT_NO_MEMORY();
|
|
|
|
|
|
|
|
done:
|
|
|
|
_PyPathConfig_Clear(&path_config);
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static _PyInitError
|
|
|
|
core_config_init_module_search_paths(_PyCoreConfig *config,
|
|
|
|
_PyPathConfig *path_config)
|
|
|
|
{
|
2019-03-15 11:08:05 -03:00
|
|
|
assert(!config->use_module_search_paths);
|
2018-07-20 21:06:16 -03:00
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
_PyWstrList_Clear(&config->module_search_paths);
|
2018-07-20 21:06:16 -03:00
|
|
|
|
|
|
|
const wchar_t *sys_path = path_config->module_search_path;
|
|
|
|
const wchar_t delim = DELIM;
|
|
|
|
const wchar_t *p = sys_path;
|
|
|
|
while (1) {
|
|
|
|
p = wcschr(sys_path, delim);
|
|
|
|
if (p == NULL) {
|
|
|
|
p = sys_path + wcslen(sys_path); /* End of string */
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t path_len = (p - sys_path);
|
|
|
|
wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
|
|
|
|
if (path == NULL) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
memcpy(path, sys_path, path_len * sizeof(wchar_t));
|
|
|
|
path[path_len] = L'\0';
|
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
int res = _PyWstrList_Append(&config->module_search_paths, path);
|
2018-07-20 21:06:16 -03:00
|
|
|
PyMem_RawFree(path);
|
2019-03-15 11:08:05 -03:00
|
|
|
if (res < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
2018-07-20 21:06:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sys_path = p + 1;
|
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
config->use_module_search_paths = 1;
|
2018-07-20 21:06:16 -03:00
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
static _PyInitError
|
|
|
|
_PyCoreConfig_CalculatePathConfig(_PyCoreConfig *config)
|
2018-07-20 21:06:16 -03:00
|
|
|
{
|
|
|
|
_PyPathConfig path_config = _PyPathConfig_INIT;
|
|
|
|
_PyInitError err;
|
|
|
|
|
|
|
|
err = _PyPathConfig_Calculate(&path_config, config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
if (!config->use_module_search_paths) {
|
2018-07-20 21:06:16 -03:00
|
|
|
err = core_config_init_module_search_paths(config, &path_config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->executable == NULL) {
|
|
|
|
if (copy_wstr(&config->executable,
|
|
|
|
path_config.program_full_path) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->prefix == NULL) {
|
|
|
|
if (copy_wstr(&config->prefix, path_config.prefix) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->exec_prefix == NULL) {
|
2018-11-18 00:41:48 -04:00
|
|
|
if (copy_wstr(&config->exec_prefix,
|
|
|
|
path_config.exec_prefix) < 0) {
|
2018-07-20 21:06:16 -03:00
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (config->dll_path == NULL) {
|
|
|
|
if (copy_wstr(&config->dll_path, path_config.dll_path) < 0) {
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-24 08:55:48 -03:00
|
|
|
if (path_config.isolated != -1) {
|
2019-03-04 21:01:27 -04:00
|
|
|
config->preconfig.isolated = path_config.isolated;
|
2018-07-20 22:54:20 -03:00
|
|
|
}
|
2018-07-24 08:55:48 -03:00
|
|
|
if (path_config.site_import != -1) {
|
|
|
|
config->site_import = path_config.site_import;
|
2018-07-20 22:54:20 -03:00
|
|
|
}
|
|
|
|
|
2018-07-20 21:06:16 -03:00
|
|
|
_PyPathConfig_Clear(&path_config);
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
err = _Py_INIT_NO_MEMORY();
|
|
|
|
|
|
|
|
error:
|
|
|
|
_PyPathConfig_Clear(&path_config);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 21:49:17 -03:00
|
|
|
_PyInitError
|
|
|
|
_PyCoreConfig_InitPathConfig(_PyCoreConfig *config)
|
|
|
|
{
|
|
|
|
/* Do we need to calculate the path? */
|
2019-03-15 11:08:05 -03:00
|
|
|
if (!config->use_module_search_paths
|
2018-07-24 21:49:17 -03:00
|
|
|
|| (config->executable == NULL)
|
|
|
|
|| (config->prefix == NULL)
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
|| (config->dll_path == NULL)
|
|
|
|
#endif
|
|
|
|
|| (config->exec_prefix == NULL))
|
|
|
|
{
|
|
|
|
_PyInitError err = _PyCoreConfig_CalculatePathConfig(config);
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->base_prefix == NULL) {
|
|
|
|
if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
|
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->base_exec_prefix == NULL) {
|
2018-11-18 00:41:48 -04:00
|
|
|
if (copy_wstr(&config->base_exec_prefix,
|
|
|
|
config->exec_prefix) < 0) {
|
2018-07-24 21:49:17 -03:00
|
|
|
return _Py_INIT_NO_MEMORY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _Py_INIT_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 08:39:15 -04:00
|
|
|
static void
|
|
|
|
pathconfig_global_init(void)
|
|
|
|
{
|
2018-07-20 21:06:16 -03:00
|
|
|
if (_Py_path_config.module_search_path != NULL) {
|
2017-12-04 08:39:15 -04:00
|
|
|
/* Already initialized */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_PyInitError err;
|
2017-12-13 21:20:52 -04:00
|
|
|
_PyCoreConfig config = _PyCoreConfig_INIT;
|
2017-12-04 08:39:15 -04:00
|
|
|
|
2019-03-25 13:54:58 -03:00
|
|
|
err = _PyCoreConfig_Read(&config);
|
2017-12-04 08:39:15 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-07-20 22:54:20 -03:00
|
|
|
err = _PyCoreConfig_SetPathConfig(&config);
|
2017-12-04 08:39:15 -04:00
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-12-13 21:20:52 -04:00
|
|
|
_PyCoreConfig_Clear(&config);
|
2017-12-04 08:39:15 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2017-12-13 21:20:52 -04:00
|
|
|
_PyCoreConfig_Clear(&config);
|
2019-03-01 07:14:41 -04:00
|
|
|
_Py_ExitInitError(err);
|
2017-12-04 08:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* External interface */
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_SetPath(const wchar_t *path)
|
|
|
|
{
|
|
|
|
if (path == NULL) {
|
|
|
|
_PyPathConfig_Clear(&_Py_path_config);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
_PyPathConfig new_config;
|
|
|
|
new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName());
|
2018-07-20 21:06:16 -03:00
|
|
|
int alloc_error = (new_config.program_full_path == NULL);
|
2017-12-04 08:39:15 -04:00
|
|
|
new_config.prefix = _PyMem_RawWcsdup(L"");
|
2018-07-20 21:06:16 -03:00
|
|
|
alloc_error |= (new_config.prefix == NULL);
|
2018-11-18 00:41:48 -04:00
|
|
|
new_config.exec_prefix = _PyMem_RawWcsdup(L"");
|
|
|
|
alloc_error |= (new_config.exec_prefix == NULL);
|
2017-12-04 08:39:15 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
new_config.dll_path = _PyMem_RawWcsdup(L"");
|
2018-07-20 21:06:16 -03:00
|
|
|
alloc_error |= (new_config.dll_path == NULL);
|
2017-12-04 08:39:15 -04:00
|
|
|
#endif
|
|
|
|
new_config.module_search_path = _PyMem_RawWcsdup(path);
|
2018-07-20 21:06:16 -03:00
|
|
|
alloc_error |= (new_config.module_search_path == NULL);
|
2017-12-04 08:39:15 -04:00
|
|
|
|
|
|
|
/* steal the home and program_name values (to leave them unchanged) */
|
|
|
|
new_config.home = _Py_path_config.home;
|
|
|
|
_Py_path_config.home = NULL;
|
|
|
|
new_config.program_name = _Py_path_config.program_name;
|
|
|
|
_Py_path_config.program_name = NULL;
|
|
|
|
|
|
|
|
_PyPathConfig_Clear(&_Py_path_config);
|
|
|
|
_Py_path_config = new_config;
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
2018-07-20 21:06:16 -03:00
|
|
|
|
|
|
|
if (alloc_error) {
|
|
|
|
Py_FatalError("Py_SetPath() failed: out of memory");
|
|
|
|
}
|
2017-12-04 08:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-12-12 07:55:04 -04:00
|
|
|
Py_SetPythonHome(const wchar_t *home)
|
2017-12-04 08:39:15 -04:00
|
|
|
{
|
|
|
|
if (home == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
PyMem_RawFree(_Py_path_config.home);
|
|
|
|
_Py_path_config.home = _PyMem_RawWcsdup(home);
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
if (_Py_path_config.home == NULL) {
|
|
|
|
Py_FatalError("Py_SetPythonHome() failed: out of memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-12-12 07:55:04 -04:00
|
|
|
Py_SetProgramName(const wchar_t *program_name)
|
2017-12-04 08:39:15 -04:00
|
|
|
{
|
|
|
|
if (program_name == NULL || program_name[0] == L'\0') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
PyMem_RawFree(_Py_path_config.program_name);
|
|
|
|
_Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
if (_Py_path_config.program_name == NULL) {
|
|
|
|
Py_FatalError("Py_SetProgramName() failed: out of memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 00:41:48 -04:00
|
|
|
void
|
|
|
|
_Py_SetProgramFullPath(const wchar_t *program_full_path)
|
|
|
|
{
|
|
|
|
if (program_full_path == NULL || program_full_path[0] == L'\0') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
PyMem_RawFree(_Py_path_config.program_full_path);
|
|
|
|
_Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
if (_Py_path_config.program_full_path == NULL) {
|
|
|
|
Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 08:39:15 -04:00
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetPath(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.module_search_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetPrefix(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetExecPrefix(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.exec_prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetProgramFullPath(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.program_full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t*
|
|
|
|
Py_GetPythonHome(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.home;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetProgramName(void)
|
|
|
|
{
|
|
|
|
pathconfig_global_init();
|
|
|
|
return _Py_path_config.program_name;
|
|
|
|
}
|
|
|
|
|
2019-03-19 12:09:27 -03:00
|
|
|
/* Compute module search path from argv[0] or the current working
|
|
|
|
directory ("-m module" case) which will be prepended to sys.argv:
|
|
|
|
sys.path[0].
|
|
|
|
|
|
|
|
Return 1 if the path is correctly resolved, but *path0_p can be NULL
|
|
|
|
if the Unicode object fail to be created.
|
|
|
|
|
2019-03-19 14:22:55 -03:00
|
|
|
Return 0 if it fails to resolve the full path (and *path0_p will be NULL).
|
|
|
|
For example, return 0 if the current working directory has been removed
|
|
|
|
(bpo-36236) or if argv is empty.
|
2019-03-19 12:09:27 -03:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
_PyPathConfig_ComputeSysPath0(const _PyWstrList *argv, PyObject **path0_p)
|
2017-12-13 16:05:57 -04:00
|
|
|
{
|
2019-03-15 11:08:05 -03:00
|
|
|
assert(_PyWstrList_CheckConsistency(argv));
|
2019-03-19 14:22:55 -03:00
|
|
|
assert(*path0_p == NULL);
|
2019-03-15 11:08:05 -03:00
|
|
|
|
2019-03-19 14:22:55 -03:00
|
|
|
if (argv->length == 0) {
|
|
|
|
/* Leave sys.path unchanged if sys.argv is empty */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *argv0 = argv->items[0];
|
|
|
|
int have_module_arg = (wcscmp(argv0, L"-m") == 0);
|
|
|
|
int have_script_arg = (!have_module_arg && (wcscmp(argv0, L"-c") != 0));
|
|
|
|
|
|
|
|
wchar_t *path0 = argv0;
|
2017-12-13 16:05:57 -04:00
|
|
|
Py_ssize_t n = 0;
|
2019-03-19 14:22:55 -03:00
|
|
|
|
|
|
|
#ifdef HAVE_REALPATH
|
2017-12-13 16:05:57 -04:00
|
|
|
wchar_t fullpath[MAXPATHLEN];
|
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
wchar_t fullpath[MAX_PATH];
|
|
|
|
#endif
|
|
|
|
|
2018-03-25 10:03:10 -03:00
|
|
|
if (have_module_arg) {
|
2019-03-19 12:09:27 -03:00
|
|
|
#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
|
2019-03-19 14:22:55 -03:00
|
|
|
if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
path0 = fullpath;
|
2019-03-19 12:09:27 -03:00
|
|
|
#else
|
2019-03-19 14:22:55 -03:00
|
|
|
path0 = L".";
|
2019-03-19 12:09:27 -03:00
|
|
|
#endif
|
2019-03-19 14:22:55 -03:00
|
|
|
n = wcslen(path0);
|
2018-03-25 10:03:10 -03:00
|
|
|
}
|
2017-12-13 16:05:57 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_READLINK
|
2019-03-19 14:22:55 -03:00
|
|
|
wchar_t link[MAXPATHLEN + 1];
|
|
|
|
int nr = 0;
|
|
|
|
|
|
|
|
if (have_script_arg) {
|
|
|
|
nr = _Py_wreadlink(path0, link, Py_ARRAY_LENGTH(link));
|
|
|
|
}
|
2017-12-13 16:05:57 -04:00
|
|
|
if (nr > 0) {
|
|
|
|
/* It's a symlink */
|
|
|
|
link[nr] = '\0';
|
2019-03-19 14:22:55 -03:00
|
|
|
if (link[0] == SEP) {
|
|
|
|
path0 = link; /* Link to absolute path */
|
|
|
|
}
|
|
|
|
else if (wcschr(link, SEP) == NULL) {
|
|
|
|
/* Link without path */
|
|
|
|
}
|
2017-12-13 16:05:57 -04:00
|
|
|
else {
|
2019-03-19 14:22:55 -03:00
|
|
|
/* Must join(dirname(path0), link) */
|
|
|
|
wchar_t *q = wcsrchr(path0, SEP);
|
|
|
|
if (q == NULL) {
|
|
|
|
/* path0 without path */
|
|
|
|
path0 = link;
|
|
|
|
}
|
2017-12-13 16:05:57 -04:00
|
|
|
else {
|
2019-03-19 14:22:55 -03:00
|
|
|
/* Must make a copy, path0copy has room for 2 * MAXPATHLEN */
|
|
|
|
wchar_t path0copy[2 * MAXPATHLEN + 1];
|
|
|
|
wcsncpy(path0copy, path0, MAXPATHLEN);
|
|
|
|
q = wcsrchr(path0copy, SEP);
|
2017-12-13 16:05:57 -04:00
|
|
|
wcsncpy(q+1, link, MAXPATHLEN);
|
|
|
|
q[MAXPATHLEN + 1] = L'\0';
|
2019-03-19 14:22:55 -03:00
|
|
|
path0 = path0copy;
|
2017-12-13 16:05:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_READLINK */
|
|
|
|
|
2019-03-19 14:22:55 -03:00
|
|
|
wchar_t *p = NULL;
|
|
|
|
|
2017-12-13 16:05:57 -04:00
|
|
|
#if SEP == '\\'
|
|
|
|
/* Special case for Microsoft filename syntax */
|
2018-03-25 10:03:10 -03:00
|
|
|
if (have_script_arg) {
|
2017-12-13 16:05:57 -04:00
|
|
|
wchar_t *q;
|
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
/* Replace the first element in argv with the full path. */
|
|
|
|
wchar_t *ptemp;
|
2019-03-19 14:22:55 -03:00
|
|
|
if (GetFullPathNameW(path0,
|
2017-12-13 16:05:57 -04:00
|
|
|
Py_ARRAY_LENGTH(fullpath),
|
|
|
|
fullpath,
|
|
|
|
&ptemp)) {
|
2019-03-19 14:22:55 -03:00
|
|
|
path0 = fullpath;
|
2017-12-13 16:05:57 -04:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-19 14:22:55 -03:00
|
|
|
p = wcsrchr(path0, SEP);
|
2017-12-13 16:05:57 -04:00
|
|
|
/* Test for alternate separator */
|
2019-03-19 14:22:55 -03:00
|
|
|
q = wcsrchr(p ? p : path0, '/');
|
2017-12-13 16:05:57 -04:00
|
|
|
if (q != NULL)
|
|
|
|
p = q;
|
|
|
|
if (p != NULL) {
|
2019-03-19 14:22:55 -03:00
|
|
|
n = p + 1 - path0;
|
2017-12-13 16:05:57 -04:00
|
|
|
if (n > 1 && p[-1] != ':')
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 14:22:55 -03:00
|
|
|
#else
|
|
|
|
/* All other filename syntaxes */
|
2018-03-25 10:03:10 -03:00
|
|
|
if (have_script_arg) {
|
2017-12-13 16:05:57 -04:00
|
|
|
#if defined(HAVE_REALPATH)
|
2019-03-19 14:22:55 -03:00
|
|
|
if (_Py_wrealpath(path0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
|
|
|
|
path0 = fullpath;
|
2017-12-13 16:05:57 -04:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-19 14:22:55 -03:00
|
|
|
p = wcsrchr(path0, SEP);
|
2017-12-13 16:05:57 -04:00
|
|
|
}
|
|
|
|
if (p != NULL) {
|
2019-03-19 14:22:55 -03:00
|
|
|
n = p + 1 - path0;
|
2017-12-13 16:05:57 -04:00
|
|
|
#if SEP == '/' /* Special case for Unix filename syntax */
|
2019-03-19 14:22:55 -03:00
|
|
|
if (n > 1) {
|
|
|
|
/* Drop trailing separator */
|
|
|
|
n--;
|
|
|
|
}
|
2017-12-13 16:05:57 -04:00
|
|
|
#endif /* Unix */
|
|
|
|
}
|
|
|
|
#endif /* All others */
|
|
|
|
|
2019-03-19 14:22:55 -03:00
|
|
|
*path0_p = PyUnicode_FromWideChar(path0, n);
|
2019-03-19 12:09:27 -03:00
|
|
|
return 1;
|
2017-12-13 16:05:57 -04:00
|
|
|
}
|
|
|
|
|
2017-12-21 11:49:13 -04:00
|
|
|
|
2019-02-03 00:26:55 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#define WCSTOK wcstok_s
|
|
|
|
#else
|
|
|
|
#define WCSTOK wcstok
|
|
|
|
#endif
|
|
|
|
|
2017-12-21 11:49:13 -04:00
|
|
|
/* Search for a prefix value in an environment file (pyvenv.cfg).
|
|
|
|
If found, copy it into the provided buffer. */
|
|
|
|
int
|
|
|
|
_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
|
|
|
|
wchar_t *value, size_t value_size)
|
|
|
|
{
|
|
|
|
int result = 0; /* meaning not found */
|
2019-03-18 22:58:14 -03:00
|
|
|
char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */
|
|
|
|
buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0';
|
2017-12-21 11:49:13 -04:00
|
|
|
|
|
|
|
fseek(env_file, 0, SEEK_SET);
|
|
|
|
while (!feof(env_file)) {
|
2019-03-18 22:58:14 -03:00
|
|
|
char * p = fgets(buffer, Py_ARRAY_LENGTH(buffer) - 1, env_file);
|
2017-12-21 11:49:13 -04:00
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2018-01-18 06:15:25 -04:00
|
|
|
|
|
|
|
size_t n = strlen(p);
|
2017-12-21 11:49:13 -04:00
|
|
|
if (p[n - 1] != '\n') {
|
|
|
|
/* line has overflowed - bail */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p[0] == '#') {
|
|
|
|
/* Comment - skip */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-18 06:15:25 -04:00
|
|
|
|
2019-03-18 21:46:25 -03:00
|
|
|
wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL);
|
2018-01-15 05:45:49 -04:00
|
|
|
if (tmpbuffer) {
|
2017-12-21 11:49:13 -04:00
|
|
|
wchar_t * state;
|
2019-02-03 00:26:55 -04:00
|
|
|
wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state);
|
2017-12-21 11:49:13 -04:00
|
|
|
if ((tok != NULL) && !wcscmp(tok, key)) {
|
2019-02-03 00:26:55 -04:00
|
|
|
tok = WCSTOK(NULL, L" \t", &state);
|
2017-12-21 11:49:13 -04:00
|
|
|
if ((tok != NULL) && !wcscmp(tok, L"=")) {
|
2019-02-03 00:26:55 -04:00
|
|
|
tok = WCSTOK(NULL, L"\r\n", &state);
|
2017-12-21 11:49:13 -04:00
|
|
|
if (tok != NULL) {
|
2019-03-18 22:58:14 -03:00
|
|
|
wcsncpy(value, tok, value_size - 1);
|
|
|
|
value[value_size - 1] = L'\0';
|
2017-12-21 11:49:13 -04:00
|
|
|
result = 1;
|
|
|
|
PyMem_RawFree(tmpbuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PyMem_RawFree(tmpbuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-04 08:39:15 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|