From: Jonathan Wakely <jwakely@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: [PATCH] Implement floordiv operator for gdb.Value
Date: Tue, 20 Sep 2016 14:46:00 -0000 [thread overview]
Message-ID: <20160920133440.GG17376@redhat.com> (raw)
In-Reply-To: <20160920132633.GA897@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 1441 bytes --]
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 <jwakely@redhat.com>
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 <jwakely@redhat.com>
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 <jwakely@redhat.com>
PR python/20624
* gdb.python/py-value.exp: Adjust expected value for dividing two
integers.
[-- Attachment #2: truediv.txt --]
[-- Type: text/plain, Size: 2625 bytes --]
commit 03ded236edda388bb55d5c8678f1b754cab8ba8e
Author: Jonathan Wakely <jwakely@redhat.com>
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 <jwakely@redhat.com>
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 <jwakely@redhat.com>
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.
next prev parent reply other threads:[~2016-09-20 13:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-20 13:34 Jonathan Wakely
2016-09-20 14:46 ` Jonathan Wakely [this message]
2016-09-20 15:41 ` Pedro Alves
2016-09-20 16:41 ` Jonathan Wakely
2016-09-20 16:42 ` Jonathan Wakely
2016-09-20 17:01 ` Pedro Alves
2016-09-20 17:11 ` Jonathan Wakely
2016-09-20 17:08 ` Paul.Koning
2016-09-20 18:20 ` Jonathan Wakely
2016-09-20 19:06 ` Paul.Koning
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160920133440.GG17376@redhat.com \
--to=jwakely@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox