Skip to content

Commit 93263d4

Browse files
authored
gh-135773: have pyvenv.cfg without home key anchor a venv and deduce home (#135831)
This is still formally undefined behaviour, but we may as well keep the *same* undefined behaviour as previous versions. PEP 796 proposes a cleaner and more consistent replacement for 3.15+
1 parent 8ac7613 commit 93263d4

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

Lib/test/test_getpath.py

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

357+
def test_venv_posix_without_home_key(self):
358+
ns = MockPosixNamespace(
359+
argv0="/venv/bin/python3",
360+
PREFIX="/usr",
361+
ENV_PATH="/usr/bin",
362+
)
363+
# Setup the bare minimum venv
364+
ns.add_known_xfile("/usr/bin/python3")
365+
ns.add_known_xfile("/venv/bin/python3")
366+
ns.add_known_link("/venv/bin/python3", "/usr/bin/python3")
367+
ns.add_known_file("/venv/pyvenv.cfg", [
368+
# home = key intentionally omitted
369+
])
370+
expected = dict(
371+
executable="/venv/bin/python3",
372+
prefix="/venv",
373+
base_prefix="/usr",
374+
)
375+
actual = getpath(ns, expected)
376+
self.assertEqual(expected, actual)
377+
357378
def test_venv_changed_name_posix(self):
358379
"Test a venv layout on *nix."
359380
ns = MockPosixNamespace(

Modules/getpath.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,9 @@ def search_up(prefix, *landmarks, test=isfile):
364364
venv_prefix = None
365365
pyvenvcfg = []
366366

367-
# Search for the 'home' key in pyvenv.cfg. Currently, we don't consider the
368-
# presence of a pyvenv.cfg file without a 'home' key to signify the
369-
# existence of a virtual environment — we quietly ignore them.
370-
# XXX: If we don't find a 'home' key, we don't look for another pyvenv.cfg!
367+
# Search for the 'home' key in pyvenv.cfg. If a home key isn't found,
368+
# then it means a venv is active and home is based on the venv's
369+
# executable (if its a symlink, home is where the symlink points).
371370
for line in pyvenvcfg:
372371
key, had_equ, value = line.partition('=')
373372
if had_equ and key.strip().lower() == 'home':
@@ -412,10 +411,8 @@ def search_up(prefix, *landmarks, test=isfile):
412411
if isfile(candidate):
413412
base_executable = candidate
414413
break
414+
# home key found; stop iterating over lines
415415
break
416-
else:
417-
# We didn't find a 'home' key in pyvenv.cfg (no break), reset venv_prefix.
418-
venv_prefix = None
419416

420417

421418
# ******************************************************************************

0 commit comments

Comments
 (0)