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 4/7] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T
Date: Mon, 9 Mar 2026 17:56:21 +0000 [thread overview]
Message-ID: <20260309175624.236491-5-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260309175624.236491-1-matthieu.longo@arm.com>
When ref_ptr<T,Policy>::new_reference() is specialized for 'PyObject'
(i.e. gdbpy_ref<>), it currently requires the argument type to be exactly
'PyObject *'. As a result, pointers to subclasses of 'PyObject' must be
explicitly cast before being passed, making call sites unnecessarily
verbose.
This patch makes ref_ptr<T,Policy>::new_reference() a template method
that accepts both T and subclasses of T, performing the cast to 'T *'
internally when needed. This removes redundant casts at call sites
without changing behavior.
---
gdb/python/py-block.c | 2 +-
gdb/python/py-inferior.c | 2 +-
gdbsupport/gdb_ref_ptr.h | 5 ++++-
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index 602e86c3766..d56253141f0 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -346,7 +346,7 @@ block_to_block_object (const struct block *block, struct objfile *objfile)
block_object *result = (block_object *) htab_find_with_hash (table, block,
hash);
if (result != nullptr)
- return gdbpy_ref<>::new_reference ((PyObject *) result);
+ return gdbpy_ref<>::new_reference (result);
result = PyObject_New (block_object, &block_object_type);
if (result == nullptr)
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 76e3da9f620..e55360282b6 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -400,7 +400,7 @@ infpy_threads (PyObject *self, PyObject *args)
for (const thread_map_t::value_type &entry : *inf_obj->threads)
{
- auto thr = gdbpy_ref<>::new_reference ((PyObject *) entry.second.get ());
+ auto thr = gdbpy_ref<>::new_reference (entry.second.get ());
if (PyTuple_SetItem (tuple.get (), i++, thr.release ()) < 0)
return nullptr;
}
diff --git a/gdbsupport/gdb_ref_ptr.h b/gdbsupport/gdb_ref_ptr.h
index 4352ab3e7c0..0eb654324c6 100644
--- a/gdbsupport/gdb_ref_ptr.h
+++ b/gdbsupport/gdb_ref_ptr.h
@@ -197,9 +197,12 @@ class ref_ptr
}
/* Acquire a new reference and return a ref_ptr that owns it. */
- static ref_ptr<T, Policy> new_reference (T *obj)
+ template <class TObj>
+ static ref_ptr<T, Policy> new_reference (TObj *obj)
{
Policy::incref (obj);
+ if constexpr (std::is_base_of<T, TObj>::value)
+ return ref_ptr<T, Policy> (static_cast<T *> (obj));
return ref_ptr<T, Policy> (obj);
}
--
2.53.0
next prev parent reply other threads:[~2026-03-09 17:59 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 ` Matthieu Longo [this message]
2026-03-09 19:40 ` [PATCH v3 4/7] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T 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 ` [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-5-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