Allow conversion of pointers to Python int
authorTom Tromey <tom@tromey.com>
Sat, 15 Sep 2018 05:20:58 +0000 (23:20 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 24 Sep 2018 05:15:12 +0000 (23:15 -0600)
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.

gdb/ChangeLog
gdb/python/py-value.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-value.exp

index 9ea593d22b8abb7557101cc03f7c6247cc78de1b..a03163ff7ddd2f7c885fd2c58c272c354af745cd 100644 (file)
@@ -1,3 +1,9 @@
+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:
index 5c6792f85fc097fb27a7c8f77bf09d06b7c84730..26e91ff2b271a34504addbeef0c507d9fda1f806 100644 (file)
@@ -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);
index 40523fa7e45cfde22c0f106ce8e8e9977963cf59..c04f09a352e95489c73e2215a5337d81a42b459e 100644 (file)
@@ -1,3 +1,9 @@
+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:
index ccf8629d4ab4012098ebdb4fdb4ea34be2e87575..2234b1e42ef32eefc856f2c14c778049daf11161 100644 (file)
@@ -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"
This page took 0.039976 seconds and 4 git commands to generate.