From patchwork Sat Sep 15 07:24:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29391 Received: (qmail 29990 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 29456 invoked by uid 89); 15 Sep 2018 07:25:06 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 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=value_contents X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.49.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Sep 2018 07:25:04 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 5022D4F67 for ; Sat, 15 Sep 2018 02:25:03 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 14wxgjpUUaSey14wxgAhLH; 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=zpTZEZIYtuOs1yPmfzVVhqAiVewKXZVM6YANU78ozl4=; b=B+iRwokH3ZDYd9DQfb9YJ00/P6 XB0chVt6vhHjqUEnLHBuox54EGrmfufnBIv1+E33Hkn6d7afOztlKcO3IdhKjVVMHAkYgofJ/rZ04 YcoixDQFxvA5WW8NHrJ2wSTzu; 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-2L; Sat, 15 Sep 2018 02:25:03 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 1/7] Allow more Python scalar conversions Date: Sat, 15 Sep 2018 01:24:53 -0600 Message-Id: <20180915072459.14934-2-tom@tromey.com> In-Reply-To: <20180915072459.14934-1-tom@tromey.com> References: <20180915072459.14934-1-tom@tromey.com> PR python/18352 points out that the gdb Python code can't convert an integer-valued gdb.Value to a Python float. While writing the test I noticed that, similarly, converting integer gdb.Values to float does not work. However, all of these cases seem reasonable. gdb/ChangeLog 2018-09-14 Tom Tromey PR python/18352; * python/py-value.c (valpy_float): Allow conversions from int or char. (valpy_int, valpy_long): Allow conversions from float. gdb/testsuite/ChangeLog 2018-09-14 Tom Tromey PR python/18352; * gdb.python/py-value.exp (test_float_conversion): New proc. Use it. --- gdb/ChangeLog | 7 +++++++ gdb/python/py-value.c | 25 ++++++++++++++++++++++--- gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.python/py-value.exp | 10 ++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 1b880fa1917..9abcf9212e6 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1497,6 +1497,12 @@ valpy_int (PyObject *self) TRY { + if (is_floating_value (value)) + { + type = builtin_type_pylong; + value = value_cast (type, value); + } + if (!is_integral_type (type)) error (_("Cannot convert value to int.")); @@ -1522,6 +1528,12 @@ valpy_long (PyObject *self) TRY { + if (is_floating_value (value)) + { + type = builtin_type_pylong; + value = value_cast (type, value); + } + type = check_typedef (type); if (!is_integral_type (type) @@ -1554,10 +1566,17 @@ valpy_float (PyObject *self) { type = check_typedef (type); - if (TYPE_CODE (type) != TYPE_CODE_FLT || !is_floating_value (value)) + if (TYPE_CODE (type) == TYPE_CODE_FLT && is_floating_value (value)) + d = target_float_to_host_double (value_contents (value), type); + else if (TYPE_CODE (type) == TYPE_CODE_INT) + { + /* Note that valpy_long accepts TYPE_CODE_PTR and some + others here here -- but casting a pointer or bool to a + float seems wrong. */ + d = value_as_long (value); + } + else error (_("Cannot convert value to float.")); - - d = target_float_to_host_double (value_contents (value), type); } CATCH (except, RETURN_MASK_ALL) { diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index eb82a7776fa..9e8fa15c28e 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -483,6 +483,15 @@ proc test_value_hash {} { gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash" } +proc test_float_conversion {} { + gdb_test "python print int(gdb.Value(0))" "0" + gdb_test "python print int(gdb.Value(0.0))" "0" + gdb_test "python print long(gdb.Value(0))" "0" + gdb_test "python print long(gdb.Value(0.0))" "0" + gdb_test "python print float(gdb.Value(0.0))" "0\\.0" + gdb_test "python print float(gdb.Value(0))" "0\\.0" +} + # Build C version of executable. C++ is built later. if { [build_inferior "${binfile}" "c"] < 0 } { return -1 @@ -501,6 +510,7 @@ test_value_compare test_objfiles test_parse_and_eval test_value_hash +test_float_conversion # The following tests require execution.