Skip to content

Commit

Permalink
Fix clearing the fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Apr 27, 2023
1 parent 05d4c12 commit 1799453
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ static inline PyObject * lookup_tp_dict(PyTypeObject *type);
static inline void init_tp_dict(PyTypeObject *type, PyObject *dict);
static inline void clear_tp_dict(PyTypeObject *type);
static inline PyObject * lookup_tp_bases(PyTypeObject *type);
static inline void clear_tp_bases(PyTypeObject *type);

int
_PyType_CheckConsistency(PyTypeObject *type)
Expand Down Expand Up @@ -862,6 +863,18 @@ init_tp_bases(PyTypeObject *type, PyObject *bases)
type->tp_bases = bases;
}

static inline void
clear_tp_bases(PyTypeObject *type)
{
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
static_builtin_state *state = _PyStaticType_GetState(type);
assert(state != NULL);
Py_CLEAR(state->tp_bases);
return;
}
Py_CLEAR(type->tp_bases);
}

static PyObject *
type_get_bases(PyTypeObject *type, void *context)
{
Expand Down Expand Up @@ -4586,34 +4599,18 @@ clear_static_tp_subclasses(PyTypeObject *type)
clear_tp_subclasses(type);
}

static void
clear_static_type_interp_data(PyTypeObject *type)
{
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
static_builtin_state *state = _PyStaticType_GetState(type);
assert(state != NULL);

Py_CLEAR(state->tp_dict);
Py_CLEAR(state->tp_bases);
Py_CLEAR(state->tp_mro);
}
else {
Py_CLEAR(type->tp_dict);
Py_CLEAR(type->tp_bases);
Py_CLEAR(type->tp_mro);
}
clear_static_tp_subclasses(type);
}

void
_PyStaticType_Dealloc(PyTypeObject *type)
{
assert(!(type->tp_flags & Py_TPFLAGS_HEAPTYPE));

type_dealloc_common(type);

clear_tp_dict(type);
clear_tp_bases(type);
clear_tp_mro(type);
Py_CLEAR(type->tp_cache);
clear_static_type_interp_data(type);
clear_static_tp_subclasses(type);

// PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
if (Py_REFCNT(type) == 0) {
Expand Down Expand Up @@ -4647,9 +4644,9 @@ type_dealloc(PyTypeObject *type)
PyObject_ClearWeakRefs((PyObject *)type);

Py_XDECREF(type->tp_base);
Py_XDECREF(type->tp_dict);
Py_XDECREF(type->tp_bases);
Py_XDECREF(type->tp_mro);
clear_tp_dict(type);
clear_tp_bases(type);
clear_tp_mro(type);
Py_XDECREF(type->tp_cache);
clear_tp_subclasses(type);

Expand Down

0 comments on commit 1799453

Please sign in to comment.