Skip to content

Commit 5eb8b07

Browse files
authored
bpo-36763: InitConfigTests tests all core config (pythonGH-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().
1 parent 54b74fe commit 5eb8b07

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

Lib/test/test_embed.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
269269
maxDiff = 4096
270270
UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
271271

272-
# core config
273-
UNTESTED_CORE_CONFIG = (
274-
# FIXME: untested core configuration variables
275-
'dll_path',
276-
'module_search_paths',
277-
)
278272
# Mark config which should be get by get_default_config()
279273
GET_DEFAULT_CONFIG = object()
280274
DEFAULT_PRE_CONFIG = {
@@ -324,6 +318,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
324318
'base_prefix': GET_DEFAULT_CONFIG,
325319
'exec_prefix': GET_DEFAULT_CONFIG,
326320
'base_exec_prefix': GET_DEFAULT_CONFIG,
321+
'module_search_paths': GET_DEFAULT_CONFIG,
327322

328323
'site_import': 1,
329324
'bytes_warning': 0,
@@ -354,6 +349,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
354349
'legacy_windows_fs_encoding': 0,
355350
})
356351
DEFAULT_CORE_CONFIG.update({
352+
'dll_path': GET_DEFAULT_CONFIG,
357353
'legacy_windows_stdio': 0,
358354
})
359355

@@ -410,7 +406,10 @@ def get_expected_config(self, expected, env, add_path=None):
410406
code = textwrap.dedent('''
411407
import json
412408
import sys
409+
import _testinternalcapi
413410
411+
configs = _testinternalcapi.get_configs()
412+
core_config = configs['core_config']
414413
data = {
415414
'stdio_encoding': sys.stdout.encoding,
416415
'stdio_errors': sys.stdout.errors,
@@ -420,8 +419,10 @@ def get_expected_config(self, expected, env, add_path=None):
420419
'base_exec_prefix': sys.base_exec_prefix,
421420
'filesystem_encoding': sys.getfilesystemencoding(),
422421
'filesystem_errors': sys.getfilesystemencodeerrors(),
423-
'module_search_paths': sys.path,
422+
'module_search_paths': core_config['module_search_paths'],
424423
}
424+
if sys.platform == 'win32':
425+
data['dll_path'] = core_config['dll_path']
425426
426427
data = json.dumps(data)
427428
data = data.encode('utf-8')
@@ -431,7 +432,7 @@ def get_expected_config(self, expected, env, add_path=None):
431432

432433
# Use -S to not import the site module: get the proper configuration
433434
# when test_embed is run from a venv (bpo-35313)
434-
args = (sys.executable, '-S', '-c', code)
435+
args = [sys.executable, '-S', '-c', code]
435436
env = dict(env)
436437
if not expected['isolated']:
437438
env['PYTHONCOERCECLOCALE'] = '0'
@@ -462,24 +463,18 @@ def get_expected_config(self, expected, env, add_path=None):
462463
for key, value in expected.items():
463464
if value is self.GET_DEFAULT_CONFIG:
464465
expected[key] = config[key]
465-
expected['module_search_paths'] = config['module_search_paths']
466+
467+
if add_path is not None:
468+
expected['module_search_paths'].append(add_path)
466469
return expected
467470

468471
def check_pre_config(self, config, expected):
469472
pre_config = dict(config['pre_config'])
470473
core_config = dict(config['core_config'])
471474
self.assertEqual(pre_config, expected)
472475

473-
def check_core_config(self, config, expected, add_path=None):
476+
def check_core_config(self, config, expected):
474477
core_config = dict(config['core_config'])
475-
if add_path is not None:
476-
paths = [*expected['module_search_paths'], add_path]
477-
if not paths[0]:
478-
del paths[0]
479-
self.assertEqual(core_config['module_search_paths'], paths)
480-
for key in self.UNTESTED_CORE_CONFIG:
481-
core_config.pop(key, None)
482-
expected.pop(key, None)
483478
self.assertEqual(core_config, expected)
484479

485480
def check_global_config(self, config):
@@ -529,7 +524,7 @@ def check_config(self, testname, expected_config, expected_preconfig, add_path=N
529524
expected_preconfig[key] = expected_config[key]
530525

531526
self.check_pre_config(config, expected_preconfig)
532-
self.check_core_config(config, expected_config, add_path)
527+
self.check_core_config(config, expected_config)
533528
self.check_global_config(config)
534529

535530
def test_init_default_config(self):
@@ -693,6 +688,18 @@ def test_init_read_set(self):
693688
self.check_config("init_read_set", core_config, preconfig,
694689
add_path="init_read_set_path")
695690

691+
def test_run_main_config(self):
692+
preconfig = {}
693+
code = ('import _testinternalcapi, json; '
694+
'print(json.dumps(_testinternalcapi.get_configs()))')
695+
core_config = {
696+
'argv': ['-c', 'arg2'],
697+
'program': 'python3',
698+
'program_name': './python3',
699+
'run_command': code + '\n',
700+
}
701+
self.check_config("run_main_config", core_config, preconfig)
702+
696703

697704
if __name__ == "__main__":
698705
unittest.main()

Programs/_testembed.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,27 @@ static int test_run_main(void)
747747
}
748748

749749

750+
static int test_run_main_config(void)
751+
{
752+
_PyCoreConfig config = _PyCoreConfig_INIT;
753+
754+
wchar_t *argv[] = {L"python3", L"-c",
755+
(L"import _testinternalcapi, json; "
756+
L"print(json.dumps(_testinternalcapi.get_configs()))"),
757+
L"arg2"};
758+
config.argv.length = Py_ARRAY_LENGTH(argv);
759+
config.argv.items = argv;
760+
config.program_name = L"./python3";
761+
762+
_PyInitError err = _Py_InitializeFromConfig(&config);
763+
if (_Py_INIT_FAILED(err)) {
764+
_Py_ExitInitError(err);
765+
}
766+
767+
return _Py_RunMain();
768+
}
769+
770+
750771
/* *********************************************************
751772
* List of test cases and the function that implements it.
752773
*
@@ -785,6 +806,7 @@ static struct TestCase TestCases[] = {
785806
{ "preinit_isolated2", test_preinit_isolated2 },
786807
{ "init_read_set", test_init_read_set },
787808
{ "run_main", test_run_main },
809+
{ "run_main_config", test_run_main_config },
788810
{ NULL, NULL }
789811
};
790812

0 commit comments

Comments
 (0)