Skip to content

Commit dc3e435

Browse files
GH-99205: remove _static field from PyThreadState and PyInterpreterState (GH-99385)
1 parent e874c2f commit dc3e435

File tree

4 files changed

+6
-18
lines changed

4 files changed

+6
-18
lines changed

Include/cpython/pystate.h

-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ struct _ts {
120120
after allocation. */
121121
int _initialized;
122122

123-
/* Was this thread state statically allocated? */
124-
int _static;
125-
126123
int py_recursion_remaining;
127124
int py_recursion_limit;
128125

Include/internal/pycore_interp.h

-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ struct _is {
116116
int _initialized;
117117
int finalizing;
118118

119-
/* Was this interpreter statically allocated? */
120-
bool _static;
121-
122119
struct _ceval_state ceval;
123120
struct _gc_runtime_state gc;
124121

Include/internal/pycore_runtime_init.h

-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ extern "C" {
8383

8484
#define _PyInterpreterState_INIT \
8585
{ \
86-
._static = 1, \
8786
.id_refcount = -1, \
8887
DLOPENFLAGS_INIT \
8988
.ceval = { \
@@ -108,7 +107,6 @@ extern "C" {
108107

109108
#define _PyThreadState_INIT \
110109
{ \
111-
._static = 1, \
112110
.py_recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \
113111
.context_ver = 1, \
114112
}

Python/pystate.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ alloc_interpreter(void)
275275
static void
276276
free_interpreter(PyInterpreterState *interp)
277277
{
278-
if (!interp->_static) {
278+
// The main interpreter is statically allocated so
279+
// should not be freed.
280+
if (interp != &_PyRuntime._main_interpreter) {
279281
PyMem_RawFree(interp);
280282
}
281283
}
@@ -359,7 +361,6 @@ PyInterpreterState_New(void)
359361
interp = &runtime->_main_interpreter;
360362
assert(interp->id == 0);
361363
assert(interp->next == NULL);
362-
assert(interp->_static);
363364

364365
interpreters->main = interp;
365366
}
@@ -374,9 +375,6 @@ PyInterpreterState_New(void)
374375
// Set to _PyInterpreterState_INIT.
375376
memcpy(interp, &initial._main_interpreter,
376377
sizeof(*interp));
377-
// We need to adjust any fields that are different from the initial
378-
// interpreter (as defined in _PyInterpreterState_INIT):
379-
interp->_static = false;
380378

381379
if (id < 0) {
382380
/* overflow or Py_Initialize() not called yet! */
@@ -762,7 +760,9 @@ alloc_threadstate(void)
762760
static void
763761
free_threadstate(PyThreadState *tstate)
764762
{
765-
if (!tstate->_static) {
763+
// The initial thread state of the interpreter is allocated
764+
// as part of the interpreter state so should not be freed.
765+
if (tstate != &tstate->interp->_initial_thread) {
766766
PyMem_RawFree(tstate);
767767
}
768768
}
@@ -845,7 +845,6 @@ new_threadstate(PyInterpreterState *interp)
845845
assert(id == 1);
846846
used_newtstate = 0;
847847
tstate = &interp->_initial_thread;
848-
assert(tstate->_static);
849848
}
850849
else {
851850
// Every valid interpreter must have at least one thread.
@@ -857,9 +856,6 @@ new_threadstate(PyInterpreterState *interp)
857856
memcpy(tstate,
858857
&initial._main_interpreter._initial_thread,
859858
sizeof(*tstate));
860-
// We need to adjust any fields that are different from the initial
861-
// thread (as defined in _PyThreadState_INIT):
862-
tstate->_static = false;
863859
}
864860
interp->threads.head = tstate;
865861

0 commit comments

Comments
 (0)