xoptions[opt] = True
return xoptions
- def _get_expected_config(self, env):
+ def _get_expected_config_impl(self):
+ env = remove_python_envvars()
code = textwrap.dedent('''
import json
import sys
except json.JSONDecodeError:
self.fail(f"fail to decode stdout: {stdout!r}")
+ def _get_expected_config(self):
+ cls = InitConfigTests
+ if cls.EXPECTED_CONFIG is None:
+ cls.EXPECTED_CONFIG = self._get_expected_config_impl()
+
+ # get a copy
+ return {key: dict(value)
+ for key, value in cls.EXPECTED_CONFIG.items()}
+
def get_expected_config(self, expected_preconfig, expected, env, api,
modify_path_cb=None):
cls = self.__class__
- if cls.EXPECTED_CONFIG is None:
- cls.EXPECTED_CONFIG = self._get_expected_config(env)
- configs = {key: dict(value)
- for key, value in self.EXPECTED_CONFIG.items()}
+ configs = self._get_expected_config()
pre_config = configs['pre_config']
for key, value in expected_preconfig.items():
if value is self.GET_DEFAULT_CONFIG:
expected[key] = config[key]
- prepend_path = expected['pythonpath_env']
- if prepend_path is not None:
- expected['module_search_paths'] = [prepend_path, *expected['module_search_paths']]
+ pythonpath_env = expected['pythonpath_env']
+ if pythonpath_env is not None:
+ paths = pythonpath_env.split(os.path.pathsep)
+ expected['module_search_paths'] = [*paths, *expected['module_search_paths']]
if modify_path_cb is not None:
expected['module_search_paths'] = expected['module_search_paths'].copy()
modify_path_cb(expected['module_search_paths'])
def check_all_configs(self, testname, expected_config=None,
expected_preconfig=None, modify_path_cb=None, stderr=None,
- *, api):
- env = remove_python_envvars()
+ *, api, env=None, ignore_stderr=False):
+ new_env = remove_python_envvars()
+ if env is not None:
+ new_env.update(env)
+ env = new_env
if api == API_ISOLATED:
default_preconfig = self.PRE_CONFIG_ISOLATED
out, err = self.run_embedded_interpreter(testname, env=env)
if stderr is None and not expected_config['verbose']:
stderr = ""
- if stderr is not None:
+ if stderr is not None and not ignore_stderr:
self.assertEqual(err.rstrip(), stderr)
try:
configs = json.loads(out)
self.check_all_configs("test_init_dont_parse_argv", config, pre_config,
api=API_PYTHON)
+ def test_init_setpath(self):
+ # Test Py_SetProgramName() + Py_SetPath()
+ config = self._get_expected_config()
+ paths = config['config']['module_search_paths']
+
+ config = {
+ 'module_search_paths': paths,
+ 'prefix': '',
+ 'base_prefix': '',
+ 'exec_prefix': '',
+ 'base_exec_prefix': '',
+ }
+ env = {'TESTPATH': os.path.pathsep.join(paths)}
+ self.check_all_configs("test_init_setpath", config,
+ api=API_COMPAT, env=env,
+ ignore_stderr=True)
+
+ def test_init_setpythonhome(self):
+ # Test Py_SetPythonHome(home) + PYTHONPATH env var
+ # + Py_SetProgramName()
+ config = self._get_expected_config()
+ paths = config['config']['module_search_paths']
+ paths_str = os.path.pathsep.join(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}")
+
+ prefix = exec_prefix = home
+ ver = sys.version_info
+ if MS_WINDOWS:
+ expected_paths = paths
+ else:
+ expected_paths = [
+ os.path.join(prefix, 'lib', f'python{ver.major}{ver.minor}.zip'),
+ os.path.join(home, 'lib', f'python{ver.major}.{ver.minor}'),
+ os.path.join(home, 'lib', f'python{ver.major}.{ver.minor}/lib-dynload')]
+
+ config = {
+ 'home': home,
+ 'module_search_paths': expected_paths,
+ 'prefix': prefix,
+ 'base_prefix': prefix,
+ 'exec_prefix': exec_prefix,
+ 'base_exec_prefix': exec_prefix,
+ 'pythonpath_env': paths_str,
+ }
+ env = {'TESTHOME': home, 'TESTPATH': paths_str}
+ self.check_all_configs("test_init_setpythonhome", config,
+ api=API_COMPAT, env=env)
+
class AuditingTests(EmbeddingTestsMixin, unittest.TestCase):
def test_open_code_hook(self):
wchar_t *zip_path, size_t zip_path_len)
{
PyStatus status;
- if (safe_wcscpy(zip_path, prefix, zip_path_len) < 0) {
- return PATHLEN_ERR();
- }
if (calculate->prefix_found > 0) {
/* Use the reduced prefix returned by Py_GetPrefix() */
+ if (safe_wcscpy(zip_path, prefix, zip_path_len) < 0) {
+ return PATHLEN_ERR();
+ }
reduce(zip_path);
reduce(zip_path);
}
* Executed via 'EmbeddingTests' in Lib/test/test_capi.py
*********************************************************/
+/* Use path starting with "./" avoids a search along the PATH */
+#define PROGRAM_NAME L"./_testembed"
+
static void _testembed_Py_Initialize(void)
{
- /* HACK: the "./" at front avoids a search along the PATH in
- Modules/getpath.c */
- Py_SetProgramName(L"./_testembed");
+ Py_SetProgramName(PROGRAM_NAME);
Py_Initialize();
}
static void config_set_program_name(PyConfig *config)
{
- /* Use path starting with "./" avoids a search along the PATH */
- const wchar_t *program_name = L"./_testembed";
+ const wchar_t *program_name = PROGRAM_NAME;
config_set_string(config, &config->program_name, program_name);
}
static int test_audit_run_command(void)
{
AuditRunCommandTest test = {"cpython.run_command"};
- wchar_t *argv[] = {L"./_testembed", L"-c", L"pass"};
+ wchar_t *argv[] = {PROGRAM_NAME, L"-c", L"pass"};
Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_hook_run, (void*)&test);
static int test_audit_run_file(void)
{
AuditRunCommandTest test = {"cpython.run_file"};
- wchar_t *argv[] = {L"./_testembed", L"filename.py"};
+ wchar_t *argv[] = {PROGRAM_NAME, L"filename.py"};
Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_hook_run, (void*)&test);
static int test_audit_run_interactivehook(void)
{
AuditRunCommandTest test = {"cpython.run_interactivehook", 10};
- wchar_t *argv[] = {L"./_testembed"};
+ wchar_t *argv[] = {PROGRAM_NAME};
return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
}
static int test_audit_run_startup(void)
{
AuditRunCommandTest test = {"cpython.run_startup", 10};
- wchar_t *argv[] = {L"./_testembed"};
+ wchar_t *argv[] = {PROGRAM_NAME};
return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
}
static int test_audit_run_stdin(void)
{
AuditRunCommandTest test = {"cpython.run_stdin"};
- wchar_t *argv[] = {L"./_testembed"};
+ wchar_t *argv[] = {PROGRAM_NAME};
return run_audit_run_test(Py_ARRAY_LENGTH(argv), argv, &test);
}
}
+static int test_init_setpath(void)
+{
+ Py_SetProgramName(PROGRAM_NAME);
+
+ char *env = getenv("TESTPATH");
+ if (!env) {
+ fprintf(stderr, "missing TESTPATH env var\n");
+ return 1;
+ }
+ wchar_t *path = Py_DecodeLocale(env, NULL);
+ if (path == NULL) {
+ fprintf(stderr, "failed to decode TESTPATH\n");
+ return 1;
+ }
+ Py_SetPath(path);
+ PyMem_RawFree(path);
+ putenv("TESTPATH=");
+
+ Py_Initialize();
+ dump_config();
+ Py_Finalize();
+ return 0;
+}
+
+
+static int mysetenv(const char *name, const char *value)
+{
+ size_t len = strlen(name) + 1 + strlen(value) + 1;
+ char *env = PyMem_RawMalloc(len);
+ if (env == NULL) {
+ fprintf(stderr, "out of memory\n");
+ return -1;
+ }
+ strcpy(env, name);
+ strcat(env, "=");
+ strcat(env, value);
+
+ putenv(env);
+
+ /* Don't call PyMem_RawFree(env), but leak env memory block:
+ putenv() does not copy the string. */
+
+ return 0;
+}
+
+
+static int test_init_setpythonhome(void)
+{
+ char *env = getenv("TESTHOME");
+ if (!env) {
+ fprintf(stderr, "missing TESTHOME env var\n");
+ return 1;
+ }
+ wchar_t *home = Py_DecodeLocale(env, NULL);
+ if (home == NULL) {
+ fprintf(stderr, "failed to decode TESTHOME\n");
+ return 1;
+ }
+ Py_SetPythonHome(home);
+ PyMem_RawFree(home);
+ putenv("TESTHOME=");
+
+ char *path = getenv("TESTPATH");
+ if (!path) {
+ fprintf(stderr, "missing TESTPATH env var\n");
+ return 1;
+ }
+
+ if (mysetenv("PYTHONPATH", path) < 0) {
+ return 1;
+ }
+ putenv("TESTPATH=");
+
+ Py_SetProgramName(PROGRAM_NAME);
+
+ Py_Initialize();
+ dump_config();
+ Py_Finalize();
+ return 0;
+}
+
+
static void configure_init_main(PyConfig *config)
{
wchar_t* argv[] = {
{"test_init_run_main", test_init_run_main},
{"test_init_main", test_init_main},
{"test_init_sys_add", test_init_sys_add},
+ {"test_init_setpath", test_init_setpath},
+ {"test_init_setpythonhome", test_init_setpythonhome},
{"test_run_main", test_run_main},
+
{"test_open_code_hook", test_open_code_hook},
{"test_audit", test_audit},
{"test_audit_subinterpreter", test_audit_subinterpreter},