]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: add accessor helpers for __dict__ in Python extension objects
authorMatthieu Longo <matthieu.longo@arm.com>
Fri, 27 Feb 2026 14:13:26 +0000 (14:13 +0000)
committerMatthieu Longo <matthieu.longo@arm.com>
Sat, 14 Mar 2026 13:21:06 +0000 (13:21 +0000)
Python extension objects that support __dict__ must inherit from
gdbpy_dict_wrapper, a wrapper class that stores the PyObject
corresponding to the __dict__ attribute. Because this dictionary
is not managed directly by Python, each extension objects must
provide appropriate accessors so that attributes stored in __dict__
are looked up correctly.

Currently, management of this dictionary is not centralized, and
each Python extension object implements its own logic to create,
access, and destroy it.

A previous patch centralized the creation logic; this patch focuses
on centralizing the access to the dictionary. It introduces two new
macros:
- gdbpy_dict_wrapper_cfg_dict_getter, which defines a getter for
  __dict__.
- gdbpy_dict_wrapper_getsetattro, which sets tp_getattro and
  tp_setattro in PyTypeObject to gdb_py_generic_getattro and
  gdb_py_generic_setattro, respectively. These helpers already
  centralizes attribute access for Python extension objects having
  a __dict__.
  Note: this macro will eventually be removed once Python extension
  types are migrated to heap-allocated types.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/python/py-corefile.c
gdb/python/py-event.c
gdb/python/py-inferior.c
gdb/python/py-infthread.c
gdb/python/py-objfile.c
gdb/python/py-progspace.c
gdb/python/py-ref.h
gdb/python/py-type.c

index 1cca0fb2f6b9fce269e637f3c1145df78fe44651..d35838c75237e8f9ae3172f2276cad57720544c3 100644 (file)
@@ -500,8 +500,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_corefile);
 
 static gdb_PyGetSetDef corefile_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, nullptr,
-    "The __dict__ for the gdb.Corefile.", nullptr },
+  gdbpy_dict_wrapper_cfg_dict_getter ("corefile"),
   { "filename", cfpy_get_filename, nullptr,
     "The filename of a valid Corefile object.", nullptr },
   { nullptr }
@@ -537,8 +536,7 @@ PyTypeObject corefile_object_type =
   0,                             /*tp_hash */
   0,                             /*tp_call*/
   0,                             /*tp_str*/
-  gdb_py_generic_getattro,       /*tp_getattro*/
-  gdb_py_generic_setattro,       /*tp_setattro*/
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB corefile object",         /* tp_doc */
index 8192d1cefa0363f60c38ff323a076aafbf957560..a7aa46dcb6f29039cd7ba7e5e3802ed5aec3abe5 100644 (file)
@@ -100,8 +100,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_event);
 
 static gdb_PyGetSetDef event_object_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, NULL,
-    "The __dict__ for this event.", NULL },
+  gdbpy_dict_wrapper_cfg_dict_getter ("event"),
   { NULL }
 };
 
@@ -123,8 +122,7 @@ PyTypeObject event_object_type =
   0,                                          /* tp_hash  */
   0,                                          /* tp_call */
   0,                                          /* tp_str */
-  gdb_py_generic_getattro,                    /* tp_getattro */
-  gdb_py_generic_setattro,                    /* tp_setattro */
+  gdbpy_dict_wrapper_getsetattro,
   0,                                          /* tp_as_buffer */
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
   "GDB event object",                         /* tp_doc */
index 9fa416f25bf859b2388798be02ad178fdb918d26..ea9f884a397a0b5bc42686917b382360773b5257 100644 (file)
@@ -1087,8 +1087,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_inferior);
 
 static gdb_PyGetSetDef inferior_object_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, nullptr,
-    "The __dict__ for this inferior.", nullptr },
+  gdbpy_dict_wrapper_cfg_dict_getter ("inferior"),
   { "arguments", infpy_get_args, infpy_set_args,
     "Arguments to this program.", nullptr },
   { "num", infpy_get_num, NULL, "ID of inferior, as assigned by GDB.", NULL },
@@ -1170,8 +1169,7 @@ PyTypeObject inferior_object_type =
   0,                             /* tp_hash  */
   0,                             /* tp_call */
   0,                             /* tp_str */
-  gdb_py_generic_getattro,       /* tp_getattro */
-  gdb_py_generic_setattro,       /* tp_setattro */
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /* tp_as_buffer */
   Py_TPFLAGS_DEFAULT,            /* tp_flags */
   "GDB inferior object",         /* tp_doc */
index 7c2d0bc023ad6a1f03cf8a0fd19c4c351d778286..cd9e2304ba83eecc39e7dee38eeaf7604a39323f 100644 (file)
@@ -415,8 +415,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_thread);
 
 static gdb_PyGetSetDef thread_object_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, nullptr,
-    "The __dict__ for this thread.", nullptr },
+  gdbpy_dict_wrapper_cfg_dict_getter ("thread"),
   { "name", thpy_get_name, thpy_set_name,
     "The name of the thread, as set by the user or the OS.", NULL },
   { "details", thpy_get_details, NULL,
@@ -479,8 +478,7 @@ PyTypeObject thread_object_type =
   0,                             /*tp_hash */
   0,                             /*tp_call*/
   0,                             /*tp_str*/
-  gdb_py_generic_getattro,       /*tp_getattro*/
-  gdb_py_generic_setattro,       /*tp_setattro*/
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB thread object",           /* tp_doc */
index ea04335697144c5540e2b4a17a9f311e24964e29..d427f14942931ae3f1509db94c9e2cc1d4fdb57f 100644 (file)
@@ -725,8 +725,7 @@ Look up a static-linkage global symbol in this objfile and return it." },
 
 static gdb_PyGetSetDef objfile_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, NULL,
-    "The __dict__ for this objfile.", NULL },
+  gdbpy_dict_wrapper_cfg_dict_getter ("objfile"),
   { "filename", objfpy_get_filename, NULL,
     "The objfile's filename, or None.", NULL },
   { "username", objfpy_get_username, NULL,
@@ -771,8 +770,7 @@ PyTypeObject objfile_object_type =
   0,                             /*tp_hash */
   0,                             /*tp_call*/
   0,                             /*tp_str*/
-  gdb_py_generic_getattro,       /*tp_getattro*/
-  gdb_py_generic_setattro,       /*tp_setattro*/
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB objfile object",                  /* tp_doc */
index d1a8479588fa34965020150d1597246eea67acdb..37b92a64634c071f350a32bbcdd023f7f82077b5 100644 (file)
@@ -751,8 +751,7 @@ GDBPY_INITIALIZE_FILE (gdbpy_initialize_pspace);
 
 static gdb_PyGetSetDef pspace_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, NULL,
-    "The __dict__ for this progspace.", NULL },
+  gdbpy_dict_wrapper_cfg_dict_getter ("progspace"),
   { "filename", pspy_get_filename, NULL,
     "The filename of the progspace's main symbol file, or None.", nullptr },
   { "symbol_file", pspy_get_symbol_file, nullptr,
@@ -814,8 +813,7 @@ PyTypeObject pspace_object_type =
   0,                             /*tp_hash */
   0,                             /*tp_call*/
   0,                             /*tp_str*/
-  gdb_py_generic_getattro,       /*tp_getattro*/
-  gdb_py_generic_setattro,       /*tp_setattro*/
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB progspace object",        /* tp_doc */
index cd547bf83a53e7c1915a43211e90221a962dc991..dc0b14814afb3be951af59718ce9c4bb43c08e51 100644 (file)
@@ -50,8 +50,7 @@ template<typename T = PyObject> using gdbpy_ref
    Access to the dict requires a custom getter defined via PyGetSetDef.
      gdb_PyGetSetDef my_object_getset[] =
      {
-       { "__dict__", gdb_py_generic_dict_getter, nullptr,
-        "The __dict__ for this object.", nullptr },
+       gdbpy_dict_wrapper_cfg_dict_getter ("object"),
        ...
        { nullptr }
      };
@@ -78,6 +77,21 @@ struct gdbpy_dict_wrapper : public PyObject
     return &wrapper->dict;
   }
 
+#define gdbpy_dict_wrapper_cfg_dict_getter(object_name)        \
+  {                                                    \
+    "__dict__", /* name */                             \
+    (getter) gdb_py_generic_dict_getter,               \
+    (setter) nullptr,                                  \
+    "The __dict__ for this " object_name ".", /* doc */        \
+    nullptr, /* closure */                             \
+  }
+
+#define gdbpy_dict_wrapper_getsetattro \
+  /*tp_getattro*/                      \
+  gdb_py_generic_getattro,             \
+  /*tp_setattro*/                      \
+  gdb_py_generic_setattro
+
   /* Allocate the dictionary pointed by 'dict'.
      Note: this method should be called once the object was allocated,
      when setting its attributes.  */
index 0dec791256d29a54d0a542c60f19b79b9544960a..e1c57928b08c9354f058cdfd86c617e668f58c7e 100644 (file)
@@ -1699,9 +1699,8 @@ PyTypeObject type_object_type =
 
 static gdb_PyGetSetDef field_object_getset[] =
 {
-  { "__dict__", gdb_py_generic_dict_getter, NULL,
-    "The __dict__ for this field.", NULL },
-  { NULL }
+  gdbpy_dict_wrapper_cfg_dict_getter ("field"),
+  { nullptr }
 };
 
 PyTypeObject field_object_type =
@@ -1722,8 +1721,7 @@ PyTypeObject field_object_type =
   0,                             /*tp_hash */
   0,                             /*tp_call*/
   0,                             /*tp_str*/
-  gdb_py_generic_getattro,       /*tp_getattro*/
-  gdb_py_generic_setattro,       /*tp_setattro*/
+  gdbpy_dict_wrapper_getsetattro,
   0,                             /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB field object",            /* tp_doc */