From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tom@tromey.com>, gdb-patches@sourceware.org
Subject: Re: [RFC 1/4] Add gdbpy_borrowed_ref
Date: Mon, 23 Feb 2026 23:57:07 -0500 [thread overview]
Message-ID: <895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca> (raw)
In-Reply-To: <20260222200759.1587070-2-tom@tromey.com>
On 2026-02-22 14:49, Tom Tromey wrote:
> 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 | 47 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 47 insertions(+)
>
> diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
> index 0a56436634d..56508996eaa 100644
> --- a/gdb/python/py-ref.h
> +++ b/gdb/python/py-ref.h
> @@ -41,6 +41,53 @@ 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. */
> +class gdbpy_borrowed_ref
> +{
> +public:
> +
> + gdbpy_borrowed_ref (PyObject *obj)
> + : m_obj (obj)
> + {
> + }
> +
> + template<typename T>
> + gdbpy_borrowed_ref (const gdbpy_ref<T> &ref)
> + : m_obj (ref.get ())
> + {
> + }
> +
> + /* Allow a (checked) conversion to any subclass of PyObject. */
> + template<typename T,
> + typename = std::is_convertible<T *, PyObject *>>
> + operator T * ()
> + {
> + gdb_assert (PyObject_TypeCheck (m_obj, T::corresponding_object_type));
> + return static_cast<T *> (m_obj);
> + }
Shouldn't gdbpy_borrowed_ref be templated? Allowing any gdbpy_ref to be
downcast to any subclass of PyObject seems a bit permissive. At least
there is a runtime check, but it would be nice to have more type safety
at compile time.
I imagine:
arch_object *arch = gdbpy_borrowed_ref<arch_object> (...); // works
arch_object *arch = gdbpy_borrowed_ref<PyObject> (...); // does not work
That being said, apart from the wrappers themselves, I don't expect that
we'll need to do that often. I suppose that it would be possible to
access the fields like this?
gdbpy_borrowed_ref<arch_object> obj;
obj->gdbarch; // accesses arch_object::gdbarch
I also toyed with defining some types like:
struct PyDict : public PyObject {};
struct PyList : public PyObject {};
struct PyLong : public PyObject {};
struct PyTuple : public PyObject {};
struct PyType : public PyObject {};
gdbpy_new_list would return a gdbpy_ref<PyList>.
gdbpy_list_append would take a gdbpy_borrowed_ref<PyList>.
And then we could have some ways of down-casting, if needed.
Simon
next prev parent reply other threads:[~2026-02-24 4:57 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-22 19:49 [RFC 0/4] Better Python safety Tom Tromey
2026-02-22 19:49 ` [RFC 1/4] Add gdbpy_borrowed_ref Tom Tromey
2026-02-24 4:57 ` Simon Marchi [this message]
2026-02-25 3:55 ` Tom Tromey
2026-02-25 15:24 ` Simon Marchi
2026-02-26 1:38 ` Tom Tromey
2026-02-22 19:49 ` [RFC 2/4] Add wrappers for some Python APIs Tom Tromey
2026-02-22 19:49 ` [RFC 3/4] Add constexpr functions to create PyMethodDef entries Tom Tromey
2026-02-22 19:49 ` [RFC 4/4] Convert some Python code to new-style Tom Tromey
2026-02-23 20:28 ` [RFC 0/4] Better Python safety Simon Marchi
2026-02-23 21:00 ` Simon Marchi
2026-02-23 23:23 ` Tom Tromey
2026-02-23 23:56 ` Tom Tromey
2026-02-24 1:05 ` Simon Marchi
2026-02-24 16:29 ` Tom Tromey
2026-02-23 21:22 ` Tom Tromey
2026-03-04 17:39 ` Matthieu Longo
2026-03-04 21:02 ` 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=895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca \
--to=simark@simark.ca \
--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