bpo-45211: Remember the stdlib dir during startup. (gh-28586)

During runtime startup we figure out the stdlib dir but currently throw that information away. This change preserves it and exposes it via PyConfig.stdlib_dir, _Py_GetStdlibDir(), and sys._stdlib_dir.

https://bugs.python.org/issue45211
This commit is contained in:
Eric Snow 2021-09-28 12:18:28 -06:00 committed by GitHub
parent 84975146a7
commit 0c50b8c0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 93 additions and 3 deletions

View file

@ -116,6 +116,8 @@
* with a semicolon separated path prior to calling Py_Initialize.
*/
#define STDLIB_SUBDIR L"lib"
#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
@ -293,12 +295,12 @@ search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path)
wcscpy_s(stdlibdir, Py_ARRAY_LENGTH(stdlibdir), prefix);
/* We initialize with the longest possible path, in case it doesn't fit.
This also gives us an initial SEP at stdlibdir[wcslen(prefix)]. */
join(stdlibdir, L"lib");
join(stdlibdir, STDLIB_SUBDIR);
do {
assert(stdlibdir[wcslen(prefix)] == SEP);
/* Due to reduce() and our initial value, this result
is guaranteed to fit. */
wcscpy(&stdlibdir[wcslen(prefix) + 1], L"lib");
wcscpy(&stdlibdir[wcslen(prefix) + 1], STDLIB_SUBDIR);
if (is_stdlibdir(stdlibdir)) {
return 1;
}
@ -1013,6 +1015,12 @@ calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
}
done:
if (pathconfig->stdlib_dir == NULL) {
pathconfig->stdlib_dir = _Py_join_relfile(prefix, STDLIB_SUBDIR);
if (pathconfig->stdlib_dir == NULL) {
return _PyStatus_NO_MEMORY();
}
}
if (pathconfig->prefix == NULL) {
pathconfig->prefix = _PyMem_RawWcsdup(prefix);
if (pathconfig->prefix == NULL) {