Skip to content

Commit 5e2c32e

Browse files
authored
bpo-40413: test_embed tests calling Py_RunMain() multiple times (pythonGH-28466)
Calling Py_InitializeFromConfig()+Py_RunMain() multiple times must not crash. Cleanup also test_get_argc_argv().
1 parent fcbf9b1 commit 5e2c32e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Lib/test/test_embed.py

Lines changed: 8 additions & 0 deletions
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

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

16781678

1679+
static int test_run_main_loop(void)
1680+
{
1681+
// bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
1682+
// times must not crash.
1683+
for (int i=0; i<5; i++) {
1684+
int exitcode = test_run_main();
1685+
if (exitcode != 0) {
1686+
return exitcode;
1687+
}
1688+
}
1689+
return 0;
1690+
}
1691+
1692+
16791693
static int test_get_argc_argv(void)
16801694
{
16811695
PyConfig config;
16821696
PyConfig_InitPythonConfig(&config);
16831697

1684-
wchar_t *argv[] = {L"python3", L"-c",
1685-
(L"import sys; "
1686-
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
1687-
L"arg2"};
1698+
wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
16881699
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
16891700
config_set_string(&config, &config.program_name, L"./python3");
16901701

@@ -1900,6 +1911,7 @@ static struct TestCase TestCases[] = {
19001911
{"test_init_warnoptions", test_init_warnoptions},
19011912
{"test_init_set_config", test_init_set_config},
19021913
{"test_run_main", test_run_main},
1914+
{"test_run_main_loop", test_run_main_loop},
19031915
{"test_get_argc_argv", test_get_argc_argv},
19041916

19051917
// Audit

0 commit comments

Comments
 (0)