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.
This commit is contained in:
Victor Stinner 2018-06-22 19:14:51 +02:00 committed by GitHub
parent bcd3a1a18d
commit 209abf7469
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View file

@ -229,6 +229,15 @@ class EmbeddingTests(unittest.TestCase):
self.assertEqual(out, '') self.assertEqual(out, '')
self.assertEqual(err, '') 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__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -0,0 +1,2 @@
Calling Py_Initialize() twice does nothing, instead of failing with a fatal
error: restore the Python 3.6 behaviour.

View file

@ -263,6 +263,19 @@ static int test_bpo20891(void)
return 0; 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. * 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_api", test_pre_initialization_api },
{ "pre_initialization_sys_options", test_pre_initialization_sys_options }, { "pre_initialization_sys_options", test_pre_initialization_sys_options },
{ "bpo20891", test_bpo20891 }, { "bpo20891", test_bpo20891 },
{ "initialize_twice", test_initialize_twice },
{ NULL, NULL } { NULL, NULL }
}; };

View file

@ -892,6 +892,11 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
_PyInitError _PyInitError
_Py_InitializeEx_Private(int install_sigs, int install_importlib) _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; _PyCoreConfig config = _PyCoreConfig_INIT;
_PyInitError err; _PyInitError err;