Skip to content

Commit 3d16fc9

Browse files
bpo-40413: test_embed tests calling Py_RunMain() multiple times (GH-28466)
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 <[email protected]>
1 parent 697b665 commit 3d16fc9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Lib/test/test_embed.py

+8
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ def test_run_main(self):
311311
self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
312312
self.assertEqual(err, '')
313313

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

315323
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
316324
maxDiff = 4096

Programs/_testembed.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -1662,15 +1662,26 @@ static int test_run_main(void)
16621662
}
16631663

16641664

1665+
static int test_run_main_loop(void)
1666+
{
1667+
// bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
1668+
// times must not crash.
1669+
for (int i=0; i<5; i++) {
1670+
int exitcode = test_run_main();
1671+
if (exitcode != 0) {
1672+
return exitcode;
1673+
}
1674+
}
1675+
return 0;
1676+
}
1677+
1678+
16651679
static int test_get_argc_argv(void)
16661680
{
16671681
PyConfig config;
16681682
PyConfig_InitPythonConfig(&config);
16691683

1670-
wchar_t *argv[] = {L"python3", L"-c",
1671-
(L"import sys; "
1672-
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
1673-
L"arg2"};
1684+
wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
16741685
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
16751686
config_set_string(&config, &config.program_name, L"./python3");
16761687

@@ -1844,6 +1855,7 @@ static struct TestCase TestCases[] = {
18441855
{"test_init_warnoptions", test_init_warnoptions},
18451856
{"test_init_set_config", test_init_set_config},
18461857
{"test_run_main", test_run_main},
1858+
{"test_run_main_loop", test_run_main_loop},
18471859
{"test_get_argc_argv", test_get_argc_argv},
18481860

18491861
// Audit

0 commit comments

Comments
 (0)