From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20861 invoked by alias); 13 May 2010 17:00:52 -0000 Received: (qmail 20853 invoked by uid 22791); 13 May 2010 17:00:51 -0000 X-SWARE-Spam-Status: No, hits=-5.1 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,TW_BJ,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 13 May 2010 17:00:46 +0000 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o4DH0hTO009230 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 13 May 2010 13:00:44 -0400 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o4DH0fct017949 for ; Thu, 13 May 2010 13:00:42 -0400 Message-ID: <4BEC3039.2060400@redhat.com> Date: Thu, 13 May 2010 17:05:00 -0000 From: Phil Muldoon User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100330 Fedora/3.0.4-1.fc12 Lightning/1.0b2pre Thunderbird/3.0.4 MIME-Version: 1.0 To: gdb-patches ml Subject: [python][patch] Return hash value for gdb.Value. Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2010-05/txt/msg00276.txt.bz2 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 * gdb.python/py-value.exp (test_value_hash): New function. 2010-05-13 Phil Muldoon 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.