[RFC,v2,07/21] gdb/python: make gdb.Symbol comparable for equality

Message ID 20241121124714.419946-8-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:47 p.m. UTC
  In subsequent patches, it will be useful to be able to compare two
gdb.Symbols. This patch makes gdb.Symbols equal iff both refer to the
same underlying struct symtab.
---
 gdb/python/py-symbol.c                 |  3 ++-
 gdb/testsuite/gdb.python/py-symbol.exp | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 38eead9cb3a..44bed85481b 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -767,7 +767,8 @@  PyTypeObject symbol_object_type = {
   "GDB symbol object",		  /*tp_doc */
   0,				  /*tp_traverse */
   0,				  /*tp_clear */
-  0,				  /*tp_richcompare */
+  gdbpy_richcompare<symbol_object, symbol, &symbol_object::symbol>,
+				    /*tp_richcompare */
   0,				  /*tp_weaklistoffset */
   0,				  /*tp_iter */
   0,				  /*tp_iternext */
diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp
index 2a74c97a137..1bfa17b4e91 100644
--- a/gdb/testsuite/gdb.python/py-symbol.exp
+++ b/gdb/testsuite/gdb.python/py-symbol.exp
@@ -201,6 +201,27 @@  if { [is_remote host] } {
 }
 gdb_test "python print (t\[0\].symtab)" "${py_symbol_c}" "get symtab"
 
+# Test comparison for equality and non-equality
+gdb_test "python print (t\[0\] == t\[0\] )"\
+	 "True" \
+	 "test symbol equality with itself"
+gdb_test "python print (t\[0\] == a\[0\] )"\
+	 "False" \
+	 "test symbol equality with other symbol"
+gdb_test "python print (t\[0\] == 123 )"\
+	 "False" \
+	 "test symbol equality with non-symbol"
+
+gdb_test "python print (t\[0\] != t\[0\] )"\
+	 "False" \
+	 "test symbol non-equality with itself"
+gdb_test "python print (t\[0\] != a\[0\] )"\
+	 "True" \
+	 "test symbol non-equality with other symbol"
+gdb_test "python print (t\[0\] != 123 )"\
+	 "True" \
+	 "test symbol non-equality with non-symbol"
+
 # C++ tests
 # Recompile binary.
 lappend opts c++