Skip to content

Commit d3b9041

Browse files
authored
bpo-38070: Add _PyRuntimeState.preinitializing (GH-16245)
Add _PyRuntimeState.preinitializing field: set to 1 while Py_PreInitialize() is running. _PyRuntimeState: rename also pre_initialized field to preinitialized.
1 parent b39afb7 commit d3b9041

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Include/internal/pycore_pystate.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,20 @@ struct _gilstate_runtime_state {
190190
/* Full Python runtime state */
191191

192192
typedef struct pyruntimestate {
193-
/* Is Python pre-initialized? Set to 1 by Py_PreInitialize() */
194-
int pre_initialized;
193+
/* Is running Py_PreInitialize()? */
194+
int preinitializing;
195+
196+
/* Is Python preinitialized? Set to 1 by Py_PreInitialize() */
197+
int preinitialized;
195198

196199
/* Is Python core initialized? Set to 1 by _Py_InitializeCore() */
197200
int core_initialized;
198201

199202
/* Is Python fully initialized? Set to 1 by Py_Initialize() */
200203
int initialized;
201204

205+
/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
206+
is called again. */
202207
PyThreadState *finalizing;
203208

204209
struct pyinterpreters {
@@ -241,7 +246,7 @@ typedef struct pyruntimestate {
241246
} _PyRuntimeState;
242247

243248
#define _PyRuntimeState_INIT \
244-
{.pre_initialized = 0, .core_initialized = 0, .initialized = 0}
249+
{.preinitialized = 0, .core_initialized = 0, .initialized = 0}
245250
/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
246251

247252
PyAPI_DATA(_PyRuntimeState) _PyRuntime;

Python/pylifecycle.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,15 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
725725
}
726726
_PyRuntimeState *runtime = &_PyRuntime;
727727

728-
if (runtime->pre_initialized) {
728+
if (runtime->preinitialized) {
729729
/* If it's already configured: ignored the new configuration */
730730
return _PyStatus_OK();
731731
}
732732

733+
/* Note: preinitialized remains 1 on error, it is only set to 0
734+
at exit on success. */
735+
runtime->preinitializing = 1;
736+
733737
PyPreConfig config;
734738
_PyPreConfig_InitFromPreConfig(&config, src_config);
735739

@@ -743,7 +747,8 @@ _Py_PreInitializeFromPyArgv(const PyPreConfig *src_config, const _PyArgv *args)
743747
return status;
744748
}
745749

746-
runtime->pre_initialized = 1;
750+
runtime->preinitializing = 0;
751+
runtime->preinitialized = 1;
747752
return _PyStatus_OK();
748753
}
749754

@@ -783,7 +788,7 @@ _Py_PreInitializeFromConfig(const PyConfig *config,
783788
}
784789
_PyRuntimeState *runtime = &_PyRuntime;
785790

786-
if (runtime->pre_initialized) {
791+
if (runtime->preinitialized) {
787792
/* Already initialized: do nothing */
788793
return _PyStatus_OK();
789794
}

0 commit comments

Comments
 (0)