[RFC,v2,05/10] gdb/python: do not hold on gdb.Type object from gdb.Value

Message ID 20250226110653.1200260-6-jan.vrany@labware.com
State New
Headers
Series Attempt to unify Python object's lifecycle |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Skipped upon request
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Skipped upon request

Commit Message

Jan Vraný Feb. 26, 2025, 11:06 a.m. UTC
  Previous commit changed type_to_type_object() so each time it is
called with particular struct value* it returns the same object.

Therefore there's no longer need to hold on type objects (gdb.Type)
from struct value_object in order to preserve identity of gdb.Type
objects held in value_object::type and value_object::dynamic_type
members. This in turn allowed for some simplification in various
functions.

While at it I changed a couple of NULLs to nullptrs.
---
 gdb/python/py-value.c | 40 +++++++---------------------------------
 1 file changed, 7 insertions(+), 33 deletions(-)
  

Patch

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 02c50b48fd2..cf1e3eae45d 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -60,7 +60,6 @@  struct value_object {
   struct value_object *prev;
   struct value *value;
   PyObject *address;
-  PyObject *type;
   PyObject *dynamic_type;
   PyObject *content_bytes;
 };
@@ -84,8 +83,6 @@  valpy_clear_value (value_object *self)
   self->value = nullptr;
 
   Py_CLEAR (self->address);
-  Py_CLEAR (self->type);
-  Py_CLEAR (self->dynamic_type);
   Py_CLEAR (self->content_bytes);
 }
 
@@ -438,14 +435,7 @@  valpy_get_type (PyObject *self, void *closure)
 {
   value_object *obj = (value_object *) self;
 
-  if (!obj->type)
-    {
-      obj->type = type_to_type_object (obj->value->type ());
-      if (!obj->type)
-	return NULL;
-    }
-  Py_INCREF (obj->type);
-  return obj->type;
+  return type_to_type_object (obj->value->type ());
 }
 
 /* Return dynamic type of the value.  */
@@ -454,13 +444,7 @@  static PyObject *
 valpy_get_dynamic_type (PyObject *self, void *closure)
 {
   value_object *obj = (value_object *) self;
-  struct type *type = NULL;
-
-  if (obj->dynamic_type != NULL)
-    {
-      Py_INCREF (obj->dynamic_type);
-      return obj->dynamic_type;
-    }
+  struct type *type = nullptr;
 
   try
     {
@@ -493,23 +477,14 @@  valpy_get_dynamic_type (PyObject *self, void *closure)
       else if (type->code () == TYPE_CODE_STRUCT)
 	type = value_rtti_type (val, NULL, NULL, NULL);
       else
-	{
-	  /* Re-use object's static type.  */
-	  type = NULL;
-	}
+	type = val->type ();
     }
   catch (const gdb_exception &except)
     {
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  if (type == NULL)
-    obj->dynamic_type = valpy_get_type (self, NULL);
-  else
-    obj->dynamic_type = type_to_type_object (type);
-
-  Py_XINCREF (obj->dynamic_type);
-  return obj->dynamic_type;
+  return type_to_type_object (type);
 }
 
 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
@@ -1937,15 +1912,14 @@  value_to_value_object (struct value *val)
   value_object *val_obj;
 
   val_obj = PyObject_New (value_object, &value_object_type);
-  if (val_obj != NULL)
+  if (val_obj != nullptr)
     {
       val->incref ();
       val_obj->value = val;
       val_obj->next = nullptr;
       val_obj->prev = nullptr;
-      val_obj->address = NULL;
-      val_obj->type = NULL;
-      val_obj->dynamic_type = NULL;
+      val_obj->address = nullptr;
+      val_obj->dynamic_type = nullptr;
       val_obj->content_bytes = nullptr;
       note_value (val_obj);
     }