Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [python][patch] Return hash value for gdb.Value.
@ 2010-05-13 17:05 Phil Muldoon
  2010-05-13 17:18 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Muldoon @ 2010-05-13 17:05 UTC (permalink / raw)
  To: gdb-patches ml

This patch implements the hash function for gdb.Value. It purely replicates
the function of id() in that it returns the object address as the hash
value. This allows gdb.Values to be hashable.

Ok?

Cheers,

Phil

--

ChangeLogs


2010-05-13  Phil Muldoon  <pmuldoon@redhat.com>

	* gdb.python/py-value.exp (test_value_hash): New function.

2010-05-13  Phil Muldoon  <pmuldoon@redhat.com>

	PR python/11482

	* python/py-value.c (valpy_hash): New function.
	(value_object_type): Register valpy_hash.

---

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 173b3c9..2a7f76b 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -432,6 +432,13 @@ valpy_get_is_optimized_out (PyObject *self, void *closure)
   Py_RETURN_FALSE;
 }
 
+/* Calculate and return the address of the PyObject as the value of
+   the builtin __hash__ call.  */
+long valpy_hash (PyObject *self)
+{
+  return (long) (intptr_t) self;
+}
+
 enum valpy_opcode
 {
   VALPY_ADD,
@@ -1097,7 +1104,7 @@ PyTypeObject value_object_type = {
   &value_object_as_number,	  /*tp_as_number*/
   0,				  /*tp_as_sequence*/
   &value_object_as_mapping,	  /*tp_as_mapping*/
-  0,				  /*tp_hash */
+  valpy_hash,		          /*tp_hash*/
   0,				  /*tp_call*/
   valpy_str,			  /*tp_str*/
   0,				  /*tp_getattro*/
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 3bfa173..67a2ff9 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -393,6 +393,23 @@ proc test_parse_and_eval {} {
     "parse_and_eval type test"
 }
 
+# Test that values are hashable.
+proc test_value_hash {} {
+  gdb_py_test_multiple "Simple Python value dictionary" \
+    "python" "" \
+    "one = gdb.Value(1)" "" \
+    "two = gdb.Value(2)" "" \
+    "three = gdb.Value(3)" "" \
+    "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \
+    "end"
+    gdb_test "python print vdict\[one\]" "one str" "Test dictionary hash"
+    gdb_test "python print vdict\[two\]" "two str" "Test dictionary hash"
+    gdb_test "python print vdict\[three\]" "three str" "Test dictionary hash"
+    gdb_test "python print one.__hash__() == hash(one)" "True" "Test inbuilt hash"
+    gdb_test "python print one.__hash__() == id(one)" "True" "Test inbuilt id"
+}
+
+
 # Start with a fresh gdb.
 
 gdb_exit
@@ -409,6 +426,7 @@ test_value_boolean
 test_value_compare
 test_objfiles
 test_parse_and_eval
+test_value_hash
 
 # The following tests require execution.
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [python][patch] Return hash value for gdb.Value.
  2010-05-13 17:05 [python][patch] Return hash value for gdb.Value Phil Muldoon
@ 2010-05-13 17:18 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2010-05-13 17:18 UTC (permalink / raw)
  To: Phil Muldoon; +Cc: gdb-patches ml

>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

Phil> This patch implements the hash function for gdb.Value. It purely
Phil> replicates the function of id() in that it returns the object
Phil> address as the hash value. This allows gdb.Values to be hashable.

Thanks for doing this.

Phil> 2010-05-13  Phil Muldoon  <pmuldoon@redhat.com>

Phil> 	* gdb.python/py-value.exp (test_value_hash): New function.

Mention the PR number here too.

Phil> +long valpy_hash (PyObject *self)

Should be 'static'.
Newline after 'long'.

Ok with those changes.

Tom


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-05-13 17:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-13 17:05 [python][patch] Return hash value for gdb.Value Phil Muldoon
2010-05-13 17:18 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox