aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2018-09-14 22:31:12 -0600
committerTom Tromey <tom@tromey.com>2018-09-23 23:12:59 -0600
commitfb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9 (patch)
tree67775531ba40c4164d51c62d66e95f7611a43811 /gdb/python
parente6b5f1e9f5aace67505b4eff43570c82b56139b9 (diff)
downloadgdb-fb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9.zip
gdb-fb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9.tar.gz
gdb-fb4fa9469c5f5d87a956d45ed3b36fecc1fc31b9.tar.bz2
Allow more Python scalar conversions
PR python/18352 points out that the gdb Python code can't convert an integer-valued gdb.Value to a Python float. While writing the test I noticed that, similarly, converting integer gdb.Values to float does not work. However, all of these cases seem reasonable. gdb/ChangeLog 2018-09-23 Tom Tromey <tom@tromey.com> PR python/18352; * python/py-value.c (valpy_float): Allow conversions from int or char. (valpy_int, valpy_long): Allow conversions from float. gdb/testsuite/ChangeLog 2018-09-23 Tom Tromey <tom@tromey.com> PR python/18352; * gdb.python/py-value.exp (test_float_conversion): New proc. Use it.
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-value.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 1b880fa..9abcf92 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1497,6 +1497,12 @@ valpy_int (PyObject *self)
TRY
{
+ if (is_floating_value (value))
+ {
+ type = builtin_type_pylong;
+ value = value_cast (type, value);
+ }
+
if (!is_integral_type (type))
error (_("Cannot convert value to int."));
@@ -1522,6 +1528,12 @@ valpy_long (PyObject *self)
TRY
{
+ if (is_floating_value (value))
+ {
+ type = builtin_type_pylong;
+ value = value_cast (type, value);
+ }
+
type = check_typedef (type);
if (!is_integral_type (type)
@@ -1554,10 +1566,17 @@ valpy_float (PyObject *self)
{
type = check_typedef (type);
- if (TYPE_CODE (type) != TYPE_CODE_FLT || !is_floating_value (value))
+ if (TYPE_CODE (type) == TYPE_CODE_FLT && is_floating_value (value))
+ d = target_float_to_host_double (value_contents (value), type);
+ else if (TYPE_CODE (type) == TYPE_CODE_INT)
+ {
+ /* Note that valpy_long accepts TYPE_CODE_PTR and some
+ others here here -- but casting a pointer or bool to a
+ float seems wrong. */
+ d = value_as_long (value);
+ }
+ else
error (_("Cannot convert value to float."));
-
- d = target_float_to_host_double (value_contents (value), type);
}
CATCH (except, RETURN_MASK_ALL)
{