Skip to content

Commit 32a6724

Browse files
bpo-46008: Move Py*State init into distinct functions. (gh-29977)
Previously, basic initialization of PyInterprterState happened in PyInterpreterState_New() (along with allocation and adding the new interpreter to the runtime state). This prevented us from initializing interpreter states that were allocated separately (e.g. statically or in a free list). We've addressed that here by factoring out a separate function just for initialization. We've done the same for PyThreadState. _PyRuntimeState was sorted out when we added it since _PyRuntime is statically allocated. However, here we update the existing init code to line up with the functions for PyInterpreterState and PyThreadState. https://bugs.python.org/issue46008
1 parent 758b74e commit 32a6724

File tree

4 files changed

+299
-103
lines changed

4 files changed

+299
-103
lines changed

Include/cpython/pystate.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ struct _ts {
7777
struct _ts *next;
7878
PyInterpreterState *interp;
7979

80+
/* Has been initialized to a safe state.
81+
82+
In order to be effective, this must be set to 0 during or right
83+
after allocation. */
84+
int _initialized;
85+
8086
int recursion_remaining;
8187
int recursion_limit;
8288
int recursion_headroom; /* Allow 50 more calls to handle any errors. */

Include/internal/pycore_interp.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ struct _is {
240240
struct _is *next;
241241

242242
struct pythreads {
243-
int _preallocated_used;
244243
uint64_t next_unique_id;
245244
struct _ts *head;
246245
/* Used in Modules/_threadmodule.c. */
@@ -262,6 +261,11 @@ struct _is {
262261
int requires_idref;
263262
PyThread_type_lock id_mutex;
264263

264+
/* Has been initialized to a safe state.
265+
266+
In order to be effective, this must be set to 0 during or right
267+
after allocation. */
268+
int _initialized;
265269
int finalizing;
266270

267271
struct _ceval_state ceval;

Include/internal/pycore_runtime.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ struct _Py_unicode_runtime_ids {
6767
/* Full Python runtime state */
6868

6969
typedef struct pyruntimestate {
70+
/* Has been initialized to a safe state.
71+
72+
In order to be effective, this must be set to 0 during or right
73+
after allocation. */
74+
int _initialized;
75+
7076
/* Is running Py_PreInitialize()? */
7177
int preinitializing;
7278

@@ -136,9 +142,18 @@ typedef struct pyruntimestate {
136142
} _PyRuntimeState;
137143

138144
#define _PyRuntimeState_INIT \
139-
{.preinitialized = 0, .core_initialized = 0, .initialized = 0}
145+
{ \
146+
._initialized = 0, \
147+
}
140148
/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */
141149

150+
static inline void
151+
_PyRuntimeState_reset(_PyRuntimeState *runtime)
152+
{
153+
/* Make it match _PyRuntimeState_INIT. */
154+
memset(runtime, 0, sizeof(*runtime));
155+
}
156+
142157

143158
PyAPI_DATA(_PyRuntimeState) _PyRuntime;
144159

0 commit comments

Comments
 (0)