From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/6] Add safety wrappers for from_{longest,ulongest}
Date: Fri, 23 Oct 2026 13:15:47 -0600 [thread overview]
Message-ID: <20261023-python-safety-fixmes-v1-2-055b9e1d237b@tromey.com> (raw)
In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com>
This adds a couple of safety wrappers for the APIs that convert from a
LONGEST or ULONGEST to Python. This allows the removal of some
FIXMEs.
---
gdb/python/py-arch.c | 14 ++------------
gdb/python/py-wrappers.h | 19 +++++++++++++++++++
2 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index a6fd12b5ce5..3de1b3c4533 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -198,21 +198,11 @@ arch_object::disassemble (gdbpy_borrowed_ref<> args,
string_file stb (styling_p);
int insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
- /* FIXME: Python safety. Eventually gdb_py_object_from_ulongest
- should throw. */
- gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc);
- if (pc_obj == nullptr)
- throw gdb_python_exception ();
-
+ gdbpy_ref<> pc_obj = gdbpy_safe_from_ulongest (pc);
gdbpy_ref<> asm_obj = gdbpy_unicode_from_string (!stb.empty ()
? stb.c_str ()
: "<unknown>");
-
- /* FIXME: Python safety. Eventually gdb_py_object_from_longest
- should throw. */
- gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
- if (len_obj == nullptr)
- throw gdb_python_exception ();
+ gdbpy_ref<> len_obj = gdbpy_safe_from_longest (insn_len);
gdbpy_dict_set_item_string (insn_dict, "addr", pc_obj);
gdbpy_dict_set_item_string (insn_dict, "asm", asm_obj);
diff --git a/gdb/python/py-wrappers.h b/gdb/python/py-wrappers.h
index 2e2d451167a..69295fa6af3 100644
--- a/gdb/python/py-wrappers.h
+++ b/gdb/python/py-wrappers.h
@@ -384,4 +384,23 @@ gdbpy_get_address (gdbpy_borrowed_ref<> obj)
return result;
}
+/* A wrapper for gdb_py_object_from_longest that throws on error. */
+static inline gdbpy_ref<>
+gdbpy_safe_from_longest (LONGEST l)
+{
+ gdbpy_ref<> result = gdb_py_object_from_longest (l);
+ if (result == nullptr)
+ throw gdb_python_exception ();
+ return result;
+}
+
+/* A wrapper for gdb_py_object_from_ulongest that throws on error. */
+static inline gdbpy_ref<>
+gdbpy_safe_from_ulongest (ULONGEST l)
+{
+ gdbpy_ref<> result = gdb_py_object_from_ulongest (l);
+ if (result == nullptr)
+ throw gdb_python_exception ();
+ return result;
+}
#endif /* GDB_PYTHON_PY_WRAPPERS_H */
--
2.49.0
next prev parent reply other threads:[~2026-10-23 19:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 19:15 [PATCH 0/6] Remove some Python safety FIXME comments Tom Tromey
2026-10-23 19:15 ` [PATCH 1/6] Add safe_target_to_connection_object Tom Tromey
2026-10-23 19:15 ` Tom Tromey [this message]
2026-10-23 19:15 ` [PATCH 3/6] Add gdbpy_safe_invalid_object_repr Tom Tromey
2026-10-23 19:15 ` [PATCH 4/6] Some symbol-related Python safety fixes Tom Tromey
2026-10-23 19:15 ` [PATCH 5/6] Safe wrappers for some string conversions Tom Tromey
2026-10-23 19:15 ` [PATCH 6/6] Some frame-related Python safety fixes 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=20261023-python-safety-fixmes-v1-2-055b9e1d237b@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