Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Matthieu Longo <matthieu.longo@arm.com>
Cc: <gdb-patches@sourceware.org>,  Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH v1 1/4] gdb/python: add gdbpy_borrowed_ref
Date: Fri, 17 Apr 2026 09:40:16 -0600	[thread overview]
Message-ID: <87340ttx27.fsf@tromey.com> (raw)
In-Reply-To: <20260409105155.1416274-2-matthieu.longo@arm.com> (Matthieu Longo's message of "Thu, 9 Apr 2026 11:51:52 +0100")

>>>>> Matthieu Longo <matthieu.longo@arm.com> writes:

> 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.

On my branch I ended up giving this a gdbpy_opt_borrowed_ref base class.

> +/* 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

Also mine isn't templated like this though I guess this makes some
sense.

IIRC I felt like this was overkill.  I didn't read your subsequent
patches yet but I assume there's some use for it.

> +  /* Allow a (checked) conversion to any subclass of PyObject.  */
> +  template<typename TObj,
> +	   typename = std::is_convertible<TObj *, T *>>
> +  operator TObj * ()
> +  {
> +    //gdb_assert (PyObject_TypeCheck (m_obj, T::corresponding_object_type));

No commented-out code.  This is unsafe though.  I would prefer a check.

I think a name other than "TObj" would be good.
Even "U" would be better.  "T" and "TObj" are too close.

> +  /* When converting a borrowed reference to a gdbpy_ref<>, a new
> +     reference is acquired.  */
> +  template <typename TObj,
> +	    typename = std::is_convertible<TObj *, T *>>
> +  operator gdbpy_ref<TObj> ()
> +  {
> +    gdb_assert (m_obj != nullptr);
> +    return gdbpy_ref<TObj>::new_reference (m_obj);
> +  }

The naming thing here too.  And also safety if we can get it.

> +
> +  gdbpy_ref<T> strong_ref () const noexcept
> +  {
> +    return static_cast<gdbpy_ref<T>> (*this);
> +  }

I think it would be clearer to directly write this in terms of
new_reference.

Tom

  reply	other threads:[~2026-04-17 15:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09 10:51 [PATCH v1 0/4] gdb/python: more fixes again for Python limited C API support Matthieu Longo
2026-04-09 10:51 ` [PATCH v1 1/4] gdb/python: add gdbpy_borrowed_ref Matthieu Longo
2026-04-17 15:40   ` Tom Tromey [this message]
2026-04-09 10:51 ` [PATCH v1 2/4] gdb/python: eval_python_command returns both exit code and result Matthieu Longo
2026-04-17 17:22   ` Tom Tromey
2026-04-09 10:51 ` [PATCH v1 3/4] gdb/python: migrate Python initialization to use the new config API (PEP 741) Matthieu Longo
2026-04-17 15:53   ` Tom Tromey
2026-04-09 10:51 ` [PATCH v1 4/4] gdb/python: work around missing symbols not yet part of Python limited API Matthieu Longo
2026-04-17 17:26   ` Tom Tromey
2026-04-15  9:18 ` [PATCH v1 0/4] gdb/python: more fixes again 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=87340ttx27.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    --cc=matthieu.longo@arm.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