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 1/6] Add safe_target_to_connection_object
Date: Fri, 23 Oct 2026 13:15:46 -0600	[thread overview]
Message-ID: <20261023-python-safety-fixmes-v1-1-055b9e1d237b@tromey.com> (raw)
In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com>

This adds safe_target_to_connection_object, a simple wrapper that lets
us remove a FIXME from the Python code.
---
 gdb/python/py-connection.c   | 17 ++++++++++++-----
 gdb/python/python-internal.h |  3 +++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index 0b10ed2e594..809c60f9ce3 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -148,6 +148,17 @@ target_to_connection_object (process_stratum_target *target)
   return conn_obj;
 }
 
+/* See python-internal.h.  */
+
+gdbpy_ref<>
+safe_target_to_connection_object (process_stratum_target *target)
+{
+  gdbpy_ref<> result = target_to_connection_object (target);
+  if (result == nullptr)
+    throw gdb_python_exception ();
+  return result;
+}
+
 /* Return a list of gdb.TargetConnection objects, one for each currently
    active connection.  The returned list is in no particular order.  */
 
@@ -160,11 +171,7 @@ gdbpy_connections ()
     {
       gdb_assert (target != nullptr);
 
-      gdbpy_ref<> conn = target_to_connection_object (target);
-      /* FIXME: Python safety.  target_to_connection_object should
-	 throw on error.  */
-      if (conn == nullptr)
-	return nullptr;
+      gdbpy_ref<> conn = safe_target_to_connection_object (target);
       gdb_assert (conn.get () != Py_None);
 
       gdbpy_list_append (list, conn);
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 2e064107cbc..b57f3b0123d 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -607,6 +607,9 @@ PyObject *gdbpy_buffer_to_membuf (gdb::unique_xmalloc_ptr<gdb_byte> buffer,
 
 struct process_stratum_target;
 gdbpy_ref<> target_to_connection_object (process_stratum_target *target);
+/* A safe variant of target_to_connection_object; this throws on
+   error.  */
+gdbpy_ref<> safe_target_to_connection_object (process_stratum_target *target);
 gdbpy_ref<> gdbpy_connections ();
 
 const struct block *block_object_to_block (PyObject *obj);

-- 
2.49.0


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