Skip to content

Commit 209abf7

Browse files
authored
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.
1 parent bcd3a1a commit 209abf7

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Lib/test/test_embed.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ def test_bpo20891(self):
229229
self.assertEqual(out, '')
230230
self.assertEqual(err, '')
231231

232+
def test_initialize_twice(self):
233+
"""
234+
bpo-33932: Calling Py_Initialize() twice should do nothing (and not
235+
crash!).
236+
"""
237+
out, err = self.run_embedded_interpreter("initialize_twice")
238+
self.assertEqual(out, '')
239+
self.assertEqual(err, '')
240+
232241

233242
if __name__ == "__main__":
234243
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Calling Py_Initialize() twice does nothing, instead of failing with a fatal
2+
error: restore the Python 3.6 behaviour.

Programs/_testembed.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ static int test_bpo20891(void)
263263
return 0;
264264
}
265265

266+
static int test_initialize_twice(void)
267+
{
268+
_testembed_Py_Initialize();
269+
270+
/* bpo-33932: Calling Py_Initialize() twice should do nothing
271+
* (and not crash!). */
272+
Py_Initialize();
273+
274+
Py_Finalize();
275+
276+
return 0;
277+
}
278+
266279

267280
/* *********************************************************
268281
* List of test cases and the function that implements it.
@@ -288,6 +301,7 @@ static struct TestCase TestCases[] = {
288301
{ "pre_initialization_api", test_pre_initialization_api },
289302
{ "pre_initialization_sys_options", test_pre_initialization_sys_options },
290303
{ "bpo20891", test_bpo20891 },
304+
{ "initialize_twice", test_initialize_twice },
291305
{ NULL, NULL }
292306
};
293307

Python/pylifecycle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,11 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
892892
_PyInitError
893893
_Py_InitializeEx_Private(int install_sigs, int install_importlib)
894894
{
895+
if (_PyRuntime.initialized) {
896+
/* bpo-33932: Calling Py_Initialize() twice does nothing. */
897+
return _Py_INIT_OK();
898+
}
899+
895900
_PyCoreConfig config = _PyCoreConfig_INIT;
896901
_PyInitError err;
897902

0 commit comments

Comments
 (0)