]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40413: test_embed tests calling Py_RunMain() multiple times (GH-28466) (GH-28471)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 20 Sep 2021 15:51:32 +0000 (08:51 -0700)
committerGitHub <noreply@github.com>
Mon, 20 Sep 2021 15:51:32 +0000 (17:51 +0200)
Calling Py_InitializeFromConfig()+Py_RunMain() multiple times must
not crash.

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

Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_embed.py
Programs/_testembed.c

index ea47004443006c0a3cbc2a8766b083f7e38310a9..cc59bf4c859d105481bde0c3cac73e530a340d9f 100644 (file)
@@ -308,6 +308,14 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
         self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
         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):
     maxDiff = 4096
index 3933b86af119404de24ac690a811df3fefb681c3..897ce7f7ae6e5193980c759ec36a613a3c3dcd49 100644 (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)
 {
     PyConfig config;
     PyConfig_InitPythonConfig(&config);
 
-    wchar_t *argv[] = {L"python3", L"-c",
-                       (L"import sys; "
-                        L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
-                       L"arg2"};
+    wchar_t *argv[] = {L"python3", L"-c", L"pass", L"arg2"};
     config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
     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_warnoptions", test_init_warnoptions},
     {"test_run_main", test_run_main},
+    {"test_run_main_loop", test_run_main_loop},
     {"test_get_argc_argv", test_get_argc_argv},
 
     // Audit