Skip to content

Commit

Permalink
pythonGH-126985: move pyconf.cfg detection from site to getpath
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <lains@riseup.net>
  • Loading branch information
FFY00 committed Nov 18, 2024
1 parent 4cd1076 commit 1380972
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
32 changes: 16 additions & 16 deletions Lib/test/test_getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def test_venv_win32(self):
])
expected = dict(
executable=r"C:\venv\Scripts\python.exe",
prefix=r"C:\Python",
exec_prefix=r"C:\Python",
prefix=r"C:\venv",
exec_prefix=r"C:\venv",
base_executable=r"C:\Python\python.exe",
base_prefix=r"C:\Python",
base_exec_prefix=r"C:\Python",
Expand Down Expand Up @@ -339,8 +339,8 @@ def test_venv_posix(self):
])
expected = dict(
executable="/venv/bin/python",
prefix="/usr",
exec_prefix="/usr",
prefix="/venv",
exec_prefix="/venv",
base_executable="/usr/bin/python",
base_prefix="/usr",
base_exec_prefix="/usr",
Expand Down Expand Up @@ -371,8 +371,8 @@ def test_venv_changed_name_posix(self):
])
expected = dict(
executable="/venv/bin/python",
prefix="/usr",
exec_prefix="/usr",
prefix="/venv",
exec_prefix="/venv",
base_executable="/usr/bin/python3",
base_prefix="/usr",
base_exec_prefix="/usr",
Expand Down Expand Up @@ -404,8 +404,8 @@ def test_venv_non_installed_zip_path_posix(self):
])
expected = dict(
executable="/venv/bin/python",
prefix="/path/to/non-installed",
exec_prefix="/path/to/non-installed",
prefix="/venv",
exec_prefix="/venv",
base_executable="/path/to/non-installed/bin/python",
base_prefix="/path/to/non-installed",
base_exec_prefix="/path/to/non-installed",
Expand Down Expand Up @@ -435,8 +435,8 @@ def test_venv_changed_name_copy_posix(self):
])
expected = dict(
executable="/venv/bin/python",
prefix="/usr",
exec_prefix="/usr",
prefix="/venv",
exec_prefix="/venv",
base_executable="/usr/bin/python9",
base_prefix="/usr",
base_exec_prefix="/usr",
Expand Down Expand Up @@ -652,8 +652,8 @@ def test_venv_framework_macos(self):
])
expected = dict(
executable=f"{venv_path}/bin/python",
prefix="/Library/Frameworks/Python.framework/Versions/9.8",
exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
prefix=venv_path,
exec_prefix=venv_path,
base_executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8",
base_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
base_exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8",
Expand Down Expand Up @@ -697,8 +697,8 @@ def test_venv_alt_framework_macos(self):
])
expected = dict(
executable=f"{venv_path}/bin/python",
prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
prefix=venv_path,
exec_prefix=venv_path,
base_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8",
base_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
base_exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8",
Expand Down Expand Up @@ -734,8 +734,8 @@ def test_venv_macos(self):
])
expected = dict(
executable="/framework/Python9.8/python",
prefix="/usr",
exec_prefix="/usr",
prefix="/framework/Python9.8",
exec_prefix="/framework/Python9.8",
base_executable="/usr/bin/python",
base_prefix="/usr",
base_exec_prefix="/usr",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When running under a virtual environment with the :mod:`site` disabled (see
:option:`-S`), :data:`sys.prefix` and :data:`sys.base_prefix` will now point
to the virtual environment, instead of the base installation.
36 changes: 22 additions & 14 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,18 @@ def search_up(prefix, *landmarks, test=isfile):


# For a venv, update the main prefix/exec_prefix but leave the base ones unchanged
# XXX: We currently do not update prefix here, but it happens in site.py
#if venv_prefix:
# base_prefix = prefix
# base_exec_prefix = exec_prefix
# prefix = exec_prefix = venv_prefix
if venv_prefix:
base_prefix = prefix
base_exec_prefix = exec_prefix
prefix = exec_prefix = venv_prefix


# After calculating prefix and exec_prefix, use their values for base_prefix and
# base_exec_prefix if they haven't been set.
if not base_prefix:
base_prefix = prefix
if not base_exec_prefix:
base_exec_prefix = exec_prefix


# ******************************************************************************
Expand Down Expand Up @@ -679,7 +686,7 @@ def search_up(prefix, *landmarks, test=isfile):
# QUIRK: POSIX uses the default prefix when in the build directory
pythonpath.append(joinpath(PREFIX, ZIP_LANDMARK))
else:
pythonpath.append(joinpath(prefix, ZIP_LANDMARK))
pythonpath.append(joinpath(base_prefix, ZIP_LANDMARK))

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

# Then add stdlib_dir and platstdlib_dir
if not stdlib_dir and prefix:
stdlib_dir = joinpath(prefix, STDLIB_SUBDIR)
if not platstdlib_dir and exec_prefix:
platstdlib_dir = joinpath(exec_prefix, PLATSTDLIB_LANDMARK)
if not stdlib_dir and base_prefix:
stdlib_dir = joinpath(base_prefix, STDLIB_SUBDIR)
if not platstdlib_dir and base_exec_prefix:
platstdlib_dir = joinpath(base_exec_prefix, PLATSTDLIB_LANDMARK)

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

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

Expand Down Expand Up @@ -788,8 +796,8 @@ def search_up(prefix, *landmarks, test=isfile):
config['base_executable'] = base_executable
config['prefix'] = prefix
config['exec_prefix'] = exec_prefix
config['base_prefix'] = base_prefix or prefix
config['base_exec_prefix'] = base_exec_prefix or exec_prefix
config['base_prefix'] = base_prefix
config['base_exec_prefix'] = base_exec_prefix

config['platlibdir'] = platlibdir
# test_embed expects empty strings, not None
Expand Down

0 comments on commit 1380972

Please sign in to comment.