aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Vrany <jan.vrany@labware.com>2024-11-21 12:31:20 +0000
committerJan Vrany <jan.vrany@labware.com>2024-11-21 13:49:11 +0000
commite8f5211fe5faccc2deee3a2cd4e7d8290e8984ca (patch)
tree9677b558c1b035294fd8b3d0bd7a8d466e8f6c40
parentf350e1aba5dac9a8e3f48425f502e4b69e9e2455 (diff)
downloadfsf-binutils-gdb-e8f5211fe5faccc2deee3a2cd4e7d8290e8984ca.zip
fsf-binutils-gdb-e8f5211fe5faccc2deee3a2cd4e7d8290e8984ca.tar.gz
fsf-binutils-gdb-e8f5211fe5faccc2deee3a2cd4e7d8290e8984ca.tar.bz2
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.
-rw-r--r--gdb/python/python-internal.h33
1 files changed, 33 insertions, 0 deletions
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index d723c4d..4d4810d 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 */