Skip to content

bpo-36471: Add _Py_RunMain() #12618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ def run_embedded_interpreter(self, *args, env=None):
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}")
Expand Down Expand Up @@ -254,6 +259,11 @@ def test_initialize_pymain(self):
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
Expand Down Expand Up @@ -549,10 +559,11 @@ def test_init_from_config(self):

'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,
Expand Down
34 changes: 22 additions & 12 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}


Expand Down
27 changes: 25 additions & 2 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 }
};

Expand Down
Loading