Skip to content

Commit 29e4314

Browse files
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 5e2c32e) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 50c0551 commit 29e4314

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎Lib/test/test_embed.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ def test_run_main(self):
308308
self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
309309
self.assertEqual(err, '')
310310

311+
def test_run_main_loop(self):
312+
# bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
313+
# times must not crash.
314+
nloop = 5
315+
out, err = self.run_embedded_interpreter("test_run_main_loop")
316+
self.assertEqual(out, "Py_RunMain(): sys.argv=['-c', 'arg2']\n" * nloop)
317+
self.assertEqual(err, '')
318+
311319

312320
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
313321
maxDiff = 4096

‎Programs/_testembed.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,15 +1617,26 @@ static int test_run_main(void)
16171617
}
16181618

16191619

1620+
static int test_run_main_loop(void)
1621+
{
1622+
// bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
1623+
// times must not crash.
1624+
for (int i=0; i<5; i++) {
1625+
int exitcode = test_run_main();
1626+
if (exitcode != 0) {
1627+
return exitcode;
1628+
}
1629+
}
1630+
return 0;
1631+
}
1632+
1633+
16201634
static int test_get_argc_argv(void)
16211635
{
16221636
PyConfig config;
16231637
PyConfig_InitPythonConfig(&config);
16241638

1625-
wchar_t *argv[] = {L"python3", L"-c",
1626-
(L"import sys; "
1627-
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
1628-
L"arg2"};
1639+
wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
16291640
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
16301641
config_set_string(&config, &config.program_name, L"./python3");
16311642

@@ -1749,6 +1760,7 @@ static struct TestCase TestCases[] = {
17491760
{"test_init_setpythonhome", test_init_setpythonhome},
17501761
{"test_init_warnoptions", test_init_warnoptions},
17511762
{"test_run_main", test_run_main},
1763+
{"test_run_main_loop", test_run_main_loop},
17521764
{"test_get_argc_argv", test_get_argc_argv},
17531765

17541766
// Audit

0 commit comments

Comments
 (0)