[binutils-gdb] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T

Matthieu Longo via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f0cfbf6ceda8396291d736858cfd713cc4a0e0da

commit f0cfbf6ceda8396291d736858cfd713cc4a0e0da
Author: Matthieu Longo <[email protected]>
Date:   Thu Feb 26 17:20:20 2026 +0000

    gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T
    
    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.
    
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 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 263819e1292..4d77242ca0d 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..ed28ccf3c07 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 ());
+      gdbpy_ref<> thr = entry.second;
       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 @@ public:
   }
 
   /* 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);
   }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.