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.