]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Return gdbpy_ref<> from type_to_type_object
authorTom Tromey <tromey@adacore.com>
Fri, 20 Feb 2026 20:05:24 +0000 (13:05 -0700)
committerTom Tromey <tromey@adacore.com>
Mon, 23 Feb 2026 12:29:12 +0000 (05:29 -0700)
This changes type_to_type_object 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-arch.c
gdb/python/py-lazy-string.c
gdb/python/py-symbol.c
gdb/python/py-type.c
gdb/python/py-value.c
gdb/python/py-xmethods.c
gdb/python/python-internal.h
gdb/python/python.c

index eb4a7c63530db878902b353e15c3b76f1da15286..cf740821dfadcde146188f3911a7063547de9062 100644 (file)
@@ -314,7 +314,7 @@ archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw)
       return nullptr;
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Implementation of gdb.void_type.  */
@@ -324,7 +324,7 @@ archpy_void_type (PyObject *self, PyObject *args)
   struct gdbarch *gdbarch;
   ARCHPY_REQUIRE_VALID (self, gdbarch);
 
-  return type_to_type_object (builtin_type (gdbarch)->builtin_void);
+  return type_to_type_object (builtin_type (gdbarch)->builtin_void).release ();
 }
 
 /* __repr__ implementation for gdb.Architecture.  */
index fe248f6e78f4b5724f394a62a0f216314cb3eafe..26998064206e7f7cb7ea64a66be563a0c74b3879 100644 (file)
@@ -235,7 +235,7 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
     str_obj->encoding = NULL;
   else
     str_obj->encoding = xstrdup (encoding);
-  str_obj->type = type_to_type_object (type);
+  str_obj->type = type_to_type_object (type).release ();
 
   return (PyObject *) str_obj;
 }
index 80c7705aa68dccf802d4c6b6e862de1cfbc49995..7b6164fb20d61ee57544a6f73445d3a450590351 100644 (file)
@@ -73,7 +73,7 @@ sympy_get_type (PyObject *self, void *closure)
       return Py_None;
     }
 
-  return type_to_type_object (symbol->type ());
+  return type_to_type_object (symbol->type ()).release ();
 }
 
 static PyObject *
index 017937c1c0ac5e952c732ad67f91907942281b49..8f8f6e326a8da6b7e79e14cd523811f3a6d237c9 100644 (file)
@@ -132,7 +132,7 @@ convert_field (struct type *type, int field)
   if (result == NULL)
     return NULL;
 
-  gdbpy_ref<> arg (type_to_type_object (type));
+  gdbpy_ref<> arg = type_to_type_object (type);
   if (arg == NULL)
     return NULL;
   if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
@@ -202,7 +202,7 @@ convert_field (struct type *type, int field)
   if (type->field (field).type () == NULL)
     arg = gdbpy_ref<>::new_reference (Py_None);
   else
-    arg.reset (type_to_type_object (type->field (field).type ()));
+    arg = type_to_type_object (type->field (field).type ());
   if (arg == NULL)
     return NULL;
   if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0)
@@ -283,7 +283,7 @@ typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
   gdbpy_ref<> type_holder;
   if (checked_type != type)
     {
-      type_holder.reset (type_to_type_object (checked_type));
+      type_holder = type_to_type_object (checked_type);
       if (type_holder == nullptr)
        return nullptr;
       py_type = type_holder.get ();
@@ -489,7 +489,7 @@ typy_strip_typedefs (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Strip typedefs and pointers/reference from a type.  Then check that
@@ -580,7 +580,7 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (array);
+  return type_to_type_object (array).release ();
 }
 
 /* Return an array type.  */
@@ -614,7 +614,7 @@ typy_pointer (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Return the range of a type represented by SELF.  The return type is
@@ -686,7 +686,7 @@ typy_reference (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Return a Type object which represents the target type of SELF.  */
@@ -702,7 +702,7 @@ typy_target (PyObject *self, PyObject *args)
       return NULL;
     }
 
-  return type_to_type_object (type->target_type ());
+  return type_to_type_object (type->target_type ()).release ();
 }
 
 /* Return a const-qualified type variant.  */
@@ -720,7 +720,7 @@ typy_const (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Return a volatile-qualified type variant.  */
@@ -738,7 +738,7 @@ typy_volatile (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Return an unqualified type variant.  */
@@ -756,7 +756,7 @@ typy_unqualified (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Return the size of the type represented by SELF, in bytes.  */
@@ -978,7 +978,7 @@ typy_legacy_template_argument (struct type *type, const struct block *block,
   if (! argtype)
     return NULL;
 
-  return type_to_type_object (argtype);
+  return type_to_type_object (argtype).release ();
 }
 
 static PyObject *
@@ -1037,7 +1037,7 @@ typy_template_argument (PyObject *self, PyObject *args)
 
   sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
   if (sym->loc_class () == LOC_TYPEDEF)
-    return type_to_type_object (sym->type ());
+    return type_to_type_object (sym->type ()).release ();
   else if (sym->loc_class () == LOC_OPTIMIZED_OUT)
     {
       PyErr_Format (PyExc_RuntimeError,
@@ -1423,7 +1423,7 @@ typy_iterator_dealloc (PyObject *obj)
 }
 
 /* Create a new Type referring to TYPE.  */
-PyObject *
+gdbpy_ref<>
 type_to_type_object (struct type *type)
 {
   type_object *type_obj;
@@ -1450,14 +1450,13 @@ type_to_type_object (struct type *type)
   else
     type_obj = typy_registry.lookup (type->arch_owner (), type);
 
-  if (type_obj != nullptr)
-    return (PyObject*)type_obj;
-
-  type_obj = PyObject_New (type_object, &type_object_type);
-  if (type_obj)
-    set_type (type_obj, type);
-
-  return (PyObject *) type_obj;
+  if (type_obj == nullptr)
+    {
+      type_obj = PyObject_New (type_object, &type_object_type);
+      if (type_obj)
+       set_type (type_obj, type);
+    }
+  return gdbpy_ref<> (type_obj);
 }
 
 struct type *
@@ -1499,7 +1498,7 @@ gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
   if (! type)
     return NULL;
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 static int
index c0fcfb882e1173d98b82d6dba9d02f49eea22d8a..a5b2303728c82a91299fa01e5449bf14b7c6ed2d 100644 (file)
@@ -432,7 +432,7 @@ valpy_get_type (PyObject *self, void *closure)
 {
   value_object *obj = (value_object *) self;
 
-  return type_to_type_object (obj->value->type ());
+  return type_to_type_object (obj->value->type ()).release ();
 }
 
 /* Return dynamic type of the value.  */
@@ -484,7 +484,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  return type_to_type_object (type);
+  return type_to_type_object (type).release ();
 }
 
 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
index 64f9807cfeef054c2cef43b179250c3d55a50149..1fafafb4d241eeb456cb38effaabe6614585f000 100644 (file)
@@ -123,7 +123,7 @@ gdbpy_get_matching_xmethod_workers
 
   gdbpy_enter enter_py;
 
-  gdbpy_ref<> py_type (type_to_type_object (obj_type));
+  gdbpy_ref<> py_type = type_to_type_object (obj_type);
   if (py_type == NULL)
     {
       gdbpy_print_stack ();
index c7e812d6893942b0181dc6f0d2cbc7c7de31d192..6fa37cf85bd6738502b13668ee6c5a3c0ee174b4 100644 (file)
@@ -501,7 +501,7 @@ gdbpy_ref<> symbol_to_symbol_object (struct symbol *sym);
 gdbpy_ref<> block_to_block_object (const struct block *block,
                                   struct objfile *objfile);
 gdbpy_ref<> value_to_value_object (struct value *v);
-PyObject *type_to_type_object (struct type *);
+gdbpy_ref<> type_to_type_object (struct type *);
 PyObject *frame_info_to_frame_object (const frame_info_ptr &frame);
 PyObject *symtab_to_linetable_object (PyObject *symtab);
 gdbpy_ref<> pspace_to_pspace_object (struct program_space *);
index aec2c7559a852f859e9940ee5078c7589d8b4f66..9862d1a726fb99ea58e0e481ff14aa230b4ae824 100644 (file)
@@ -2105,7 +2105,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
 
   gdbpy_enter enter_py;
 
-  gdbpy_ref<> type_obj (type_to_type_object (type));
+  gdbpy_ref<> type_obj = type_to_type_object (type);
   if (type_obj == NULL)
     {
       gdbpy_print_stack ();