[3/7] Allow conversion of pointers to Python int
Commit Message
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 why 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-14 Tom Tromey <tom@tromey.com>
PR python/18170:
* python/py-value.c (valpy_int): Allow conversion from pointer
type.
gdb/testsuite/ChangeLog
2018-09-14 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 | 6 ++++++
gdb/python/py-value.c | 3 ++-
gdb/testsuite/ChangeLog | 6 ++++++
gdb/testsuite/gdb.python/py-value.exp | 3 +++
4 files changed, 17 insertions(+), 1 deletion(-)
@@ -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);
@@ -129,6 +129,9 @@ 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"
+ 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"