From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id rKWgGFIUn2m53wkAWB0awg (envelope-from ) for ; Wed, 25 Feb 2026 10:25:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1772033106; bh=YwlBcTyi38DR+HKGdWOGjYho2v8NZ1znMg+Q+jpUlus=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=lxiDB7Y/ilP6YBl6rCMJLRnejvHGdSv0z95Cux4CDCIYzaLd/9FdeepOylskdZB2X mv6KU4yn5hwLrm3Hkrs+lYnBRImKauTNVLKoub9SRAdYOcnDMiK8Mz57WTGHruE0P7 1I4ShC5V8nOahQpFh6sRhtjdYIMCXRrgHCCCTkFw= Received: by simark.ca (Postfix, from userid 112) id 462CD1E0DD; Wed, 25 Feb 2026 10:25:06 -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=ovajZDDQ; 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 71A361E089 for ; Wed, 25 Feb 2026 10:25:05 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id D4CDC4BAD14C for ; Wed, 25 Feb 2026 15:25:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D4CDC4BAD14C 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=ovajZDDQ Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 073A04BA23C8 for ; Wed, 25 Feb 2026 15:24:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 073A04BA23C8 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 073A04BA23C8 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=1772033079; cv=none; b=FvuQ1Ya4Yjg+EuImz9zIHXoRqREbE0A0c0/IGdG0G1PVwbv7x4+eBeHR0MqUxPW5LTiRcnq0YkkW4UZyIASkcI98O2Dna0DLlVLVytQe6d3KXHiTw/Sib8x9nIKEjz0lspNORGzs8o58p82y+ExKV4r2i1Hs38kVV8ivr6p9N88= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1772033079; c=relaxed/simple; bh=YwlBcTyi38DR+HKGdWOGjYho2v8NZ1znMg+Q+jpUlus=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=YpbvKI8ykeRc653ttjvMpzRbP+LsSznHn3RyndTFigvGpKJ2H51dY/cHHDi6ITCb5hUhUwdfeqZbv5gJl7lVtasORmbrsVFaved0MW29WG1guJZ/KzfKqPK9RZvum15lZDLHcllksx5cj8pNARpwKM4K6KkOFTYBl6HTcH2/8Xk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 073A04BA23C8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1772033077; bh=YwlBcTyi38DR+HKGdWOGjYho2v8NZ1znMg+Q+jpUlus=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=ovajZDDQdGgMOH6B9oYB9d26iKHXKolut/JX92gFb2KCb3yKz8u3SnuxlYyx3j9Du KULvpVZwMLfuAjbUWF2p1p0AfUONMhy6o9cvNxdetxW9syvvdVWKHsX6RZ9n5SGE7W 1IyQvzhvVC1ithBCfD/tRjKwWcYJf4c55aSuV2CI= Received: by simark.ca (Postfix) id 8D9891E089; Wed, 25 Feb 2026 10:24:36 -0500 (EST) Message-ID: <1f217600-f127-4622-96ab-3e1d35843302@simark.ca> Date: Wed, 25 Feb 2026 10:24:36 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 1/4] Add gdbpy_borrowed_ref To: Tom Tromey Cc: gdb-patches@sourceware.org References: <20260222200759.1587070-1-tom@tromey.com> <20260222200759.1587070-2-tom@tromey.com> <895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca> <87pl5tqxww.fsf@tromey.com> Content-Language: en-US From: Simon Marchi In-Reply-To: <87pl5tqxww.fsf@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-24 22:55, Tom Tromey wrote: >>> + template>> + typename = std::is_convertible> >>> + operator T * () >>> + { >>> + gdb_assert (PyObject_TypeCheck (m_obj, T::corresponding_object_type)); >>> + return static_cast (m_obj); >>> + } > > Simon> Shouldn't gdbpy_borrowed_ref be templated? Allowing any gdbpy_ref to be > Simon> downcast to any subclass of PyObject seems a bit permissive. At least > Simon> there is a runtime check, but it would be nice to have more type safety > Simon> at compile time. > > Yeah, however this does not actually come up very much. > > In fact all the casts I am aware are of 'self', which means we could > maybe solve this a special case: turn functions into methods on the > object, and then have the wrappers wrap a pointer-to-member-function. > > This would mean that 'this' would be implicitly a borrowed reference, > but OTOH this isn't likely to be a big source of confusion and it would > make the code even simpler. > > Simon> arch_object *arch = gdbpy_borrowed_ref (...); // works > Simon> arch_object *arch = gdbpy_borrowed_ref (...); // does not work > > Simon> That being said, apart from the wrappers themselves, I don't expect that > Simon> we'll need to do that often. I suppose that it would be possible to > Simon> access the fields like this? > > As long as there's an operator*. But if we did the method approach, > there'd be basically no need for this at all. > > Simon> I also toyed with defining some types like: > > Simon> struct PyDict : public PyObject {}; > Simon> struct PyList : public PyObject {}; > Simon> struct PyLong : public PyObject {}; > Simon> struct PyTuple : public PyObject {}; > Simon> struct PyType : public PyObject {}; > > Simon> gdbpy_new_list would return a gdbpy_ref. > Simon> gdbpy_list_append would take a gdbpy_borrowed_ref. > > Simon> And then we could have some ways of down-casting, if needed. > > It could be done. I'm ambivalent about it since I don't think this is a > common source of errors in gdb, I guess mainly because the needed > type-checks are re-done by Python anyway. > > OTOH we've had multiple refcount bugs and multiple cases where exception > checks are missed. Those are the problems I really set out to solve. Ack. I agree it's fine to go for that, at least at first. Simon