Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFC 3/4] Add constexpr functions to create PyMethodDef entries
Date: Sun, 22 Feb 2026 12:49:36 -0700	[thread overview]
Message-ID: <20260222200759.1587070-4-tom@tromey.com> (raw)
In-Reply-To: <20260222200759.1587070-1-tom@tromey.com>

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 <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_PYTHON_PY_SAFETY_H
+#define GDB_PYTHON_PY_SAFETY_H
+
+#include <type_traits>
+#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<auto F>
+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<decltype (F),
+					       gdbpy_borrowed_ref,
+					       gdbpy_borrowed_ref,
+					       gdbpy_borrowed_ref>;
+      if constexpr (std::is_void_v<result_type>)
+	{
+	  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<auto F>
+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<decltype (F),
+						   gdbpy_borrowed_ref>;
+	  if constexpr (std::is_void_v<result_type>)
+	    {
+	      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<auto F>
+constexpr PyMethodDef
+wrap_varargs (std::string_view name, std::string_view doc)
+{
+  using namespace safety_details;
+  return {
+    name.data (),
+    (PyCFunction) wrapped_for_varargs<F>,
+    /* 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


  parent reply	other threads:[~2026-02-22 20:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-22 19:49 [RFC 0/4] Better Python safety Tom Tromey
2026-02-22 19:49 ` [RFC 1/4] Add gdbpy_borrowed_ref Tom Tromey
2026-02-24  4:57   ` Simon Marchi
2026-02-25  3:55     ` Tom Tromey
2026-02-25 15:24       ` Simon Marchi
2026-02-26  1:38       ` Tom Tromey
2026-02-22 19:49 ` [RFC 2/4] Add wrappers for some Python APIs Tom Tromey
2026-02-22 19:49 ` Tom Tromey [this message]
2026-02-22 19:49 ` [RFC 4/4] Convert some Python code to new-style Tom Tromey
2026-02-23 20:28 ` [RFC 0/4] Better Python safety Simon Marchi
2026-02-23 21:00 ` Simon Marchi
2026-02-23 23:23   ` Tom Tromey
2026-02-23 23:56     ` Tom Tromey
2026-02-24  1:05       ` Simon Marchi
2026-02-24 16:29         ` Tom Tromey
2026-02-23 21:22 ` Tom Tromey
2026-03-04 17:39 ` Matthieu Longo
2026-03-04 21:02   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260222200759.1587070-4-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox