Skip to content

gh-91985: Ensure in-tree builds override platstdlib_dir in every path calculation #93641

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

Merged
merged 19 commits into from
Jun 16, 2022
Prev Previous commit
Next Next commit
apply suggested changes
  • Loading branch information
neonene committed Jun 14, 2022
commit 19e14285c20aba9db9bedac40eb41c2c58b608e3
34 changes: 29 additions & 5 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,7 @@ def test_init_setpythonhome(self):
self.check_all_configs("test_init_setpythonhome", config,
api=API_COMPAT, env=env)

@unittest.skipUnless(MS_WINDOWS, 'Windows only')
def test_init_is_python_build_win32(self):
def test_init_is_python_build_with_home(self):
# Test _Py_path_config._is_python_build field (gh-91985)
config = self._get_expected_config()
paths = config['config']['module_search_paths']
Expand All @@ -1320,8 +1319,16 @@ def test_init_is_python_build_win32(self):
self.fail(f"Unable to find home in {paths!r}")

prefix = exec_prefix = home
stdlib = os.path.join(home, "Lib")
expected_paths = [paths[0], stdlib, os.path.join(home, 'DLLs')]
if MS_WINDOWS:
stdlib = os.path.join(home, "Lib")
# Because we are specifying 'home', module search paths
# are fairly static
expected_paths = [paths[0], stdlib, os.path.join(home, 'DLLs')]
else:
version = f'{sys.version_info.major}.{sys.version_info.minor}'
stdlib = os.path.join(home, sys.platlibdir, f'python{version}')
expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)

config = {
'home': home,
'module_search_paths': expected_paths,
Expand All @@ -1341,7 +1348,24 @@ def test_init_is_python_build_win32(self):

env['NEGATIVE_ISPYTHONBUILD'] = '0'
config['_is_python_build'] = 1
config['module_search_paths'][-1] = os.path.dirname(self.test_exe)

exedir = os.path.dirname(sys.executable)
with open(os.path.join(exedir, 'pybuilddir.txt'), encoding='utf8') as f:
platstdlib = os.path.join(exedir, f'{f.read()}\n$'.splitlines()[0])
platstdlib = os.path.normpath(platstdlib)

if MS_WINDOWS:
expected_paths = [paths[0], stdlib, platstdlib]
else:
prefix = exec_prefix = sys.prefix
expected_paths = [self.module_search_paths(prefix=prefix)[0],
stdlib, platstdlib]
# stdlib calculation (/Lib) is not yet supported.
stdlib = os.path.join(home, sys.platlibdir, f'python{version}')

config.update(module_search_paths=expected_paths,
prefix=prefix, base_prefix=prefix,
exec_prefix=exec_prefix, base_exec_prefix=exec_prefix)
self.check_all_configs("test_init_is_python_build", config,
api=API_COMPAT, env=env)

Expand Down
15 changes: 7 additions & 8 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1566,25 +1566,24 @@ static int test_init_is_python_build(void)
PyConfig config;
_PyConfig_InitCompatConfig(&config);
config_set_program_name(&config);
// Ensure 'home_was_set' is turned on in getpath.py
// gh-91985: in-tree builds fail to check for build directory landmarks
// under the effect of 'home' or PYTHONHOME environment variable.
config_set_string(&config, &config.home, home);
PyMem_RawFree(home);
putenv("TESTHOME=");

// Use an impossible value so we can detect whether it isn't updated
// during initialization.
config._is_python_build = INT_MAX;
env = getenv("NEGATIVE_ISPYTHONBUILD");
if (env) {
if (strcmp(env, "0") != 0) {
config._is_python_build++;
}
putenv("NEGATIVE_ISPYTHONBUILD=");
if (env && strcmp(env, "0") != 0) {
config._is_python_build++;
}
init_from_config_clear(&config);
Py_Finalize();
// Second initialization
config._is_python_build = -1;
init_from_config_clear(&config);
dump_config(); // home and _is_python_build are from _Py_path_config
dump_config(); // home and _is_python_build are cached in _Py_path_config
Py_Finalize();
return 0;
}
Expand Down