Skip to content
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
Erlend E. Aasland committed May 26, 2021
commit 6d7cdaf5d07853711b444ba755250ddd23e359a9
36 changes: 8 additions & 28 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,45 +762,25 @@ typedef struct lru_list_elem {
PyObject *key, *result;
} lru_list_elem;

static int
lru_list_elem_clear(lru_list_elem *link)
{
Py_CLEAR(link->key);
Py_CLEAR(link->result);
return 0;
}

static int
lru_list_elem_traverse(lru_list_elem *link, visitproc visit, void *arg)
{
Py_VISIT(link->key);
Py_VISIT(link->result);
Py_VISIT(Py_TYPE(link));
return 0;
}

static void
lru_list_elem_dealloc(lru_list_elem *link)
{
PyTypeObject *tp = Py_TYPE(link);
PyObject_GC_UnTrack(link);
(void)lru_list_elem_clear(link);
PyObject_GC_Del(link);
Py_CLEAR(link->key);
Py_CLEAR(link->result);
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
tp->tp_free(link);
Comment thread
erlend-aasland marked this conversation as resolved.
Py_DECREF(tp);
}

static PyType_Slot lru_list_elem_type_slots[] = {
{Py_tp_dealloc, lru_list_elem_dealloc},
{Py_tp_traverse, lru_list_elem_traverse},
{Py_tp_clear, lru_list_elem_clear},
{0, 0}
};

static PyType_Spec lru_list_elem_type_spec = {
.name = "functools._lru_list_elem",
.basicsize = sizeof(lru_list_elem),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_HAVE_GC),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = lru_list_elem_type_slots
};

Expand Down Expand Up @@ -1065,8 +1045,8 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
self->root.next == &self->root)
{
/* Cache is not full, so put the result in a new link */
link = (lru_list_elem *)PyObject_GC_New(lru_list_elem,
self->lru_list_elem_type);
link = (lru_list_elem *)PyObject_New(lru_list_elem,
self->lru_list_elem_type);
if (link == NULL) {
Py_DECREF(key);
Py_DECREF(result);
Expand All @@ -1076,7 +1056,6 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
link->hash = hash;
link->key = key;
link->result = result;
PyObject_GC_Track(link);
/* What is really needed here is a SetItem variant with a "no clobber"
option. If the __eq__ call triggers a reentrant call that adds
this same key, then this setitem call will update the cache dict
Expand Down Expand Up @@ -1365,7 +1344,8 @@ lru_cache_tp_traverse(lru_cache_object *self, visitproc visit, void *arg)
lru_list_elem *link = self->root.next;
while (link != &self->root) {
lru_list_elem *next = link->next;
Comment thread
erlend-aasland marked this conversation as resolved.
Py_VISIT(link);
Py_VISIT(link->key);
Py_VISIT(link->result);
link = next;
}
Py_VISIT(self->func);
Expand Down