[Python-checkins] bpo-33932: Calling Py_Initialize() twice does nothing (GH-7845)

Miss Islington (bot) webhook-mailer at python.org
Fri Jun 22 13:33:51 EDT 2018


https://github.com/python/cpython/commit/3747dd16d5d2af3499f586386e49740a0454cf44
commit: 3747dd16d5d2af3499f586386e49740a0454cf44
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-06-22T10:33:48-07:00
summary:

bpo-33932: Calling Py_Initialize() twice does nothing (GH-7845)


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 at redhat.com>

files:
A Misc/NEWS.d/next/C API/2018-06-21-15-29-59.bpo-33932.VSlXyS.rst
M Lib/test/test_embed.py
M Programs/_testembed.c
M Python/pylifecycle.c

diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index c52cb9948782..f3b60433ccc1 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -229,6 +229,15 @@ def test_bpo20891(self):
         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
index 000000000000..90ca3ece0919
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2018-06-21-15-29-59.bpo-33932.VSlXyS.rst	
@@ -0,0 +1,2 @@
+Calling Py_Initialize() twice does nothing, instead of failing with a fatal
+error: restore the Python 3.6 behaviour.
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 7406470ae65c..b8827f074b9c 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -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 }
 };
 
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 2ef96f8d99ef..fdb759f480be 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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;
 



More information about the Python-checkins mailing list