[Python-checkins] bpo-40413: test_embed tests calling Py_RunMain() multiple times (GH-28466)

miss-islington webhook-mailer at python.org
Mon Sep 20 04:48:06 EDT 2021


https://github.com/python/cpython/commit/3d16fc90ce36acaa846ee88bdd124f84d49395b3
commit: 3d16fc90ce36acaa846ee88bdd124f84d49395b3
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-09-20T01:47:58-07:00
summary:

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 5e2c32e08ed77081cabd9d51f0589f81c1572732)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
M Lib/test/test_embed.py
M Programs/_testembed.c

diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index 479d712da65d9..0a66e7f6b9a82 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -311,6 +311,14 @@ def test_run_main(self):
         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
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index f8de6bc07934b..b7d4675a955a5 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -1662,15 +1662,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");
 
@@ -1844,6 +1855,7 @@ static struct TestCase TestCases[] = {
     {"test_init_warnoptions", test_init_warnoptions},
     {"test_init_set_config", test_init_set_config},
     {"test_run_main", test_run_main},
+    {"test_run_main_loop", test_run_main_loop},
     {"test_get_argc_argv", test_get_argc_argv},
 
     // Audit



More information about the Python-checkins mailing list