[binutils-gdb] Allow conversion of pointers to Python int

Tom Tromey tromey@sourceware.org
Mon Sep 24 05:16:00 GMT 2018


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

commit f5769a2c696affc3ae1274e2329777d7d4d7e8be
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Sep 14 23:20:58 2018 -0600

    Allow conversion of pointers to Python int
    
    PR python/18170 questions why it's not possible to convert a pointer
    value to a Python int.
    
    Digging a bit shows that the Python 2.7 int() constructor will happily
    return a long in some cases.  And, it seems gdb already understands
    this in other places -- this is what gdb_py_object_from_longest
    handles.
    
    So, this patch simply extends valpy_int to allow pointer conversions,
    as valpy_long does.
    
    gdb/ChangeLog
    2018-09-23  Tom Tromey  <tom@tromey.com>
    
    	PR python/18170:
    	* python/py-value.c (valpy_int): Allow conversion from pointer
    	type.
    
    gdb/testsuite/ChangeLog
    2018-09-23  Tom Tromey  <tom@tromey.com>
    
    	PR python/18170:
    	* gdb.python/py-value.exp (test_value_numeric_ops): Add tests to
    	convert pointers to int and long.

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

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 9ea593d..a03163f 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2018-09-23  Tom Tromey  <tom@tromey.com>
 
+	PR python/18170:
+	* python/py-value.c (valpy_int): Allow conversion from pointer
+	type.
+
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
 	PR python/20126:
 	* python/py-value.c (valpy_int): Respect type sign.
 
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 5c6792f..26e91ff 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1503,7 +1503,8 @@ valpy_int (PyObject *self)
 	  value = value_cast (type, value);
 	}
 
-      if (!is_integral_type (type))
+      if (!is_integral_type (type)
+	  && TYPE_CODE (type) != TYPE_CODE_PTR)
 	error (_("Cannot convert value to int."));
 
       l = value_as_long (value);
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 40523fa..c04f09a 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,11 @@
 2018-09-23  Tom Tromey  <tom@tromey.com>
 
+	PR python/18170:
+	* gdb.python/py-value.exp (test_value_numeric_ops): Add tests to
+	convert pointers to int and long.
+
+2018-09-23  Tom Tromey  <tom@tromey.com>
+
 	PR python/20126:
 	* gdb.python/py-value.exp (test_value_numeric_ops): Add
 	signed-ness conversion tests.
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index ccf8629..2234b1e 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -130,6 +130,11 @@ proc test_value_numeric_ops {} {
   gdb_test "print (void *) 5" ".*" ""
   gdb_test_no_output "python b = gdb.history (0)" ""
 
+  gdb_test "python print(int(b))" "5" "convert pointer to int"
+  if {!$gdb_py_is_py3k} {
+    gdb_test "python print(long(b))" "5" "convert pointer to long"
+  }
+
   gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer"
   gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value"
   gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values"
@@ -138,8 +143,10 @@ proc test_value_numeric_ops {} {
     "result = r" "use value as string index"
   gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \
     "result = 1" "use value as tuple index"
-  gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
-    "result = 1" "use value as array index"
+  if {!$gdb_py_is_py3k} {
+    gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \
+      "result = 1" "use value as array index"
+  }
 
   gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \
       "f+" "int conversion respect type sign"



More information about the Gdb-cvs mailing list