]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Return gdbpy_ref<> from gdbpy_registry::lookup
authorTom Tromey <tromey@adacore.com>
Fri, 20 Feb 2026 20:34:45 +0000 (13:34 -0700)
committerTom Tromey <tromey@adacore.com>
Mon, 23 Feb 2026 12:29:12 +0000 (05:29 -0700)
This changes gdbpy_registry::lookup to return a gdbpy_ref<>, using the
type system to convey that a new reference is always returned.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdb/python/py-symbol.c
gdb/python/py-symtab.c
gdb/python/py-type.c
gdb/python/python-internal.h

index 7b6164fb20d61ee57544a6f73445d3a450590351..e8ff2dfe28a2c3f9368be0b508281bf1896fb6e7 100644 (file)
@@ -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);
 
index ed5abac1372c4ed43ee4ba2f85b5e6e83ba7080e..27d6c4beba9c5e34c9e453b52600875400d9ff88 100644 (file)
@@ -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);
index 8f8f6e326a8da6b7e79e14cd523811f3a6d237c9..b6807801f7e47eb823f49d8a74fe8a58c76a5224 100644 (file)
@@ -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 *
index 1f0964f24d3b3f125938dab3150e39dd95ee3262..fdd353ffbeb86f15c8035ccb904f3e1c7ed906d8 100644 (file)
@@ -1191,14 +1191,13 @@ public:
   }
 
   /* 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: