Skip to content

Commit 2822f0d

Browse files
Isolate tp_dict.
1 parent fb7a025 commit 2822f0d

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Objects/structseq.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
511511
Py_ssize_t n_members = count_members(desc, &n_unnamed_members);
512512
PyMemberDef *members = NULL;
513513

514-
int initialized = 1;
515514
if ((type->tp_flags & Py_TPFLAGS_READY) == 0) {
516515
assert(type->tp_name == NULL);
517516
assert(type->tp_members == NULL);
@@ -524,7 +523,6 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
524523
initialize_static_fields(type, desc, members, tp_flags);
525524

526525
_Py_SetImmortal(type);
527-
initialized = 0;
528526
}
529527
#ifndef NDEBUG
530528
else {
@@ -543,13 +541,10 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
543541
desc->name);
544542
goto error;
545543
}
546-
// This should be dropped if tp_dict is made per-interpreter.
547-
if (initialized) {
548-
return 0;
549-
}
550544

551545
if (initialize_structseq_dict(
552-
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
546+
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0)
547+
{
553548
goto error;
554549
}
555550

Objects/typeobject.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ static_builtin_state_clear(PyInterpreterState *interp, PyTypeObject *self)
172172
static inline PyObject *
173173
lookup_tp_dict(PyTypeObject *self)
174174
{
175+
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
176+
PyInterpreterState *interp = _PyInterpreterState_GET();
177+
static_builtin_state *state = _PyStaticType_GetState(interp, self);
178+
assert(state != NULL);
179+
return state->tp_dict;
180+
}
175181
return self->tp_dict;
176182
}
177183

@@ -184,12 +190,26 @@ _PyType_GetDict(PyTypeObject *self)
184190
static inline void
185191
set_tp_dict(PyTypeObject *self, PyObject *dict)
186192
{
193+
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
194+
PyInterpreterState *interp = _PyInterpreterState_GET();
195+
static_builtin_state *state = _PyStaticType_GetState(interp, self);
196+
assert(state != NULL);
197+
state->tp_dict = dict;
198+
return;
199+
}
187200
self->tp_dict = dict;
188201
}
189202

190203
static inline void
191204
clear_tp_dict(PyTypeObject *self)
192205
{
206+
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
207+
PyInterpreterState *interp = _PyInterpreterState_GET();
208+
static_builtin_state *state = _PyStaticType_GetState(interp, self);
209+
assert(state != NULL);
210+
Py_CLEAR(state->tp_dict);
211+
return;
212+
}
193213
Py_CLEAR(self->tp_dict);
194214
}
195215

@@ -4738,13 +4758,11 @@ static void
47384758
clear_static_type_objects(PyInterpreterState *interp, PyTypeObject *type)
47394759
{
47404760
if (_Py_IsMainInterpreter(interp)) {
4741-
clear_tp_dict(type);
47424761
Py_CLEAR(type->tp_cache);
47434762
}
4744-
else {
4745-
clear_tp_bases(type);
4746-
clear_tp_mro(type);
4747-
}
4763+
clear_tp_dict(type);
4764+
clear_tp_bases(type);
4765+
clear_tp_mro(type);
47484766
clear_static_tp_subclasses(type);
47494767
}
47504768

0 commit comments

Comments
 (0)