This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Allow more Python scalar conversions


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=fb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9

commit fb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Sep 14 22:31:12 2018 -0600

    Allow more Python scalar conversions
    
    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-23  Tom Tromey  <tom@tromey.com>
    
    	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-23  Tom Tromey  <tom@tromey.com>
    
    	PR python/18352;
    	* gdb.python/py-value.exp (test_float_conversion): New proc.
    	Use it.

Diff:
---
 gdb/ChangeLog                         |  7 +++++++
 gdb/python/py-value.c                 | 25 ++++++++++++++++++++++---
 gdb/testsuite/ChangeLog               |  6 ++++++
 gdb/testsuite/gdb.python/py-value.exp | 13 +++++++++++++
 4 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d864e43..bef96c0 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,12 @@
 2018-09-23  Tom Tromey  <tom@tromey.com>
 
+	PR python/18352;
+	* python/py-value.c (valpy_float): Allow conversions from int or
+	char.
+	(valpy_int, valpy_long): Allow conversions from float.
+
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
 	* ctf.c (ctf_start): Use gdb_fopen_cloexec.
 	* common/scoped_mmap.c (mmap_file): Use gdb_open_cloexec.
 
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 1b880fa..9abcf92 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/ChangeLog b/gdb/testsuite/ChangeLog
index 3fabd80..4f7fa93 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
+	PR python/18352;
+	* gdb.python/py-value.exp (test_float_conversion): New proc.
+	Use it.
+
 2018-09-20  Hafiz Abid Qadeer  <abidh@codesourcery.com>
 
 	gdb.base/reggroups.exp (fetch_reggroups): Add '_' in match pattern.
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index eb82a77..ac08faa 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -483,6 +483,18 @@ proc test_value_hash {} {
     gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash"
 }
 
+proc test_float_conversion {} {
+    global gdb_py_is_py3k
+    gdb_test "python print(int(gdb.Value(0)))" "0"
+    gdb_test "python print(int(gdb.Value(2.5)))" "2"
+    if {!$gdb_py_is_py3k} {
+	gdb_test "python print(long(gdb.Value(0)))" "0"
+	gdb_test "python print(long(gdb.Value(2.5)))" "2"
+    }
+    gdb_test "python print(float(gdb.Value(2.5)))" "2\\.5"
+    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 +513,7 @@ test_value_compare
 test_objfiles
 test_parse_and_eval
 test_value_hash
+test_float_conversion
 
 # The following tests require execution.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]