From: Jan Vrany Date: Thu, 21 Nov 2024 12:31:20 +0000 (+0000) Subject: gdb/python: add template function to implement equality comparison X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e8f5211fe5faccc2deee3a2cd4e7d8290e8984ca;p=thirdparty%2Fbinutils-gdb.git gdb/python: add template function to implement equality comparison This commit adds gdbpy_richcompare template to implement "default" equality and non-equality comparison for GDB Python objects. The "default" behavior is to consider two GDB Python objects as equal if both refer to the same underlying GDB structure. --- diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d723c4d577b..4d4810dc4cc 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1145,4 +1145,37 @@ gdbpy_type_ready (PyTypeObject *type, PyObject *mod = nullptr) # define PyType_Ready POISONED_PyType_Ready #endif +/* Implements default equality and non-equality comparisons for GDB + Python objects. + + All other comparison operators will throw a TypeError Python exception. + + Two Python objects of type P are considered equal if both point to the + same underlying GBB structure of type D. The last template parameter + specifies the member of Python object holding reference to underlying + GBB structure. */ + +template +PyObject * +gdbpy_richcompare (PyObject *self, PyObject *other, int op) +{ + int result; + + if (!PyObject_TypeCheck (other, self->ob_type) + || (op != Py_EQ && op != Py_NE)) + { + Py_INCREF (Py_NotImplemented); + return Py_NotImplemented; + } + + if ( (P *)self->*data == (P *)other->*data) + result = Py_EQ; + else + result = Py_NE; + + if (op == result) + Py_RETURN_TRUE; + Py_RETURN_FALSE; +} + #endif /* PYTHON_PYTHON_INTERNAL_H */