diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-10-22 23:20:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-22 23:20:03 (GMT) |
commit | 17c61045c51512add61a9e75e9c7343cf4e4fb82 (patch) | |
tree | 59a98afdea508cacfb4dc4f10acf2f5b402ea0b2 /Modules/getpath.c | |
parent | f30ad65dbf3c6b1b5eec14dc954d65ef32327857 (diff) | |
download | cpython-17c61045c51512add61a9e75e9c7343cf4e4fb82.zip cpython-17c61045c51512add61a9e75e9c7343cf4e4fb82.tar.gz cpython-17c61045c51512add61a9e75e9c7343cf4e4fb82.tar.bz2 |
bpo-45506: Normalize _PyPathConfig.stdlib_dir when calculated. (#29040)
The recently added PyConfig.stdlib_dir was being set with ".." entries. When __file__ was added for from modules this caused a problem on out-of-tree builds. This PR fixes that by normalizing "stdlib_dir" when it is calculated in getpath.c.
https://bugs.python.org/issue45506
Diffstat (limited to 'Modules/getpath.c')
-rw-r--r-- | Modules/getpath.c | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/Modules/getpath.c b/Modules/getpath.c index 1405023..4dbd502 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -520,6 +520,42 @@ search_for_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig, static PyStatus +calculate_set_stdlib_dir(PyCalculatePath *calculate, _PyPathConfig *pathconfig) +{ + // Note that, unlike calculate_set_prefix(), here we allow a negative + // prefix_found. That means the source tree Lib dir gets used. + if (!calculate->prefix_found) { + return _PyStatus_OK(); + } + PyStatus status; + wchar_t *prefix = calculate->prefix; + if (!_Py_isabs(prefix)) { + prefix = _PyMem_RawWcsdup(prefix); + if (prefix == NULL) { + return _PyStatus_NO_MEMORY(); + } + status = absolutize(&prefix); + if (_PyStatus_EXCEPTION(status)) { + return status; + } + } + wchar_t buf[MAXPATHLEN + 1]; + int res = _Py_normalize_path(prefix, buf, Py_ARRAY_LENGTH(buf)); + if (prefix != calculate->prefix) { + PyMem_RawFree(prefix); + } + if (res < 0) { + return PATHLEN_ERR(); + } + pathconfig->stdlib_dir = _PyMem_RawWcsdup(buf); + if (pathconfig->stdlib_dir == NULL) { + return _PyStatus_NO_MEMORY(); + } + return _PyStatus_OK(); +} + + +static PyStatus calculate_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig) { wchar_t prefix[MAXPATHLEN+1]; @@ -1494,12 +1530,10 @@ calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig) } if (pathconfig->stdlib_dir == NULL) { - if (calculate->prefix_found) { - /* This must be done *before* calculate_set_prefix() is called. */ - pathconfig->stdlib_dir = _PyMem_RawWcsdup(calculate->prefix); - if (pathconfig->stdlib_dir == NULL) { - return _PyStatus_NO_MEMORY(); - } + /* This must be done *before* calculate_set_prefix() is called. */ + status = calculate_set_stdlib_dir(calculate, pathconfig); + if (_PyStatus_EXCEPTION(status)) { + return status; } } |