mirror of
https://github.com/python/cpython.git
synced 2025-08-27 12:16:04 +00:00
bpo-20443: _PyConfig_Read() gets the absolute path of run_filename (GH-14053)
Python now gets the absolute path of the script filename specified on the command line (ex: "python3 script.py"): the __file__ attribute of the __main__ module, sys.argv[0] and sys.path[0] become an absolute path, rather than a relative path. * Add _Py_isabs() and _Py_abspath() functions. * _PyConfig_Read() now tries to get the absolute path of run_filename, but keeps the relative path if _Py_abspath() fails. * Reimplement os._getfullpathname() using _Py_abspath(). * Use _Py_isabs() in getpath.c.
This commit is contained in:
parent
080b6b40fa
commit
3939c321c9
10 changed files with 211 additions and 42 deletions
|
@ -1734,6 +1734,103 @@ _Py_wrealpath(const wchar_t *path,
|
|||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef MS_WINDOWS
|
||||
int
|
||||
_Py_isabs(const wchar_t *path)
|
||||
{
|
||||
return (path[0] == SEP);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* Get an absolute path.
|
||||
On error (ex: fail to get the current directory), return -1.
|
||||
On memory allocation failure, set *abspath_p to NULL and return 0.
|
||||
On success, return a newly allocated to *abspath_p to and return 0.
|
||||
The string must be freed by PyMem_RawFree(). */
|
||||
int
|
||||
_Py_abspath(const wchar_t *path, wchar_t **abspath_p)
|
||||
{
|
||||
#ifdef MS_WINDOWS
|
||||
wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
|
||||
DWORD result;
|
||||
|
||||
result = GetFullPathNameW(path,
|
||||
Py_ARRAY_LENGTH(woutbuf), woutbuf,
|
||||
NULL);
|
||||
if (!result) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (result > Py_ARRAY_LENGTH(woutbuf)) {
|
||||
if ((size_t)result <= (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t)) {
|
||||
woutbufp = PyMem_RawMalloc((size_t)result * sizeof(wchar_t));
|
||||
}
|
||||
else {
|
||||
woutbufp = NULL;
|
||||
}
|
||||
if (!woutbufp) {
|
||||
*abspath_p = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = GetFullPathNameW(path, result, woutbufp, NULL);
|
||||
if (!result) {
|
||||
PyMem_RawFree(woutbufp);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (woutbufp != woutbuf) {
|
||||
*abspath_p = woutbufp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*abspath_p = _PyMem_RawWcsdup(woutbufp);
|
||||
return 0;
|
||||
#else
|
||||
if (_Py_isabs(path)) {
|
||||
*abspath_p = _PyMem_RawWcsdup(path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t cwd[MAXPATHLEN + 1];
|
||||
cwd[Py_ARRAY_LENGTH(cwd) - 1] = 0;
|
||||
if (!_Py_wgetcwd(cwd, Py_ARRAY_LENGTH(cwd) - 1)) {
|
||||
/* unable to get the current directory */
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t cwd_len = wcslen(cwd);
|
||||
size_t path_len = wcslen(path);
|
||||
size_t len = cwd_len + 1 + path_len + 1;
|
||||
if (len <= (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t)) {
|
||||
*abspath_p = PyMem_RawMalloc(len * sizeof(wchar_t));
|
||||
}
|
||||
else {
|
||||
*abspath_p = NULL;
|
||||
}
|
||||
if (*abspath_p == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t *abspath = *abspath_p;
|
||||
memcpy(abspath, cwd, cwd_len * sizeof(wchar_t));
|
||||
abspath += cwd_len;
|
||||
|
||||
*abspath = (wchar_t)SEP;
|
||||
abspath++;
|
||||
|
||||
memcpy(abspath, path, path_len * sizeof(wchar_t));
|
||||
abspath += path_len;
|
||||
|
||||
*abspath = 0;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Get the current directory. buflen is the buffer size in wide characters
|
||||
including the null character. Decode the path from the locale encoding.
|
||||
|
||||
|
|
|
@ -2137,6 +2137,11 @@ config_update_argv(PyConfig *config, Py_ssize_t opt_index)
|
|||
/* Force sys.argv[0] = '-m'*/
|
||||
arg0 = L"-m";
|
||||
}
|
||||
else if (config->run_filename != NULL) {
|
||||
/* run_filename is converted to an absolute path: update argv */
|
||||
arg0 = config->run_filename;
|
||||
}
|
||||
|
||||
if (arg0 != NULL) {
|
||||
arg0 = _PyMem_RawWcsdup(arg0);
|
||||
if (arg0 == NULL) {
|
||||
|
@ -2183,6 +2188,37 @@ core_read_precmdline(PyConfig *config, _PyPreCmdline *precmdline)
|
|||
}
|
||||
|
||||
|
||||
/* Get run_filename absolute path */
|
||||
static PyStatus
|
||||
config_run_filename_abspath(PyConfig *config)
|
||||
{
|
||||
if (!config->run_filename) {
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
#ifndef MS_WINDOWS
|
||||
if (_Py_isabs(config->run_filename)) {
|
||||
/* path is already absolute */
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
#endif
|
||||
|
||||
wchar_t *abs_filename;
|
||||
if (_Py_abspath(config->run_filename, &abs_filename) < 0) {
|
||||
/* failed to get the absolute path of the command line filename:
|
||||
ignore the error, keep the relative path */
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
if (abs_filename == NULL) {
|
||||
return _PyStatus_NO_MEMORY();
|
||||
}
|
||||
|
||||
PyMem_RawFree(config->run_filename);
|
||||
config->run_filename = abs_filename;
|
||||
return _PyStatus_OK();
|
||||
}
|
||||
|
||||
|
||||
static PyStatus
|
||||
config_read_cmdline(PyConfig *config)
|
||||
{
|
||||
|
@ -2208,11 +2244,22 @@ config_read_cmdline(PyConfig *config)
|
|||
goto done;
|
||||
}
|
||||
|
||||
status = config_run_filename_abspath(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
status = config_update_argv(config, opt_index);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else {
|
||||
status = config_run_filename_abspath(config);
|
||||
if (_PyStatus_EXCEPTION(status)) {
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
if (config->use_environment) {
|
||||
status = config_init_env_warnoptions(config, &env_warnoptions);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue