Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb47bca

Browse files
authored
bpo-34008: Allow to call Py_Main() after Py_Initialize() (GH-8043)
Py_Main() can again be called after Py_Initialize(), as in Python 3.6. The new configuration is ignored, except of _PyMainInterpreterConfig.argv which is used to update sys.argv.
1 parent 2c5c0a3 commit fb47bca

5 files changed

Lines changed: 52 additions & 6 deletions

File tree

Lib/test/test_embed.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ def test_initialize_twice(self):
238238
self.assertEqual(out, '')
239239
self.assertEqual(err, '')
240240

241+
def test_initialize_pymain(self):
242+
"""
243+
bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
244+
"""
245+
out, err = self.run_embedded_interpreter("initialize_pymain")
246+
self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
247+
self.assertEqual(err, '')
248+
241249

242250
if __name__ == "__main__":
243251
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Py_Main() can again be called after Py_Initialize(), as in Python 3.6.

Modules/main.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,9 +2696,13 @@ pymain_main(_PyMain *pymain)
26962696

26972697
pymain_init_stdio(pymain);
26982698

2699-
pymain->err = _Py_InitializeCore(&pymain->config);
2700-
if (_Py_INIT_FAILED(pymain->err)) {
2701-
_Py_FatalInitError(pymain->err);
2699+
/* bpo-34008: For backward compatibility reasons, calling Py_Main() after
2700+
Py_Initialize() ignores the new configuration. */
2701+
if (!_PyRuntime.initialized) {
2702+
pymain->err = _Py_InitializeCore(&pymain->config);
2703+
if (_Py_INIT_FAILED(pymain->err)) {
2704+
_Py_FatalInitError(pymain->err);
2705+
}
27022706
}
27032707

27042708
if (pymain_init_python_main(pymain) < 0) {

Programs/_testembed.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ static int test_initialize_twice(void)
276276
return 0;
277277
}
278278

279+
static int test_initialize_pymain(void)
280+
{
281+
wchar_t *argv[] = {L"PYTHON", L"-c",
282+
L"import sys; print(f'Py_Main() after Py_Initialize: sys.argv={sys.argv}')",
283+
L"arg2"};
284+
_testembed_Py_Initialize();
285+
286+
/* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
287+
Py_Main(Py_ARRAY_LENGTH(argv), argv);
288+
289+
Py_Finalize();
290+
291+
return 0;
292+
}
293+
279294

280295
/* *********************************************************
281296
* List of test cases and the function that implements it.
@@ -302,6 +317,7 @@ static struct TestCase TestCases[] = {
302317
{ "pre_initialization_sys_options", test_pre_initialization_sys_options },
303318
{ "bpo20891", test_bpo20891 },
304319
{ "initialize_twice", test_initialize_twice },
320+
{ "initialize_pymain", test_initialize_pymain },
305321
{ NULL, NULL }
306322
};
307323

Python/pylifecycle.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,22 @@ _Py_InitializeCore(const _PyCoreConfig *core_config)
772772
return _Py_INIT_OK();
773773
}
774774

775+
/* Py_Initialize() has already been called: update the main interpreter
776+
configuration. Example of bpo-34008: Py_Main() called after
777+
Py_Initialize(). */
778+
static _PyInitError
779+
_Py_ReconfigureMainInterpreter(PyInterpreterState *interp,
780+
const _PyMainInterpreterConfig *config)
781+
{
782+
if (config->argv != NULL) {
783+
int res = PyDict_SetItemString(interp->sysdict, "argv", config->argv);
784+
if (res < 0) {
785+
return _Py_INIT_ERR("fail to set sys.argv");
786+
}
787+
}
788+
return _Py_INIT_OK();
789+
}
790+
775791
/* Update interpreter state based on supplied configuration settings
776792
*
777793
* After calling this function, most of the restrictions on the interpreter
@@ -793,9 +809,6 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
793809
if (!_PyRuntime.core_initialized) {
794810
return _Py_INIT_ERR("runtime core not initialized");
795811
}
796-
if (_PyRuntime.initialized) {
797-
return _Py_INIT_ERR("main interpreter already initialized");
798-
}
799812

800813
/* Get current thread state and interpreter pointer */
801814
tstate = PyThreadState_GET();
@@ -810,6 +823,10 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
810823
return _Py_INIT_ERR("failed to copy main interpreter config");
811824
}
812825

826+
if (_PyRuntime.initialized) {
827+
return _Py_ReconfigureMainInterpreter(interp, config);
828+
}
829+
813830
if (interp->core_config._disable_importlib) {
814831
/* Special mode for freeze_importlib: run with no import system
815832
*

0 commit comments

Comments
 (0)