This is the mail archive of the
gdb-cvs@sourceware.org
mailing list for the GDB project.
[binutils-gdb] Preserve sign when converting gdb.Value to Python int
- From: Tom Tromey <tromey at sourceware dot org>
- To: gdb-cvs at sourceware dot org
- Date: 24 Sep 2018 05:16:23 -0000
- Subject: [binutils-gdb] Preserve sign when converting gdb.Value to Python int
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1c1e54f6b4b6de83aa3f31e6584f5bb4d6242930
commit 1c1e54f6b4b6de83aa3f31e6584f5bb4d6242930
Author: Tom Tromey <tom@tromey.com>
Date: Fri Sep 14 22:44:10 2018 -0600
Preserve sign when converting gdb.Value to Python int
PR python/20126 points out that sometimes the conversion of a
gdb.Value can result in a negative Python integer. This happens
because valpy_int does not examine the signedness of the value's type.
gdb/ChangeLog
2018-09-23 Tom Tromey <tom@tromey.com>
PR python/20126:
* python/py-value.c (valpy_int): Respect type sign.
gdb/testsuite/ChangeLog
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:
---
gdb/ChangeLog | 5 +++++
gdb/python/py-value.c | 5 ++++-
gdb/testsuite/ChangeLog | 6 ++++++
gdb/testsuite/gdb.python/py-value.exp | 8 ++++++++
4 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index bef96c0..9ea593d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
2018-09-23 Tom Tromey <tom@tromey.com>
+ PR python/20126:
+ * python/py-value.c (valpy_int): Respect type sign.
+
+2018-09-23 Tom Tromey <tom@tromey.com>
+
PR python/18352;
* python/py-value.c (valpy_float): Allow conversions from int or
char.
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 9abcf92..5c6792f 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1514,7 +1514,10 @@ valpy_int (PyObject *self)
}
END_CATCH
- return gdb_py_object_from_longest (l);
+ if (TYPE_UNSIGNED (type))
+ return gdb_py_object_from_ulongest (l);
+ else
+ return gdb_py_object_from_longest (l);
}
#endif
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 4f7fa93..40523fa 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,11 @@
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.
+
+2018-09-23 Tom Tromey <tom@tromey.com>
+
PR python/18352;
* gdb.python/py-value.exp (test_float_conversion): New proc.
Use it.
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index ac08faa..ccf8629 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -79,6 +79,7 @@ proc test_value_creation {} {
proc test_value_numeric_ops {} {
global gdb_prompt
+ global gdb_py_is_py3k
gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0
gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0
@@ -140,6 +141,13 @@ proc test_value_numeric_ops {} {
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"
+ if {!$gdb_py_is_py3k} {
+ gdb_test "python print('%x' % long(gdb.parse_and_eval('-1ull')))" \
+ "f+" "long conversion respect type sign"
+ }
+
# Test some invalid operations.
gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" {