Preserve sign when converting gdb.Value to Python int
authorTom Tromey <tom@tromey.com>
Sat, 15 Sep 2018 04:44:10 +0000 (22:44 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 24 Sep 2018 05:13:00 +0000 (23:13 -0600)
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.

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

index bef96c0193f472952a05cb3033174d34c2cc6ecc..9ea593d22b8abb7557101cc03f7c6247cc78de1b 100644 (file)
@@ -1,3 +1,8 @@
+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;
index 9abcf9212e6580337cef235fe9242e51940479ee..5c6792f85fc097fb27a7c8f77bf09d06b7c84730 100644 (file)
@@ -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
 
index 4f7fa938ebfaf964a4b5004d5968fd0b86e273d3..40523fa7e45cfde22c0f106ce8e8e9977963cf59 100644 (file)
@@ -1,3 +1,9 @@
+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;
index ac08faa3f6f7f1d1d1cd487655061ab5b258682c..ccf8629d4ab4012098ebdb4fdb4ea34be2e87575 100644 (file)
@@ -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" {
This page took 0.04186 seconds and 4 git commands to generate.