Skip to content

Commit

Permalink
bpo-42972: Fully support GC protocol for _operator heap types (GH-26371
Browse files Browse the repository at this point in the history
…) (GH-26413)

(cherry picked from commit f4b70c2)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
  • Loading branch information
miss-islington and Erlend Egeberg Aasland committed May 27, 2021
1 parent 0574b06 commit d1c7329
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,19 +1004,27 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)ig;
}

static int
itemgetter_clear(itemgetterobject *ig)
{
Py_CLEAR(ig->item);
return 0;
}

static void
itemgetter_dealloc(itemgetterobject *ig)
{
PyTypeObject *tp = Py_TYPE(ig);
PyObject_GC_UnTrack(ig);
Py_XDECREF(ig->item);
(void)itemgetter_clear(ig);
tp->tp_free(ig);
Py_DECREF(tp);
}

static int
itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(ig));
Py_VISIT(ig->item);
return 0;
}
Expand Down Expand Up @@ -1113,6 +1121,7 @@ static PyType_Slot itemgetter_type_slots[] = {
{Py_tp_dealloc, itemgetter_dealloc},
{Py_tp_call, itemgetter_call},
{Py_tp_traverse, itemgetter_traverse},
{Py_tp_clear, itemgetter_clear},
{Py_tp_methods, itemgetter_methods},
{Py_tp_new, itemgetter_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
Expand Down Expand Up @@ -1250,12 +1259,19 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)ag;
}

static int
attrgetter_clear(attrgetterobject *ag)
{
Py_CLEAR(ag->attr);
return 0;
}

static void
attrgetter_dealloc(attrgetterobject *ag)
{
PyTypeObject *tp = Py_TYPE(ag);
PyObject_GC_UnTrack(ag);
Py_XDECREF(ag->attr);
(void)attrgetter_clear(ag);
tp->tp_free(ag);
Py_DECREF(tp);
}
Expand All @@ -1264,6 +1280,7 @@ static int
attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
{
Py_VISIT(ag->attr);
Py_VISIT(Py_TYPE(ag));
return 0;
}

Expand Down Expand Up @@ -1435,6 +1452,7 @@ static PyType_Slot attrgetter_type_slots[] = {
{Py_tp_dealloc, attrgetter_dealloc},
{Py_tp_call, attrgetter_call},
{Py_tp_traverse, attrgetter_traverse},
{Py_tp_clear, attrgetter_clear},
{Py_tp_methods, attrgetter_methods},
{Py_tp_new, attrgetter_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
Expand Down Expand Up @@ -1505,23 +1523,32 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)mc;
}

static int
methodcaller_clear(methodcallerobject *mc)
{
Py_CLEAR(mc->name);
Py_CLEAR(mc->args);
Py_CLEAR(mc->kwds);
return 0;
}

static void
methodcaller_dealloc(methodcallerobject *mc)
{
PyTypeObject *tp = Py_TYPE(mc);
PyObject_GC_UnTrack(mc);
Py_XDECREF(mc->name);
Py_XDECREF(mc->args);
Py_XDECREF(mc->kwds);
(void)methodcaller_clear(mc);
tp->tp_free(mc);
Py_DECREF(tp);
}

static int
methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
{
Py_VISIT(mc->name);
Py_VISIT(mc->args);
Py_VISIT(mc->kwds);
Py_VISIT(Py_TYPE(mc));
return 0;
}

Expand Down Expand Up @@ -1680,6 +1707,7 @@ static PyType_Slot methodcaller_type_slots[] = {
{Py_tp_dealloc, methodcaller_dealloc},
{Py_tp_call, methodcaller_call},
{Py_tp_traverse, methodcaller_traverse},
{Py_tp_clear, methodcaller_clear},
{Py_tp_methods, methodcaller_methods},
{Py_tp_new, methodcaller_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
Expand Down

0 comments on commit d1c7329

Please sign in to comment.