Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-91985: Fix sys.path calculation with PYTHONHOME on Windows (alt) #92321

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,14 @@ def test_init_setpythonhome(self):
# Because we are specifying 'home', module search paths
# are fairly static
expected_paths = [paths[0], stdlib, os.path.join(home, 'DLLs')]
try:
# gh-91985 Replace the DLLs path according to pybuilddir.txt
exedir = os.path.dirname(self.test_exe)
with open(os.path.join(exedir, 'pybuilddir.txt'), encoding='utf8') as f:
expected_paths[-1] = os.path.normpath(
os.path.join(exedir, f'{f.read()}\n$'.splitlines()[0]))
except FileNotFoundError:
pass
else:
version = f'{sys.version_info.major}.{sys.version_info.minor}'
stdlib = os.path.join(home, sys.platlibdir, f'python{version}')
Expand All @@ -1284,6 +1292,39 @@ def test_init_setpythonhome(self):
self.check_all_configs("test_init_setpythonhome", config,
api=API_COMPAT, env=env)

@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
def test_init_setpythonhome_in_tmpdir(self):
# Test Py_SetPythonHome(home) with PYTHONPATH env var,
# running in a temp directory where pybuilddir.txt does not exist.
config = self._get_expected_config()
paths = config['config']['module_search_paths']

for path in paths:
if not os.path.isdir(path):
continue
if os.path.exists(os.path.join(path, 'os.py')):
home = os.path.dirname(path)
break
else:
self.fail(f"Unable to find home in {paths!r}")

pyddir = os.path.dirname(
import_helper.import_module('_testinternalcapi').__file__)
with self.tmpdir_with_python() as tmpdir:
config = {
'home': home,
'pythonpath_env': pyddir,
'module_search_paths': [
os.path.join(tmpdir, os.path.basename(paths[0])), # zip
os.path.join(home, 'Lib'),
os.path.join(home, 'DLLs'),
],
}
self.default_program_name(config)
env = {'TESTHOME': home, 'PYTHONPATH': pyddir}
self.check_all_configs("test_init_setpythonhome", config,
api=API_COMPAT, env=env)

def copy_paths_by_env(self, config):
all_configs = self._get_expected_config()
paths = all_configs['config']['module_search_paths']
Expand Down
11 changes: 10 additions & 1 deletion Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,16 @@ def search_up(prefix, *landmarks, test=isfile):
stdlib_dir = ''

if not platstdlib_dir:
if exec_prefix:
if os_name == 'nt' and real_executable_dir and \
isfile(joinpath(real_executable_dir, BUILDDIR_TXT)):
try:
platstdlib_dir = joinpath(
real_executable_dir,
readlines(joinpath(real_executable_dir, BUILDDIR_TXT))[0],
)
except IndexError:
platstdlib_dir = real_executable_dir
elif exec_prefix:
platstdlib_dir = joinpath(exec_prefix, PLATSTDLIB_LANDMARK)
else:
platstdlib_dir = ''
Expand Down