2017-12-04 20:39:15 +08:00
|
|
|
/* Path configuration like module_search_path (sys.path) */
|
|
|
|
|
|
|
|
#include "Python.h"
|
2023-09-06 21:56:08 +08:00
|
|
|
#include "pycore_initconfig.h" // _PyStatus_OK()
|
|
|
|
#include "pycore_fileutils.h" // _Py_wgetcwd()
|
2018-11-01 10:15:58 +08:00
|
|
|
#include "pycore_pathconfig.h"
|
2020-04-15 08:57:50 +08:00
|
|
|
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
|
2018-03-25 21:03:10 +08:00
|
|
|
#include <wchar.h>
|
2023-09-06 21:56:08 +08:00
|
|
|
|
|
|
|
#include "marshal.h" // PyMarshal_ReadObjectFromString
|
|
|
|
#include "osdefs.h" // DELIM
|
|
|
|
|
2020-04-14 23:52:15 +08:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
# include <windows.h> // GetFullPathNameW(), MAX_PATH
|
2021-12-03 08:08:42 +08:00
|
|
|
# include <pathcch.h>
|
|
|
|
# include <shlwapi.h>
|
2020-04-14 23:52:15 +08:00
|
|
|
#endif
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
/* External interface */
|
|
|
|
|
|
|
|
/* Stored values set by C API functions */
|
|
|
|
typedef struct _PyPathConfig {
|
|
|
|
/* Full path to the Python program */
|
|
|
|
wchar_t *program_full_path;
|
|
|
|
wchar_t *prefix;
|
|
|
|
wchar_t *exec_prefix;
|
|
|
|
wchar_t *stdlib_dir;
|
|
|
|
/* Set by Py_SetPath */
|
|
|
|
wchar_t *module_search_path;
|
|
|
|
/* Set by _PyPathConfig_UpdateGlobal */
|
|
|
|
wchar_t *calculated_module_search_path;
|
|
|
|
/* Python program name */
|
|
|
|
wchar_t *program_name;
|
|
|
|
/* Set by Py_SetPythonHome() or PYTHONHOME environment variable */
|
|
|
|
wchar_t *home;
|
2022-06-17 05:41:57 +08:00
|
|
|
int _is_python_build;
|
2021-12-03 08:08:42 +08:00
|
|
|
} _PyPathConfig;
|
|
|
|
|
|
|
|
# define _PyPathConfig_INIT \
|
2022-06-17 05:41:57 +08:00
|
|
|
{.module_search_path = NULL, ._is_python_build = 0}
|
2021-12-03 08:08:42 +08:00
|
|
|
|
|
|
|
|
2017-12-04 20:39:15 +08:00
|
|
|
_PyPathConfig _Py_path_config = _PyPathConfig_INIT;
|
|
|
|
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
const wchar_t *
|
|
|
|
_PyPathConfig_GetGlobalModuleSearchPath(void)
|
2018-07-21 08:06:16 +08:00
|
|
|
{
|
2021-12-03 08:08:42 +08:00
|
|
|
return _Py_path_config.module_search_path;
|
2018-07-21 08:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
void
|
|
|
|
_PyPathConfig_ClearGlobal(void)
|
2017-12-04 20:39:15 +08:00
|
|
|
{
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
#define CLEAR(ATTR) \
|
|
|
|
do { \
|
2021-12-03 08:08:42 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.ATTR); \
|
|
|
|
_Py_path_config.ATTR = NULL; \
|
2017-12-04 20:39:15 +08:00
|
|
|
} while (0)
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
CLEAR(program_full_path);
|
|
|
|
CLEAR(prefix);
|
|
|
|
CLEAR(exec_prefix);
|
|
|
|
CLEAR(stdlib_dir);
|
|
|
|
CLEAR(module_search_path);
|
|
|
|
CLEAR(calculated_module_search_path);
|
|
|
|
CLEAR(program_name);
|
|
|
|
CLEAR(home);
|
2022-06-17 05:41:57 +08:00
|
|
|
_Py_path_config._is_python_build = 0;
|
2019-09-26 08:22:35 +08:00
|
|
|
|
2017-12-04 20:39:15 +08:00
|
|
|
#undef CLEAR
|
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
PyStatus
|
|
|
|
_PyPathConfig_ReadGlobal(PyConfig *config)
|
2017-12-04 20:39:15 +08:00
|
|
|
{
|
2021-12-03 08:08:42 +08:00
|
|
|
PyStatus status = _PyStatus_OK();
|
2017-12-04 20:39:15 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
#define COPY(ATTR) \
|
2019-09-24 00:47:29 +08:00
|
|
|
do { \
|
2021-12-03 08:08:42 +08:00
|
|
|
if (_Py_path_config.ATTR && !config->ATTR) { \
|
|
|
|
status = PyConfig_SetString(config, &config->ATTR, _Py_path_config.ATTR); \
|
|
|
|
if (_PyStatus_EXCEPTION(status)) goto done; \
|
2019-09-24 00:47:29 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
#define COPY2(ATTR, SRCATTR) \
|
|
|
|
do { \
|
|
|
|
if (_Py_path_config.SRCATTR && !config->ATTR) { \
|
|
|
|
status = PyConfig_SetString(config, &config->ATTR, _Py_path_config.SRCATTR); \
|
|
|
|
if (_PyStatus_EXCEPTION(status)) goto done; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2022-06-17 05:41:57 +08:00
|
|
|
#define COPY_INT(ATTR) \
|
|
|
|
do { \
|
|
|
|
assert(_Py_path_config.ATTR >= 0); \
|
|
|
|
if ((_Py_path_config.ATTR >= 0) && (config->ATTR <= 0)) { \
|
|
|
|
config->ATTR = _Py_path_config.ATTR; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
COPY(prefix);
|
|
|
|
COPY(exec_prefix);
|
|
|
|
COPY(stdlib_dir);
|
|
|
|
COPY(program_name);
|
|
|
|
COPY(home);
|
|
|
|
COPY2(executable, program_full_path);
|
2022-06-17 05:41:57 +08:00
|
|
|
COPY_INT(_is_python_build);
|
2021-12-03 08:08:42 +08:00
|
|
|
// module_search_path must be initialised - not read
|
|
|
|
#undef COPY
|
|
|
|
#undef COPY2
|
2022-06-17 05:41:57 +08:00
|
|
|
#undef COPY_INT
|
2018-07-21 08:06:16 +08:00
|
|
|
|
|
|
|
done:
|
2019-05-27 22:39:22 +08:00
|
|
|
return status;
|
2018-07-21 08:06:16 +08:00
|
|
|
}
|
|
|
|
|
2019-09-26 21:51:50 +08:00
|
|
|
PyStatus
|
2021-12-03 08:08:42 +08:00
|
|
|
_PyPathConfig_UpdateGlobal(const PyConfig *config)
|
2019-09-26 21:51:50 +08:00
|
|
|
{
|
2019-09-24 00:47:29 +08:00
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
#define COPY(ATTR) \
|
|
|
|
do { \
|
|
|
|
if (config->ATTR) { \
|
|
|
|
PyMem_RawFree(_Py_path_config.ATTR); \
|
|
|
|
_Py_path_config.ATTR = _PyMem_RawWcsdup(config->ATTR); \
|
|
|
|
if (!_Py_path_config.ATTR) goto error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
#define COPY2(ATTR, SRCATTR) \
|
|
|
|
do { \
|
|
|
|
if (config->SRCATTR) { \
|
|
|
|
PyMem_RawFree(_Py_path_config.ATTR); \
|
|
|
|
_Py_path_config.ATTR = _PyMem_RawWcsdup(config->SRCATTR); \
|
|
|
|
if (!_Py_path_config.ATTR) goto error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2022-06-17 05:41:57 +08:00
|
|
|
#define COPY_INT(ATTR) \
|
|
|
|
do { \
|
|
|
|
if (config->ATTR > 0) { \
|
|
|
|
_Py_path_config.ATTR = config->ATTR; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
COPY(prefix);
|
|
|
|
COPY(exec_prefix);
|
|
|
|
COPY(stdlib_dir);
|
|
|
|
COPY(program_name);
|
|
|
|
COPY(home);
|
|
|
|
COPY2(program_full_path, executable);
|
2022-06-17 05:41:57 +08:00
|
|
|
COPY_INT(_is_python_build);
|
2021-12-03 08:08:42 +08:00
|
|
|
#undef COPY
|
|
|
|
#undef COPY2
|
2022-06-17 05:41:57 +08:00
|
|
|
#undef COPY_INT
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.module_search_path);
|
|
|
|
_Py_path_config.module_search_path = NULL;
|
|
|
|
PyMem_RawFree(_Py_path_config.calculated_module_search_path);
|
|
|
|
_Py_path_config.calculated_module_search_path = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
size_t cch = 1;
|
|
|
|
for (Py_ssize_t i = 0; i < config->module_search_paths.length; ++i) {
|
|
|
|
cch += 1 + wcslen(config->module_search_paths.items[i]);
|
2018-07-25 08:49:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
wchar_t *path = (wchar_t*)PyMem_RawMalloc(sizeof(wchar_t) * cch);
|
|
|
|
if (!path) {
|
|
|
|
goto error;
|
2018-07-25 08:49:17 +08:00
|
|
|
}
|
2021-12-03 08:08:42 +08:00
|
|
|
wchar_t *p = path;
|
|
|
|
for (Py_ssize_t i = 0; i < config->module_search_paths.length; ++i) {
|
|
|
|
wcscpy(p, config->module_search_paths.items[i]);
|
|
|
|
p = wcschr(p, L'\0');
|
|
|
|
*p++ = DELIM;
|
|
|
|
*p = L'\0';
|
2018-07-25 08:49:17 +08:00
|
|
|
}
|
2019-06-30 01:34:11 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
do {
|
|
|
|
*p = L'\0';
|
|
|
|
} while (p != path && *--p == DELIM);
|
|
|
|
_Py_path_config.calculated_module_search_path = path;
|
|
|
|
} while (0);
|
2019-06-30 01:34:11 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
2019-05-27 22:39:22 +08:00
|
|
|
return _PyStatus_OK();
|
2018-07-25 08:49:17 +08:00
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
error:
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
2018-07-25 08:49:17 +08:00
|
|
|
|
2017-12-04 20:39:15 +08:00
|
|
|
|
2020-03-26 02:27:36 +08:00
|
|
|
static void _Py_NO_RETURN
|
|
|
|
path_out_of_memory(const char *func)
|
|
|
|
{
|
|
|
|
_Py_FatalErrorFunc(func, "out of memory");
|
|
|
|
}
|
|
|
|
|
2023-06-01 15:14:02 +08:00
|
|
|
// Removed in Python 3.13 API, but kept for the stable ABI
|
|
|
|
PyAPI_FUNC(void)
|
2017-12-04 20:39:15 +08:00
|
|
|
Py_SetPath(const wchar_t *path)
|
|
|
|
{
|
|
|
|
if (path == NULL) {
|
2021-12-03 08:08:42 +08:00
|
|
|
_PyPathConfig_ClearGlobal();
|
2017-12-04 20:39:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2019-09-24 00:47:29 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.prefix);
|
|
|
|
PyMem_RawFree(_Py_path_config.exec_prefix);
|
2021-09-29 02:18:28 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.stdlib_dir);
|
2019-09-24 00:47:29 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.module_search_path);
|
2021-12-03 08:08:42 +08:00
|
|
|
PyMem_RawFree(_Py_path_config.calculated_module_search_path);
|
2019-09-24 00:47:29 +08:00
|
|
|
|
|
|
|
_Py_path_config.prefix = _PyMem_RawWcsdup(L"");
|
|
|
|
_Py_path_config.exec_prefix = _PyMem_RawWcsdup(L"");
|
2021-09-29 02:18:28 +08:00
|
|
|
// XXX Copy this from the new module_search_path?
|
|
|
|
if (_Py_path_config.home != NULL) {
|
|
|
|
_Py_path_config.stdlib_dir = _PyMem_RawWcsdup(_Py_path_config.home);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_Py_path_config.stdlib_dir = _PyMem_RawWcsdup(L"");
|
|
|
|
}
|
2019-09-24 00:47:29 +08:00
|
|
|
_Py_path_config.module_search_path = _PyMem_RawWcsdup(path);
|
2021-12-03 08:08:42 +08:00
|
|
|
_Py_path_config.calculated_module_search_path = NULL;
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
2018-07-21 08:06:16 +08:00
|
|
|
|
2020-11-11 04:10:22 +08:00
|
|
|
if (_Py_path_config.prefix == NULL
|
2019-09-24 00:47:29 +08:00
|
|
|
|| _Py_path_config.exec_prefix == NULL
|
2021-09-29 02:18:28 +08:00
|
|
|
|| _Py_path_config.stdlib_dir == NULL
|
2019-09-24 00:47:29 +08:00
|
|
|
|| _Py_path_config.module_search_path == NULL)
|
|
|
|
{
|
2020-03-26 02:27:36 +08:00
|
|
|
path_out_of_memory(__func__);
|
2018-07-21 08:06:16 +08:00
|
|
|
}
|
2017-12-04 20:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-18 21:20:38 +08:00
|
|
|
void
|
2017-12-12 19:55:04 +08:00
|
|
|
Py_SetPythonHome(const wchar_t *home)
|
2017-12-04 20:39:15 +08:00
|
|
|
{
|
2021-12-03 08:08:42 +08:00
|
|
|
int has_value = home && home[0];
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
PyMem_RawFree(_Py_path_config.home);
|
2022-11-04 01:34:32 +08:00
|
|
|
_Py_path_config.home = NULL;
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
if (has_value) {
|
|
|
|
_Py_path_config.home = _PyMem_RawWcsdup(home);
|
|
|
|
}
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
if (has_value && _Py_path_config.home == NULL) {
|
2020-03-26 02:27:36 +08:00
|
|
|
path_out_of_memory(__func__);
|
2017-12-04 20:39:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-18 21:20:38 +08:00
|
|
|
void
|
2017-12-12 19:55:04 +08:00
|
|
|
Py_SetProgramName(const wchar_t *program_name)
|
2017-12-04 20:39:15 +08:00
|
|
|
{
|
2021-12-03 08:08:42 +08:00
|
|
|
int has_value = program_name && program_name[0];
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
|
|
|
PyMem_RawFree(_Py_path_config.program_name);
|
2022-11-04 01:34:32 +08:00
|
|
|
_Py_path_config.program_name = NULL;
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
if (has_value) {
|
|
|
|
_Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
|
|
|
|
}
|
2017-12-04 20:39:15 +08:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
if (has_value && _Py_path_config.program_name == NULL) {
|
2020-03-26 02:27:36 +08:00
|
|
|
path_out_of_memory(__func__);
|
2017-12-04 20:39:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetPath(void)
|
|
|
|
{
|
2021-12-03 08:08:42 +08:00
|
|
|
/* If the user has provided a path, return that */
|
|
|
|
if (_Py_path_config.module_search_path) {
|
|
|
|
return _Py_path_config.module_search_path;
|
|
|
|
}
|
|
|
|
/* If we have already done calculations, return the calculated path */
|
|
|
|
return _Py_path_config.calculated_module_search_path;
|
2017-12-04 20:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-29 02:18:28 +08:00
|
|
|
wchar_t *
|
|
|
|
_Py_GetStdlibDir(void)
|
|
|
|
{
|
|
|
|
wchar_t *stdlib_dir = _Py_path_config.stdlib_dir;
|
|
|
|
if (stdlib_dir != NULL && stdlib_dir[0] != L'\0') {
|
|
|
|
return stdlib_dir;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 20:39:15 +08:00
|
|
|
wchar_t *
|
|
|
|
Py_GetPrefix(void)
|
|
|
|
{
|
|
|
|
return _Py_path_config.prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetExecPrefix(void)
|
|
|
|
{
|
|
|
|
return _Py_path_config.exec_prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetProgramFullPath(void)
|
|
|
|
{
|
|
|
|
return _Py_path_config.program_full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t*
|
|
|
|
Py_GetPythonHome(void)
|
|
|
|
{
|
|
|
|
return _Py_path_config.home;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *
|
|
|
|
Py_GetProgramName(void)
|
|
|
|
{
|
|
|
|
return _Py_path_config.program_name;
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:08:42 +08:00
|
|
|
|
|
|
|
|
2019-03-19 23:09:27 +08: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].
|
|
|
|
|
2019-05-16 22:39:26 +08:00
|
|
|
Return 1 if the path is correctly resolved and written into *path0_p.
|
2019-03-19 23:09:27 +08:00
|
|
|
|
2019-05-16 22:39:26 +08:00
|
|
|
Return 0 if it fails to resolve the full path. For example, return 0 if the
|
|
|
|
current working directory has been removed (bpo-36236) or if argv is empty.
|
|
|
|
|
|
|
|
Raise an exception and return -1 on error.
|
2019-03-19 23:09:27 +08:00
|
|
|
*/
|
|
|
|
int
|
2019-05-27 22:39:22 +08:00
|
|
|
_PyPathConfig_ComputeSysPath0(const PyWideStringList *argv, PyObject **path0_p)
|
2017-12-14 04:05:57 +08:00
|
|
|
{
|
2019-05-27 22:39:22 +08:00
|
|
|
assert(_PyWideStringList_CheckConsistency(argv));
|
2019-03-15 22:08:05 +08:00
|
|
|
|
2019-03-20 01:22:55 +08: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-14 04:05:57 +08:00
|
|
|
Py_ssize_t n = 0;
|
2019-03-20 01:22:55 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_REALPATH
|
2017-12-14 04:05:57 +08:00
|
|
|
wchar_t fullpath[MAXPATHLEN];
|
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
wchar_t fullpath[MAX_PATH];
|
|
|
|
#endif
|
|
|
|
|
2018-03-25 21:03:10 +08:00
|
|
|
if (have_module_arg) {
|
2019-03-19 23:09:27 +08:00
|
|
|
#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
|
2019-03-20 01:22:55 +08:00
|
|
|
if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
path0 = fullpath;
|
2019-03-19 23:09:27 +08:00
|
|
|
#else
|
2019-03-20 01:22:55 +08:00
|
|
|
path0 = L".";
|
2019-03-19 23:09:27 +08:00
|
|
|
#endif
|
2019-03-20 01:22:55 +08:00
|
|
|
n = wcslen(path0);
|
2018-03-25 21:03:10 +08:00
|
|
|
}
|
2017-12-14 04:05:57 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_READLINK
|
2019-03-20 01:22:55 +08:00
|
|
|
wchar_t link[MAXPATHLEN + 1];
|
|
|
|
int nr = 0;
|
2020-06-22 15:27:20 +08:00
|
|
|
wchar_t path0copy[2 * MAXPATHLEN + 1];
|
2019-03-20 01:22:55 +08:00
|
|
|
|
|
|
|
if (have_script_arg) {
|
|
|
|
nr = _Py_wreadlink(path0, link, Py_ARRAY_LENGTH(link));
|
|
|
|
}
|
2017-12-14 04:05:57 +08:00
|
|
|
if (nr > 0) {
|
|
|
|
/* It's a symlink */
|
|
|
|
link[nr] = '\0';
|
2019-03-20 01:22:55 +08:00
|
|
|
if (link[0] == SEP) {
|
|
|
|
path0 = link; /* Link to absolute path */
|
|
|
|
}
|
|
|
|
else if (wcschr(link, SEP) == NULL) {
|
|
|
|
/* Link without path */
|
|
|
|
}
|
2017-12-14 04:05:57 +08:00
|
|
|
else {
|
2019-03-20 01:22:55 +08:00
|
|
|
/* Must join(dirname(path0), link) */
|
|
|
|
wchar_t *q = wcsrchr(path0, SEP);
|
|
|
|
if (q == NULL) {
|
|
|
|
/* path0 without path */
|
|
|
|
path0 = link;
|
|
|
|
}
|
2017-12-14 04:05:57 +08:00
|
|
|
else {
|
2019-03-20 01:22:55 +08:00
|
|
|
/* Must make a copy, path0copy has room for 2 * MAXPATHLEN */
|
|
|
|
wcsncpy(path0copy, path0, MAXPATHLEN);
|
|
|
|
q = wcsrchr(path0copy, SEP);
|
2017-12-14 04:05:57 +08:00
|
|
|
wcsncpy(q+1, link, MAXPATHLEN);
|
|
|
|
q[MAXPATHLEN + 1] = L'\0';
|
2019-03-20 01:22:55 +08:00
|
|
|
path0 = path0copy;
|
2017-12-14 04:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_READLINK */
|
|
|
|
|
2019-03-20 01:22:55 +08:00
|
|
|
wchar_t *p = NULL;
|
|
|
|
|
2017-12-14 04:05:57 +08:00
|
|
|
#if SEP == '\\'
|
|
|
|
/* Special case for Microsoft filename syntax */
|
2018-03-25 21:03:10 +08:00
|
|
|
if (have_script_arg) {
|
2017-12-14 04:05:57 +08:00
|
|
|
wchar_t *q;
|
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
/* Replace the first element in argv with the full path. */
|
|
|
|
wchar_t *ptemp;
|
2019-03-20 01:22:55 +08:00
|
|
|
if (GetFullPathNameW(path0,
|
2017-12-14 04:05:57 +08:00
|
|
|
Py_ARRAY_LENGTH(fullpath),
|
|
|
|
fullpath,
|
|
|
|
&ptemp)) {
|
2019-03-20 01:22:55 +08:00
|
|
|
path0 = fullpath;
|
2017-12-14 04:05:57 +08:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-20 01:22:55 +08:00
|
|
|
p = wcsrchr(path0, SEP);
|
2017-12-14 04:05:57 +08:00
|
|
|
/* Test for alternate separator */
|
2019-03-20 01:22:55 +08:00
|
|
|
q = wcsrchr(p ? p : path0, '/');
|
2017-12-14 04:05:57 +08:00
|
|
|
if (q != NULL)
|
|
|
|
p = q;
|
|
|
|
if (p != NULL) {
|
2019-03-20 01:22:55 +08:00
|
|
|
n = p + 1 - path0;
|
2017-12-14 04:05:57 +08:00
|
|
|
if (n > 1 && p[-1] != ':')
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 01:22:55 +08:00
|
|
|
#else
|
|
|
|
/* All other filename syntaxes */
|
2018-03-25 21:03:10 +08:00
|
|
|
if (have_script_arg) {
|
2017-12-14 04:05:57 +08:00
|
|
|
#if defined(HAVE_REALPATH)
|
2019-03-20 01:22:55 +08:00
|
|
|
if (_Py_wrealpath(path0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
|
|
|
|
path0 = fullpath;
|
2017-12-14 04:05:57 +08:00
|
|
|
}
|
|
|
|
#endif
|
2019-03-20 01:22:55 +08:00
|
|
|
p = wcsrchr(path0, SEP);
|
2017-12-14 04:05:57 +08:00
|
|
|
}
|
|
|
|
if (p != NULL) {
|
2019-03-20 01:22:55 +08:00
|
|
|
n = p + 1 - path0;
|
2017-12-14 04:05:57 +08:00
|
|
|
#if SEP == '/' /* Special case for Unix filename syntax */
|
2019-03-20 01:22:55 +08:00
|
|
|
if (n > 1) {
|
|
|
|
/* Drop trailing separator */
|
|
|
|
n--;
|
|
|
|
}
|
2017-12-14 04:05:57 +08:00
|
|
|
#endif /* Unix */
|
|
|
|
}
|
|
|
|
#endif /* All others */
|
|
|
|
|
2019-05-16 22:39:26 +08:00
|
|
|
PyObject *path0_obj = PyUnicode_FromWideChar(path0, n);
|
|
|
|
if (path0_obj == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*path0_p = path0_obj;
|
2019-03-19 23:09:27 +08:00
|
|
|
return 1;
|
2017-12-14 04:05:57 +08:00
|
|
|
}
|