Skip to content

Commit 5fe0a39

Browse files
committed
feat: relative venv home path
1 parent 621a8bd commit 5fe0a39

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Lib/test/test_getpath.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,42 @@ def test_venv_posix(self):
354354
actual = getpath(ns, expected)
355355
self.assertEqual(expected, actual)
356356

357+
def test_venv_relative_home(self):
358+
ns = MockPosixNamespace(
359+
argv0="/somedir/venv/bin/python3",
360+
PREFIX="/usr/local-fallback",
361+
ENV_PATH="/usr/bin",
362+
)
363+
364+
ns.add_known_xfile("/somedir/runtime/bin/python3")
365+
ns.add_known_file("/somedir/runtime/lib/python9.8/os.py")
366+
ns.add_known_dir("/somedir/runtime/lib/python9.8/lib-dynload")
367+
368+
ns.add_known_xfile("/somedir/venv/bin/python3")
369+
# NOTE: Normally a relative symlink would be used, but the mock
370+
# realpath() doesn't handle relative symlinks, so point it to
371+
# where it ultimately would resolve to.
372+
ns.add_known_link("/somedir/venv/bin/python3", "/somedir/runtime/bin/python3")
373+
ns.add_known_file("/somedir/venv/pyvenv.cfg", [
374+
"home = ../runtime/bin"
375+
])
376+
expected = dict(
377+
executable="/somedir/venv/bin/python3",
378+
prefix="/somedir/venv",
379+
exec_prefix="/somedir/venv",
380+
base_executable="/somedir/runtime/bin/python3",
381+
base_prefix="/somedir/runtime",
382+
base_exec_prefix="/somedir/runtime",
383+
module_search_paths_set=1,
384+
module_search_paths=[
385+
"/somedir/runtime/lib/python98.zip",
386+
"/somedir/runtime/lib/python9.8",
387+
"/somedir/runtime/lib/python9.8/lib-dynload",
388+
],
389+
)
390+
actual = getpath(ns, expected)
391+
self.assertEqual(expected, actual)
392+
357393
def test_venv_changed_name_posix(self):
358394
"Test a venv layout on *nix."
359395
ns = MockPosixNamespace(

Modules/getpath.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,13 @@ def search_up(prefix, *landmarks, test=isfile):
374374
# If PYTHONHOME was set, ignore 'home' from pyvenv.cfg.
375375
if home:
376376
break
377+
value = value.strip()
378+
if not isabs(value):
379+
value = realpath(joinpath(venv_prefix, value))
377380
# Override executable_dir/real_executable_dir with the value from 'home'.
378381
# These values may be later used to calculate prefix/base_prefix, if a more
379382
# reliable source — like the runtime library (libpython) path — isn't available.
380-
executable_dir = real_executable_dir = value.strip()
383+
executable_dir = real_executable_dir = value
381384
# If base_executable — which points to the Python interpreted from
382385
# the base installation — isn't set (eg. when embedded), try to find
383386
# it in 'home'.

0 commit comments

Comments
 (0)