Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>, Tom Tromey <tom@tromey.com>
Cc: Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v2 9/9] gdb/python: add accessor helpers for __dict__ in Python extension objects
Date: Tue, 3 Mar 2026 16:16:59 +0000	[thread overview]
Message-ID: <20260303161659.397427-10-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260303161659.397427-1-matthieu.longo@arm.com>

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_PyGetSetDef, which defines a getter for __dict__.
- gdbpy_dict_wrapper_PyTypeObject_tps, 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.
---
 gdb/python/py-corefile.c  |  6 ++----
 gdb/python/py-event.c     |  6 ++----
 gdb/python/py-inferior.c  |  6 ++----
 gdb/python/py-infthread.c |  6 ++----
 gdb/python/py-objfile.c   |  6 ++----
 gdb/python/py-progspace.c |  6 ++----
 gdb/python/py-ref.h       | 18 ++++++++++++++++--
 gdb/python/py-type.c      |  8 +++-----
 8 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 5aefd2092c8..1b2c66be6c7 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -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_PyGetSetDef ("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_PyTypeObject_tps,
   0,				  /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
   "GDB corefile object",	  /* tp_doc */
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index 47c2ba4c84d..1ba1836694e 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -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_PyGetSetDef ("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_PyTypeObject_tps,
   0,                                          /* tp_as_buffer */
   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
   "GDB event object",                         /* tp_doc */
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index e1cd1b8f9dd..1c5f0348a0c 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -1033,8 +1033,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_PyGetSetDef ("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 },
@@ -1116,8 +1115,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_PyTypeObject_tps,
   0,				  /* tp_as_buffer */
   Py_TPFLAGS_DEFAULT,		  /* tp_flags */
   "GDB inferior object",	  /* tp_doc */
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index ca9a34632d6..3b099f7ce53 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -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_PyGetSetDef ("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_PyTypeObject_tps,
   0,				  /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
   "GDB thread object",		  /* tp_doc */
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 289182c794d..493df334059 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -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_PyGetSetDef ("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_PyTypeObject_tps,
   0,				  /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
   "GDB objfile object",		  /* tp_doc */
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index c38fd740286..eedfe1966bb 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -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_PyGetSetDef ("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_PyTypeObject_tps,
   0,				  /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
   "GDB progspace object",	  /* tp_doc */
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index f67e247cc98..6db5ccf4858 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -54,8 +54,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_PyGetSetDef ("object"),
        ...
        { nullptr }
      };
@@ -82,6 +81,21 @@ struct gdbpy_dict_wrapper : public PyObject
     return &wrapper->dict;
   }
 
+  #define gdbpy_dict_wrapper_PyGetSetDef(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_PyTypeObject_tps				\
+    /*tp_getattro*/							\
+    gdb_py_generic_getattro,						\
+    /*tp_setattro*/							\
+    gdb_py_generic_setattro
+
   template <class T>
   static bool
   allocate_dict (gdbpy_ref<T>& self)
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index a4b046be1ad..c34ebabe511 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1700,9 +1700,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_PyGetSetDef ("field"),
+  { nullptr }
 };
 
 PyTypeObject field_object_type =
@@ -1723,8 +1722,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_PyTypeObject_tps,
   0,				  /*tp_as_buffer*/
   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
   "GDB field object",		  /* tp_doc */
-- 
2.53.0


  parent reply	other threads:[~2026-03-03 16:20 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-03 16:16 [PATCH v2 0/9] gdb: more fixes for Python limited C API support Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 1/9] gdb: switch tuple object helpers to Python limited API equivalents Matthieu Longo
2026-03-03 18:09   ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 2/9] gdb: introduce rgb_color type to simplify existing code Matthieu Longo
2026-03-03 18:16   ` Tom Tromey
2026-03-04 16:30     ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 3/9] gdb: switch bytes object helpers to Python limited API equivalents Matthieu Longo
2026-03-03 18:03   ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 4/9] gdb: add new helpers for retrieving a type's fully qualified name Matthieu Longo
2026-03-03 18:59   ` Tom Tromey
2026-03-06 17:49     ` Matthieu Longo
2026-03-06 19:45       ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 5/9] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T Matthieu Longo
2026-03-03 18:18   ` Tom Tromey
2026-03-04 16:56     ` Matthieu Longo
2026-03-04 18:55       ` Tom Tromey
2026-03-06 11:37     ` Matthieu Longo
2026-03-06 11:43       ` Matthieu Longo
2026-03-06 16:47       ` Tom Tromey
2026-03-09 11:38         ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 6/9] gdb/python: flatten functions calling PyObject_New and use gdbpy_ref Matthieu Longo
2026-03-03 18:22   ` Tom Tromey
2026-03-09 11:41     ` Matthieu Longo
2026-03-03 18:22   ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 7/9] gdb/python: accept gdbpy_ref in init helpers and return bool Matthieu Longo
2026-03-03 18:24   ` Tom Tromey
2026-03-09 13:25     ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 8/9] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper Matthieu Longo
2026-03-03 18:30   ` Tom Tromey
2026-03-06 12:03     ` Matthieu Longo
2026-03-03 16:16 ` Matthieu Longo [this message]
2026-03-03 19:02   ` [PATCH v2 9/9] gdb/python: add accessor helpers for __dict__ in Python extension objects Tom Tromey
2026-03-06 14:33     ` Matthieu Longo
2026-03-06 16:04       ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260303161659.397427-10-matthieu.longo@arm.com \
    --to=matthieu.longo@arm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox