[RFC,v2,06/21] gdb/python: add template function to implement equality comparison

Message ID 20241121124714.419946-7-jan.vrany@labware.com
State New
Headers
Series Add Python "JIT" API |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Skipped upon request
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Skipped upon request

Commit Message

Jan Vraný Nov. 21, 2024, 12:46 p.m. UTC
  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 | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
  

Patch

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 <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 */