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 5/7] gdb/python: flatten functions calling PyObject_New and use gdbpy_ref
Date: Mon, 9 Mar 2026 17:56:22 +0000 [thread overview]
Message-ID: <20260309175624.236491-6-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260309175624.236491-1-matthieu.longo@arm.com>
This patch aims at systematically using gdbpy_ref<> at all call sites
of PyObject_New(). This prepares for future patches that expect
gdbby_ref<> parameters and affect return handling.
As part of this change, flattening the affected functions so that the
return logic becomes clearer and more flexible to adjust.
---
gdb/python/py-corefile.c | 48 +++++++++++++++++++--------------------
gdb/python/py-inferior.c | 35 +++++++++++++---------------
gdb/python/py-progspace.c | 31 +++++++++++++------------
3 files changed, 57 insertions(+), 57 deletions(-)
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 762348121a7..de88a964efb 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -119,33 +119,33 @@ gdbpy_core_file_from_inferior (inferior *inf)
return gdbpy_ref<>::new_reference (Py_None);
PyObject *result = (PyObject *) cfpy_inferior_corefile_data_key.get (inf);
- if (result == nullptr)
- {
- gdbpy_ref<corefile_object> object
- (PyObject_New (corefile_object, &corefile_object_type));
- if (object == nullptr)
- return nullptr;
+ if (result != nullptr)
+ return gdbpy_ref<>::new_reference (result);
- /* Ensure the 'inferior' field is set to NULL. If the PyDict_New
- call fails then the gdb.Corefile will be discarded and
- cfpy_dealloc will be called, which requires that the 'inferior' be
- set to NULL. */
- object->inferior = nullptr;
- object->mapped_files = nullptr;
- object->dict = PyDict_New ();
- if (object->dict == nullptr)
- return nullptr;
+ gdbpy_ref<corefile_object> object
+ (PyObject_New (corefile_object, &corefile_object_type));
+ if (object == nullptr)
+ return nullptr;
- /* Now that the gdb.Corefile has been successfully initialised and we
- know that it is going to be passed back to the user, move it out
- of the invalid state by setting the 'inferior' field to a non NULL
- value. */
- object->inferior = inf;
- cfpy_inferior_corefile_data_key.set (inf, object.get ());
- result = (PyObject *) object.release ();
- }
+ /* Ensure the 'inferior' field is set to NULL. If the PyDict_New call fails
+ then the gdb.Corefile will be discarded and cfpy_dealloc will be called,
+ which requires that the 'inferior' be set to NULL. */
+ object->inferior = nullptr;
+ object->mapped_files = nullptr;
+ object->dict = PyDict_New ();
+ if (object->dict == nullptr)
+ return nullptr;
+
+ /* Now that the gdb.Corefile has been successfully initialised and we know
+ that it is going to be passed back to the user, move it out of the invalid
+ state by setting the 'inferior' field to a non NULL value. */
+ object->inferior = inf;
+
+ /* PyObject_New initializes the new object with a refcount of 1. This counts
+ for the reference we are keeping in the inferior corefile data. */
+ cfpy_inferior_corefile_data_key.set (inf, object.get ());
- return gdbpy_ref<>::new_reference (result);
+ return gdbpy_ref<>::new_reference (object.release ());
}
/* Return true if OBJ is valid. */
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index e55360282b6..4ad0ea1b264 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -213,29 +213,26 @@ python_free_objfile (struct objfile *objfile)
gdbpy_ref<inferior_object>
inferior_to_inferior_object (struct inferior *inferior)
{
- inferior_object *inf_obj;
+ inferior_object *result = infpy_inf_data_key.get (inferior);
+ if (result != nullptr)
+ return gdbpy_ref<inferior_object>::new_reference (result);
- inf_obj = infpy_inf_data_key.get (inferior);
- if (!inf_obj)
- {
- inf_obj = PyObject_New (inferior_object, &inferior_object_type);
- if (!inf_obj)
- return NULL;
+ gdbpy_ref<inferior_object> inf_obj
+ (PyObject_New (inferior_object, &inferior_object_type));
+ if (inf_obj == nullptr)
+ return nullptr;
- inf_obj->inferior = inferior;
- inf_obj->threads = new thread_map_t ();
- inf_obj->dict = PyDict_New ();
- if (inf_obj->dict == nullptr)
- return nullptr;
+ inf_obj->inferior = inferior;
+ inf_obj->threads = new thread_map_t ();
+ inf_obj->dict = PyDict_New ();
+ if (inf_obj->dict == nullptr)
+ return nullptr;
- /* PyObject_New initializes the new object with a refcount of 1. This
- counts for the reference we are keeping in the inferior data. */
- infpy_inf_data_key.set (inferior, inf_obj);
- }
+ /* PyObject_New initializes the new object with a refcount of 1. This counts
+ for the reference we are keeping in the inferior data. */
+ infpy_inf_data_key.set (inferior, inf_obj.get ());
- /* We are returning a new reference. */
- gdb_assert (inf_obj != nullptr);
- return gdbpy_ref<inferior_object>::new_reference (inf_obj);
+ return gdbpy_ref<inferior_object>::new_reference (inf_obj.release ());
}
/* Called when a new inferior is created. Notifies any Python event
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 5a23c4c7177..f2585103346 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -585,21 +585,24 @@ gdbpy_ref<>
pspace_to_pspace_object (struct program_space *pspace)
{
PyObject *result = (PyObject *) pspy_pspace_data_key.get (pspace);
- if (result == NULL)
- {
- gdbpy_ref<pspace_object> object
- ((pspace_object *) PyObject_New (pspace_object, &pspace_object_type));
- if (object == NULL)
- return NULL;
- if (!pspy_initialize (object))
- return NULL;
-
- object->pspace = pspace;
- pspy_pspace_data_key.set (pspace, object.get ());
- result = (PyObject *) object.release ();
- }
+ if (result != nullptr)
+ return gdbpy_ref<>::new_reference (result);
+
+ gdbpy_ref<pspace_object> object
+ (PyObject_New (pspace_object, &pspace_object_type));
+ if (object == nullptr)
+ return nullptr;
+
+ if (!pspy_initialize (object))
+ return nullptr;
+
+ object->pspace = pspace;
+
+ /* PyObject_New initializes the new object with a refcount of 1. This counts
+ for the reference we are keeping in the pspace data. */
+ pspy_pspace_data_key.set (pspace, object.get ());
- return gdbpy_ref<>::new_reference (result);
+ return gdbpy_ref<>::new_reference (object.release ());
}
/* See python-internal.h. */
--
2.53.0
next prev parent reply other threads:[~2026-03-09 18:00 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 ` Matthieu Longo [this message]
2026-03-09 20:07 ` [PATCH v3 5/7] gdb/python: flatten functions calling PyObject_New and use gdbpy_ref Tom Tromey
2026-03-09 17:56 ` [PATCH v3 6/7] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper Matthieu Longo
2026-03-09 20:09 ` 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-6-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