Skip to content

Commit 91c9987

Browse files
authored
bpo-36763: Add test for _PyCoreConfig_SetString() (GH-13275)
test_embed: add test_init_read_set() to test newly added APIs: test module_search_paths and executable.
1 parent e883091 commit 91c9987

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

Lib/test/test_embed.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
273273
UNTESTED_CORE_CONFIG = (
274274
# FIXME: untested core configuration variables
275275
'dll_path',
276-
'executable',
277276
'module_search_paths',
278277
)
279278
# Mark config which should be get by get_default_config()
@@ -310,7 +309,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
310309
'filesystem_errors': GET_DEFAULT_CONFIG,
311310

312311
'pycache_prefix': None,
313-
'program_name': './_testembed',
312+
'program_name': GET_DEFAULT_CONFIG,
314313
'argv': [""],
315314
'program': '',
316315

@@ -319,6 +318,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
319318

320319
'module_search_path_env': None,
321320
'home': None,
321+
'executable': GET_DEFAULT_CONFIG,
322322

323323
'prefix': GET_DEFAULT_CONFIG,
324324
'base_prefix': GET_DEFAULT_CONFIG,
@@ -404,7 +404,7 @@ def main_xoptions(self, xoptions_list):
404404
xoptions[opt] = True
405405
return xoptions
406406

407-
def get_expected_config(self, expected, env):
407+
def get_expected_config(self, expected, env, add_path=None):
408408
expected = dict(self.DEFAULT_CORE_CONFIG, **expected)
409409

410410
code = textwrap.dedent('''
@@ -420,6 +420,7 @@ def get_expected_config(self, expected, env):
420420
'base_exec_prefix': sys.base_exec_prefix,
421421
'filesystem_encoding': sys.getfilesystemencoding(),
422422
'filesystem_errors': sys.getfilesystemencodeerrors(),
423+
'module_search_paths': sys.path,
423424
}
424425
425426
data = json.dumps(data)
@@ -447,20 +448,38 @@ def get_expected_config(self, expected, env):
447448
except json.JSONDecodeError:
448449
self.fail(f"fail to decode stdout: {stdout!r}")
449450

451+
if expected['executable'] is self.GET_DEFAULT_CONFIG:
452+
if sys.platform == 'win32':
453+
expected['executable'] = self.test_exe
454+
else:
455+
if expected['program_name'] is not self.GET_DEFAULT_CONFIG:
456+
expected['executable'] = os.path.abspath(expected['program_name'])
457+
else:
458+
expected['executable'] = os.path.join(os.getcwd(), '_testembed')
459+
if expected['program_name'] is self.GET_DEFAULT_CONFIG:
460+
expected['program_name'] = './_testembed'
461+
450462
for key, value in expected.items():
451463
if value is self.GET_DEFAULT_CONFIG:
452464
expected[key] = config[key]
465+
expected['module_search_paths'] = config['module_search_paths']
453466
return expected
454467

455468
def check_pre_config(self, config, expected):
456469
pre_config = dict(config['pre_config'])
457470
core_config = dict(config['core_config'])
458471
self.assertEqual(pre_config, expected)
459472

460-
def check_core_config(self, config, expected):
473+
def check_core_config(self, config, expected, add_path=None):
461474
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)
462480
for key in self.UNTESTED_CORE_CONFIG:
463481
core_config.pop(key, None)
482+
expected.pop(key, None)
464483
self.assertEqual(core_config, expected)
465484

466485
def check_global_config(self, config):
@@ -485,7 +504,7 @@ def check_global_config(self, config):
485504

486505
self.assertEqual(config['global_config'], expected)
487506

488-
def check_config(self, testname, expected_config, expected_preconfig):
507+
def check_config(self, testname, expected_config, expected_preconfig, add_path=None):
489508
env = dict(os.environ)
490509
# Remove PYTHON* environment variables to get deterministic environment
491510
for key in list(env):
@@ -504,13 +523,13 @@ def check_config(self, testname, expected_config, expected_preconfig):
504523
self.fail(f"fail to decode stdout: {out!r}")
505524

506525
expected_preconfig = dict(self.DEFAULT_PRE_CONFIG, **expected_preconfig)
507-
expected_config = self.get_expected_config(expected_config, env)
526+
expected_config = self.get_expected_config(expected_config, env, add_path)
508527
for key in self.COPY_PRE_CONFIG:
509528
if key not in expected_preconfig:
510529
expected_preconfig[key] = expected_config[key]
511530

512531
self.check_pre_config(config, expected_preconfig)
513-
self.check_core_config(config, expected_config)
532+
self.check_core_config(config, expected_config, add_path)
514533
self.check_global_config(config)
515534

516535
def test_init_default_config(self):
@@ -665,6 +684,15 @@ def test_preinit_isolated2(self):
665684
}
666685
self.check_config("preinit_isolated2", config, preconfig)
667686

687+
def test_init_read_set(self):
688+
preconfig = {}
689+
core_config = {
690+
'program_name': './init_read_set',
691+
'executable': 'my_executable',
692+
}
693+
self.check_config("init_read_set", core_config, preconfig,
694+
add_path="init_read_set_path")
695+
668696

669697
if __name__ == "__main__":
670698
unittest.main()

Programs/_testembed.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
/* FIXME: PEP 587 makes these functions public */
2+
#ifndef Py_BUILD_CORE_MODULE
3+
# define Py_BUILD_CORE_MODULE
4+
#endif
5+
16
#include <Python.h>
7+
#include "pycore_coreconfig.h" /* FIXME: PEP 587 makes these functions public */
28
#include "pythread.h"
39
#include <inttypes.h>
410
#include <stdio.h>
@@ -679,6 +685,47 @@ static int test_init_dev_mode(void)
679685
}
680686

681687

688+
static int test_init_read_set(void)
689+
{
690+
_PyInitError err;
691+
_PyCoreConfig config = _PyCoreConfig_INIT;
692+
693+
err = _PyCoreConfig_DecodeLocale(&config.program_name, "./init_read_set");
694+
if (_Py_INIT_FAILED(err)) {
695+
goto fail;
696+
}
697+
698+
err = _PyCoreConfig_Read(&config);
699+
if (_Py_INIT_FAILED(err)) {
700+
goto fail;
701+
}
702+
703+
if (_PyWstrList_Append(&config.module_search_paths,
704+
L"init_read_set_path") < 0) {
705+
err = _Py_INIT_NO_MEMORY();
706+
goto fail;
707+
}
708+
709+
/* override executable computed by _PyCoreConfig_Read() */
710+
err = _PyCoreConfig_SetString(&config.executable, L"my_executable");
711+
if (_Py_INIT_FAILED(err)) {
712+
goto fail;
713+
}
714+
715+
err = _Py_InitializeFromConfig(&config);
716+
_PyCoreConfig_Clear(&config);
717+
if (_Py_INIT_FAILED(err)) {
718+
goto fail;
719+
}
720+
dump_config();
721+
Py_Finalize();
722+
return 0;
723+
724+
fail:
725+
_Py_ExitInitError(err);
726+
}
727+
728+
682729
static int test_run_main(void)
683730
{
684731
_PyCoreConfig config = _PyCoreConfig_INIT;
@@ -736,6 +783,7 @@ static struct TestCase TestCases[] = {
736783
{ "init_isolated", test_init_isolated },
737784
{ "preinit_isolated1", test_preinit_isolated1 },
738785
{ "preinit_isolated2", test_preinit_isolated2 },
786+
{ "init_read_set", test_init_read_set },
739787
{ "run_main", test_run_main },
740788
{ NULL, NULL }
741789
};

0 commit comments

Comments
 (0)