gdb/python: do not hold on gdb.Type object from gdb.Value

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.

Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Jan Vrany 2025-03-19 21:12:53 +00:00
parent 974c8ea576
commit ea8b10443b

View File

@ -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);
}