Skip to content

Commit faa36b8

Browse files
committed
add partial_richcompare for functools.partial
1 parent a0305c5 commit faa36b8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Modules/_functoolsmodule.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,43 @@ static PyMethodDef partial_methods[] = {
477477
{NULL, NULL} /* sentinel */
478478
};
479479

480+
static PyObject *
481+
partial_richcompare(PyObject *self, PyObject *other, int op)
482+
{
483+
partialobject *a, *b;
484+
PyObject *res;
485+
int eq;
486+
487+
if (op != Py_EQ && op != Py_NE) {
488+
Py_RETURN_NOTIMPLEMENTED;
489+
}
490+
491+
a = (partialobject *) self;
492+
b = (partialobject *) other;
493+
494+
eq = PyObject_RichCompareBool(a->fn, b->fn, Py_EQ);
495+
if (eq == 1) {
496+
eq = PyObject_RichCompareBool(a->args, b->args, Py_EQ);
497+
if (eq == 1) {
498+
eq = PyObject_RichCompareBool(a->kw, b->kw, Py_EQ);
499+
}
500+
}
501+
502+
if (eq < 0) {
503+
return NULL;
504+
}
505+
506+
if (op == Py_EQ) {
507+
res = eq ? Py_True : Py_False;
508+
}
509+
else {
510+
res = eq ? Py_False : Py_True;
511+
}
512+
513+
Py_INCREF(res);
514+
return res;
515+
}
516+
480517
static PyType_Slot partial_type_slots[] = {
481518
{Py_tp_dealloc, partial_dealloc},
482519
{Py_tp_repr, partial_repr},
@@ -486,6 +523,7 @@ static PyType_Slot partial_type_slots[] = {
486523
{Py_tp_doc, (void *)partial_doc},
487524
{Py_tp_traverse, partial_traverse},
488525
{Py_tp_clear, partial_clear},
526+
{Py_tp_richcompare, partial_richcompare},
489527
{Py_tp_methods, partial_methods},
490528
{Py_tp_members, partial_memberlist},
491529
{Py_tp_getset, partial_getsetlist},

0 commit comments

Comments
 (0)