Skip to content

Commit 16ecabb

Browse files
committed
Resolve base_executable based on path if it ultimately points to the same file as the executable being run once symlinks are resolved
1 parent ae7f621 commit 16ecabb

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

Lib/test/test_getpath.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_venv_non_installed_zip_path_posix(self):
400400
ns.add_known_file("/path/to/non-installed/lib/python9.8/os.py")
401401
ns.add_known_dir("/path/to/non-installed/lib/python9.8/lib-dynload")
402402
ns.add_known_file("/venv/pyvenv.cfg", [
403-
r"home = /path/to/non-installed"
403+
r"home = /path/to/non-installed/bin"
404404
])
405405
expected = dict(
406406
executable="/venv/bin/python",
@@ -837,28 +837,28 @@ def test_PYTHONHOME_in_venv(self):
837837
ns = MockPosixNamespace(
838838
argv0="/venv/bin/python",
839839
PREFIX="/usr",
840-
ENV_PYTHONHOME="/pythonhome",
840+
ENV_PYTHONHOME="/pythonhome-p:/pythonhome-e",
841841
)
842842
# Setup venv
843843
ns.add_known_xfile("/venv/bin/python")
844844
ns.add_known_file("/venv/pyvenv.cfg", [
845845
r"home = /usr/bin"
846846
])
847-
# Seutup PYTHONHOME
848-
ns.add_known_file("/pythonhome/lib/python9.8/os.py")
849-
ns.add_known_dir("/pythonhome/lib/python9.8/lib-dynload")
847+
# Setup PYTHONHOME
848+
ns.add_known_file("/pythonhome-p/lib/python9.8/os.py")
849+
ns.add_known_dir("/pythonhome-e/lib/python9.8/lib-dynload")
850850

851851
expected = dict(
852852
executable="/venv/bin/python",
853853
prefix="/venv",
854854
exec_prefix="/venv",
855-
base_prefix="/pythonhome",
856-
base_exec_prefix="/pythonhome",
855+
base_prefix="/pythonhome-p",
856+
base_exec_prefix="/pythonhome-e",
857857
module_search_paths_set=1,
858858
module_search_paths=[
859-
"/pythonhome/lib/python98.zip",
860-
"/pythonhome/lib/python9.8",
861-
"/pythonhome/lib/python9.8/lib-dynload",
859+
"/pythonhome-p/lib/python98.zip",
860+
"/pythonhome-p/lib/python9.8",
861+
"/pythonhome-e/lib/python9.8/lib-dynload",
862862
],
863863
)
864864
actual = getpath(ns, expected)

Modules/getpath.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,23 @@ def search_up(prefix, *landmarks, test=isfile):
382382
# the base installation — isn't set (eg. when embedded), try to find
383383
# it in 'home'.
384384
if not base_executable:
385-
# First try to resolve symlinked executables, since that may be
386-
# more accurate than assuming the executable in 'home'.
385+
_home_executable = joinpath(executable_dir, basename(executable))
386+
_realpath_executable = None
387387
try:
388-
base_executable = realpath(executable)
389-
if base_executable == executable:
390-
# No change, so probably not a link. Clear it and fall back
391-
base_executable = ''
388+
_realpath_executable = realpath(executable)
389+
390+
# If the realpath of the executable identified in home is the same as
391+
# the realpath of the venv's executable use the (potentially not fully
392+
# resolved via realpath) path from home.
393+
if realpath(_home_executable) == _realpath_executable:
394+
base_executable = _home_executable
392395
except OSError:
393396
pass
397+
if not base_executable:
398+
if _realpath_executable and _realpath_executable != executable:
399+
# Try to resolve symlinked executables, since that may be
400+
# more accurate than assuming the executable in 'home'.
401+
base_executable = _realpath_executable
394402
if not base_executable:
395403
base_executable = joinpath(executable_dir, basename(executable))
396404
# It's possible "python" is executed from within a posix venv but that

0 commit comments

Comments
 (0)