From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id hWaNAhLAnGlzswcAWB0awg (envelope-from ) for ; Mon, 23 Feb 2026 16:01:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1771880465; bh=B8puQaNx0a4M/JfKzTJP8oPFaKs3Yf6/s0dsAV06Sr4=; h=Date:Subject:To:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=V0aOrbOCvnwX4wyxxuLE+awQTh9uIApSBM4NIC3cZ3L+sgHcCTnQ/BWTFshkfhIzQ WrmWjs6GuKgi5zwvxQWdE9xOYcY0ZrD1kdxL1cUPM+wijmPAdTJ3u5qs3gWBoMpyEE 6S7zT/ZIqSTeNg/LnduyYWtheBI3+amyppZKAsro= Received: by simark.ca (Postfix, from userid 112) id E318E1E08D; Mon, 23 Feb 2026 16:01:05 -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=n0aq0NQd; 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 A0F0D1E08D for ; Mon, 23 Feb 2026 16:01:04 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 418D04BA23EF for ; Mon, 23 Feb 2026 21:01:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 418D04BA23EF 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=n0aq0NQd Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 71B6A4BA23C6 for ; Mon, 23 Feb 2026 21:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 71B6A4BA23C6 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 71B6A4BA23C6 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=1771880437; cv=none; b=HelohqikVK8QUdy1o3S44eJFH12HaWYHYYDnG9CRralCEz7a0XmGkIwR0SJGoNONRhKnlEDwytliVaw8Qam47r/hpWMJiXTchrG9/NTid2t5XYtXYIpNvqgudqrabLlJ1Syo8DZE5XO1MyL8mcDX5I5VN/cf+Bd06+BBPykbO68= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771880437; c=relaxed/simple; bh=B8puQaNx0a4M/JfKzTJP8oPFaKs3Yf6/s0dsAV06Sr4=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=aDPuXiJBHN2xHMVwGl5DQ126DoB6CnUnG6nGwIqtBRLNE5gnQCjQamK6vKa1xSxHrqpM050gwGVUAmNuvSn9jgDQGynL60291/XP3o6C3iiGV2M/Liew+cFLdzfl44XnJGzTq6krdQ6FNcWtohq0NED8xRThPfCWW0aknwKsQIs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 71B6A4BA23C6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1771880436; bh=B8puQaNx0a4M/JfKzTJP8oPFaKs3Yf6/s0dsAV06Sr4=; h=Date:Subject:To:References:From:In-Reply-To:From; b=n0aq0NQdKLyWAJ26q5thHb0Uk5Bx/ZxHShuZTPKZ+OeJWEXmBPkuH3Yce0gGCyi3d y+MyZvKyQQVBVAyWmVUZmjcJ/MAYA8gHMY3TvsMBfXdMBzRL74ZnI/AlDbx0HhkT7G r3GbALmy17/a0++u6fOjHpp1ZBMJAOPk6oS885v4= Received: by simark.ca (Postfix) id A495C1E08D; Mon, 23 Feb 2026 16:00:35 -0500 (EST) Message-ID: Date: Mon, 23 Feb 2026 16:00:35 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 0/4] Better Python safety To: Tom Tromey , gdb-patches@sourceware.org References: <20260222200759.1587070-1-tom@tromey.com> Content-Language: en-US From: Simon Marchi In-Reply-To: <20260222200759.1587070-1-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 series is a rough draft showing how I think Python safety could > be improved. > > The basic idea is to use C++ features: more fully use gdbpy_ref<> to > avoid reference-counting bugs, introduce a new gdbpy_borrowed_ref to > manage borrowed references; throw exceptions on failure rather than > explicit error checks; and finally wrap Python C APIs to enforce these > rules. > > This approach also lets us implement Python methods in a more natural > style. Explicit try/catch when calling gdb APIs is no longer needed, > and methods can simply return the appropriate type. > > This series is nowhere near complete, but I did mostly convert > py-arch.c and py-frame.c. (Discussion of some holes below.) > > A nice example of the simplification is shown by > gdbpy_all_architecture_names, which is now just: > > gdbpy_ref<> > gdbpy_all_architecture_names (gdbpy_borrowed_ref self) > { > gdbpy_ref<> list = gdbpy_new_list (0); > > std::vector name_list = gdbarch_printable_names (); > for (const char *name : name_list) > gdbpy_list_append (list, gdbpy_unicode_from_string (name)); > > return list; > } Just some comments before I get into the code itself: We have something really similar in the other project I work on, Babeltrace. The base API is in C, and it is very refcount heavy. We wrote C++ bindings around that, that I think are really nice and make it almost impossible to make a mistake in refcount. We have BorrowedObject: https://git.efficios.com/?p=babeltrace.git;a=blob;f=src/cpp-common/bt2/borrowed-object.hpp;h=3ea62072df3f0dbfbf2ad3434980c4f7f597dd39;hb=HEAD which holds a weak reference on an object. It can't hold nullptr, meaning that if you receive a BorrowedObject of some kind, you know there is an object in it. We then have OptionalBorrowedObject, which is like BorrowedObject but can be nullptr / empty: https://git.efficios.com/?p=babeltrace.git;a=blob;f=src/cpp-common/bt2/shared-object.hpp;h=8d6666e2ed4c658150ff449245764287c5c72f94;hb=HEAD This is similar to gdbpy_borrowed_ref. The distinction between BorrowedObject and OptionalBorrowedObject is nice, as it allows declaring parameters or return values that can't be nullptr / empty, a bit like C++ references vs pointers. It might be good to have that too. The wrapper around PyList_Append, for instance, would take non-optional parameters. And finally, SharedObject, which holds a strong reference: https://git.efficios.com/?p=babeltrace.git;a=blob;f=src/cpp-common/bt2/optional-borrowed-object.hpp;h=cfabdf203542924742d8eaf24bb7ff33ebfbf14e;hb=HEAD This is similar to gdbpy_ref. Finally (1), is there something specific to GDB here? I'm wondering if we could make it a separate project (and import it in GDB), because it's something I would be tempted to use outside of GDB. And finally (2), I was kind of surprised that something like this didn't exist already, we're certainly not the first ones to want to use the CPython in a modern C++ program. I searched around and found nanobind: https://nanobind.readthedocs.io/en/latest/ It seems to do more than what we want (like automatic C++ <-> Python data structure conversion), but the wrappers shown here seem to be like what we want: https://nanobind.readthedocs.io/en/latest/exchanging.html#option-3-wrappers I'm not convinced that going with this library would be easier in the long run, but I think we should at least take a moment to check what is out there before writing something new. Simon