Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 10/10] Return gdbpy_ref<> from gdbpy_registry::lookup
Date: Fri, 20 Feb 2026 14:03:56 -0700	[thread overview]
Message-ID: <20260220-python-safety-minor-v1-10-4c4b12e445af@adacore.com> (raw)
In-Reply-To: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com>

This changes gdbpy_registry::lookup to return a gdbpy_ref<>, using the
type system to convey that a new reference is always returned.
---
 gdb/python/py-symbol.c       | 13 ++++++-------
 gdb/python/py-symtab.c       |  8 ++++----
 gdb/python/py-type.c         | 16 ++++++++--------
 gdb/python/python-internal.h |  7 +++----
 4 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 7b6164fb20d..e8ff2dfe28a 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -339,18 +339,17 @@ set_symbol (symbol_object *obj, struct symbol *symbol)
 gdbpy_ref<>
 symbol_to_symbol_object (struct symbol *sym)
 {
-  symbol_object *sym_obj;
-
   /* Look if there's already a gdb.Symbol object for given SYMBOL
      and if so, return it.  */
+  gdbpy_ref<> result;
   if (sym->is_objfile_owned ())
-    sym_obj = sympy_registry.lookup (sym->objfile (), sym);
+    result = sympy_registry.lookup (sym->objfile (), sym);
   else
-    sym_obj = sympy_registry.lookup (sym->arch (), sym);
-  if (sym_obj != nullptr)
-    return gdbpy_ref<> (sym_obj);
+    result = sympy_registry.lookup (sym->arch (), sym);
+  if (result != nullptr)
+    return result;
 
-  sym_obj = PyObject_New (symbol_object, &symbol_object_type);
+  symbol_object *sym_obj = PyObject_New (symbol_object, &symbol_object_type);
   if (sym_obj)
     set_symbol (sym_obj, sym);
 
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index ed5abac1372..27d6c4beba9 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -377,10 +377,10 @@ symtab_to_symtab_object (struct symtab *symtab)
      and if so, return it.  */
   if (symtab != nullptr)
     {
-      symtab_obj = stpy_registry.lookup (symtab->compunit ()->objfile (),
-					 symtab);
-      if (symtab_obj != nullptr)
-	return gdbpy_ref<> (symtab_obj);
+      gdbpy_ref<> result
+	= stpy_registry.lookup (symtab->compunit ()->objfile (), symtab);
+      if (result != nullptr)
+	return result;
     }
 
   symtab_obj = PyObject_New (symtab_object, &symtab_object_type);
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 8f8f6e326a8..b6807801f7e 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1426,8 +1426,6 @@ typy_iterator_dealloc (PyObject *obj)
 gdbpy_ref<>
 type_to_type_object (struct type *type)
 {
-  type_object *type_obj;
-
   try
     {
       /* Try not to let stub types leak out to Python.  */
@@ -1445,18 +1443,20 @@ type_to_type_object (struct type *type)
 
   /* Look if there's already a gdb.Type object for given TYPE
      and if so, return it.  */
+  gdbpy_ref<> result;
   if (type->is_objfile_owned ())
-    type_obj = typy_registry.lookup (type->objfile_owner (), type);
+    result = typy_registry.lookup (type->objfile_owner (), type);
   else
-    type_obj = typy_registry.lookup (type->arch_owner (), type);
+    result = typy_registry.lookup (type->arch_owner (), type);
 
-  if (type_obj == nullptr)
+  if (result == nullptr)
     {
-      type_obj = PyObject_New (type_object, &type_object_type);
-      if (type_obj)
+      type_object *type_obj = PyObject_New (type_object, &type_object_type);
+      if (type_obj != nullptr)
 	set_type (type_obj, type);
+      result = gdbpy_ref<> (type_obj);
     }
-  return gdbpy_ref<> (type_obj);
+  return result;
 }
 
 struct type *
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 1f0964f24d3..fdd353ffbeb 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1191,14 +1191,13 @@ class gdbpy_registry
   }
 
   /* Lookup pre-existing Python object for given VAL.  Return such object
-     if found, otherwise return NULL.  This method always returns new
-     reference.  */
+     if found, otherwise return NULL.  */
   template <typename O>
-  obj_type *lookup (O *owner, val_type *val) const
+  gdbpy_ref<> lookup (O *owner, val_type *val) const
   {
     obj_type *obj = get_storage (owner)->lookup (val);
     Py_XINCREF (static_cast<PyObject *> (obj));
-    return obj;
+    return gdbpy_ref<> (obj);
   }
 
 private:

-- 
2.53.0


  parent reply	other threads:[~2026-02-20 21:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-20 21:03 [PATCH 00/10] Mildly better refcount safety for Python Tom Tromey
2026-02-20 21:03 ` [PATCH 01/10] Return gdbpy_ref<> from symtab_and_line_to_sal_object Tom Tromey
2026-02-21  1:58   ` Simon Marchi
2026-02-21  2:13     ` Simon Marchi
2026-02-21 22:33       ` Tom Tromey
2026-02-23 12:28     ` Tom Tromey
2026-02-20 21:03 ` [PATCH 02/10] Return gdbpy_ref<> from symbol_to_symbol_object Tom Tromey
2026-02-21  2:28   ` Simon Marchi
2026-02-21 22:35     ` Tom Tromey
2026-02-20 21:03 ` [PATCH 03/10] Return gdbpy_ref<> from symtab_to_symtab_object Tom Tromey
2026-02-20 21:03 ` [PATCH 04/10] Return gdbpy_ref<> from block_to_block_object Tom Tromey
2026-02-20 21:03 ` [PATCH 05/10] Return gdbpy_ref<> from value_to_value_object Tom Tromey
2026-02-20 21:03 ` [PATCH 06/10] Return gdbpy_ref<> from type_to_type_object Tom Tromey
2026-02-20 21:03 ` [PATCH 07/10] Return gdbpy_ref<> from frame_info_to_frame_object Tom Tromey
2026-02-20 21:03 ` [PATCH 08/10] Return gdbpy_ref<> from symtab_to_linetable_object Tom Tromey
2026-02-20 21:03 ` [PATCH 09/10] Return gdbpy_ref<> from gdbarch_to_arch_object Tom Tromey
2026-02-20 21:03 ` Tom Tromey [this message]
2026-02-21  2:31 ` [PATCH 00/10] Mildly better refcount safety for Python Simon Marchi

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=20260220-python-safety-minor-v1-10-4c4b12e445af@adacore.com \
    --to=tromey@adacore.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