mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
GH-126985: Don't override venv detection with PYTHONHOME (#127968)
This commit is contained in:
parent
46006a1b35
commit
b74c8f58e8
2 changed files with 38 additions and 3 deletions
|
@ -832,6 +832,37 @@ class MockGetPathTests(unittest.TestCase):
|
||||||
actual = getpath(ns, expected)
|
actual = getpath(ns, expected)
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
|
def test_PYTHONHOME_in_venv(self):
|
||||||
|
"Make sure prefix/exec_prefix still point to the venv if PYTHONHOME was used."
|
||||||
|
ns = MockPosixNamespace(
|
||||||
|
argv0="/venv/bin/python",
|
||||||
|
PREFIX="/usr",
|
||||||
|
ENV_PYTHONHOME="/pythonhome",
|
||||||
|
)
|
||||||
|
# Setup venv
|
||||||
|
ns.add_known_xfile("/venv/bin/python")
|
||||||
|
ns.add_known_file("/venv/pyvenv.cfg", [
|
||||||
|
r"home = /usr/bin"
|
||||||
|
])
|
||||||
|
# Seutup PYTHONHOME
|
||||||
|
ns.add_known_file("/pythonhome/lib/python9.8/os.py")
|
||||||
|
ns.add_known_dir("/pythonhome/lib/python9.8/lib-dynload")
|
||||||
|
|
||||||
|
expected = dict(
|
||||||
|
executable="/venv/bin/python",
|
||||||
|
prefix="/venv",
|
||||||
|
exec_prefix="/venv",
|
||||||
|
base_prefix="/pythonhome",
|
||||||
|
base_exec_prefix="/pythonhome",
|
||||||
|
module_search_paths_set=1,
|
||||||
|
module_search_paths=[
|
||||||
|
"/pythonhome/lib/python98.zip",
|
||||||
|
"/pythonhome/lib/python9.8",
|
||||||
|
"/pythonhome/lib/python9.8/lib-dynload",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
actual = getpath(ns, expected)
|
||||||
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
# ******************************************************************************
|
# ******************************************************************************
|
||||||
|
|
||||||
|
|
|
@ -344,9 +344,10 @@ elif use_environment and ENV_PYTHONHOME and not py_setpath:
|
||||||
|
|
||||||
venv_prefix = None
|
venv_prefix = None
|
||||||
|
|
||||||
# Calling Py_SetPythonHome(), Py_SetPath() or
|
# Calling Py_SetPath() will override venv detection.
|
||||||
# setting $PYTHONHOME will override venv detection.
|
# Calling Py_SetPythonHome() or setting $PYTHONHOME will override the 'home' key
|
||||||
if not home and not py_setpath:
|
# specified in pyvenv.cfg.
|
||||||
|
if not py_setpath:
|
||||||
try:
|
try:
|
||||||
# prefix2 is just to avoid calculating dirname again later,
|
# prefix2 is just to avoid calculating dirname again later,
|
||||||
# as the path in venv_prefix is the more common case.
|
# as the path in venv_prefix is the more common case.
|
||||||
|
@ -370,6 +371,9 @@ if not home and not py_setpath:
|
||||||
for line in pyvenvcfg:
|
for line in pyvenvcfg:
|
||||||
key, had_equ, value = line.partition('=')
|
key, had_equ, value = line.partition('=')
|
||||||
if had_equ and key.strip().lower() == 'home':
|
if had_equ and key.strip().lower() == 'home':
|
||||||
|
# If PYTHONHOME was set, ignore 'home' from pyvenv.cfg.
|
||||||
|
if home:
|
||||||
|
break
|
||||||
# Override executable_dir/real_executable_dir with the value from 'home'.
|
# Override executable_dir/real_executable_dir with the value from 'home'.
|
||||||
# These values may be later used to calculate prefix/base_prefix, if a more
|
# These values may be later used to calculate prefix/base_prefix, if a more
|
||||||
# reliable source — like the runtime library (libpython) path — isn't available.
|
# reliable source — like the runtime library (libpython) path — isn't available.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue