Implement __eq__ for Cursor

This commit is contained in:
Kovid Goyal 2016-10-31 23:23:40 +05:30
parent a5368314af
commit 8dda11bee9

View File

@ -40,6 +40,12 @@ Cursor_dealloc(Cursor* self) {
Py_TYPE(self)->tp_free((PyObject*)self);
}
#define EQ(x) (a->x == b->x)
#define PEQ(x) (PyObject_RichCompareBool(a->x, b->x, Py_EQ) == 1)
int is_eq(Cursor *a, Cursor *b) {
return EQ(bold) && EQ(italic) && EQ(strikethrough) && EQ(reverse) && EQ(decoration) && EQ(fg) && EQ(bg) && EQ(decoration_fg) && PEQ(x) && PEQ(y) && PEQ(shape) && PEQ(blink) && PEQ(color) && PEQ(hidden);
}
// Boilerplate {{{
static PyMemberDef Cursor_members[] = {
@ -49,6 +55,15 @@ static PyMemberDef Cursor_members[] = {
{"blink", T_OBJECT_EX, offsetof(Cursor, blink), 0, "blink"},
{"color", T_OBJECT_EX, offsetof(Cursor, color), 0, "color"},
{"hidden", T_OBJECT_EX, offsetof(Cursor, hidden), 0, "hidden"},
{"bold", T_UBYTE, offsetof(Cursor, bold), 0, "bold"},
{"italic", T_UBYTE, offsetof(Cursor, italic), 0, "italic"},
{"strikethrough", T_UBYTE, offsetof(Cursor, strikethrough), 0, "strikethrough"},
{"reverse", T_UBYTE, offsetof(Cursor, reverse), 0, "reverse"},
{"decoration", T_UBYTE, offsetof(Cursor, decoration), 0, "decoration"},
{"fg", T_UINT, offsetof(Cursor, fg), 0, "fg"},
{"bg", T_UINT, offsetof(Cursor, bg), 0, "bg"},
{"decoration_fg", T_UINT, offsetof(Cursor, decoration_fg), 0, "decoration_fg"},
{NULL} /* Sentinel */
};
@ -56,6 +71,10 @@ static PyMethodDef Cursor_methods[] = {
{NULL} /* Sentinel */
};
static PyObject *
richcmp(PyObject *obj1, PyObject *obj2, int op);
PyTypeObject Cursor_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"fast_data_types.Cursor",
@ -80,7 +99,7 @@ PyTypeObject Cursor_Type = {
"Cursors", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
richcmp, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
@ -96,5 +115,22 @@ PyTypeObject Cursor_Type = {
0, /* tp_alloc */
Cursor_new, /* tp_new */
};
// }}
// }}}
static PyObject *
richcmp(PyObject *obj1, PyObject *obj2, int op)
{
PyObject *result = NULL;
int eq;
if (op != Py_EQ || op != Py_NE) { Py_RETURN_NOTIMPLEMENTED; }
if (!PyObject_TypeCheck(obj1, &Cursor_Type)) { Py_RETURN_FALSE; }
if (!PyObject_TypeCheck(obj2, &Cursor_Type)) { Py_RETURN_FALSE; }
eq = is_eq((Cursor*)obj1, (Cursor*)obj2);
if (op == Py_NE) result = eq ? Py_False : Py_True;
else result = eq ? Py_True : Py_False;
Py_INCREF(result);
return result;
}