From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id UWaJC/NynmmzSAkAWB0awg (envelope-from ) for ; Tue, 24 Feb 2026 22:56:35 -0500 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (768-bit key; unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=bH7jLVps; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 291841E0DD; Tue, 24 Feb 2026 22:56:35 -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.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,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 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 CE7AD1E08D for ; Tue, 24 Feb 2026 22:56:33 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 7303C4B9DB4A for ; Wed, 25 Feb 2026 03:56:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7303C4B9DB4A Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=bH7jLVps Received: from omta36.uswest2.a.cloudfilter.net (omta36.uswest2.a.cloudfilter.net [35.89.44.35]) by sourceware.org (Postfix) with ESMTPS id 426AA4BA2E14 for ; Wed, 25 Feb 2026 03:56:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 426AA4BA2E14 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 426AA4BA2E14 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771991763; cv=none; b=JDry2HIfP7vxEUUXq53iM6T514zmfVTSLHQTgheWHtLWe/5LZ2eoPknoj5K22j///WnT3aV+PO4elHLsNadPFHxoEu+BGzST9kfO3G7l934rcVcoFi9b9mLxAtJvmg2CylNQs55oBQ08qXVriN5QS6QSMUbKHKvtOfPWBdZWB0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771991763; c=relaxed/simple; bh=hFDicK7f2lD7zk3p39geiej6pvJW4B1npkPFefiC3yw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hdRL1DY0Wf7JppcLD57D4W2eihHKqLrAgpctmG32ZAi3LwTKb+1XwTBQ1dWvXLkSZViblZkN+E8byyRpVEdEXBUxXZyX04QmV5mByrlcnHYhxN8iQb3CJ8pX2bGS/HdshGy0+XvLYpwUTo8yXHo+8KMeF4mvkOXazNO47nd+UVs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 426AA4BA2E14 Received: from eig-obgw-6006b.ext.cloudfilter.net ([10.0.30.211]) by cmsmtp with ESMTPS id v2dCv3VcgVCBNv60IvrqJJ; Wed, 25 Feb 2026 03:56:02 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id v60HvNnxMvXvHv60HvsCdm; Wed, 25 Feb 2026 03:56:01 +0000 X-Authority-Analysis: v=2.4 cv=e4IGSbp/ c=1 sm=1 tr=0 ts=699e72d1 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=HzLeVaNsDn8A:10 a=ItBw4LHWJt0A:10 a=R68VMzF8TUe8whNOY5MA:9 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:Date:References:In-Reply-To :Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=5KFKzaaM4sBkdjj0tPz/LBKXyTzJRYvTzrMxRdef2Ic=; b=bH7jLVpsq/Q/gkwyfzmdC+cN7y IhGtYfQouJkaDJ5aO79Slqlyde5BHTOmCaTHHp6RKiRCZeGmWrxHT6mMLsmAVPv6ExdEiYt4y8aaO t7nCEedJsp2FdNuYMyC8Hqzpw; Received: from 97-122-122-234.hlrn.qwest.net ([97.122.122.234]:60188 helo=prentzel) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vv60H-00000003Yjk-18mK; Tue, 24 Feb 2026 20:56:01 -0700 From: Tom Tromey To: Simon Marchi Cc: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [RFC 1/4] Add gdbpy_borrowed_ref In-Reply-To: <895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca> (Simon Marchi's message of "Mon, 23 Feb 2026 23:57:07 -0500") References: <20260222200759.1587070-1-tom@tromey.com> <20260222200759.1587070-2-tom@tromey.com> <895a8aec-9f2b-47b3-99a6-a249905b8ca4@simark.ca> X-Attribution: Tom Date: Tue, 24 Feb 2026 20:55:59 -0700 Message-ID: <87pl5tqxww.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.122.234 X-Source-L: No X-Exim-ID: 1vv60H-00000003Yjk-18mK X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-122-234.hlrn.qwest.net (prentzel) [97.122.122.234]:60188 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfL2TRDIasJWfO34AXAQ9CjmlysGp8PDQxBxp7u072cpZWpgYVyTYJdtMuukwR4zEW+t5v2nDAEEKvkpg0tsMLwtNX+m75U9gzVkRuuUKcvafvZ6A/Hqh rWt/ELJl5lubZhOy6MeLN929Wyje7MzqJyYQi/0AMG2YyG1S7GaPwzsJXCTtkkkOGEpX0VeijGvmPDAvEpN29P1B4X7x+juZNjg= 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 >> + 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. Tom