From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id s5PDK8IvnWmzIAgAWB0awg (envelope-from ) for ; Mon, 23 Feb 2026 23:57:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1771909058; bh=0qP18xkhcNgVXYt/RJ4l1fZjYHQ0BoUK65xxNW9xMpk=; h=Date:Subject:To:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=UQQaWchMBdBk4sPyvgsyhxiU/G2R9ejJUYU/OT0derTFmcXHwhkY0JzSAGlzU8TO/ 8HkSUj5DnsiXEvzZi62DVykrXfJymDJ4CE6XISMgvNPc9Wy9iV51U5gNgOyE1lB9oy Rn5pkI/8J6QbZF4ISk8u0u1kIUVLgnPCoY/tnmp0= Received: by simark.ca (Postfix, from userid 112) id 9C76D1E08D; Mon, 23 Feb 2026 23:57:38 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=h3uAQSbp; dkim-atps=neutral Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 5B3C11E08D for ; Mon, 23 Feb 2026 23:57:37 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id DB0994B9DB56 for ; Tue, 24 Feb 2026 04:57:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB0994B9DB56 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=h3uAQSbp Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 4B9DC4BA23E6 for ; Tue, 24 Feb 2026 04:57:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B9DC4BA23E6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4B9DC4BA23E6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771909030; cv=none; b=T7WBlqJPEJJKFHCbMGUPNRTbd+m10ps25rXtFQP5MN2iXrR0GyMGvcAJuwYOJJnKNhGs6wx4Ysn4fVnwa2QAK/CWPX3ii+EqzIWqHKEUESjoRrWEpfYeG+xBk5wY1SIWol+VZCqKhRLR/++xrAtlF9uHxJYJ+Dugklvlqhl6nTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771909030; c=relaxed/simple; bh=0qP18xkhcNgVXYt/RJ4l1fZjYHQ0BoUK65xxNW9xMpk=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=MMoJLdHuu8h5Npa+Ll/jnvtgymXfF7BkwoI7IhZgMDv9xc49dInUg/E6Nz3e7YRmLBqjCrkLb9PXCw7U14+iJ7IBdg6vGUVOoinL+B48LfhKOyTDYit+svhLOUXz/0uoPSoRZ5M2uihHh7fk8fc3K3QmwgUEVA7Fd/geanCoWAY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B9DC4BA23E6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1771909028; bh=0qP18xkhcNgVXYt/RJ4l1fZjYHQ0BoUK65xxNW9xMpk=; h=Date:Subject:To:References:From:In-Reply-To:From; b=h3uAQSbpv49eFCU9YElozpNftUm60HH0eIplunY3wOADAsiI9j5CbcOcfXwh4EV6y 3gl398BhW8+1Uu/hrmQTpDvLe9c6thhreZpCAraObu6WBsYOZI5hlI8r4DKqJ6i4Fj WR+ZZRc3PaTsSSmzfTISTzEvN/UZSm6DzjFJ0tbk= Received: by simark.ca (Postfix) id 1C2511E08D; Mon, 23 Feb 2026 23:57:08 -0500 (EST) Message-ID: <895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca> Date: Mon, 23 Feb 2026 23:57:07 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 1/4] Add gdbpy_borrowed_ref To: Tom Tromey , gdb-patches@sourceware.org References: <20260222200759.1587070-1-tom@tromey.com> <20260222200759.1587070-2-tom@tromey.com> Content-Language: en-US From: Simon Marchi In-Reply-To: <20260222200759.1587070-2-tom@tromey.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 using gdbpy_ref > = gdb::ref_ptr; > > +/* 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 > + gdbpy_borrowed_ref (const gdbpy_ref &ref) > + : m_obj (ref.get ()) > + { > + } > + > + /* Allow a (checked) conversion to any subclass of PyObject. */ > + template + typename = std::is_convertible> > + operator T * () > + { > + gdb_assert (PyObject_TypeCheck (m_obj, T::corresponding_object_type)); > + return static_cast (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 (...); // works arch_object *arch = gdbpy_borrowed_ref (...); // 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 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. gdbpy_list_append would take a gdbpy_borrowed_ref. And then we could have some ways of down-casting, if needed. Simon