Skip to content

Commit 7d5e366

Browse files
committed
add comparison functions based on tp_richcompare
1 parent 88f5c2c commit 7d5e366

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

graalpython/com.oracle.graal.python.cext/src/typeobject.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ static PyObject* wrap_richcmpfunc(richcmpfunc f, PyObject* a, PyObject* b, PyObj
103103
return f(a, b, (int)PyLong_AsLong(n));
104104
}
105105

106+
#undef RICHCMP_WRAPPER
107+
#define RICHCMP_WRAPPER(NAME, OP) \
108+
static PyObject* wrap_richcmpfunc_##NAME(richcmpfunc f, \
109+
PyObject* a, \
110+
PyObject* b) { \
111+
return f(a, b, OP); \
112+
}
113+
114+
RICHCMP_WRAPPER(lt, Py_LT)
115+
RICHCMP_WRAPPER(le, Py_LE)
116+
RICHCMP_WRAPPER(eq, Py_EQ)
117+
RICHCMP_WRAPPER(ne, Py_NE)
118+
RICHCMP_WRAPPER(gt, Py_GT)
119+
RICHCMP_WRAPPER(ge, Py_GE)
120+
106121
static PyObject* wrap_ssizeobjargproc(ssizeobjargproc f, PyObject* a, PyObject* size, PyObject* b) {
107122
return PyLong_FromLong(f(a, PyLong_AsSsize_t(size), b));
108123
}
@@ -315,7 +330,15 @@ int PyType_Ready(PyTypeObject* cls) {
315330
ADD_SLOT("__getattr__", cls->tp_getattro, -2);
316331
ADD_SLOT_CONV("__setattr__", wrap_setattrofunc, cls->tp_setattro, -3);
317332
ADD_SLOT("__clear__", cls->tp_clear, -1);
318-
ADD_SLOT_CONV("__compare__", wrap_richcmpfunc, cls->tp_richcompare, -3);
333+
if (cls->tp_richcompare) {
334+
ADD_SLOT_CONV("__compare__", wrap_richcmpfunc, cls->tp_richcompare, -3);
335+
ADD_SLOT_CONV("__lt__", wrap_richcmpfunc_lt, cls->tp_richcompare, -2);
336+
ADD_SLOT_CONV("__le__", wrap_richcmpfunc_le, cls->tp_richcompare, -2);
337+
ADD_SLOT_CONV("__eq__", wrap_richcmpfunc_eq, cls->tp_richcompare, -2);
338+
ADD_SLOT_CONV("__ne__", wrap_richcmpfunc_ne, cls->tp_richcompare, -2);
339+
ADD_SLOT_CONV("__gt__", wrap_richcmpfunc_gt, cls->tp_richcompare, -2);
340+
ADD_SLOT_CONV("__ge__", wrap_richcmpfunc_ge, cls->tp_richcompare, -2);
341+
}
319342
ADD_SLOT("__iter__", cls->tp_iter, -1);
320343
ADD_SLOT("__next__", cls->tp_iternext, -1);
321344
ADD_SLOT("__get__", cls->tp_descr_get, -3);

0 commit comments

Comments
 (0)