[binutils-gdb] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper

Matthieu Longo via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=28d828f4df9c30b25ba0513dd6cc10ba37a4c967

commit 28d828f4df9c30b25ba0513dd6cc10ba37a4c967
Author: Matthieu Longo <[email protected]>
Date:   Fri Feb 27 10:30:36 2026 +0000

    gdb/python: add gdbpy_dict_wrapper:allocate_dict helper
    
    Python extension objects that support __dict__ must inherit from
    gdbpy_dict_wrapper, a wrapper class that stores the PyObject
    corresponding to the __dict__ attribute.
    
    Currently, management of this dictionary is not centralized, and
    each Python extension object implements its own logic to create,
    access, and destroy it.
    
    This patch focuses on the allocation of the dictionary, introduces
    a new method, gdbpy_dict_wrapper::allocate_dict(), and
    adapts the existing code to use this method.
    
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/python/py-corefile.c  | 3 +--
 gdb/python/py-event.c     | 7 +++----
 gdb/python/py-inferior.c  | 3 +--
 gdb/python/py-infthread.c | 7 +++----
 gdb/python/py-objfile.c   | 7 +++----
 gdb/python/py-progspace.c | 3 +--
 gdb/python/py-ref.h       | 9 +++++++++
 gdb/python/py-type.c      | 9 +++------
 8 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 25af8710d5e..1cca0fb2f6b 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -132,8 +132,7 @@ gdbpy_core_file_from_inferior (inferior *inf)
      which requires that the 'inferior' be set to NULL.  */
   object->inferior = nullptr;
   object->mapped_files = nullptr;
-  object->dict = PyDict_New ();
-  if (object->dict == nullptr)
+  if (!object->allocate_dict ())
     return nullptr;
 
   /* Now that the gdb.Corefile has been successfully initialised and we know
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index 39e1dd9c75f..8192d1cefa0 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -31,11 +31,10 @@ create_event_object (PyTypeObject *py_type)
 {
   gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
   if (event_obj == NULL)
-    return NULL;
+    return nullptr;
 
-  event_obj->dict = PyDict_New ();
-  if (!event_obj->dict)
-    return NULL;
+  if (!event_obj->allocate_dict ())
+    return nullptr;
 
   return event_obj;
 }
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index f4fdd4df6cc..9fa416f25bf 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -224,8 +224,7 @@ inferior_to_inferior_object (struct inferior *inferior)
 
   inf_obj->inferior = inferior;
   inf_obj->threads = new thread_map_t ();
-  inf_obj->dict = PyDict_New ();
-  if (inf_obj->dict == nullptr)
+  if (!inf_obj->allocate_dict ())
     return nullptr;
 
   /* PyObject_New initializes the new object with a refcount of 1.  This counts
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index 652355990ee..7c2d0bc023a 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -41,16 +41,15 @@ create_thread_object (struct thread_info *tp)
 
   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
   if (inf_obj == NULL)
-    return NULL;
+    return nullptr;
 
   thread_obj.reset (PyObject_New (thread_object, &thread_object_type));
   if (thread_obj == NULL)
-    return NULL;
+    return nullptr;
 
   thread_obj->thread = tp;
   thread_obj->inf_obj = (PyObject *) inf_obj.release ();
-  thread_obj->dict = PyDict_New ();
-  if (thread_obj->dict == nullptr)
+  if (!thread_obj->allocate_dict ())
     return nullptr;
 
   return thread_obj;
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index bbe21d32549..daf1b44747c 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -201,12 +201,11 @@ objfpy_dealloc (PyObject *o)
 static bool
 objfpy_initialize (gdbpy_ref<objfile_object> &self)
 {
-  self->objfile = NULL;
-
-  self->dict = PyDict_New ();
-  if (self->dict == NULL)
+  if (!self->allocate_dict ())
     return false;
 
+  self->objfile = NULL;
+
   self->printers = PyList_New (0);
   if (self->printers == NULL)
     return false;
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index f2585103346..d1a8479588f 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -171,8 +171,7 @@ pspy_initialize (gdbpy_ref<pspace_object> &self)
 {
   self->pspace = NULL;
 
-  self->dict = PyDict_New ();
-  if (self->dict == NULL)
+  if (!self->allocate_dict ())
     return false;
 
   self->printers = PyList_New (0);
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index 0a56436634d..cd547bf83a5 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -77,6 +77,15 @@ struct gdbpy_dict_wrapper : public PyObject
     auto *wrapper = reinterpret_cast<gdbpy_dict_wrapper *> (self);
     return &wrapper->dict;
   }
+
+  /* Allocate the dictionary pointed by 'dict'.
+     Note: this method should be called once the object was allocated,
+     when setting its attributes.  */
+  bool allocate_dict ()
+  {
+    dict = PyDict_New ();
+    return dict != nullptr;
+  }
 };
 
 #endif /* GDB_PYTHON_PY_REF_H */
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 7ba77ad1d4a..0dec791256d 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -93,12 +93,9 @@ field_new (void)
   gdbpy_ref<field_object> result (PyObject_New (field_object,
 						&field_object_type));
 
-  if (result != NULL)
-    {
-      result->dict = PyDict_New ();
-      if (!result->dict)
-	return NULL;
-    }
+  if (result != nullptr && !result->allocate_dict ())
+    return nullptr;
+
   return (PyObject *) result.release ();
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.