From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8cPyIYVim2lpsgUAWB0awg (envelope-from ) for ; Sun, 22 Feb 2026 15:09:41 -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=DqO4itHV; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 824EF1E0BA; Sun, 22 Feb 2026 15:09:41 -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 6D9381E08D for ; Sun, 22 Feb 2026 15:09:40 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 5BC364BA2E1C for ; Sun, 22 Feb 2026 20:09:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5BC364BA2E1C 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=DqO4itHV Received: from omta040.useast.a.cloudfilter.net (omta040.useast.a.cloudfilter.net [44.202.169.39]) by sourceware.org (Postfix) with ESMTPS id E1F794BA23F4 for ; Sun, 22 Feb 2026 20:08:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1F794BA23F4 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 E1F794BA23F4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771790895; cv=none; b=fuyRhXqZERutuQhxcscucIskyCxdAq+CkxVXoGbYQbZEur69bzkhTuDUy06br659JWoBjB1QpEeQHUhpRhVlrNNpd9fBS9aKtZ3aAaTcFkvQIc0xC3vVxmcfkB3EYOWVpvwUSjVS1qUt2xmqPUGNgOpFIgyNptV974Ji0BulzhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771790895; c=relaxed/simple; bh=Xng+SBToOiDTfDTyt2GUvvHnmiB5QwHoxEcDTnYzqyA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=bOVxl4qTpNIL5EZEdJJ335ct5EbKzBnsa65SdPgiDBubNKQ/qKAOQfCL1w9QgLECXas9L19MNaDZUCuZe0TBq7ILR3cSPavIwoZekCiYLBy0fNyYsKB0guJauxmgsTKPTO2gX4NRoYQ5IgRrlH3/LwBdIorujUI4tSAE90Welcs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1F794BA23F4 Received: from eig-obgw-6006b.ext.cloudfilter.net ([10.0.30.211]) by cmsmtp with ESMTPS id u949v0gAHCxrGuFkUvF3Gz; Sun, 22 Feb 2026 20:08:14 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id uFkTvqTepvXvHuFkTvUEE2; Sun, 22 Feb 2026 20:08:14 +0000 X-Authority-Analysis: v=2.4 cv=e4IGSbp/ c=1 sm=1 tr=0 ts=699b622e a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=HzLeVaNsDn8A:10 a=ItBw4LHWJt0A:10 a=mDV3o1hIAAAA:8 a=4Aei8iY7hsenCpfh7JIA:9 a=hpBUXDQk55J5isa0HelX:22 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=PEQGHP3RzABykz9r+UudAqW7kIK8IgwDLMK+QMXVEs0=; b=DqO4itHVsEGT5LOsc+VFYAjAY6 1jDCLYGsvem0HJ/bC+p7UakI3kf3HNGCSRgrkXMhQknUePjgfnQyvtQtgKjT03+DPtK6icUSn+wtA 6ZP8Wm9wLLzj7pVz3mJwJvGN5; 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 1vuFkT-00000003hGc-1tu5; Sun, 22 Feb 2026 13:08:13 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC 3/4] Add constexpr functions to create PyMethodDef entries Date: Sun, 22 Feb 2026 12:49:36 -0700 Message-ID: <20260222200759.1587070-4-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: 1vuFkT-00000003hGc-1tu5 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: 4 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfECHxwX5NM9TLQRrBzwZBVJHpnHJnf7ti1bHfUCyVKmyl32pxcTeL+gXnO89Zg+YYKQeD4+jcmkN1DnFaP3Ny2sJOgjKr1L/NPx1lv7TFpGmKzjRjpvk RPe6M8T+PXRcnjI3V/1OmeQVEK96EY3klWneOPMBnf/Fz9EGJ7coWevOXv7nCkWdmTdBykGtbWTUYawpRp8YxZfSKgT/LhU1baI= 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 couple of new constexpr functions to create PyMethodDef entries. This provides a few safety benefits: * The new-style API approach (see previous patch) is implemented by the wrapper. That is, exceptions are caught here and transformed. * The implementation functions can now return any reasonable type, with automatic conversion by the wrapper. * The function API and the appropriate METH_* flags are handled together, avoiding any possible discrepancy. This approach also means that we can modify the old rule that gdb calls must be wrapped in a try/catch -- the try/catch is now provided by the wrapper function, so the implementation can be written in a more natural way. Note this patch is not really complete. There should be one more wrapper for case where a method takes a single argument (though we probably cannot use METH_O unfortunately). And, it would be nice to share the wrapper implementations. However this requires some relatively hairy template metaprogramming. --- gdb/python/py-safety.h | 190 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 gdb/python/py-safety.h diff --git a/gdb/python/py-safety.h b/gdb/python/py-safety.h new file mode 100644 index 00000000000..fd6802468ed --- /dev/null +++ b/gdb/python/py-safety.h @@ -0,0 +1,190 @@ +/* Wrappers for some Python safety. + + Copyright (C) 2026 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef GDB_PYTHON_PY_SAFETY_H +#define GDB_PYTHON_PY_SAFETY_H + +#include +#include "py-ref.h" +#include "py-wrappers.h" + +/* Implementation details of the method-wrapping safety code are put + into this namespace, just to emphasize that these shouldn't be used + elsewhere. */ + +namespace safety_details +{ +/* Overloads of "result_converter" are used by the safety wrappers to + convert a function's real return value to a Python object. A new + reference will always be returned. */ + +static inline PyObject * +result_converter (bool value) +{ + if (value) + Py_RETURN_TRUE; + Py_RETURN_FALSE; +} + +static inline PyObject * +result_converter (LONGEST value) +{ + return gdb_py_object_from_longest (value).release (); +} + +static inline PyObject * +result_converter (int value) +{ + return gdb_py_object_from_longest (value).release (); +} + +static inline PyObject * +result_converter (ULONGEST value) +{ + return gdb_py_object_from_ulongest (value).release (); +} + +static inline PyObject * +result_converter (const char *value) +{ + // FIXME do we actually want PyUnicode_Decode with host_charset here + return PyUnicode_FromString (value); +} + +static inline PyObject * +result_converter (gdbpy_ref<> &&value) +{ + return value.release (); +} + +/* This is a template function because using a lambda in wrap_varargs + caused compiler errors due to the necessary cast to + PyCFunction. */ +template +PyObject * +wrapped_for_varargs (PyObject *self, PyObject *args, PyObject *kw) +{ + try + { + gdbpy_borrowed_ref self_arg (self); + gdbpy_borrowed_ref args_arg (args); + gdbpy_borrowed_ref kw_arg (kw); + using result_type = std::invoke_result_t; + if constexpr (std::is_void_v) + { + F (self_arg, args_arg, kw_arg); + Py_RETURN_NONE; + } + else + return result_converter (F (self_arg, args_arg, kw_arg)); + } + catch (const gdb_python_exception &pye) + { + gdb_assert (PyErr_Occurred () != nullptr); + return nullptr; + } + catch (const gdb_exception &exc) + { + return gdbpy_handle_gdb_exception (nullptr, exc); + } +} + +} /* namespace safety_details */ + +/* This is used to create the PyMethodDef for a no-argument method. + It takes the underlying implementation function as a template + argument, and also arguments for the method name and documentation + string. + + The underlying function should accept a single gdbpy_borrowed_ref + argument. This is the 'self' argument. The function can return + any type (see the result_converter overloads); and should throw an + exception on error. If gdb_python_exception is thrown, the Python + exception must already have been set. +*/ +template +constexpr PyMethodDef +wrap_noargs (std::string_view name, std::string_view doc) +{ + using namespace safety_details; + return { + name.data (), + [] (PyObject *self, PyObject *args) -> PyObject * + { + try + { + gdbpy_borrowed_ref self_arg (self); + using result_type = std::invoke_result_t; + if constexpr (std::is_void_v) + { + F (self_arg); + Py_RETURN_NONE; + } + else + return result_converter (F (self_arg)); + } + catch (const gdb_python_exception &pye) + { + gdb_assert (PyErr_Occurred () != nullptr); + return nullptr; + } + catch (const gdb_exception &exc) + { + return gdbpy_handle_gdb_exception (nullptr, exc); + } + }, + METH_NOARGS, + doc.data (), + }; +} + +/* This is used to create the PyMethodDef for a varargs method. It + takes the underlying implementation function as a template + argument, and also arguments for the method name and documentation + string. + + The underlying function should accept a three gdbpy_borrowed_ref + arguments: the 'self' argument, the arguments, and the keywords. + The function can return any type (see the result_converter + overloads); and should throw an exception on error. If + gdb_python_exception is thrown, the Python exception must already + have been set. + + The gdb policy is that varargs methods must also accept keywords, + and this is enforced here. +*/ +template +constexpr PyMethodDef +wrap_varargs (std::string_view name, std::string_view doc) +{ + using namespace safety_details; + return { + name.data (), + (PyCFunction) wrapped_for_varargs, + /* gdb's rule is that varargs should also use keywords. */ + METH_VARARGS | METH_KEYWORDS, + doc.data (), + }; +} + +#endif /* GDB_PYTHON_PY_SAFETY_H */ -- 2.49.0