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 7/9] gdb/python: accept gdbpy_ref in init helpers and return bool
Date: Tue, 3 Mar 2026 16:16:57 +0000 [thread overview]
Message-ID: <20260303161659.397427-8-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260303161659.397427-1-matthieu.longo@arm.com>
Passing 'gdbpy_ref<> &' instead of raw 'PyObject *' to init helpers
makes ownership of PyObject clearer at call sites, and removes
unnecessary '.get()' calls.
Changing the return type from 'int' to 'bool' improves readability
and better expresses the success/failure semantics.
---
gdb/python/py-objfile.c | 27 ++++++++++++---------------
gdb/python/py-progspace.c | 22 +++++++++++-----------
2 files changed, 23 insertions(+), 26 deletions(-)
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 5d7cfe83ec2..feae62c32a8 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -198,36 +198,36 @@ objfpy_dealloc (PyObject *o)
/* Initialize an objfile_object.
The result is a boolean indicating success. */
-static int
-objfpy_initialize (objfile_object *self)
+static bool
+objfpy_initialize (gdbpy_ref<objfile_object> &self)
{
self->objfile = NULL;
self->dict = PyDict_New ();
if (self->dict == NULL)
- return 0;
+ return false;
self->printers = PyList_New (0);
if (self->printers == NULL)
- return 0;
+ return false;
self->frame_filters = PyDict_New ();
if (self->frame_filters == NULL)
- return 0;
+ return false;
self->frame_unwinders = PyList_New (0);
if (self->frame_unwinders == NULL)
- return 0;
+ return false;
self->type_printers = PyList_New (0);
if (self->type_printers == NULL)
- return 0;
+ return false;
self->xmethods = PyList_New (0);
if (self->xmethods == NULL)
- return 0;
+ return false;
- return 1;
+ return true;
}
static PyObject *
@@ -235,11 +235,8 @@ objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
{
gdbpy_ref<objfile_object> self ((objfile_object *) type->tp_alloc (type, 0));
- if (self != NULL)
- {
- if (!objfpy_initialize (self.get ()))
- return NULL;
- }
+ if (self != nullptr && ! objfpy_initialize (self))
+ return nullptr;
return (PyObject *) self.release ();
}
@@ -682,7 +679,7 @@ objfile_to_objfile_object (struct objfile *objfile)
((objfile_object *) PyObject_New (objfile_object, &objfile_object_type));
if (object == NULL)
return NULL;
- if (!objfpy_initialize (object.get ()))
+ if (! objfpy_initialize (object))
return NULL;
object->objfile = objfile;
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 146360d890d..15f7fbead65 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -166,40 +166,40 @@ pspy_dealloc (PyObject *self)
/* Initialize a pspace_object.
The result is a boolean indicating success. */
-static int
-pspy_initialize (pspace_object *self)
+static bool
+pspy_initialize (gdbpy_ref<pspace_object> &self)
{
self->pspace = NULL;
self->dict = PyDict_New ();
if (self->dict == NULL)
- return 0;
+ return false;
self->printers = PyList_New (0);
if (self->printers == NULL)
- return 0;
+ return false;
self->frame_filters = PyDict_New ();
if (self->frame_filters == NULL)
- return 0;
+ return false;
self->frame_unwinders = PyList_New (0);
if (self->frame_unwinders == NULL)
- return 0;
+ return false;
self->type_printers = PyList_New (0);
if (self->type_printers == NULL)
- return 0;
+ return false;
self->xmethods = PyList_New (0);
if (self->xmethods == NULL)
- return 0;
+ return false;
self->missing_file_handlers = PyList_New (0);
if (self->missing_file_handlers == nullptr)
- return 0;
+ return false;
- return 1;
+ return true;
}
PyObject *
@@ -593,7 +593,7 @@ pspace_to_pspace_object (struct program_space *pspace)
if (object == nullptr)
return nullptr;
- if (! pspy_initialize (object.get ()))
+ if (! pspy_initialize (object))
return nullptr;
object->pspace = pspace;
--
2.53.0
next prev parent reply other threads:[~2026-03-03 16:22 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 ` Matthieu Longo [this message]
2026-03-03 18:24 ` [PATCH v2 7/9] gdb/python: accept gdbpy_ref in init helpers and return bool 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 ` [PATCH v2 9/9] gdb/python: add accessor helpers for __dict__ in Python extension objects Matthieu Longo
2026-03-03 19:02 ` 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-8-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