Skip to content

Commit 1380972

Browse files
committed
pythonGH-126985: move pyconf.cfg detection from site to getpath
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 4cd1076 commit 1380972

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

Lib/test/test_getpath.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def test_venv_win32(self):
9292
])
9393
expected = dict(
9494
executable=r"C:\venv\Scripts\python.exe",
95-
prefix=r"C:\Python",
96-
exec_prefix=r"C:\Python",
95+
prefix=r"C:\venv",
96+
exec_prefix=r"C:\venv",
9797
base_executable=r"C:\Python\python.exe",
9898
base_prefix=r"C:\Python",
9999
base_exec_prefix=r"C:\Python",
@@ -339,8 +339,8 @@ def test_venv_posix(self):
339339
])
340340
expected = dict(
341341
executable="/venv/bin/python",
342-
prefix="/usr",
343-
exec_prefix="/usr",
342+
prefix="/venv",
343+
exec_prefix="/venv",
344344
base_executable="/usr/bin/python",
345345
base_prefix="/usr",
346346
base_exec_prefix="/usr",
@@ -371,8 +371,8 @@ def test_venv_changed_name_posix(self):
371371
])
372372
expected = dict(
373373
executable="/venv/bin/python",
374-
prefix="/usr",
375-
exec_prefix="/usr",
374+
prefix="/venv",
375+
exec_prefix="/venv",
376376
base_executable="/usr/bin/python3",
377377
base_prefix="/usr",
378378
base_exec_prefix="/usr",
@@ -404,8 +404,8 @@ def test_venv_non_installed_zip_path_posix(self):
404404
])
405405
expected = dict(
406406
executable="/venv/bin/python",
407-
prefix="/path/to/non-installed",
408-
exec_prefix="/path/to/non-installed",
407+
prefix="/venv",
408+
exec_prefix="/venv",
409409
base_executable="/path/to/non-installed/bin/python",
410410
base_prefix="/path/to/non-installed",
411411
base_exec_prefix="/path/to/non-installed",
@@ -435,8 +435,8 @@ def test_venv_changed_name_copy_posix(self):
435435
])
436436
expected = dict(
437437
executable="/venv/bin/python",
438-
prefix="/usr",
439-
exec_prefix="/usr",
438+
prefix="/venv",
439+
exec_prefix="/venv",
440440
base_executable="/usr/bin/python9",
441441
base_prefix="/usr",
442442
base_exec_prefix="/usr",
@@ -652,8 +652,8 @@ def test_venv_framework_macos(self):
652652
])
653653
expected = dict(
654654
executable=f"{venv_path}/bin/python",
655-
prefix="/Library/Frameworks/Python.framework/Versions/9.8",
656-
exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
655+
prefix=venv_path,
656+
exec_prefix=venv_path,
657657
base_executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8",
658658
base_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
659659
base_exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
@@ -697,8 +697,8 @@ def test_venv_alt_framework_macos(self):
697697
])
698698
expected = dict(
699699
executable=f"{venv_path}/bin/python",
700-
prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
701-
exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
700+
prefix=venv_path,
701+
exec_prefix=venv_path,
702702
base_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8",
703703
base_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
704704
base_exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
@@ -734,8 +734,8 @@ def test_venv_macos(self):
734734
])
735735
expected = dict(
736736
executable="/framework/Python9.8/python",
737-
prefix="/usr",
738-
exec_prefix="/usr",
737+
prefix="/framework/Python9.8",
738+
exec_prefix="/framework/Python9.8",
739739
base_executable="/usr/bin/python",
740740
base_prefix="/usr",
741741
base_exec_prefix="/usr",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When running under a virtual environment with the :mod:`site` disabled (see
2+
:option:`-S`), :data:`sys.prefix` and :data:`sys.base_prefix` will now point
3+
to the virtual environment, instead of the base installation.

Modules/getpath.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,18 @@ def search_up(prefix, *landmarks, test=isfile):
640640

641641

642642
# For a venv, update the main prefix/exec_prefix but leave the base ones unchanged
643-
# XXX: We currently do not update prefix here, but it happens in site.py
644-
#if venv_prefix:
645-
# base_prefix = prefix
646-
# base_exec_prefix = exec_prefix
647-
# prefix = exec_prefix = venv_prefix
643+
if venv_prefix:
644+
base_prefix = prefix
645+
base_exec_prefix = exec_prefix
646+
prefix = exec_prefix = venv_prefix
647+
648+
649+
# After calculating prefix and exec_prefix, use their values for base_prefix and
650+
# base_exec_prefix if they haven't been set.
651+
if not base_prefix:
652+
base_prefix = prefix
653+
if not base_exec_prefix:
654+
base_exec_prefix = exec_prefix
648655

649656

650657
# ******************************************************************************
@@ -679,7 +686,7 @@ def search_up(prefix, *landmarks, test=isfile):
679686
# QUIRK: POSIX uses the default prefix when in the build directory
680687
pythonpath.append(joinpath(PREFIX, ZIP_LANDMARK))
681688
else:
682-
pythonpath.append(joinpath(prefix, ZIP_LANDMARK))
689+
pythonpath.append(joinpath(base_prefix, ZIP_LANDMARK))
683690

684691
if os_name == 'nt' and use_environment and winreg:
685692
# QUIRK: Windows also lists paths in the registry. Paths are stored
@@ -714,13 +721,13 @@ def search_up(prefix, *landmarks, test=isfile):
714721
# Then add any entries compiled into the PYTHONPATH macro.
715722
if PYTHONPATH:
716723
for p in PYTHONPATH.split(DELIM):
717-
pythonpath.append(joinpath(prefix, p))
724+
pythonpath.append(joinpath(base_prefix, p))
718725

719726
# Then add stdlib_dir and platstdlib_dir
720-
if not stdlib_dir and prefix:
721-
stdlib_dir = joinpath(prefix, STDLIB_SUBDIR)
722-
if not platstdlib_dir and exec_prefix:
723-
platstdlib_dir = joinpath(exec_prefix, PLATSTDLIB_LANDMARK)
727+
if not stdlib_dir and base_prefix:
728+
stdlib_dir = joinpath(base_prefix, STDLIB_SUBDIR)
729+
if not platstdlib_dir and base_exec_prefix:
730+
platstdlib_dir = joinpath(base_exec_prefix, PLATSTDLIB_LANDMARK)
724731

725732
if os_name == 'nt':
726733
# QUIRK: Windows generates paths differently
@@ -750,7 +757,8 @@ def search_up(prefix, *landmarks, test=isfile):
750757

751758
# QUIRK: Non-Windows replaces prefix/exec_prefix with defaults when running
752759
# in build directory. This happens after pythonpath calculation.
753-
if os_name != 'nt' and build_prefix:
760+
# Virtual environments using the build directory Python still keep their prefix.
761+
if not venv_prefix and os_name != 'nt' and build_prefix:
754762
prefix = config.get('prefix') or PREFIX
755763
exec_prefix = config.get('exec_prefix') or EXEC_PREFIX or prefix
756764

@@ -788,8 +796,8 @@ def search_up(prefix, *landmarks, test=isfile):
788796
config['base_executable'] = base_executable
789797
config['prefix'] = prefix
790798
config['exec_prefix'] = exec_prefix
791-
config['base_prefix'] = base_prefix or prefix
792-
config['base_exec_prefix'] = base_exec_prefix or exec_prefix
799+
config['base_prefix'] = base_prefix
800+
config['base_exec_prefix'] = base_exec_prefix
793801

794802
config['platlibdir'] = platlibdir
795803
# test_embed expects empty strings, not None

0 commit comments

Comments
 (0)