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 v3 6/7] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper
Date: Mon, 9 Mar 2026 17:56:23 +0000 [thread overview]
Message-ID: <20260309175624.236491-7-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260309175624.236491-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.
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.
---
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 | 7 +++++++
gdb/python/py-type.c | 9 +++------
8 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index de88a964efb..0c2821cb07e 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 4ad0ea1b264..eaf224fe808 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 0f29e9fb7a4..7afec0112fb 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..f0cdb38498e 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -77,6 +77,13 @@ struct gdbpy_dict_wrapper : public PyObject
auto *wrapper = reinterpret_cast<gdbpy_dict_wrapper *> (self);
return &wrapper->dict;
}
+
+ 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 ec4126d0589..0893da5aec9 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 ();
}
--
2.53.0
next prev parent reply other threads:[~2026-03-09 18:01 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-09 17:56 [PATCH v3 0/7] gdb: more fixes for Python limited C API support Matthieu Longo
2026-03-09 17:56 ` [PATCH v3 1/7] gdb: introduce rgb_color type to simplify existing code Matthieu Longo
2026-03-09 19:22 ` Tom Tromey
2026-03-11 15:03 ` Simon Marchi
2026-03-11 17:55 ` Matthieu Longo
2026-03-11 18:04 ` Simon Marchi
2026-03-11 18:16 ` Tom Tromey
2026-03-09 17:56 ` [PATCH v3 2/7] gdb: fail configure if Python version is too old for limited API Matthieu Longo
2026-03-24 18:53 ` Tom Tromey
2026-03-31 10:16 ` Matthieu Longo
2026-04-07 13:36 ` Matthieu Longo
2026-04-07 20:39 ` Tom Tromey
2026-03-09 17:56 ` [PATCH v3 3/7] gdb: add new helpers for retrieving a type's fully qualified name Matthieu Longo
2026-03-24 18:44 ` Tom Tromey
2026-03-09 17:56 ` [PATCH v3 4/7] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T Matthieu Longo
2026-03-09 19:40 ` Tom Tromey
2026-03-10 12:26 ` Matthieu Longo
2026-03-09 17:56 ` [PATCH v3 5/7] gdb/python: flatten functions calling PyObject_New and use gdbpy_ref Matthieu Longo
2026-03-09 20:07 ` Tom Tromey
2026-03-09 17:56 ` Matthieu Longo [this message]
2026-03-09 20:09 ` [PATCH v3 6/7] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper Tom Tromey
2026-03-10 12:26 ` Matthieu Longo
2026-03-09 17:56 ` [PATCH v3 7/7] gdb/python: add accessor helpers for __dict__ in Python extension objects Matthieu Longo
2026-03-13 18:09 ` Tom Tromey
2026-03-23 10:28 ` [PATCH v3 0/7] gdb: more fixes for Python limited C API support Matthieu Longo
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=20260309175624.236491-7-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