bpo-36763: InitConfigTests tests all core config (GH-13331)
Remove UNTESTED_CORE_CONFIG from test_embed.InitConfigTests: all core config fields are now tested! Changes: * Test also dll_path on Windows * Add run_main_config unit test: test config using _Py_RunMain().
This commit is contained in:
parent
54b74fe9df
commit
5eb8b07f87
|
@ -269,12 +269,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
maxDiff = 4096
|
maxDiff = 4096
|
||||||
UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
|
UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
|
||||||
|
|
||||||
# core config
|
|
||||||
UNTESTED_CORE_CONFIG = (
|
|
||||||
# FIXME: untested core configuration variables
|
|
||||||
'dll_path',
|
|
||||||
'module_search_paths',
|
|
||||||
)
|
|
||||||
# Mark config which should be get by get_default_config()
|
# Mark config which should be get by get_default_config()
|
||||||
GET_DEFAULT_CONFIG = object()
|
GET_DEFAULT_CONFIG = object()
|
||||||
DEFAULT_PRE_CONFIG = {
|
DEFAULT_PRE_CONFIG = {
|
||||||
|
@ -324,6 +318,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
'base_prefix': GET_DEFAULT_CONFIG,
|
'base_prefix': GET_DEFAULT_CONFIG,
|
||||||
'exec_prefix': GET_DEFAULT_CONFIG,
|
'exec_prefix': GET_DEFAULT_CONFIG,
|
||||||
'base_exec_prefix': GET_DEFAULT_CONFIG,
|
'base_exec_prefix': GET_DEFAULT_CONFIG,
|
||||||
|
'module_search_paths': GET_DEFAULT_CONFIG,
|
||||||
|
|
||||||
'site_import': 1,
|
'site_import': 1,
|
||||||
'bytes_warning': 0,
|
'bytes_warning': 0,
|
||||||
|
@ -354,6 +349,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
'legacy_windows_fs_encoding': 0,
|
'legacy_windows_fs_encoding': 0,
|
||||||
})
|
})
|
||||||
DEFAULT_CORE_CONFIG.update({
|
DEFAULT_CORE_CONFIG.update({
|
||||||
|
'dll_path': GET_DEFAULT_CONFIG,
|
||||||
'legacy_windows_stdio': 0,
|
'legacy_windows_stdio': 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -410,7 +406,10 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
code = textwrap.dedent('''
|
code = textwrap.dedent('''
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
import _testinternalcapi
|
||||||
|
|
||||||
|
configs = _testinternalcapi.get_configs()
|
||||||
|
core_config = configs['core_config']
|
||||||
data = {
|
data = {
|
||||||
'stdio_encoding': sys.stdout.encoding,
|
'stdio_encoding': sys.stdout.encoding,
|
||||||
'stdio_errors': sys.stdout.errors,
|
'stdio_errors': sys.stdout.errors,
|
||||||
|
@ -420,8 +419,10 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
'base_exec_prefix': sys.base_exec_prefix,
|
'base_exec_prefix': sys.base_exec_prefix,
|
||||||
'filesystem_encoding': sys.getfilesystemencoding(),
|
'filesystem_encoding': sys.getfilesystemencoding(),
|
||||||
'filesystem_errors': sys.getfilesystemencodeerrors(),
|
'filesystem_errors': sys.getfilesystemencodeerrors(),
|
||||||
'module_search_paths': sys.path,
|
'module_search_paths': core_config['module_search_paths'],
|
||||||
}
|
}
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
data['dll_path'] = core_config['dll_path']
|
||||||
|
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
data = data.encode('utf-8')
|
data = data.encode('utf-8')
|
||||||
|
@ -431,7 +432,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
|
|
||||||
# Use -S to not import the site module: get the proper configuration
|
# Use -S to not import the site module: get the proper configuration
|
||||||
# when test_embed is run from a venv (bpo-35313)
|
# when test_embed is run from a venv (bpo-35313)
|
||||||
args = (sys.executable, '-S', '-c', code)
|
args = [sys.executable, '-S', '-c', code]
|
||||||
env = dict(env)
|
env = dict(env)
|
||||||
if not expected['isolated']:
|
if not expected['isolated']:
|
||||||
env['PYTHONCOERCECLOCALE'] = '0'
|
env['PYTHONCOERCECLOCALE'] = '0'
|
||||||
|
@ -462,7 +463,9 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
for key, value in expected.items():
|
for key, value in expected.items():
|
||||||
if value is self.GET_DEFAULT_CONFIG:
|
if value is self.GET_DEFAULT_CONFIG:
|
||||||
expected[key] = config[key]
|
expected[key] = config[key]
|
||||||
expected['module_search_paths'] = config['module_search_paths']
|
|
||||||
|
if add_path is not None:
|
||||||
|
expected['module_search_paths'].append(add_path)
|
||||||
return expected
|
return expected
|
||||||
|
|
||||||
def check_pre_config(self, config, expected):
|
def check_pre_config(self, config, expected):
|
||||||
|
@ -470,16 +473,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
core_config = dict(config['core_config'])
|
core_config = dict(config['core_config'])
|
||||||
self.assertEqual(pre_config, expected)
|
self.assertEqual(pre_config, expected)
|
||||||
|
|
||||||
def check_core_config(self, config, expected, add_path=None):
|
def check_core_config(self, config, expected):
|
||||||
core_config = dict(config['core_config'])
|
core_config = dict(config['core_config'])
|
||||||
if add_path is not None:
|
|
||||||
paths = [*expected['module_search_paths'], add_path]
|
|
||||||
if not paths[0]:
|
|
||||||
del paths[0]
|
|
||||||
self.assertEqual(core_config['module_search_paths'], paths)
|
|
||||||
for key in self.UNTESTED_CORE_CONFIG:
|
|
||||||
core_config.pop(key, None)
|
|
||||||
expected.pop(key, None)
|
|
||||||
self.assertEqual(core_config, expected)
|
self.assertEqual(core_config, expected)
|
||||||
|
|
||||||
def check_global_config(self, config):
|
def check_global_config(self, config):
|
||||||
|
@ -529,7 +524,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
expected_preconfig[key] = expected_config[key]
|
expected_preconfig[key] = expected_config[key]
|
||||||
|
|
||||||
self.check_pre_config(config, expected_preconfig)
|
self.check_pre_config(config, expected_preconfig)
|
||||||
self.check_core_config(config, expected_config, add_path)
|
self.check_core_config(config, expected_config)
|
||||||
self.check_global_config(config)
|
self.check_global_config(config)
|
||||||
|
|
||||||
def test_init_default_config(self):
|
def test_init_default_config(self):
|
||||||
|
@ -693,6 +688,18 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
||||||
self.check_config("init_read_set", core_config, preconfig,
|
self.check_config("init_read_set", core_config, preconfig,
|
||||||
add_path="init_read_set_path")
|
add_path="init_read_set_path")
|
||||||
|
|
||||||
|
def test_run_main_config(self):
|
||||||
|
preconfig = {}
|
||||||
|
code = ('import _testinternalcapi, json; '
|
||||||
|
'print(json.dumps(_testinternalcapi.get_configs()))')
|
||||||
|
core_config = {
|
||||||
|
'argv': ['-c', 'arg2'],
|
||||||
|
'program': 'python3',
|
||||||
|
'program_name': './python3',
|
||||||
|
'run_command': code + '\n',
|
||||||
|
}
|
||||||
|
self.check_config("run_main_config", core_config, preconfig)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -747,6 +747,27 @@ static int test_run_main(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int test_run_main_config(void)
|
||||||
|
{
|
||||||
|
_PyCoreConfig config = _PyCoreConfig_INIT;
|
||||||
|
|
||||||
|
wchar_t *argv[] = {L"python3", L"-c",
|
||||||
|
(L"import _testinternalcapi, json; "
|
||||||
|
L"print(json.dumps(_testinternalcapi.get_configs()))"),
|
||||||
|
L"arg2"};
|
||||||
|
config.argv.length = Py_ARRAY_LENGTH(argv);
|
||||||
|
config.argv.items = argv;
|
||||||
|
config.program_name = L"./python3";
|
||||||
|
|
||||||
|
_PyInitError err = _Py_InitializeFromConfig(&config);
|
||||||
|
if (_Py_INIT_FAILED(err)) {
|
||||||
|
_Py_ExitInitError(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _Py_RunMain();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* *********************************************************
|
/* *********************************************************
|
||||||
* List of test cases and the function that implements it.
|
* List of test cases and the function that implements it.
|
||||||
*
|
*
|
||||||
|
@ -785,6 +806,7 @@ static struct TestCase TestCases[] = {
|
||||||
{ "preinit_isolated2", test_preinit_isolated2 },
|
{ "preinit_isolated2", test_preinit_isolated2 },
|
||||||
{ "init_read_set", test_init_read_set },
|
{ "init_read_set", test_init_read_set },
|
||||||
{ "run_main", test_run_main },
|
{ "run_main", test_run_main },
|
||||||
|
{ "run_main_config", test_run_main_config },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue