Skip to content

Commit 17a42ac

Browse files
PyInterpreterState.global_config -> _PyRuntimeState.config.
1 parent 147c460 commit 17a42ac

File tree

4 files changed

+56
-41
lines changed

4 files changed

+56
-41
lines changed

Include/internal/pycore_interp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ struct _is {
135135
PyObject *codec_error_registry;
136136
int codecs_initialized;
137137

138-
PyConfig global_config;
139138
_PyInterpreterConfig config;
140139

141140
#ifdef HAVE_DLOPEN

Include/internal/pycore_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef struct pyruntimestate {
118118
struct _gilstate_runtime_state gilstate;
119119

120120
PyPreConfig preconfig;
121+
PyConfig config;
121122

122123
// Audit values must be preserved when Py_Initialize()/Py_Finalize()
123124
// is called multiple times.

Python/pylifecycle.c

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -456,26 +456,28 @@ _Py_SetLocaleFromEnv(int category)
456456

457457

458458
static int
459-
interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
459+
runtime_update_config(_PyRuntimeState *runtime, int only_update_path_config)
460460
{
461-
const PyConfig *config = &tstate->interp->global_config;
462-
463461
if (!only_update_path_config) {
464-
PyStatus status = _PyConfig_Write(config, tstate->interp->runtime);
462+
PyStatus status = _PyConfig_Write(&runtime->config, runtime);
465463
if (_PyStatus_EXCEPTION(status)) {
466464
_PyErr_SetFromPyStatus(status);
467465
return -1;
468466
}
469467
}
470468

471-
if (_Py_IsMainInterpreter(tstate->interp)) {
472-
PyStatus status = _PyPathConfig_UpdateGlobal(config);
473-
if (_PyStatus_EXCEPTION(status)) {
474-
_PyErr_SetFromPyStatus(status);
475-
return -1;
476-
}
469+
PyStatus status = _PyPathConfig_UpdateGlobal(&runtime->config);
470+
if (_PyStatus_EXCEPTION(status)) {
471+
_PyErr_SetFromPyStatus(status);
472+
return -1;
477473
}
478474

475+
return 0;
476+
}
477+
478+
static int
479+
interpreter_update_config(PyThreadState *tstate)
480+
{
479481
// Update the sys module for the new configuration
480482
if (_PySys_UpdateConfig(tstate) < 0) {
481483
return -1;
@@ -504,13 +506,20 @@ _Py_SetConfig(const PyConfig *src_config)
504506
goto done;
505507
}
506508

507-
status = _PyConfig_Copy(&tstate->interp->global_config, &config);
509+
status = _PyConfig_Copy(&tstate->interp->runtime->config, &config);
508510
if (_PyStatus_EXCEPTION(status)) {
509511
_PyErr_SetFromPyStatus(status);
510512
goto done;
511513
}
512514

513-
res = interpreter_update_config(tstate, 0);
515+
if (_Py_IsMainInterpreter(tstate->interp)) {
516+
res = runtime_update_config(tstate->interp->runtime, 0);
517+
if (res < 0) {
518+
goto done;
519+
}
520+
}
521+
522+
res = interpreter_update_config(tstate);
514523

515524
done:
516525
PyConfig_Clear(&config);
@@ -546,13 +555,15 @@ pyinit_core_reconfigure(_PyRuntimeState *runtime,
546555
if (interp == NULL) {
547556
return _PyStatus_ERR("can't make main interpreter");
548557
}
558+
assert(interp->runtime == runtime);
559+
assert(_Py_IsMainInterpreter(interp));
549560

550561
status = _PyConfig_Write(config, runtime);
551562
if (_PyStatus_EXCEPTION(status)) {
552563
return status;
553564
}
554565

555-
status = _PyConfig_Copy(&interp->global_config, config);
566+
status = _PyConfig_Copy(&runtime->config, config);
556567
if (_PyStatus_EXCEPTION(status)) {
557568
return status;
558569
}
@@ -645,9 +656,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
645656
if (interp == NULL) {
646657
return _PyStatus_ERR("can't make main interpreter");
647658
}
659+
assert(interp->runtime == runtime);
648660
assert(_Py_IsMainInterpreter(interp));
649661

650-
status = _PyConfig_Copy(&interp->global_config, config);
662+
status = _PyConfig_Copy(&runtime->config, config);
651663
if (_PyStatus_EXCEPTION(status)) {
652664
return status;
653665
}
@@ -1067,7 +1079,12 @@ pyinit_core(_PyRuntimeState *runtime,
10671079
static PyStatus
10681080
pyinit_main_reconfigure(PyThreadState *tstate)
10691081
{
1070-
if (interpreter_update_config(tstate, 0) < 0) {
1082+
if (_Py_IsMainInterpreter(tstate->interp)) {
1083+
if (runtime_update_config(tstate->interp->runtime, 0) < 0) {
1084+
return _PyStatus_ERR("fail to reconfigure Python");
1085+
}
1086+
}
1087+
if (interpreter_update_config(tstate) < 0) {
10711088
return _PyStatus_ERR("fail to reconfigure Python");
10721089
}
10731090
return _PyStatus_OK();
@@ -1098,13 +1115,18 @@ init_interp_main(PyThreadState *tstate)
10981115
return _PyStatus_OK();
10991116
}
11001117

1101-
// Initialize the import-related configuration.
1102-
status = _PyConfig_InitImportConfig(&interp->global_config);
1103-
if (_PyStatus_EXCEPTION(status)) {
1104-
return status;
1105-
}
1118+
if (is_main_interp) {
1119+
// Initialize the import-related configuration.
1120+
status = _PyConfig_InitImportConfig(&interp->runtime->config);
1121+
if (_PyStatus_EXCEPTION(status)) {
1122+
return status;
1123+
}
11061124

1107-
if (interpreter_update_config(tstate, 1) < 0) {
1125+
if (runtime_update_config(tstate->interp->runtime, 1) < 0) {
1126+
return _PyStatus_ERR("failed to update the Python config");
1127+
}
1128+
}
1129+
if (interpreter_update_config(tstate) < 0) {
11081130
return _PyStatus_ERR("failed to update the Python config");
11091131
}
11101132

@@ -1766,14 +1788,14 @@ Py_FinalizeEx(void)
17661788
/* Copy the core config, PyInterpreterState_Delete() free
17671789
the core config memory */
17681790
#ifdef Py_REF_DEBUG
1769-
int show_ref_count = tstate->interp->global_config.show_ref_count;
1791+
int show_ref_count = runtime->config.show_ref_count;
17701792
#endif
17711793
#ifdef Py_TRACE_REFS
1772-
int dump_refs = tstate->interp->global_config.dump_refs;
1773-
wchar_t *dump_refs_file = tstate->interp->global_config.dump_refs_file;
1794+
int dump_refs = runtime->config.dump_refs;
1795+
wchar_t *dump_refs_file = runtime->config.dump_refs_file;
17741796
#endif
17751797
#ifdef WITH_PYMALLOC
1776-
int malloc_stats = tstate->interp->global_config.malloc_stats;
1798+
int malloc_stats = runtime->config.malloc_stats;
17771799
#endif
17781800

17791801
/* Remaining daemon threads will automatically exit
@@ -1972,27 +1994,18 @@ new_interpreter(PyThreadState **tstate_p, const _PyInterpreterConfig *config)
19721994
PyThreadState *save_tstate = PyThreadState_Swap(tstate);
19731995

19741996
/* Copy the current interpreter config into the new interpreter */
1975-
const PyConfig *global_config;
19761997
#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
19771998
if (save_tstate != NULL) {
1978-
global_config = _PyInterpreterState_GetGlobalConfig(save_tstate->interp);
19791999
if (config == NULL) {
19802000
config = _PyInterpreterState_GetConfig(save_tstate->interp);
19812001
}
19822002
}
19832003
else
19842004
#endif
1985-
{
2005+
if (config == NULL) {
19862006
/* No current thread state, copy from the main interpreter */
19872007
PyInterpreterState *main_interp = _PyInterpreterState_Main();
1988-
global_config = _PyInterpreterState_GetGlobalConfig(main_interp);
1989-
if (config == NULL) {
1990-
config = _PyInterpreterState_GetConfig(main_interp);
1991-
}
1992-
}
1993-
status = _PyConfig_Copy(&interp->global_config, global_config);
1994-
if (_PyStatus_EXCEPTION(status)) {
1995-
goto error;
2008+
config = _PyInterpreterState_GetConfig(main_interp);
19962009
}
19972010
status = _PyInterpreterConfig_Copy(&interp->config, config);
19982011
if (_PyStatus_EXCEPTION(status)) {

Python/pystate.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ init_runtime(_PyRuntimeState *runtime,
119119
// Set it to the ID of the main thread of the main interpreter.
120120
runtime->main_thread = PyThread_get_thread_ident();
121121

122+
PyConfig_InitPythonConfig(&runtime->config);
123+
122124
runtime->unicode_ids.next_index = unicode_next_index;
123125
runtime->unicode_ids.lock = unicode_ids_mutex;
124126

@@ -172,6 +174,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
172174

173175
#undef FREE_LOCK
174176
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
177+
178+
PyConfig_Clear(&runtime->config);
175179
}
176180

177181
#ifdef HAVE_FORK
@@ -289,7 +293,6 @@ init_interpreter(PyInterpreterState *interp,
289293

290294
_PyEval_InitState(&interp->ceval, pending_lock);
291295
_PyGC_InitState(&interp->gc);
292-
PyConfig_InitPythonConfig(&interp->global_config);
293296
_PyType_InitCache(interp);
294297

295298
interp->_initialized = 1;
@@ -397,7 +400,6 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
397400

398401
Py_CLEAR(interp->audit_hooks);
399402

400-
PyConfig_Clear(&interp->global_config);
401403
Py_CLEAR(interp->codec_search_path);
402404
Py_CLEAR(interp->codec_search_cache);
403405
Py_CLEAR(interp->codec_error_registry);
@@ -2151,7 +2153,7 @@ _PyInterpreterState_GetConfig(PyInterpreterState *interp)
21512153
const PyConfig*
21522154
_PyInterpreterState_GetGlobalConfig(PyInterpreterState *interp)
21532155
{
2154-
return &interp->global_config;
2156+
return &interp->runtime->config;
21552157
}
21562158

21572159

@@ -2160,7 +2162,7 @@ _Py_CopyConfig(PyConfig *config)
21602162
{
21612163
PyInterpreterState *interp = PyInterpreterState_Get();
21622164

2163-
PyStatus status = _PyConfig_Copy(config, &interp->global_config);
2165+
PyStatus status = _PyConfig_Copy(config, &interp->runtime->config);
21642166
if (PyStatus_Exception(status)) {
21652167
_PyErr_SetFromPyStatus(status);
21662168
return -1;

0 commit comments

Comments
 (0)