From patchwork Sat Sep 15 07:24:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29390 Received: (qmail 29913 invoked by alias); 15 Sep 2018 07:25:22 -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 29466 invoked by uid 89); 15 Sep 2018 07:25:07 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=ba X-HELO: gateway22.websitewelcome.com Received: from gateway22.websitewelcome.com (HELO gateway22.websitewelcome.com) (192.185.46.233) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Sep 2018 07:25:05 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway22.websitewelcome.com (Postfix) with ESMTP id D93863F5F for ; Sat, 15 Sep 2018 02:25:03 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 14wxgYQiySjJA14wxgACz1; Sat, 15 Sep 2018 02:25:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=nK9ySLmtXvQxsWwj2aUgwjAbUQBQ0iRkM/FUEOSKJjs=; b=kaUEGZJzjajwJ8aRW7AWwTm8O+ 6zF5SoIuZ4BR3eFYEbusDyKv4fk7DsP0ILgRa+EOpaMF13wwVsC1tHyMEuwO1N/F5D7r1DFe4W989 94Jt7c6w6TzknJc+DNx+zyqoy; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:41846 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1g14wx-00253W-ML; Sat, 15 Sep 2018 02:25:03 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 3/7] Allow conversion of pointers to Python int Date: Sat, 15 Sep 2018 01:24:55 -0600 Message-Id: <20180915072459.14934-4-tom@tromey.com> In-Reply-To: <20180915072459.14934-1-tom@tromey.com> References: <20180915072459.14934-1-tom@tromey.com> PR python/18170 questions why it's not possible to convert a pointer value to a Python int. Digging a bit shows that the Python 2.7 int() constructor will happily return a long in some cases. And, it seems gdb already understands this in other places -- this is why gdb_py_object_from_longest handles. So, this patch simply extends valpy_int to allow pointer conversions, as valpy_long does. gdb/ChangeLog 2018-09-14 Tom Tromey PR python/18170: * python/py-value.c (valpy_int): Allow conversion from pointer type. gdb/testsuite/ChangeLog 2018-09-14 Tom Tromey PR python/18170: * gdb.python/py-value.exp (test_value_numeric_ops): Add tests to convert pointers to int and long. --- gdb/ChangeLog | 6 ++++++ gdb/python/py-value.c | 3 ++- gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.python/py-value.exp | 3 +++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 5c6792f85fc..26e91ff2b27 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1503,7 +1503,8 @@ valpy_int (PyObject *self) value = value_cast (type, value); } - if (!is_integral_type (type)) + if (!is_integral_type (type) + && TYPE_CODE (type) != TYPE_CODE_PTR) error (_("Cannot convert value to int.")); l = value_as_long (value); diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index aed50d1c8cf..222cf1dea6f 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -129,6 +129,9 @@ proc test_value_numeric_ops {} { gdb_test "print (void *) 5" ".*" "" gdb_test_no_output "python b = gdb.history (0)" "" + gdb_test "python print(int(b))" "5" "convert pointer to int" + gdb_test "python print(long(b))" "5" "convert pointer to long" + gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer" 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"