From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 3/7] Allow conversion of pointers to Python int
Date: Sat, 15 Sep 2018 07:25:00 -0000 [thread overview]
Message-ID: <20180915072459.14934-4-tom@tromey.com> (raw)
In-Reply-To: <20180915072459.14934-1-tom@tromey.com>
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(-)
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 5c6792f85fc..26e91ff2b27 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -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);
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index aed50d1c8cf..222cf1dea6f 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -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"
--
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-15 7:25 ` [PATCH 6/7] Consolidate gdb.GdbError handling 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 5/7] Check for negative argument in Type.template_argument Tom Tromey
2018-09-15 7:25 ` [PATCH 1/7] Allow more Python scalar conversions Tom Tromey
2018-09-24 2:06 ` 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 4/7] Report Python errors coming from gdb.post_event 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-4-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).