Implement floordiv operator for gdb.Value

Message ID 20160920133440.GG17376@redhat.com
State New, archived
Headers

Commit Message

Jonathan Wakely Sept. 20, 2016, 1:34 p.m. UTC
  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.
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.
  

Patch

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.