Skip to content

Commit 834b7c1

Browse files
authored
gh-106718: Treat PyConfig.stdlib_dir as highest-priority setting for stdlib_dir when calculating paths (GH-108730)
1 parent 821a7ac commit 834b7c1

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

Lib/test/test_getpath.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,20 @@ def test_symlink_buildpath_macos(self):
818818
actual = getpath(ns, expected)
819819
self.assertEqual(expected, actual)
820820

821+
def test_explicitly_set_stdlib_dir(self):
822+
"""Test the explicitly set stdlib_dir in the config is respected."""
823+
ns = MockPosixNamespace(
824+
PREFIX="/usr",
825+
argv0="python",
826+
ENV_PATH="/usr/bin",
827+
)
828+
ns["config"]["stdlib_dir"] = "/custom_stdlib_dir"
829+
expected = dict(
830+
stdlib_dir="/custom_stdlib_dir",
831+
)
832+
actual = getpath(ns, expected)
833+
self.assertEqual(expected, actual)
834+
821835

822836
# ******************************************************************************
823837

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When PyConfig.stdlib_dir is explicitly set, it's now respected and won't be
2+
overridden by PyConfig.home.

Modules/getpath.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ def search_up(prefix, *landmarks, test=isfile):
229229

230230
pythonpath = config.get('module_search_paths')
231231
pythonpath_was_set = config.get('module_search_paths_set')
232+
stdlib_dir = config.get('stdlib_dir')
233+
stdlib_dir_was_set_in_config = bool(stdlib_dir)
232234

233235
real_executable_dir = None
234-
stdlib_dir = None
235236
platstdlib_dir = None
236237

237238
# ******************************************************************************
@@ -507,11 +508,12 @@ def search_up(prefix, *landmarks, test=isfile):
507508
build_stdlib_prefix = build_prefix
508509
else:
509510
build_stdlib_prefix = search_up(build_prefix, *BUILDSTDLIB_LANDMARKS)
510-
# Always use the build prefix for stdlib
511-
if build_stdlib_prefix:
512-
stdlib_dir = joinpath(build_stdlib_prefix, 'Lib')
513-
else:
514-
stdlib_dir = joinpath(build_prefix, 'Lib')
511+
# Use the build prefix for stdlib when not explicitly set
512+
if not stdlib_dir_was_set_in_config:
513+
if build_stdlib_prefix:
514+
stdlib_dir = joinpath(build_stdlib_prefix, 'Lib')
515+
else:
516+
stdlib_dir = joinpath(build_prefix, 'Lib')
515517
# Only use the build prefix for prefix if it hasn't already been set
516518
if not prefix:
517519
prefix = build_stdlib_prefix
@@ -543,8 +545,9 @@ def search_up(prefix, *landmarks, test=isfile):
543545
prefix, had_delim, exec_prefix = home.partition(DELIM)
544546
if not had_delim:
545547
exec_prefix = prefix
546-
# Reset the standard library directory if it was already set
547-
stdlib_dir = None
548+
# Reset the standard library directory if it was not explicitly set
549+
if not stdlib_dir_was_set_in_config:
550+
stdlib_dir = None
548551

549552

550553
# First try to detect prefix by looking alongside our runtime library, if known
@@ -560,7 +563,8 @@ def search_up(prefix, *landmarks, test=isfile):
560563
if STDLIB_SUBDIR and STDLIB_LANDMARKS and not prefix:
561564
if any(isfile(joinpath(library_dir, f)) for f in STDLIB_LANDMARKS):
562565
prefix = library_dir
563-
stdlib_dir = joinpath(prefix, STDLIB_SUBDIR)
566+
if not stdlib_dir_was_set_in_config:
567+
stdlib_dir = joinpath(prefix, STDLIB_SUBDIR)
564568

565569

566570
# Detect prefix by looking for zip file
@@ -571,7 +575,7 @@ def search_up(prefix, *landmarks, test=isfile):
571575
prefix = executable_dir
572576
else:
573577
prefix = search_up(executable_dir, ZIP_LANDMARK)
574-
if prefix:
578+
if prefix and not stdlib_dir_was_set_in_config:
575579
stdlib_dir = joinpath(prefix, STDLIB_SUBDIR)
576580
if not isdir(stdlib_dir):
577581
stdlib_dir = None

0 commit comments

Comments
 (0)