Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6da20a4

Browse files
authored
bpo-36301: Test Python init with isolated (GH-12569)
Add test_preinit_isolated1() and test_preinit_isolated2() test_embed.
1 parent 6cd658b commit 6da20a4

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lib/test/test_embed.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,26 @@ def test_init_isolated(self):
662662
}
663663
self.check_config("init_isolated", config, preconfig)
664664

665+
def test_preinit_isolated1(self):
666+
# _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
667+
preconfig = {}
668+
config = {
669+
'isolated': 1,
670+
'use_environment': 0,
671+
'user_site_directory': 0,
672+
}
673+
self.check_config("preinit_isolated1", config, preconfig)
674+
675+
def test_preinit_isolated2(self):
676+
# _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
677+
preconfig = {}
678+
config = {
679+
'isolated': 1,
680+
'use_environment': 0,
681+
'user_site_directory': 0,
682+
}
683+
self.check_config("preinit_isolated2", config, preconfig)
684+
665685

666686
if __name__ == "__main__":
667687
unittest.main()

Programs/_testembed.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,74 @@ static int test_init_isolated(void)
648648
}
649649

650650

651+
/* _PyPreConfig.isolated=1, _PyCoreConfig.isolated=0 */
652+
static int test_preinit_isolated1(void)
653+
{
654+
_PyInitError err;
655+
656+
_PyPreConfig preconfig = _PyPreConfig_INIT;
657+
658+
/* Set coerce_c_locale and utf8_mode to not depend on the locale */
659+
preconfig.coerce_c_locale = 0;
660+
preconfig.utf8_mode = 0;
661+
preconfig.isolated = 1;
662+
663+
err = _Py_PreInitializeFromPreConfig(&preconfig);
664+
if (_Py_INIT_FAILED(err)) {
665+
_Py_ExitInitError(err);
666+
}
667+
668+
_PyCoreConfig config = _PyCoreConfig_INIT;
669+
config.program_name = L"./_testembed";
670+
671+
test_init_env_dev_mode_putenvs();
672+
err = _Py_InitializeFromConfig(&config);
673+
if (_Py_INIT_FAILED(err)) {
674+
_Py_ExitInitError(err);
675+
}
676+
dump_config();
677+
Py_Finalize();
678+
return 0;
679+
}
680+
681+
682+
/* _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1 */
683+
static int test_preinit_isolated2(void)
684+
{
685+
_PyInitError err;
686+
687+
_PyPreConfig preconfig = _PyPreConfig_INIT;
688+
689+
/* Set coerce_c_locale and utf8_mode to not depend on the locale */
690+
preconfig.coerce_c_locale = 0;
691+
preconfig.utf8_mode = 0;
692+
preconfig.isolated = 0;
693+
694+
err = _Py_PreInitializeFromPreConfig(&preconfig);
695+
if (_Py_INIT_FAILED(err)) {
696+
_Py_ExitInitError(err);
697+
}
698+
699+
/* Test _PyCoreConfig.isolated=1 */
700+
_PyCoreConfig config = _PyCoreConfig_INIT;
701+
702+
Py_IsolatedFlag = 0;
703+
config.isolated = 1;
704+
705+
/* Use path starting with "./" avoids a search along the PATH */
706+
config.program_name = L"./_testembed";
707+
708+
test_init_env_dev_mode_putenvs();
709+
err = _Py_InitializeFromConfig(&config);
710+
if (_Py_INIT_FAILED(err)) {
711+
_Py_ExitInitError(err);
712+
}
713+
dump_config();
714+
Py_Finalize();
715+
return 0;
716+
}
717+
718+
651719
static int test_init_dev_mode(void)
652720
{
653721
_PyCoreConfig config = _PyCoreConfig_INIT;
@@ -699,6 +767,8 @@ static struct TestCase TestCases[] = {
699767
{ "init_env_dev_mode_alloc", test_init_env_dev_mode_alloc },
700768
{ "init_dev_mode", test_init_dev_mode },
701769
{ "init_isolated", test_init_isolated },
770+
{ "preinit_isolated1", test_preinit_isolated1 },
771+
{ "preinit_isolated2", test_preinit_isolated2 },
702772
{ NULL, NULL }
703773
};
704774

0 commit comments

Comments
 (0)