Skip to content

Commit 652fcc6

Browse files
Factor out static_builtin_state_init() and static_builtin_state_clear().
1 parent 1bd26c4 commit 652fcc6

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

Objects/typeobject.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static_builtin_index_clear(PyTypeObject *self)
9898
self->tp_static_builtin_index = 0;
9999
}
100100

101+
/* For static types we store some state in an array on each interpreter. */
101102
static_builtin_type_state *
102103
_PyStaticType_GetState(PyTypeObject *self)
103104
{
@@ -109,6 +110,38 @@ _PyStaticType_GetState(PyTypeObject *self)
109110
return &(interp->types.builtins[static_builtin_index_get(self)]);
110111
}
111112

113+
static void
114+
static_builtin_state_init(PyTypeObject *self)
115+
{
116+
/* It should only be called once for each builtin type. */
117+
assert(!static_builtin_index_is_set(self));
118+
119+
PyInterpreterState *interp = _PyInterpreterState_GET();
120+
static_builtin_index_set(self, interp->types.num_builtins_initialized);
121+
interp->types.num_builtins_initialized++;
122+
123+
/* Now we initialize the type's per-interpreter state. */
124+
static_builtin_type_state *state = _PyStaticType_GetState(self);
125+
assert(state != NULL);
126+
state->type = self;
127+
}
128+
129+
static void
130+
static_builtin_state_clear(PyTypeObject *self)
131+
{
132+
/* Reset the type's per-interpreter state.
133+
This basically undoes what static_builtin_state_init() did. */
134+
static_builtin_type_state *state = _PyStaticType_GetState(self);
135+
assert(state != NULL);
136+
state->type = NULL;
137+
static_builtin_index_clear(self);
138+
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
139+
140+
PyInterpreterState *interp = _PyInterpreterState_GET();
141+
assert(interp->types.num_builtins_initialized > 0);
142+
interp->types.num_builtins_initialized--;
143+
}
144+
112145
// Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
113146

114147
/* end static builtin helpers */
@@ -4297,7 +4330,6 @@ clear_static_tp_subclasses(PyTypeObject *type)
42974330
Py_CLEAR(type->tp_subclasses);
42984331
}
42994332

4300-
43014333
void
43024334
_PyStaticType_Dealloc(PyTypeObject *type)
43034335
{
@@ -4319,17 +4351,7 @@ _PyStaticType_Dealloc(PyTypeObject *type)
43194351
type->tp_flags &= ~Py_TPFLAGS_READY;
43204352

43214353
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
4322-
/* Reset the type's per-interpreter state.
4323-
This basically undoes what _PyStaticType_InitBuiltin() did. */
4324-
static_builtin_type_state *state = _PyStaticType_GetState(type);
4325-
assert(state != NULL);
4326-
state->type = NULL;
4327-
static_builtin_index_clear(type);
4328-
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
4329-
4330-
PyInterpreterState *interp = _PyInterpreterState_GET();
4331-
assert(interp->types.num_builtins_initialized > 0);
4332-
interp->types.num_builtins_initialized--;
4354+
static_builtin_state_clear(type);
43334355
}
43344356
}
43354357

@@ -6750,18 +6772,7 @@ _PyStaticType_InitBuiltin(PyTypeObject *self)
67506772
{
67516773
self->tp_flags = self->tp_flags | _Py_TPFLAGS_STATIC_BUILTIN;
67526774

6753-
/* It should only be called once for each builtin type. */
6754-
assert(!static_builtin_index_is_set(self));
6755-
6756-
/* For static types we store some state in an array on each interpreter. */
6757-
PyInterpreterState *interp = _PyInterpreterState_GET();
6758-
static_builtin_index_set(self, interp->types.num_builtins_initialized);
6759-
interp->types.num_builtins_initialized++;
6760-
6761-
/* Now we initialize the type's per-interpreter state. */
6762-
static_builtin_type_state *state = _PyStaticType_GetState(self);
6763-
assert(state != NULL);
6764-
state->type = self;
6775+
static_builtin_state_init(self);
67656776

67666777
return PyType_Ready(self);
67676778
}

0 commit comments

Comments
 (0)