bpo-42260: Main init modify sys.flags in-place (GH-23150) · python/cpython@af1d64d · GitHub
Skip to content

Commit af1d64d

Browse files
authored
bpo-42260: Main init modify sys.flags in-place (GH-23150)
When Py_Initialize() is called twice, the second call now updates more sys attributes for the configuration, rather than only sys.argv. * Rename _PySys_InitMain() to _PySys_UpdateConfig(). * _PySys_UpdateConfig() now modifies sys.flags in-place, instead of creating a new flags object. * Remove old commented sys.flags flags (unbuffered and skip_first). * Add private _PySys_GetObject() function. * When Py_Initialize(), Py_InitializeFromConfig() and
1 parent 58ca33b commit af1d64d

4 files changed

Lines changed: 89 additions & 74 deletions

File tree

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 1 deletion
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When :c:func:`Py_Initialize` is called twice, the second call now updates
2+
more :mod:`sys` attributes for the configuration, rather than only
3+
:data:`sys.argv`. Patch by Victor Stinner.

Python/pylifecycle.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -949,19 +949,10 @@ pyinit_core(_PyRuntimeState *runtime,
949949
configuration. Example of bpo-34008: Py_Main() called after
950950
Py_Initialize(). */
951951
static PyStatus
952-
_Py_ReconfigureMainInterpreter(PyThreadState *tstate)
952+
pyinit_main_reconfigure(PyThreadState *tstate)
953953
{
954-
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
955-
956-
PyObject *argv = _PyWideStringList_AsList(&config->argv);
957-
if (argv == NULL) {
958-
return _PyStatus_NO_MEMORY(); \
959-
}
960-
961-
int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);
962-
Py_DECREF(argv);
963-
if (res < 0) {
964-
return _PyStatus_ERR("fail to set sys.argv");
954+
if (_PySys_UpdateConfig(tstate) < 0) {
955+
return _PyStatus_ERR("fail to update sys for the new conf");
965956
}
966957
return _PyStatus_OK();
967958
}
@@ -995,7 +986,7 @@ init_interp_main(PyThreadState *tstate)
995986
}
996987
}
997988

998-
if (_PySys_InitMain(tstate) < 0) {
989+
if (_PySys_UpdateConfig(tstate) < 0) {
999990
return _PyStatus_ERR("can't finish initializing sys");
1000991
}
1001992

@@ -1100,7 +1091,7 @@ pyinit_main(PyThreadState *tstate)
11001091
}
11011092

11021093
if (interp->runtime->initialized) {
1103-
return _Py_ReconfigureMainInterpreter(tstate);
1094+
return pyinit_main_reconfigure(tstate);
11041095
}
11051096

11061097
PyStatus status = init_interp_main(tstate);
@@ -1111,19 +1102,6 @@ pyinit_main(PyThreadState *tstate)
11111102
}
11121103

11131104

1114-
PyStatus
1115-
_Py_InitializeMain(void)
1116-
{
1117-
PyStatus status = _PyRuntime_Initialize();
1118-
if (_PyStatus_EXCEPTION(status)) {
1119-
return status;
1120-
}
1121-
_PyRuntimeState *runtime = &_PyRuntime;
1122-
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1123-
return pyinit_main(tstate);
1124-
}
1125-
1126-
11271105
PyStatus
11281106
Py_InitializeFromConfig(const PyConfig *config)
11291107
{
@@ -1191,6 +1169,19 @@ Py_Initialize(void)
11911169
}
11921170

11931171

1172+
PyStatus
1173+
_Py_InitializeMain(void)
1174+
{
1175+
PyStatus status = _PyRuntime_Initialize();
1176+
if (_PyStatus_EXCEPTION(status)) {
1177+
return status;
1178+
}
1179+
_PyRuntimeState *runtime = &_PyRuntime;
1180+
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
1181+
return pyinit_main(tstate);
1182+
}
1183+
1184+
11941185
static void
11951186
finalize_modules_delete_special(PyThreadState *tstate, int verbose)
11961187
{

Python/sysmodule.c

Lines changed: 67 additions & 46 deletions

0 commit comments

Comments
 (0)