From patchwork Mon May 23 17:25:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 12474 Received: (qmail 36319 invoked by alias); 23 May 2016 17:25:45 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 36040 invoked by uid 89); 23 May 2016 17:25:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL, BAYES_00, FSL_HELO_HOME, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=no version=3.3.2 spammy=H*m:tromey, H*m:tom X-HELO: gproxy10-pub.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10-pub.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Mon, 23 May 2016 17:25:24 +0000 Received: (qmail 6127 invoked by uid 0); 23 May 2016 17:25:22 -0000 Received: from unknown (HELO cmgw4) (10.0.90.85) by gproxy10.mail.unifiedlayer.com with SMTP; 23 May 2016 17:25:22 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id xtRK1s00j2f2jeq01tRNcz; Mon, 23 May 2016 11:25:22 -0600 X-Authority-Analysis: v=2.1 cv=EftbHpWC c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=PnD2wP_eR3oA:10 a=_v2sUkyEFrwA:10 a=yrkiwgmsf1kA:10 a=zstS-IiYAAAA:8 a=ODKyl3x42Rc6z-HnDqYA:9 a=5JMILemEcPbwR2IJ:21 a=Rlz2es5yXKV-T_pM:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from [71.215.116.141] (port=40406 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_2) (envelope-from ) id 1b4tbT-0007wu-0X; Mon, 23 May 2016 11:25:19 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA v2 2/2] Fix PR python/17386 - add __index__ method to gdb.Value Date: Mon, 23 May 2016 11:25:13 -0600 Message-Id: <1464024313-11932-3-git-send-email-tom@tromey.com> In-Reply-To: <1464024313-11932-1-git-send-email-tom@tromey.com> References: <1464024313-11932-1-git-send-email-tom@tromey.com> X-Identified-User: {36111:box522.bluehost.com:elynrobi:tromey.com} {sentby:smtp auth 71.215.116.141 authed with tom+tromey.com} This patch fixes PR python/17386. The bug is that gdb.Value does not implement the Python __index__ method. This method is needed to convert a Python object to an index and is used by various operations in Python, such as indexing an array. The fix is to implement the nb_index method for gdb.Value. nb_index was added in Python 2.5. I don't have a good way to test Python 2.4, but I made an attempt to accomodate it. I chose to use valpy_long in all cases because this simplifies porting to Python 3, and because there didn't seem to be any harm. Built and regtested on x86-64 Fedora 23. 2016-05-23 Tom Tromey PR python/17386: * python/py-value.c (value_object_as_number): Add nb_inplace_floor_divide, nb_inplace_true_divide, nb_index. 2016-05-23 Tom Tromey PR python/17386: * gdb.python/py-value.exp (test_value_numeric_ops): Add tests that use value as an index. --- gdb/ChangeLog | 6 ++++++ gdb/python/py-value.c | 8 +++++++- gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.python/py-value.exp | 7 +++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 5142429..a848c14 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,11 @@ 2016-05-23 Tom Tromey + PR python/17386: + * python/py-value.c (value_object_as_number): Add + nb_inplace_floor_divide, nb_inplace_true_divide, nb_index. + +2016-05-23 Tom Tromey + * python/py-value.c (value_object_as_number): Add nb_inplace_divide for Python 2. diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 268f6c8..e0b017a 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1838,7 +1838,13 @@ static PyNumberMethods value_object_as_number = { NULL, /* nb_inplace_xor */ NULL, /* nb_inplace_or */ NULL, /* nb_floor_divide */ - valpy_divide /* nb_true_divide */ + valpy_divide, /* nb_true_divide */ + NULL, /* nb_inplace_floor_divide */ + NULL /* nb_inplace_true_divide */ +#ifndef HAVE_LIBPYTHON_2_4 + /* This was added in Python 2.5. */ + , valpy_long /* nb_index */ +#endif /* HAVE_LIBPYTHON_2_4 */ }; static PyMappingMethods value_object_as_mapping = { diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 1f0e3f9..edcf3b2 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2016-05-23 Tom Tromey + PR python/17386: + * gdb.python/py-value.exp (test_value_numeric_ops): Add tests that + use value as an index. + +2016-05-23 Tom Tromey + PR python/19438, PR python/18393: * gdb.python/py-progspace.exp: Add "dir" test. * gdb.python/py-objfile.exp: Add "dir" test. diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index a9dbe97..57a9ba1 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -124,6 +124,13 @@ proc test_value_numeric_ops {} { gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value" gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values" + gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \ + "result = r" "use value as string index" + gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \ + "result = 1" "use value as tuple index" + gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \ + "result = 1" "use value as array index" + # Test some invalid operations. gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {