GeezerVison is showing github.com — bpo-33932: Calling Py_Initialize() twice does nothing (GH-7845) · miss-islington/cpython@546892a
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_embed.py +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -229,6 +229,15 @@ def test_bpo20891(self): |
229 | 229 | self.assertEqual(out, '') |
230 | 230 | self.assertEqual(err, '') |
231 | 231 |
|
| 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 | + |
232 | 241 |
|
233 | 242 | if __name__ == "__main__": |
234 | 243 | unittest.main() |
Collapse file
Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +Calling Py_Initialize() twice does nothing, instead of failing with a fatal |
| 2 | +error: restore the Python 3.6 behaviour. |
Collapse file
Copy file name to clipboardExpand all lines: Programs/_testembed.c +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -263,6 +263,19 @@ static int test_bpo20891(void) |
263 | 263 | return 0; |
264 | 264 | } |
265 | 265 |
|
| 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 | + |
266 | 279 |
|
267 | 280 | /* ********************************************************* |
268 | 281 | * List of test cases and the function that implements it. |
@@ -288,6 +301,7 @@ static struct TestCase TestCases[] = { |
288 | 301 | { "pre_initialization_api", test_pre_initialization_api }, |
289 | 302 | { "pre_initialization_sys_options", test_pre_initialization_sys_options }, |
290 | 303 | { "bpo20891", test_bpo20891 }, |
| 304 | + { "initialize_twice", test_initialize_twice }, |
291 | 305 | { NULL, NULL } |
292 | 306 | }; |
293 | 307 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: Python/pylifecycle.c +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -895,6 +895,11 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config) |
895 | 895 | _PyInitError |
896 | 896 | _Py_InitializeEx_Private(int install_sigs, int install_importlib) |
897 | 897 | { |
| 898 | + if (_PyRuntime.initialized) { |
| 899 | + /* bpo-33932: Calling Py_Initialize() twice does nothing. */ |
| 900 | + return _Py_INIT_OK(); |
| 901 | + } |
| 902 | + |
898 | 903 | _PyCoreConfig config = _PyCoreConfig_INIT; |
899 | 904 | _PyInitError err; |
900 | 905 |
|
|