From patchwork Tue Sep 20 13:34:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 15797 Received: (qmail 79326 invoked by alias); 20 Sep 2016 13:34:44 -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 79304 invoked by uid 89); 20 Sep 2016 13:34:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.0 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=3.x, fg, 3x, UD:3.x X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 20 Sep 2016 13:34:42 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AE06B89C37 for ; Tue, 20 Sep 2016 13:34:41 +0000 (UTC) Received: from localhost (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8KDYe9L032680 for ; Tue, 20 Sep 2016 09:34:41 -0400 Date: Tue, 20 Sep 2016 14:34:40 +0100 From: Jonathan Wakely To: gdb-patches@sourceware.org Subject: Re: [PATCH] Implement floordiv operator for gdb.Value Message-ID: <20160920133440.GG17376@redhat.com> References: <20160920132633.GA897@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20160920132633.GA897@redhat.com> X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.0 (2016-08-17) On 20/09/16 14:26 +0100, Jonathan Wakely wrote: >This is my attempt to implement the // operator on gdb.Value objects. >There is already BINOP_INTDIV which works fine for integral types, but >for floats I use BINOP_DIV and then call floor() on the result. This >doesn't support decimal floats though. > >Is this a reasonable solution? Is the test sufficient? > >I have a follow-up patch which changes the meaning of the / operator >for gdb.Value when built against Python 3, to be consistent with >Python (see comment 1 in the Bugzilla PR) but I expect that to be more >controversial :-) Here's the follow-up, which I'm only posting for curiosity value. It's a bit of a hack, possibly buggy, and needs more thought about whether this is even a good idea. From: Jonathan Wakely Date: Tue, 20 Sep 2016 14:32:50 +0100 Subject: [PATCH] gdb: Make gdb.Value division consistent with Python In Python 3.x dividing integers does not perform integer division, so make gdb.Value behave the same way when built with Python 3.x or later. gdb/ChangeLog: 2016-09-20 Jonathan Wakely PR python/20624 * python/py-value.c (valpy_binop_throw) [PY_MAJOR_VERSION >= 3]: Convert integers to double for VALPY_DIV to perform exact division. gdb/testsuite/ChangeLog: 2016-09-20 Jonathan Wakely PR python/20624 * gdb.python/py-value.exp: Adjust expected value for dividing two integers. commit 03ded236edda388bb55d5c8678f1b754cab8ba8e Author: Jonathan Wakely Date: Tue Sep 20 14:32:50 2016 +0100 gdb: Make gdb.Value division consistent with Python In Python 3.x dividing integers does not perform integer division, so make gdb.Value behave the same way when built with Python 3.x or later. gdb/ChangeLog: 2016-09-20 Jonathan Wakely PR python/20624 * python/py-value.c (valpy_binop_throw) [PY_MAJOR_VERSION >= 3]: Convert integers to double for VALPY_DIV to perform exact division. gdb/testsuite/ChangeLog: 2016-09-20 Jonathan Wakely PR python/20624 * gdb.python/py-value.exp: Adjust expected value for dividing two integers. diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index f6a6c11..28877be 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1110,7 +1110,22 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) op = BINOP_MUL; break; case VALPY_DIV: +#if PY_MAJOR_VERSION < 3 op = BINOP_DIV; +#else + { + struct type *ltype = value_type (arg1); + struct type *rtype = value_type (arg2); + ltype = check_typedef (ltype); + rtype = check_typedef (rtype); + if (is_integral_type (ltype) && is_integral_type (rtype)) + { + long l = value_as_long (arg1); + arg1 = value_from_double (builtin_type_pyfloat, (double)l); + } + op = BINOP_DIV; + } +#endif break; case VALPY_REM: op = BINOP_REM; diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index 81837e9..c1bdc06 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -83,7 +83,7 @@ proc test_value_numeric_ops {} { gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values" gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values" gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values" - gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values" + gdb_test "python print ('result = ' + str(i/j))" " = 2.5" "divide two integer values" gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" # Remainder of float is implemented in Python but not in GDB's value system.