static _PyInitError calculate_path_impl(const _PyCoreConfig *core_config, PyCalculatePath *calculate, _PyPathConfig *config) { _PyInitError err; err = calculate_program_full_path(core_config, calculate, config); if (_Py_INIT_FAILED(err)) { return err; } err = calculate_argv0_path(calculate, config->program_full_path); if (_Py_INIT_FAILED(err)) { return err; } calculate_read_pyenv(calculate); wchar_t prefix[MAXPATHLEN+1]; memset(prefix, 0, sizeof(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(core_config, calculate, exec_prefix); if ((!calculate->prefix_found || !calculate->exec_prefix_found) && !core_config->_frozen) { fprintf(stderr, "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); } err = calculate_module_search_path(core_config, calculate, prefix, exec_prefix, config); if (_Py_INIT_FAILED(err)) { return err; } calculate_reduce_prefix(calculate, prefix); config->prefix = _PyMem_RawWcsdup(prefix); if (config->prefix == NULL) { return _Py_INIT_NO_MEMORY(); } calculate_reduce_exec_prefix(calculate, exec_prefix); config->exec_prefix = _PyMem_RawWcsdup(exec_prefix); if (config->exec_prefix == NULL) { return _Py_INIT_NO_MEMORY(); } return _Py_INIT_OK(); }
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()); int alloc_error = (new_config.program_full_path == NULL); new_config.prefix = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.prefix == NULL); #ifdef MS_WINDOWS new_config.dll_path = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.dll_path == NULL); #else new_config.exec_prefix = _PyMem_RawWcsdup(L""); alloc_error |= (new_config.exec_prefix == NULL); #endif new_config.module_search_path = _PyMem_RawWcsdup(path); alloc_error |= (new_config.module_search_path == NULL); /* 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); if (alloc_error) { Py_FatalError("Py_SetPath() failed: out of memory"); } }
static _PyInitError calculate_path_impl(const _PyCoreConfig *core_config, PyCalculatePath *calculate, _PyPathConfig *config) { _PyInitError err; err = get_dll_path(calculate, config); if (_Py_INIT_FAILED(err)) { return err; } err = get_program_full_path(core_config, calculate, config); if (_Py_INIT_FAILED(err)) { return err; } /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */ wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_full_path); reduce(calculate->argv0_path); wchar_t prefix[MAXPATHLEN+1]; memset(prefix, 0, sizeof(prefix)); /* Search for a sys.path file */ if (calculate_pth_file(config, prefix)) { goto done; } calculate_pyvenv_file(calculate); /* Calculate zip archive path from DLL or exe path */ change_ext(calculate->zip_path, config->dll_path[0] ? config->dll_path : config->program_full_path, L".zip"); calculate_home_prefix(calculate, prefix); err = calculate_module_search_path(core_config, calculate, config, prefix); if (_Py_INIT_FAILED(err)) { return err; } done: config->prefix = _PyMem_RawWcsdup(prefix); if (config->prefix == NULL) { return _Py_INIT_NO_MEMORY(); } return _Py_INIT_OK(); }
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; }
void Py_SetProgramName(const wchar_t *program_name) { 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"); } }
void Py_SetPythonHome(const wchar_t *home) { 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"); } }
static _PyInitError get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config) { wchar_t dll_path[MAXPATHLEN+1]; memset(dll_path, 0, sizeof(dll_path)); #ifdef Py_ENABLE_SHARED extern HANDLE PyWin_DLLhModule; if (PyWin_DLLhModule) { if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) { dll_path[0] = 0; } } #else dll_path[0] = 0; #endif config->dll_path = _PyMem_RawWcsdup(dll_path); if (config->dll_path == NULL) { return _Py_INIT_NO_MEMORY(); } return _Py_INIT_OK(); }
static _PyInitError calculate_program_full_path(const _PyCoreConfig *core_config, PyCalculatePath *calculate, _PyPathConfig *config) { wchar_t program_full_path[MAXPATHLEN+1]; memset(program_full_path, 0, sizeof(program_full_path)); #ifdef __APPLE__ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 uint32_t nsexeclength = MAXPATHLEN; #else unsigned long nsexeclength = MAXPATHLEN; #endif char execpath[MAXPATHLEN+1]; #endif /* If there is no slash in the argv0 path, then we have to * assume python is on the user's $PATH, since there's no * other way to find a directory to start the search from. If * $PATH isn't exported, you lose. */ 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 * "#!/opt/python2.3/bin/python", the kernel only passes "python" * as argv[0], which falls through to the $PATH search below. * If /opt/python2.3/bin isn't in your path, or is near the end, * this algorithm may incorrectly find /usr/bin/python. To work * around this, we can use _NSGetExecutablePath to get a better * hint of what the intended interpreter was, although this * will fail if a relative path was used. but in that case, * absolutize() should help us out below */ else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) { size_t len; wchar_t *path = Py_DecodeLocale(execpath, &len); if (path == NULL) { return DECODE_LOCALE_ERR("executable path", len); } wcsncpy(program_full_path, path, MAXPATHLEN); PyMem_RawFree(path); } #endif /* __APPLE__ */ else if (calculate->path_env) { wchar_t *path = calculate->path_env; while (1) { wchar_t *delim = wcschr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) { len = MAXPATHLEN; } wcsncpy(program_full_path, path, len); program_full_path[len] = '\0'; } else { wcsncpy(program_full_path, path, MAXPATHLEN); } joinpath(program_full_path, core_config->program_name); if (isxfile(program_full_path)) { break; } if (!delim) { program_full_path[0] = L'\0'; break; } path = delim + 1; } } else { program_full_path[0] = '\0'; } if (program_full_path[0] != SEP && program_full_path[0] != '\0') { absolutize(program_full_path); } config->program_full_path = _PyMem_RawWcsdup(program_full_path); if (config->program_full_path == NULL) { return _Py_INIT_NO_MEMORY(); } return _Py_INIT_OK(); }