From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id HqUfJ2ipCWrnlQIAWB0awg (envelope-from ) for ; Sun, 17 May 2026 07:41:28 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O9OJx5hn; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 8E7AA1E024; Sun, 17 May 2026 07:41:28 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,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 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 E1E671E024 for ; Sun, 17 May 2026 07:41:26 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 381884BA23F0 for ; Sun, 17 May 2026 11:41:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 381884BA23F0 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O9OJx5hn Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 09EBF4BA23E8 for ; Sun, 17 May 2026 11:40:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09EBF4BA23E8 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 09EBF4BA23E8 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779018053; cv=none; b=oyCXvV+MrJuMtSWlemRDUDSgBtgd4JYBJTUWXKnaiuhd+0fuCWz69Rvj0LxyX6KCBgWhZ1okmXniccH8GQ5Izf2o/ZsAebSlPQkvF9c88F7/THNGcVciEbwXwyw1wnxUkQvz1zkucuuEmujBHBEHMqmq+uW54y5RWet0lrDwNP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779018053; c=relaxed/simple; bh=5ddvm1cpnXtVU15uyriMmf/P7mHFtdMi7Z8TLGyiLs4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=szE5aso+alUE6jUZW6CIjnNFauLggaCebKZEKfZpEwfxZT6hAHBLz5ApWP7TaX3BN8tcD6Nrc0jd78veshg/wDv0kU6fQJ7RV5owOywYaR5bI5zhM2fKEF39ax/w9lyQwn63QuXHRqJZoADGYgN24rT77gZNC7rSS+N/uaurp1M= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O9OJx5hn DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09EBF4BA23E8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779018052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aYNIBS8mpw1n1DYbDkDdl1L9kSSsAOy0R9uWaIqoVRw=; b=O9OJx5hnTiAzQb896pzykdSXpEfa7W14UZyGf8/zlWRajEiIL5ONU+BKhvbXFjkpaUFjmj RM/kC0DTAcybF0wju5oY+0sGR1Svr7ScIKMzWrYr77mz/ibu3SjciUSg5RR7u6AA4Nre3Z QyIVxRUgDYw7D2L9hiCfRUvsLDKOtWk= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-544-UQeZcUgPPa2bp4oJALaI3A-1; Sun, 17 May 2026 07:40:51 -0400 X-MC-Unique: UQeZcUgPPa2bp4oJALaI3A-1 X-Mimecast-MFC-AGG-ID: UQeZcUgPPa2bp4oJALaI3A_1779018050 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-48fd3dbd16aso10354425e9.0 for ; Sun, 17 May 2026 04:40:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779018050; x=1779622850; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aYNIBS8mpw1n1DYbDkDdl1L9kSSsAOy0R9uWaIqoVRw=; b=GqCWqpZIViXISSrxON+9OGxMprox5dvRC8AjdvqDG6TeLb1PxuUJ421X3DYh51itJb yIKh2mxi4OOr/nEhpx7jtOUjBb4H5/cI6630N5/rj2hs2xyWoBjmvPMaDcxfVe6X0o6c Fq8Jig+/8tn0nS0usn5p04Q2CVjmMzOKXOw/tPGBGBqrvgE83lBABs8h0rBbdwVesGP5 1qfUlXJOeRblJ/Y/8LycQ6ShtNhaQY5BEOTU33yEaH11nAOAmPKmWOCKv59kelRt4sOL 9MC+14Dj25x+/9H7wtNb90xqaEjoKeM0iPTJN8dIXtSfQG/7UDLwny/P27RtjRcaQMJr iAeQ== X-Forwarded-Encrypted: i=1; AFNElJ+9X+Ns3xZcrbW5tNpZbfsOcN+ZJLyxNJp6LC3Lbbm3O3ODOQsUkwGt6VIglvFnAyP07iLFqF+WbzLJFg==@sourceware.org X-Gm-Message-State: AOJu0YwFMzW4w8yk42LqdCwo7Vz29mbn6EUQZ/R32YxxffYBtlgQrLaB t+atJPlf7dE1NHZlvgv3bFCcdTeRjBIYITzANiqVITC7r5itibqFdyQ7B0dai3BYn+rq2HLvHua UjWaSa0gt5j5HDrWjpa/FWJraJgoU1q8CNNEuDNSTMLmm0kavmpfF06Ym8/Z6Gojk/dDjmDU= X-Gm-Gg: Acq92OH+yNcmRRXG+zLulw7qhtlJtMkbGiLJfPV2CqXkMozKga0BhFOg6e+Wbz4+U9T Mt/hVFIU2CvO//twcnA1lCIqLerlsu19k0ipwLpWCW29kPF6cAqKfQ6sZPOeZqjpZ3uxQRsVlkC Au8M8luEoY7VXZOFIpmAbPQngbD91H+/XwNri3EbUqtNBk3Gj/vpo0qXIj6tcH99cz6n3leT+Ef sP6wPqCFvO/eKWeML/RsmawWQCB6NKAswlj9oCYHRaYhgHlrREemuLc2X9ba2rqCNtl/NvJAA3x jGaPefyKdMnmwnHE0xXWHuIE3W5P6s4H6RvVuJ2ex8VmdqwPa7FSU1Bwj/Gx0ljbtq8Vi3XiPOA 2zlhpsDwVzgwPkRHhdLEMTyKO82v8Sw== X-Received: by 2002:a05:600c:828d:b0:48e:6db3:ff2e with SMTP id 5b1f17b1804b1-48fe61eb313mr155370875e9.15.1779018049852; Sun, 17 May 2026 04:40:49 -0700 (PDT) X-Received: by 2002:a05:600c:828d:b0:48e:6db3:ff2e with SMTP id 5b1f17b1804b1-48fe61eb313mr155370595e9.15.1779018049395; Sun, 17 May 2026 04:40:49 -0700 (PDT) Received: from localhost ([109.144.213.219]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fe53767ecsm169789535e9.10.2026.05.17.04.40.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 May 2026 04:40:48 -0700 (PDT) From: Andrew Burgess To: Tom Tromey , gdb-patches@sourceware.org Cc: Tom Tromey Subject: Re: [PATCH v2 1/4] Add gdbpy_borrowed_ref In-Reply-To: <20260515-python-safety-initial-v2-1-6129cadf258a@tromey.com> References: <20260515-python-safety-initial-v2-0-6129cadf258a@tromey.com> <20260515-python-safety-initial-v2-1-6129cadf258a@tromey.com> Date: Sun, 17 May 2026 12:40:46 +0100 Message-ID: <87cxyuclhd.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: vZ_KfrCumaGKI94XxwQqA1m8feJjBcpRdBH-TkQgChE_1779018050 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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 Tom Tromey writes: > This adds new gdbpy_opt_borrowed_ref and gdbpy_borrowed_ref classes. > These class is primarily for code "documentation" purposes -- it makes type: These CLASSES ARE primarily .... > it clear to the reader that a given reference is borrowed. However, > they also add a tiny bit of safety, in that conversion to gdbpy_ref<> > will either be rejected (by the "opt" class) or acquire a new > reference. > --- > gdb/python/py-ref.h | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 80 insertions(+) > > diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h > index dc0b14814af..ef6b9fb427c 100644 > --- a/gdb/python/py-ref.h > +++ b/gdb/python/py-ref.h > @@ -41,6 +41,86 @@ struct gdbpy_ref_policy > template using gdbpy_ref > = gdb::ref_ptr; > > +/* A class representing an optional borrowed reference. It is > + "optional" because NULL is a valid value. > + > + 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 prevented. > + > + An optional borrowed reference is only used in situations where > + Python says NULL is valid. For example, it is used as the type of > + the "keywords" argument to a varargs method. Most code should > + prefer an ordinary gdbpy_borrowed_ref, see below. */ > +class gdbpy_opt_borrowed_ref > +{ > +public: > + > + gdbpy_opt_borrowed_ref (PyObject *obj) > + : m_obj (obj) > + { > + } > + > + template > + gdbpy_opt_borrowed_ref (const gdbpy_ref &ref) > + : m_obj (ref.get ()) > + { > + } > + > + operator PyObject * () > + { > + return m_obj; > + } Could/should this be marked as 'const'? > + > + operator gdbpy_ref<> () = delete; > + > +protected: > + PyObject *m_obj; > +}; > + > +/* A borrowed reference that is guaranteed not to be NULL. > + > + Like gdbpy_opt_borrowed_ref, this mostly serves a documentary > + purpose. However, it also allows a checked cast to any subclass of > + PyObject, and conversion to a gdbpy_ref<> will automatically > + acquire a new reference -- a safety improvement over plain > + PyObject*. */ > +class gdbpy_borrowed_ref : public gdbpy_opt_borrowed_ref > +{ > +public: > + > + gdbpy_borrowed_ref (PyObject *obj) > + : gdbpy_opt_borrowed_ref (obj) > + { > + gdb_assert (m_obj != nullptr); > + } > + > + template > + gdbpy_borrowed_ref (const gdbpy_ref &ref) > + : gdbpy_opt_borrowed_ref (ref) > + { > + gdb_assert (m_obj != nullptr); > + } > + > + gdbpy_borrowed_ref (std::nullptr_t) = delete; > + > + /* 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); > + } OK, please excuse my ignorance here, I might be completely wrong, but I believe the line 'typename = std::is_convertible' is here for the purpose of SFINAE. I believe this was copied from gdb_ref_ptr.h, but I think this line might be wrong, both there and here. I think std::is_convertible is either true or false, or the type will be true_type or false_type, but in all cases, I think the type is well defined, so there will never be substitution failure. I think what you need here is: typename = gdb::Requires> But it might be that I'm just not understanding what's going on here, in which case, feel free to correct me. > + > + /* When converting a borrowed reference to a gdbpy_ref<>, a new > + reference is acquired. */ > + operator gdbpy_ref<> () > + { > + return gdbpy_ref<>::new_reference (m_obj); > + } Again with the 'const' maybe? Thanks, Andrew > +}; > + > /* 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