summaryrefslogtreecommitdiffstats
path: root/Programs/_testembed.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-05-16 15:38:16 (GMT)
committerGitHub <noreply@github.com>2019-05-16 15:38:16 (GMT)
commit9ef5dcaa0b3c7c7ba28dbb3ec0c9507d9d05e3a9 (patch)
tree56b9b45660cc83960c2752e22ee47090632ebb09 /Programs/_testembed.c
parentae239f6b0626e926613a4a1dbafa323bd41fec32 (diff)
downloadcpython-9ef5dcaa0b3c7c7ba28dbb3ec0c9507d9d05e3a9.zip
cpython-9ef5dcaa0b3c7c7ba28dbb3ec0c9507d9d05e3a9.tar.gz
cpython-9ef5dcaa0b3c7c7ba28dbb3ec0c9507d9d05e3a9.tar.bz2
bpo-36763: Add _Py_InitializeMain() (GH-13362)
* Add a private _Py_InitializeMain() function. * Add again _PyCoreConfig._init_main. * _Py_InitializeFromConfig() now uses _init_main to decide if _Py_InitializeMainInterpreter() should be called. * _PyCoreConfig: rename _frozen to pathconfig_warnings, its value is now the opposite of Py_FrozenFlag. * Add an unit test for _init_main=0 and _Py_InitializeMain().
Diffstat (limited to 'Programs/_testembed.c')
-rw-r--r--Programs/_testembed.c62
1 files changed, 50 insertions, 12 deletions
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 6eee2e8..4ee2cd1 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -757,34 +757,71 @@ fail:
}
-static int test_run_main(void)
+wchar_t *init_main_argv[] = {
+ L"python3", L"-c",
+ (L"import _testinternalcapi, json; "
+ L"print(json.dumps(_testinternalcapi.get_configs()))"),
+ L"arg2"};
+
+
+static void configure_init_main(_PyCoreConfig *config)
+{
+ config->argv.length = Py_ARRAY_LENGTH(init_main_argv);
+ config->argv.items = init_main_argv;
+ config->program_name = L"./python3";
+}
+
+
+static int test_init_run_main(void)
{
_PyCoreConfig config = _PyCoreConfig_INIT;
+ configure_init_main(&config);
+
+ _PyInitError err = _Py_InitializeFromConfig(&config);
+ if (_Py_INIT_FAILED(err)) {
+ _Py_ExitInitError(err);
+ }
+
+ return _Py_RunMain();
+}
- wchar_t *argv[] = {L"python3", L"-c",
- (L"import sys; "
- L"print(f'_Py_RunMain(): sys.argv={sys.argv}')"),
- L"arg2"};
- config.argv.length = Py_ARRAY_LENGTH(argv);
- config.argv.items = argv;
- config.program_name = L"./python3";
+
+static int test_init_main(void)
+{
+ _PyCoreConfig config = _PyCoreConfig_INIT;
+ configure_init_main(&config);
+ config._init_main = 0;
_PyInitError err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) {
_Py_ExitInitError(err);
}
+ /* sys.stdout don't exist yet: it is created by _Py_InitializeMain() */
+ int res = PyRun_SimpleString(
+ "import sys; "
+ "print('Run Python code before _Py_InitializeMain', "
+ "file=sys.stderr)");
+ if (res < 0) {
+ exit(1);
+ }
+
+ err = _Py_InitializeMain();
+ if (_Py_INIT_FAILED(err)) {
+ _Py_ExitInitError(err);
+ }
+
return _Py_RunMain();
}
-static int test_run_main_config(void)
+static int test_run_main(void)
{
_PyCoreConfig config = _PyCoreConfig_INIT;
wchar_t *argv[] = {L"python3", L"-c",
- (L"import _testinternalcapi, json; "
- L"print(json.dumps(_testinternalcapi.get_configs()))"),
+ (L"import sys; "
+ L"print(f'_Py_RunMain(): sys.argv={sys.argv}')"),
L"arg2"};
config.argv.length = Py_ARRAY_LENGTH(argv);
config.argv.items = argv;
@@ -837,8 +874,9 @@ static struct TestCase TestCases[] = {
{ "preinit_isolated1", test_preinit_isolated1 },
{ "preinit_isolated2", test_preinit_isolated2 },
{ "init_read_set", test_init_read_set },
+ { "init_run_main", test_init_run_main },
+ { "init_main", test_init_main },
{ "run_main", test_run_main },
- { "run_main_config", test_run_main_config },
{ NULL, NULL }
};