Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>, Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 1/4] gdb/python: add gdbpy_borrowed_ref
Date: Tue, 28 Apr 2026 17:24:13 +0100	[thread overview]
Message-ID: <20260428162416.511367-2-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260428162416.511367-1-matthieu.longo@arm.com>

From: Tom Tromey <tom@tromey.com>

This adds a new gdbpy_borrowed_ref class.  This class is primarily for
code "documentation" purposes -- it makes it clear to the reader that
a given reference is borrowed.  However, it also adds a tiny bit of
safety, in that conversion to gdbpy_ref<> will acquire a new
reference.
---
 gdb/python/py-ref.h | 115 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)

diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index dc0b14814af..8190dc38627 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -41,6 +41,121 @@ struct gdbpy_ref_policy
 template<typename T = PyObject> using gdbpy_ref
   = gdb::ref_ptr<T, gdbpy_ref_policy>;
 
+/* A class representing a borrowed reference.
+
+   This is a simple wrapper for a PyObject*.  Aside from documenting
+   what the code does, the main advantage of using this is that
+   conversion to a gdbpy_ref<> is guaranteed to make a new
+   reference.  */
+template <class T = PyObject>
+class gdbpy_borrowed_ref
+{
+public:
+
+  gdbpy_borrowed_ref () noexcept
+    : m_obj (nullptr)
+  {
+  }
+
+  gdbpy_borrowed_ref (const std::nullptr_t) noexcept
+    : m_obj (nullptr)
+  {
+  }
+
+  template <typename U,
+	    typename = std::is_convertible<U *, T*>>
+  gdbpy_borrowed_ref (U *obj) noexcept
+    : m_obj (obj)
+  {
+  }
+
+  template <typename U,
+	    typename = std::is_convertible<U *, T*>>
+  gdbpy_borrowed_ref (const gdbpy_ref<U> &ref) noexcept
+    : m_obj (ref.get ())
+  {
+  }
+
+  gdbpy_borrowed_ref (const gdbpy_borrowed_ref &other) noexcept
+    : m_obj (other.m_obj)
+  {
+  }
+
+  gdbpy_borrowed_ref &operator= (const gdbpy_borrowed_ref &other)
+  {
+    m_obj = other.m_obj;
+    return *this;
+  }
+
+  operator bool () const noexcept
+  {
+    return m_obj != nullptr;
+  }
+
+  operator T * () const noexcept
+  {
+    return m_obj;
+  }
+
+  /* Explicit version of the previous implicit cast operator.  */
+  T *get () const noexcept
+  {
+    return m_obj;
+  }
+
+  /* When converting a borrowed reference to a gdbpy_ref<>, a new
+     reference is acquired.  */
+  template <typename U,
+	    typename = std::is_convertible<U *, T *>>
+  operator gdbpy_ref<U> ()
+  {
+    if (m_obj != nullptr)
+      return gdbpy_ref<U>::new_reference (m_obj);
+    return nullptr;
+  }
+
+  /* Convert a borrowed reference to a strong one, i.e. gdbpy_ref<>.  */
+  gdbpy_ref<T> strong_ref () const noexcept
+  {
+    if (m_obj != nullptr)
+      return gdbpy_ref<T>::new_reference (m_obj);
+    return nullptr;
+  }
+
+  /* Let users refer to members of the underlying pointer.  */
+  T *operator-> () const noexcept
+  {
+    return m_obj;
+  }
+
+private:
+  T *m_obj;
+};
+
+template<typename T>
+inline bool operator== (const gdbpy_borrowed_ref<T> &lhs, const std::nullptr_t)
+{
+  return lhs.get () == nullptr;
+}
+
+template<typename T>
+inline bool operator!= (const gdbpy_borrowed_ref<T> &lhs, const std::nullptr_t)
+{
+  return lhs.get () != nullptr;
+}
+
+template<typename T>
+inline bool operator== (const std::nullptr_t, const gdbpy_borrowed_ref<T> &rhs)
+{
+  return nullptr == rhs.get ();
+}
+
+template<typename T>
+inline bool operator!= (const std::nullptr_t, const gdbpy_borrowed_ref<T> &rhs)
+{
+  return nullptr != rhs.get ();
+}
+
 /* A wrapper class for Python extension objects that have a __dict__ attribute.
 
    Any Python C object extension needing __dict__ should inherit from this
-- 
2.54.0


  reply	other threads:[~2026-04-28 16:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28 16:24 [PATCH v2 0/4] gdb/python: more fixes again for Python limited C API support Matthieu Longo
2026-04-28 16:24 ` Matthieu Longo [this message]
2026-05-15 16:56   ` [PATCH v2 1/4] gdb/python: add gdbpy_borrowed_ref Tom Tromey
2026-05-18 14:25     ` Matthieu Longo
2026-05-19 19:25       ` Tom Tromey
2026-05-20 16:04         ` Matthieu Longo
2026-05-21 14:35           ` Tom Tromey
2026-05-21 23:18             ` Tom Tromey
2026-04-28 16:24 ` [PATCH v2 2/4] gdb/python: eval_python_command returns result of the evaluation Matthieu Longo
2026-05-15 16:51   ` Tom Tromey
2026-05-18 14:37     ` Matthieu Longo
2026-05-19 19:24       ` Tom Tromey
2026-05-20 10:07         ` Matthieu Longo
2026-05-20 15:06           ` Tom Tromey
2026-04-28 16:24 ` [PATCH v2 3/4] gdb/python: migrate Python initialization to use the new config API (PEP 741) Matthieu Longo
2026-05-14 19:25   ` Tom Tromey
2026-04-28 16:24 ` [PATCH v2 4/4] gdb/python: work around missing symbols not yet part of Python limited API Matthieu Longo
2026-05-14 19:26   ` Tom Tromey
2026-05-15 10:10     ` Matthieu Longo
2026-05-14 10:23 ` [PATCH v2 0/4] gdb/python: more fixes again for Python limited C API support Matthieu Longo
2026-05-14 19:27   ` Tom Tromey
2026-05-15  9:26     ` Matthieu Longo
2026-05-15 14:48       ` 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=20260428162416.511367-2-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