]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: add template function to implement equality comparison
authorJan Vrany <jan.vrany@labware.com>
Thu, 21 Nov 2024 12:31:20 +0000 (12:31 +0000)
committerJan Vrany <jan.vrany@labware.com>
Thu, 21 Nov 2024 13:49:11 +0000 (13:49 +0000)
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.

gdb/python/python-internal.h

index d723c4d577b1d4ccade84ad15b07a340c234b5bc..4d4810dc4cca87107a5644384b0bd978a63379f6 100644 (file)
@@ -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 <typename P, typename D, D* P::*data>
+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 */