Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 3/6] Add gdbpy_safe_invalid_object_repr
Date: Fri, 23 Oct 2026 13:15:48 -0600	[thread overview]
Message-ID: <20261023-python-safety-fixmes-v1-3-055b9e1d237b@tromey.com> (raw)
In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com>

This adds gdbpy_safe_invalid_object_repr, a simple wrapper that lets
us remove some Python FIXMEs.
---
 gdb/python/py-arch.c       |  4 +---
 gdb/python/py-connection.c |  5 +----
 gdb/python/py-frame.c      |  4 +---
 gdb/python/py-symbol.c     |  5 +----
 gdb/python/py-wrappers.h   | 11 +++++++++++
 5 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index 3de1b3c4533..d9639797bb8 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -286,9 +286,7 @@ gdbpy_ref<>
 arch_object::repr ()
 {
   if (gdbarch == nullptr)
-    /* FIXME: Python safety.  Eventually gdb_py_invalid_object_repr
-       should throw.  */
-    return gdbpy_ref<> (gdb_py_invalid_object_repr (this));
+    return gdbpy_safe_invalid_object_repr (this);
 
   auto arch_info = gdbarch_bfd_arch_info (gdbarch);
   return gdbpy_unicode_from_format ("<%s arch_name=%s printable_name=%s>",
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index 809c60f9ce3..313a5202ee3 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -248,10 +248,7 @@ gdbpy_ref<>
 connection_object::repr ()
 {
   if (target == nullptr)
-    /* FIXME: Python safety.  gdb_py_invalid_object_repr ought to
-       throw on error, and return gdbpy_ref<>, but currently does
-       not.  */
-    return gdbpy_ref<> (gdb_py_invalid_object_repr (this));
+    return gdbpy_safe_invalid_object_repr (this);
 
   return (gdbpy_unicode_from_format
 	  ("<%s num=%d, what=\"%s\">",
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 4aedd7db563..58b08c3d454 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -173,9 +173,7 @@ frame_object::repr ()
 {
   frame_info_ptr f_info = frame_find_by_id (frame_id);
   if (f_info == nullptr)
-    /* FIXME: Python safety.  gdb_py_invalid_object_repr should
-       throw on error. */
-    return gdbpy_ref<> (gdb_py_invalid_object_repr (this));
+    return gdbpy_safe_invalid_object_repr (this);
 
   return gdbpy_unicode_from_format ("<%s level=%d frame-id=%s>",
 				    gdbpy_py_obj_tp_name (this).c_str (),
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 3be1d933493..b5dcca5658d 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -320,10 +320,7 @@ gdbpy_ref<>
 symbol_object::repr ()
 {
   if (symbol == nullptr)
-    /* FIXME: Python safety.  gdb_py_invalid_object_repr ought to
-       throw on error, and return gdbpy_ref<>, but currently does
-       not.  */
-    return gdbpy_ref<> (gdb_py_invalid_object_repr (this));
+    return gdbpy_safe_invalid_object_repr (this);
 
   return gdbpy_unicode_from_format ("<%s print_name=%s>",
 				    gdbpy_py_obj_tp_name (this).c_str (),
diff --git a/gdb/python/py-wrappers.h b/gdb/python/py-wrappers.h
index 69295fa6af3..1f7d4bfc467 100644
--- a/gdb/python/py-wrappers.h
+++ b/gdb/python/py-wrappers.h
@@ -403,4 +403,15 @@ gdbpy_safe_from_ulongest (ULONGEST l)
     throw gdb_python_exception ();
   return result;
 }
+
+/* A wrapper for gdb_py_invalid_object_repr that throws on error.  */
+static inline gdbpy_ref<>
+gdbpy_safe_invalid_object_repr (gdbpy_borrowed_ref<> self)
+{
+  gdbpy_ref<> result (gdb_py_invalid_object_repr (self));
+  if (result == nullptr)
+    throw gdb_python_exception ();
+  return result;
+}
+
 #endif /* GDB_PYTHON_PY_WRAPPERS_H */

-- 
2.49.0


  parent reply	other threads:[~2026-10-23 19:17 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 ` [PATCH 2/6] Add safety wrappers for from_{longest,ulongest} Tom Tromey
2026-10-23 19:15 ` Tom Tromey [this message]
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-3-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