| Message ID | 20260521-python-safety-initial-v3-0-d0679c36e499@tromey.com |
|---|---|
| Headers |
Return-Path: <gdb-patches-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C97D848F664D for <patchwork@sourceware.org>; Thu, 21 May 2026 23:28:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C97D848F664D 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=oDyYVsb3 X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta36.uswest2.a.cloudfilter.net (omta36.uswest2.a.cloudfilter.net [35.89.44.35]) by sourceware.org (Postfix) with ESMTPS id 6B62348F8A4D for <gdb-patches@sourceware.org>; Thu, 21 May 2026 23:28:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B62348F8A4D 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 6B62348F8A4D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=35.89.44.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779406080; cv=none; b=af+LC8bpSeqF6KFVIMT7AO8e+bOJj6H7pl+9n3IB6CV9Im5QwYcVA3bXy+ZxAeiFlp96+DOnPP813Zw5EDuXeK5HNXmoei2nyQvDncVHpJPmtKkt0FcBaPCuVJsaXMYyRuFPM5HuevIlG0hR8h9paJ+2eD3WegGZMFKVblnM9Og= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779406080; c=relaxed/simple; bh=hq3nf6gpmG7LOjcsRk0ov+iochkf03NiHQMSdc1ZVlA=; h=DKIM-Signature:From:Subject:Date:Message-Id:MIME-Version:To; b=oMIIq8/5xnflhhZZB00L+fEeAuNDA91aRwnOV9FGct8ojjQ0t5mF7hUG0N300DHblWMb8BPO3pRuBT8ljRUtaXtChAAFJD7vgW7ExsRXYo7L/YX7IsoUI3vF8ROJSPaxTyPl+PZLVu/jSNiokVFZFtKKQzovvqRj0i8l01CBeR4= ARC-Authentication-Results: i=1; sourceware.org; dkim=policy (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=oDyYVsb3 reason="signing key too small" DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B62348F8A4D Received: from eig-obgw-5003b.ext.cloudfilter.net ([10.0.29.155]) by cmsmtp with ESMTPS id QCN1wGAIYPpWVQCo2wobAA; Thu, 21 May 2026 23:27:58 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id QCo1wGhVhkh2OQCo2woiua; Thu, 21 May 2026 23:27:58 +0000 X-Authority-Analysis: v=2.4 cv=YpgPR5YX c=1 sm=1 tr=0 ts=6a0f94fe a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=ItBw4LHWJt0A:10 a=CCpqsmhAAAAA:8 a=zstS-IiYAAAA:8 a=D_ZWRSUw-nte3DH8E_EA:9 a=QEXdDO2ut3YA:10 a=ul9cdbp4aOFLsgKbc677:22 a=4G6NA9xxw8l3yy4pmD5M:22 a=DCx65vhANUyCzuf5D8fC:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Cc:To:Content-Transfer-Encoding:Content-Type:MIME-Version: Message-Id:Date:Subject:From:Sender:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=92OlyS2GVeoXZ9umKJljrtvapdJQnawnfGW8N+e4aSU=; b=oDyYVsb3476zUfKdtOlpuzHf9z h1xkR26qszAHjq8/88s/D3/ff6emtcsr1cXyfN1QduVelgy5nKqOx1oVcONJnxBa4ey8ZQZkY8XcS adcqmHyf5t5z3CJqvkWRhcjfe; Received: from 75-166-225-82.hlrn.qwest.net ([75.166.225.82]:37536 helo=[192.168.0.17]) by box5379.bluehost.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.2) (envelope-from <tom@tromey.com>) id 1wQCo1-00000002l41-29g6; Thu, 21 May 2026 17:27:57 -0600 From: Tom Tromey <tom@tromey.com> Subject: [PATCH v3 0/4] Python safety initial work Date: Thu, 21 May 2026 17:27:51 -0600 Message-Id: <20260521-python-safety-initial-v3-0-d0679c36e499@tromey.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAPeUD2oC/42OQQ6CMBREr2K6tkpbiujKexgXn/ILP5GWtA2RE O5uYedKly+ZeTMLixgII7sdFhZwokjeZVDHAzM9uA45tZmZLGRVaKH5OKfeOx7BYpo5OUoEL15 K2VhVQm1QsdwdA1p6797HM3MDEXkTwJl+s/lAHbnzADFh2PI9xeTDvN+YxNb6tTgJXvDaCq2Vq lurL/cU/IDzyfiBbZOT/Esjs6YS8mqgtVLX8KVZ1/UDxZRWnSIBAAA= X-Change-ID: 20260515-python-safety-initial-422bf34a8ce3 To: gdb-patches@sourceware.org Cc: Tom Tromey <tom@tromey.com> X-Mailer: b4 0.14.3 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: 75.166.225.82 X-Source-L: No X-Exim-ID: 1wQCo1-00000002l41-29g6 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-225-82.hlrn.qwest.net ([192.168.0.17]) [75.166.225.82]:37536 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfEygCutZMD1vWNgyZU4mv/yHoTgBCk20xMowsvhxfDRC1gBVD5XNBUFQrLMCTbKMyhDM9pIbftTQ/wd/mkft9MxyBrvbJqugNulDKlNokTEnYf4x70B/ Da+KHiPX/RHksQqQUexM4JUzTYCCHejkOAulJFJzPnne0xrkm4aM7q3oh8Ik7GtreywJ67aJCJlNgYU1us14RBOIEFikbRVSXy4= X-Spam-Status: No, score=-3006.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, TXREP, T_SPF_PERMERROR shortcircuit=no autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list <gdb-patches.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=subscribe> Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org |
| Series | Python safety initial work | |
Message
Tom Tromey
May 21, 2026, 11:27 p.m. UTC
I rewrote my earlier Python safety series to use methods a while back.
And while I like the result, I got bogged down converting things, so I
thought I'd send some initial work, with the idea that if this looks
good, other code can be transformed in pieces.
Since this is extracted from a larger series, there is some code in
here that isn't currently used in-tree. This is mainly true for the
gdb functions that wrap Python APIs, see patch 2.
Patch 4 shows what the end effect is, but the patch is somewhat hard
to read IMO. So for instance now the 'title' getter for a TUI window
is just an ordinary method:
const std::string &
gdbpy_tui_window::title ()
{
require_valid ();
return window->title ();
}
Since it wants to only operate on a valid window, require_valid will
throw if that isn't the case.
Note there's no explicit error checking, no casts, no messing with
reference counts, and the return type is just a string. All these
boilerplate details are handled by a template wrapper function.
Signed-off-by: Tom Tromey <tom@tromey.com>
---
Changes in v3:
- Updated per review
- Changed gdbpy_opt_borrowed_ref and gdbpy_borrowed_ref to templates
- Link to v2: https://inbox.sourceware.org/gdb-patches/20260515-python-safety-initial-v2-0-6129cadf258a@tromey.com
Changes in v2:
- Avoids Py_RETURN_* macros
- Link to v1: https://inbox.sourceware.org/gdb-patches/20260515-python-safety-initial-v1-0-8f155338df57@tromey.com
---
Tom Tromey (4):
Add gdbpy_borrowed_ref
Add wrappers for some Python APIs
Add wrappers for Python implementation functions and methods
Convert py-tui.c to the "python safety" approach
gdb/python/py-ref.h | 86 +++++++++++
gdb/python/py-safety.h | 346 +++++++++++++++++++++++++++++++++++++++++
gdb/python/py-tui.c | 209 +++++++++++--------------
gdb/python/py-wrappers.h | 361 +++++++++++++++++++++++++++++++++++++++++++
gdb/python/python-internal.h | 8 +-
gdb/python/python.c | 5 +-
6 files changed, 890 insertions(+), 125 deletions(-)
---
base-commit: 1500a5d93a2ae02ee227d6b227acfeda9f6d4f6a
change-id: 20260515-python-safety-initial-422bf34a8ce3
Best regards,
Comments
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
Tom> I rewrote my earlier Python safety series to use methods a while back.
Tom> And while I like the result, I got bogged down converting things, so I
Tom> thought I'd send some initial work, with the idea that if this looks
Tom> good, other code can be transformed in pieces.
Ping.
Tom
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes: >>>>> "Tom" == Tom Tromey <tom@tromey.com> writes: Tom> I rewrote my earlier Python safety series to use methods a while back. Tom> And while I like the result, I got bogged down converting things, so I Tom> thought I'd send some initial work, with the idea that if this looks Tom> good, other code can be transformed in pieces. Tom> Ping. Ping again - any thoughts on this? Tom
On 7/19/26 7:16 PM, Tom Tromey wrote: >>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes: > >>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes: > Tom> I rewrote my earlier Python safety series to use methods a while back. > Tom> And while I like the result, I got bogged down converting things, so I > Tom> thought I'd send some initial work, with the idea that if this looks > Tom> good, other code can be transformed in pieces. > > Tom> Ping. > > Ping again - any thoughts on this? > > Tom I applied the series and tried to use it. I found this code: ... /* Note this returns a borrowed reference. */ PyObject *arg = PyTuple_GetItem (args, i); ... and decided to try to convert all PyTuple_GetItem calls. The result of that exercise is attached. I ended up also touching the wrapper function: ... static inline gdbpy_borrowed_ref<> gdbpy_tuple_get_item (gdbpy_borrowed_ref<> tuple, Py_ssize_t pos) { - PyObject *result = PyTuple_GetItem (tuple, pos); + gdbpy_opt_borrowed_ref<> result = PyTuple_GetItem (tuple, pos); if (result == nullptr) throw gdb_python_exception (); - return result; + return (PyObject *)result; } ... but perhaps you left that out intentionally? Anyway, having done this exercise, I like the idea of making borrowed refs explicit in a type. I haven't used the gdbpy_tuple_get_item wrapper, AFAICT it was not applicable. Acked-By: Tom de Vries <tdevries@suse.de> Thanks, - Tom
>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
Tom> I found this code:
Tom> ...
Tom> /* Note this returns a borrowed reference. */
Tom> PyObject *arg = PyTuple_GetItem (args, i);
Tom> ...
Tom> and decided to try to convert all PyTuple_GetItem calls. The result
Tom> of that exercise is attached.
Tom> I ended up also touching the wrapper function:
Tom> ...
Tom> static inline gdbpy_borrowed_ref<>
Tom> gdbpy_tuple_get_item (gdbpy_borrowed_ref<> tuple, Py_ssize_t pos)
Tom> {
Tom> - PyObject *result = PyTuple_GetItem (tuple, pos);
Tom> + gdbpy_opt_borrowed_ref<> result = PyTuple_GetItem (tuple, pos);
Tom> if (result == nullptr)
Tom> throw gdb_python_exception ();
Tom> - return result;
Tom> + return (PyObject *)result;
Tom> }
Tom> ...
Tom> but perhaps you left that out intentionally?
Yeah, in this spot it didn't seem to really be necessary.
The idea of the wrapper file is to eventually isolate all "raw" calls to
the Python API.
Tom> I haven't used the gdbpy_tuple_get_item wrapper, AFAICT it was not
Tom> applicable.
The long term goal is to convert everything to call the wrappers.
This way exception handling will be enforced.
Tom
Tom> Anyway, having done this exercise, I like the idea of making borrowed Tom> refs explicit in a type. ... Tom> Acked-By: Tom de Vries <tdevries@suse.de> FWIW I am going to check this in now. Tom
On 8/7/26 9:48 PM, Tom Tromey wrote: >>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes: > > Tom> I found this code: > Tom> ... > Tom> /* Note this returns a borrowed reference. */ > Tom> PyObject *arg = PyTuple_GetItem (args, i); > Tom> ... > Tom> and decided to try to convert all PyTuple_GetItem calls. The result > Tom> of that exercise is attached. > > Tom> I ended up also touching the wrapper function: > Tom> ... > Tom> static inline gdbpy_borrowed_ref<> > Tom> gdbpy_tuple_get_item (gdbpy_borrowed_ref<> tuple, Py_ssize_t pos) > Tom> { > Tom> - PyObject *result = PyTuple_GetItem (tuple, pos); > Tom> + gdbpy_opt_borrowed_ref<> result = PyTuple_GetItem (tuple, pos); > Tom> if (result == nullptr) > Tom> throw gdb_python_exception (); > Tom> - return result; > Tom> + return (PyObject *)result; > Tom> } > Tom> ... > Tom> but perhaps you left that out intentionally? > > Yeah, in this spot it didn't seem to really be necessary. > The idea of the wrapper file is to eventually isolate all "raw" calls to > the Python API. > > Tom> I haven't used the gdbpy_tuple_get_item wrapper, AFAICT it was not > Tom> applicable. > > The long term goal is to convert everything to call the wrappers. > This way exception handling will be enforced. Ok, I've started over and converted valpy_call, now indeed using gdbpy_tuple_get_item. Thanks, - Tom