bpo-40413: test_embed tests calling Py_RunMain() multiple times (GH-28466) (GH-28471)

Calling Py_InitializeFromConfig()+Py_RunMain() multiple times must
not crash.

Cleanup also test_get_argc_argv().
(cherry picked from commit 5e2c32e08e)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2021-09-20 08:51:32 -07:00 committed by GitHub
parent 50c0551f97
commit 29e431419a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -308,6 +308,14 @@ def test_run_main(self):
self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']") self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
self.assertEqual(err, '') self.assertEqual(err, '')
def test_run_main_loop(self):
# bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
# times must not crash.
nloop = 5
out, err = self.run_embedded_interpreter("test_run_main_loop")
self.assertEqual(out, "Py_RunMain(): sys.argv=['-c', 'arg2']\n" * nloop)
self.assertEqual(err, '')
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
maxDiff = 4096 maxDiff = 4096

View File

@ -1617,15 +1617,26 @@ static int test_run_main(void)
} }
static int test_run_main_loop(void)
{
// bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
// times must not crash.
for (int i=0; i<5; i++) {
int exitcode = test_run_main();
if (exitcode != 0) {
return exitcode;
}
}
return 0;
}
static int test_get_argc_argv(void) static int test_get_argc_argv(void)
{ {
PyConfig config; PyConfig config;
PyConfig_InitPythonConfig(&config); PyConfig_InitPythonConfig(&config);
wchar_t *argv[] = {L"python3", L"-c", wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
(L"import sys; "
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
L"arg2"};
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv); config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
config_set_string(&config, &config.program_name, L"./python3"); config_set_string(&config, &config.program_name, L"./python3");
@ -1749,6 +1760,7 @@ static struct TestCase TestCases[] = {
{"test_init_setpythonhome", test_init_setpythonhome}, {"test_init_setpythonhome", test_init_setpythonhome},
{"test_init_warnoptions", test_init_warnoptions}, {"test_init_warnoptions", test_init_warnoptions},
{"test_run_main", test_run_main}, {"test_run_main", test_run_main},
{"test_run_main_loop", test_run_main_loop},
{"test_get_argc_argv", test_get_argc_argv}, {"test_get_argc_argv", test_get_argc_argv},
// Audit // Audit