Skip to content

Commit 3efc8e8

Browse files
itamaroambv
andauthored
Prefer the resolved base executable for exec_prefix discovery
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 72b32a4 commit 3efc8e8

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎Lib/test/test_getpath.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,41 @@ def test_venv_symlink_to_separate_exec_prefix_posix(self):
542542
actual = getpath(ns, expected)
543543
self.assertEqual(expected, actual)
544544

545+
def test_venv_separate_exec_prefix_with_misleading_home_posix(self):
546+
"Test that a stale lib-dynload near home does not override the base executable."
547+
ns = MockPosixNamespace(
548+
argv0="/venv/bin/python",
549+
PREFIX="/stdlib",
550+
EXEC_PREFIX="/build/exec",
551+
)
552+
ns.add_known_xfile("/exec/bin/python")
553+
ns.add_known_xfile("/public/bin/python")
554+
ns.add_known_link("/public/bin/python", "/exec/bin/python")
555+
ns.add_known_xfile("/venv/bin/python")
556+
ns.add_known_link("/venv/bin/python", "/public/bin/python")
557+
ns.add_known_file("/stdlib/lib/python9.8/os.py")
558+
ns.add_known_dir("/exec/lib/python9.8/lib-dynload")
559+
# An empty directory near the symlink must not win.
560+
ns.add_known_dir("/public/lib/python9.8/lib-dynload")
561+
ns.add_known_file("/venv/pyvenv.cfg", [
562+
"home = /public/bin",
563+
])
564+
expected = dict(
565+
executable="/venv/bin/python",
566+
prefix="/venv",
567+
exec_prefix="/venv",
568+
base_executable="/exec/bin/python",
569+
base_prefix="/stdlib",
570+
base_exec_prefix="/exec",
571+
module_search_paths_set=1,
572+
module_search_paths=[
573+
"/stdlib/lib/python98.zip",
574+
"/stdlib/lib/python9.8",
575+
"/exec/lib/python9.8/lib-dynload",
576+
],
577+
)
578+
actual = getpath(ns, expected)
579+
self.assertEqual(expected, actual)
545580
def test_venv_non_installed_zip_path_posix(self):
546581
"Test a venv created from non-installed python has correct zip path."""
547582
ns = MockPosixNamespace(

‎Modules/getpath.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,13 @@ def search_up(prefix, *landmarks, test=isfile):
633633
exec_prefix = prefix
634634
if not exec_prefix and prefix and isdir(joinpath(prefix, PLATSTDLIB_LANDMARK)):
635635
exec_prefix = prefix
636-
if not exec_prefix and executable_dir:
637-
exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir)
638636
if not exec_prefix and base_executable_dir and base_executable_dir != executable_dir:
639637
# The venv's resolved base executable may live under a separate
640638
# exec_prefix (e.g. ./configure --exec-prefix) even when the
641639
# stdlib was found from 'home'.
642640
exec_prefix = search_up(base_executable_dir, PLATSTDLIB_LANDMARK, test=isdir)
641+
if not exec_prefix and executable_dir:
642+
exec_prefix = search_up(executable_dir, PLATSTDLIB_LANDMARK, test=isdir
643643
if not exec_prefix and EXEC_PREFIX:
644644
exec_prefix = EXEC_PREFIX
645645
if not exec_prefix or not isdir(joinpath(exec_prefix, PLATSTDLIB_LANDMARK)):

0 commit comments

Comments
 (0)