Skip to content

Commit b7b20af

Browse files
vstinnerJake Taylor
authored andcommitted
bpo-38234: Fix test_embed.test_init_setpath_config() on FreeBSD (pythonGH-16406)
Explicitly preinitializes with a Python preconfiguration to avoid Py_SetPath() implicit preinitialization with a compat preconfiguration. Fix also test_init_setpath() and test_init_setpythonhome() on macOS: use self.test_exe as the executable (and base_executable), rather than shutil.which('python3').
1 parent dc23671 commit b7b20af

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Lib/test/test_embed.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
MS_WINDOWS = (os.name == 'nt')
18+
MACOS = (sys.platform == 'darwin')
1819

1920
PYMEM_ALLOCATOR_NOT_SET = 0
2021
PYMEM_ALLOCATOR_DEBUG = 2
@@ -1011,7 +1012,10 @@ def default_program_name(self, config):
10111012
executable = self.test_exe
10121013
else:
10131014
program_name = 'python3'
1014-
executable = shutil.which(program_name) or ''
1015+
if MACOS:
1016+
executable = self.test_exe
1017+
else:
1018+
executable = shutil.which(program_name) or ''
10151019
config.update({
10161020
'program_name': program_name,
10171021
'base_executable': executable,
@@ -1054,13 +1058,8 @@ def test_init_setpath_config(self):
10541058
'executable': 'conf_executable',
10551059
}
10561060
env = {'TESTPATH': os.path.pathsep.join(paths)}
1057-
# Py_SetPath() preinitialized Python using the compat API,
1058-
# so we need preconfig_api=API_COMPAT.
10591061
self.check_all_configs("test_init_setpath_config", config,
1060-
api=API_PYTHON,
1061-
preconfig_api=API_COMPAT,
1062-
env=env,
1063-
ignore_stderr=True)
1062+
api=API_PYTHON, env=env, ignore_stderr=True)
10641063

10651064
def module_search_paths(self, prefix=None, exec_prefix=None):
10661065
config = self._get_expected_config()

Programs/_testembed.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,17 @@ static int test_init_setpath(void)
14481448

14491449
static int test_init_setpath_config(void)
14501450
{
1451+
PyStatus status;
1452+
PyPreConfig preconfig;
1453+
PyPreConfig_InitPythonConfig(&preconfig);
1454+
1455+
/* Explicitly preinitializes with Python preconfiguration to avoid
1456+
Py_SetPath() implicit preinitialization with compat preconfiguration. */
1457+
status = Py_PreInitialize(&preconfig);
1458+
if (PyStatus_Exception(status)) {
1459+
Py_ExitStatusException(status);
1460+
}
1461+
14511462
char *env = getenv("TESTPATH");
14521463
if (!env) {
14531464
fprintf(stderr, "missing TESTPATH env var\n");
@@ -1462,7 +1473,6 @@ static int test_init_setpath_config(void)
14621473
PyMem_RawFree(path);
14631474
putenv("TESTPATH=");
14641475

1465-
PyStatus status;
14661476
PyConfig config;
14671477

14681478
status = PyConfig_InitPythonConfig(&config);

0 commit comments

Comments
 (0)