From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cW0tBMNim2lpsgUAWB0awg (envelope-from ) for ; Sun, 22 Feb 2026 15:10:43 -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=F+X+zcEd; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0A5DA1E08D; Sun, 22 Feb 2026 15:10:43 -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 7E5BD1E08D for ; Sun, 22 Feb 2026 15:10:42 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 19FCE4BA2E1C for ; Sun, 22 Feb 2026 20:10:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 19FCE4BA2E1C 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=F+X+zcEd Received: from omta038.useast.a.cloudfilter.net (omta038.useast.a.cloudfilter.net [44.202.169.37]) by sourceware.org (Postfix) with ESMTPS id 821C94BA2E17 for ; Sun, 22 Feb 2026 20:08:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 821C94BA2E17 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 821C94BA2E17 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771790901; cv=none; b=ievz53y/w80hGqbb1QWSpWIIeJw6u8xFQ8s/V9uU40Xz/VN80sM8tIXPE7wRV+QWA9y41dNmX6t1TIU80yDmkT34U7kWwT6yqHQZo+aWaHidSbTwQx1sqQpA8Q+ACyTg9my9CnzdSt3/O8PCsqUZ2sZel4jjdWJyrnuQCAQ1If8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771790901; c=relaxed/simple; bh=Yn7CrDwMPMaS0OcwWu6jJoqpofC7vAZmGhJoTXRCV/w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rGr/rz1ZQcQiU1abUVx8nB9Y5nB5DaL6zn+ZrHSOtXtpv1sboKXvCouGSK3zgY4frWxibzoT5O4KqD26zNh6CNVZZx3Uc2RQ7LRVUSAzBq6TWyZMCDxfHLCA02a8horks3UhrEmZDNzHG6j9qCCPP5TVbWxD5na8tWbqWT0A99s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 821C94BA2E17 Received: from eig-obgw-5007b.ext.cloudfilter.net ([10.0.29.167]) by cmsmtp with ESMTPS id uBsIvPVMbSkcfuFkTvj5Bg; Sun, 22 Feb 2026 20:08:13 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id uFkTvDyFyV6WMuFkTvcNdv; Sun, 22 Feb 2026 20:08:13 +0000 X-Authority-Analysis: v=2.4 cv=E//Npbdl c=1 sm=1 tr=0 ts=699b622d a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=HzLeVaNsDn8A:10 a=ItBw4LHWJt0A:10 a=g6Znvwp4j0f3TE_DeFEA:9 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: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=3cvCxAKDBp4CfGustKX3skaldhCbuFXypTLIxxS51n4=; b=F+X+zcEd7SRe34T3tVT2qcBgjZ YIuwU4oAOtGjAZY+hpb11L9pBnp2o+nV+pTHZIpgIIFjwjx7PB+Xwgl+njuVNRWMIkq2OmpTJXxjH N5x8Vynso2zMm97njt7ulYCbZ; Received: from 97-122-122-234.hlrn.qwest.net ([97.122.122.234]:37760 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vuFkS-00000003hGc-3c1n; Sun, 22 Feb 2026 13:08:12 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC 1/4] Add gdbpy_borrowed_ref Date: Sun, 22 Feb 2026 12:49:34 -0700 Message-ID: <20260222200759.1587070-2-tom@tromey.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260222200759.1587070-1-tom@tromey.com> References: <20260222200759.1587070-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 1vuFkS-00000003hGc-3c1n X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-122-234.hlrn.qwest.net (localhost.localdomain) [97.122.122.234]:37760 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: MS4xfAq+bMd+1ZlxtR5LKWCrI57pfXU4wVDXRV8XAlKxkL12tjW5PmRUWFWCq2d3y/I00d7uFksxhllKa1djU222Tp09pBphUX1ZYtFv+Vwq2PppZ7iApafb lSXezULmSeRukapbqdd4fY2dXtfxD9GAFWgAOo+mYUmC34rwAt8TsdOEU+IX3Yh0qx5vHZbfBr88ACnNbfqK7t2JMVEPM5xtqJ8= 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 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> + operator T * () + { + gdb_assert (PyObject_TypeCheck (m_obj, T::corresponding_object_type)); + return static_cast (m_obj); + } + + operator PyObject * () + { + return m_obj; + } + + /* When converting a borrowed reference to a gdbpy_ref<>, a new + reference is acquired. */ + operator gdbpy_ref<> () + { + gdb_assert (m_obj != nullptr); + return gdbpy_ref<>::new_reference (m_obj); + } + +private: + PyObject *m_obj; +}; + /* A wrapper class for Python extension objects that have a __dict__ attribute. Any Python C object extension needing __dict__ should inherit from this -- 2.49.0