]> granicus.if.org Git - python/commitdiff
bpo-33932: Calling Py_Initialize() twice does nothing (GH-7845)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 22 Jun 2018 17:33:48 +0000 (10:33 -0700)
committerGitHub <noreply@github.com>
Fri, 22 Jun 2018 17:33:48 +0000 (10:33 -0700)
Calling Py_Initialize() twice does nothing, instead of failing with a
fatal error: restore the Python 3.6 behaviour.
(cherry picked from commit 209abf746985526bce255e2fba97d3246924885d)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
Lib/test/test_embed.py
Misc/NEWS.d/next/C API/2018-06-21-15-29-59.bpo-33932.VSlXyS.rst [new file with mode: 0644]
Programs/_testembed.c
Python/pylifecycle.c

index c52cb9948782df0d2f64190f1f29f5296f48f58c..f3b60433ccc1319020e757690a318b0536c5cf44 100644 (file)
@@ -229,6 +229,15 @@ class EmbeddingTests(unittest.TestCase):
         self.assertEqual(out, '')
         self.assertEqual(err, '')
 
+    def test_initialize_twice(self):
+        """
+        bpo-33932: Calling Py_Initialize() twice should do nothing (and not
+        crash!).
+        """
+        out, err = self.run_embedded_interpreter("initialize_twice")
+        self.assertEqual(out, '')
+        self.assertEqual(err, '')
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/C API/2018-06-21-15-29-59.bpo-33932.VSlXyS.rst b/Misc/NEWS.d/next/C API/2018-06-21-15-29-59.bpo-33932.VSlXyS.rst
new file mode 100644 (file)
index 0000000..90ca3ec
--- /dev/null
@@ -0,0 +1,2 @@
+Calling Py_Initialize() twice does nothing, instead of failing with a fatal
+error: restore the Python 3.6 behaviour.
index 7406470ae65c58a968a587ff91e7a16dbd3f4f99..b8827f074b9ccef4805d24dc3fcaf57e84da0b13 100644 (file)
@@ -263,6 +263,19 @@ static int test_bpo20891(void)
     return 0;
 }
 
+static int test_initialize_twice(void)
+{
+    _testembed_Py_Initialize();
+
+    /* bpo-33932: Calling Py_Initialize() twice should do nothing
+     * (and not crash!). */
+    Py_Initialize();
+
+    Py_Finalize();
+
+    return 0;
+}
+
 
 /* *********************************************************
  * List of test cases and the function that implements it.
@@ -288,6 +301,7 @@ static struct TestCase TestCases[] = {
     { "pre_initialization_api", test_pre_initialization_api },
     { "pre_initialization_sys_options", test_pre_initialization_sys_options },
     { "bpo20891", test_bpo20891 },
+    { "initialize_twice", test_initialize_twice },
     { NULL, NULL }
 };
 
index 2ef96f8d99ef2a60a50cfadc52d86dac47fcf217..fdb759f480be3bde4d64de44f3b867c2f7401cb7 100644 (file)
@@ -895,6 +895,11 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
 _PyInitError
 _Py_InitializeEx_Private(int install_sigs, int install_importlib)
 {
+    if (_PyRuntime.initialized) {
+        /* bpo-33932: Calling Py_Initialize() twice does nothing. */
+        return _Py_INIT_OK();
+    }
+
     _PyCoreConfig config = _PyCoreConfig_INIT;
     _PyInitError err;