Commit 2f54908a authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36471: Add _Py_RunMain() (GH-12618)

* Add config_read_cmdline() subfunction. Remove _PyCmdline structure.
* _PyCoreConfig_Read() now also parses config->argv command line
  arguments
parent 5f45979b
......@@ -41,6 +41,9 @@ PyAPI_FUNC(_PyInitError) _Py_InitializeFromWideArgs(
int argc,
wchar_t **argv);
PyAPI_FUNC(int) _Py_RunMain(void);
PyAPI_FUNC(void) _Py_NO_RETURN _Py_ExitInitError(_PyInitError err);
/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
......
......@@ -53,7 +53,12 @@ class EmbeddingTestsMixin:
stderr=subprocess.PIPE,
universal_newlines=True,
env=env)
(out, err) = p.communicate()
try:
(out, err) = p.communicate()
except:
p.terminate()
p.wait()
raise
if p.returncode != 0 and support.verbose:
print(f"--- {cmd} failed ---")
print(f"stdout:\n{out}")
......@@ -254,6 +259,11 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
self.assertEqual(err, '')
def test_run_main(self):
out, err = self.run_embedded_interpreter("run_main")
self.assertEqual(out.rstrip(), "_Py_RunMain(): sys.argv=['-c', 'arg2']")
self.assertEqual(err, '')
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
maxDiff = 4096
......@@ -549,10 +559,11 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'pycache_prefix': 'conf_pycache_prefix',
'program_name': './conf_program_name',
'argv': ['-c', 'pass'],
'argv': ['-c', 'arg2'],
'program': 'conf_program',
'xoptions': ['core_xoption1=3', 'core_xoption2=', 'core_xoption3'],
'warnoptions': ['error::ResourceWarning', 'default::BytesWarning'],
'run_command': 'pass\n',
'site_import': 0,
'bytes_warning': 1,
......
......@@ -567,20 +567,22 @@ exit_sigint(void)
}
static int
pymain_main(_PyArgv *args)
static void _Py_NO_RETURN
pymain_exit_error(_PyInitError err)
{
_PyInitError err;
pymain_free();
_Py_ExitInitError(err);
}
err = pymain_init(args);
if (_Py_INIT_FAILED(err)) {
goto exit_init_error;
}
int
_Py_RunMain(void)
{
int exitcode = 0;
err = pymain_run_python(&exitcode);
_PyInitError err = pymain_run_python(&exitcode);
if (_Py_INIT_FAILED(err)) {
goto exit_init_error;
pymain_exit_error(err);
}
if (Py_FinalizeEx() < 0) {
......@@ -596,10 +598,18 @@ pymain_main(_PyArgv *args)
}
return exitcode;
}
exit_init_error:
pymain_free();
_Py_ExitInitError(err);
static int
pymain_main(_PyArgv *args)
{
_PyInitError err = pymain_init(args);
if (_Py_INIT_FAILED(err)) {
pymain_exit_error(err);
}
return _Py_RunMain();
}
......
......@@ -447,9 +447,11 @@ static int test_init_from_config(void)
Py_SetProgramName(L"./globalvar");
config.program_name = L"./conf_program_name";
static wchar_t* argv[2] = {
static wchar_t* argv[] = {
L"python3",
L"-c",
L"pass",
L"arg2",
};
config.argv.length = Py_ARRAY_LENGTH(argv);
config.argv.items = argv;
......@@ -528,7 +530,6 @@ static int test_init_from_config(void)
config._frozen = 1;
err = _Py_InitializeFromConfig(&config);
/* Don't call _PyCoreConfig_Clear() since all strings are static */
if (_Py_INIT_FAILED(err)) {
_Py_ExitInitError(err);
}
......@@ -712,6 +713,27 @@ static int test_init_dev_mode(void)
}
static int test_run_main(void)
{
_PyCoreConfig config = _PyCoreConfig_INIT;
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";
_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.
*
......@@ -748,6 +770,7 @@ static struct TestCase TestCases[] = {
{ "init_isolated", test_init_isolated },
{ "preinit_isolated1", test_preinit_isolated1 },
{ "preinit_isolated2", test_preinit_isolated2 },
{ "run_main", test_run_main },
{ NULL, NULL }
};
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment