Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Isolate tp_dict.
  • Loading branch information
ericsnowcurrently committed May 2, 2023
commit 2822f0d32f95ed9d22259a1fbed2d240daef39f6
9 changes: 2 additions & 7 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
Py_ssize_t n_members = count_members(desc, &n_unnamed_members);
PyMemberDef *members = NULL;

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

_Py_SetImmortal(type);
initialized = 0;
}
#ifndef NDEBUG
else {
Expand All @@ -543,13 +541,10 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
desc->name);
goto error;
}
// This should be dropped if tp_dict is made per-interpreter.
if (initialized) {
return 0;
}

if (initialize_structseq_dict(
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0)
{
goto error;
}

Expand Down
28 changes: 23 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ static_builtin_state_clear(PyInterpreterState *interp, PyTypeObject *self)
static inline PyObject *
lookup_tp_dict(PyTypeObject *self)
{
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
assert(state != NULL);
return state->tp_dict;
}
return self->tp_dict;
}

Expand All @@ -184,12 +190,26 @@ _PyType_GetDict(PyTypeObject *self)
static inline void
set_tp_dict(PyTypeObject *self, PyObject *dict)
{
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
assert(state != NULL);
state->tp_dict = dict;
return;
}
self->tp_dict = dict;
}

static inline void
clear_tp_dict(PyTypeObject *self)
{
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
assert(state != NULL);
Py_CLEAR(state->tp_dict);
return;
}
Py_CLEAR(self->tp_dict);
}

Expand Down Expand Up @@ -4738,13 +4758,11 @@ static void
clear_static_type_objects(PyInterpreterState *interp, PyTypeObject *type)
{
if (_Py_IsMainInterpreter(interp)) {
clear_tp_dict(type);
Py_CLEAR(type->tp_cache);
}
else {
clear_tp_bases(type);
clear_tp_mro(type);
}
clear_tp_dict(type);
clear_tp_bases(type);
clear_tp_mro(type);
clear_static_tp_subclasses(type);
}

Expand Down