From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 1/7] Allow more Python scalar conversions
Date: Sat, 15 Sep 2018 07:25:00 -0000 [thread overview]
Message-ID: <20180915072459.14934-2-tom@tromey.com> (raw)
In-Reply-To: <20180915072459.14934-1-tom@tromey.com>
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-14 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-14 Tom Tromey <tom@tromey.com>
PR python/18352;
* gdb.python/py-value.exp (test_float_conversion): New proc.
Use it.
---
gdb/ChangeLog | 7 +++++++
gdb/python/py-value.c | 25 ++++++++++++++++++++++---
gdb/testsuite/ChangeLog | 6 ++++++
gdb/testsuite/gdb.python/py-value.exp | 10 ++++++++++
4 files changed, 45 insertions(+), 3 deletions(-)
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 1b880fa1917..9abcf9212e6 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)
{
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index eb82a7776fa..9e8fa15c28e 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -483,6 +483,15 @@ proc test_value_hash {} {
gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash"
}
+proc test_float_conversion {} {
+ gdb_test "python print int(gdb.Value(0))" "0"
+ gdb_test "python print int(gdb.Value(0.0))" "0"
+ gdb_test "python print long(gdb.Value(0))" "0"
+ gdb_test "python print long(gdb.Value(0.0))" "0"
+ gdb_test "python print float(gdb.Value(0.0))" "0\\.0"
+ gdb_test "python print float(gdb.Value(0))" "0\\.0"
+}
+
# Build C version of executable. C++ is built later.
if { [build_inferior "${binfile}" "c"] < 0 } {
return -1
@@ -501,6 +510,7 @@ test_value_compare
test_objfiles
test_parse_and_eval
test_value_hash
+test_float_conversion
# The following tests require execution.
--
2.17.1
next prev parent reply other threads:[~2018-09-15 7:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-15 7:25 [PATCH 0/7] some small python fixes (one including a doc patch) Tom Tromey
2018-09-15 7:25 ` Tom Tromey [this message]
2018-09-24 2:06 ` [PATCH 1/7] Allow more Python scalar conversions Simon Marchi
2018-09-24 4:58 ` Tom Tromey
2018-09-15 7:25 ` [PATCH 2/7] Preserve sign when converting gdb.Value to Python int Tom Tromey
2018-09-15 7:25 ` [PATCH 7/7] Allow setting a parameter to raise gdb.GdbError Tom Tromey
2018-09-15 9:16 ` Eli Zaretskii
2018-09-24 2:39 ` Simon Marchi
2018-09-24 4:58 ` Tom Tromey
2018-09-24 5:14 ` Tom Tromey
2018-09-24 7:10 ` Eli Zaretskii
2018-09-24 10:24 ` Tom Tromey
2018-09-24 12:39 ` Eli Zaretskii
2018-09-15 7:25 ` [PATCH 4/7] Report Python errors coming from gdb.post_event Tom Tromey
2018-09-15 7:25 ` [PATCH 3/7] Allow conversion of pointers to Python int Tom Tromey
2018-09-15 7:25 ` [PATCH 6/7] Consolidate gdb.GdbError handling Tom Tromey
2018-09-15 7:25 ` [PATCH 5/7] Check for negative argument in Type.template_argument Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180915072459.14934-2-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox