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: [PATCH 3/4] Convert gdbpy_frame_stop_reason_string to the safety API
Date: Wed, 19 Aug 2026 17:54:41 -0600	[thread overview]
Message-ID: <20260819-python-safety-frame-v1-3-563cb6b9e7a6@tromey.com> (raw)
In-Reply-To: <20260819-python-safety-frame-v1-0-563cb6b9e7a6@tromey.com>

This converts gdbpy_frame_stop_reason_string to the Python safety API.

This changes the function to accept keyword arguments as well,
following the outcome of an earlier discussion.  A new test is added
for this.
---
 gdb/python/py-frame.c                 | 20 ++++++++------------
 gdb/python/python-internal.h          |  3 ++-
 gdb/python/python.c                   |  4 ++--
 gdb/testsuite/gdb.python/py-frame.exp |  3 +++
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 3910f19ef83..4dd174cf5ae 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -466,24 +466,20 @@ gdbpy_selected_frame ()
 /* Implementation of gdb.stop_reason_string (Integer) -> String.
    Return a string explaining the unwind stop reason.  */
 
-PyObject *
-gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
+const char *
+gdbpy_frame_stop_reason_string (gdbpy_borrowed_ref<> args,
+				gdbpy_opt_borrowed_ref<> kw)
 {
   int reason;
-  const char *str;
 
-  if (!PyArg_ParseTuple (args, "i", &reason))
-    return NULL;
+  static const char *keywords[] = { "reason", nullptr };
+  gdbpy_arg_parse_tuple_and_keywords (args, kw, "i", keywords, &reason);
 
   if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
-    {
-      PyErr_SetString (PyExc_ValueError,
-		       _("Invalid frame stop reason."));
-      return NULL;
-    }
+    gdbpy_err_set_string (PyExc_ValueError,
+			  _("Invalid frame stop reason."));
 
-  str = unwind_stop_reason_to_string ((enum unwind_stop_reason) reason);
-  return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
+  return unwind_stop_reason_to_string ((enum unwind_stop_reason) reason);
 }
 
 /* Implements the equality comparison for Frame objects.
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 76ccdc6b0c6..5796b80037a 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -465,7 +465,8 @@ extern PyObject *gdbpy_history_count (PyObject *self, PyObject *args);
 PyObject *gdbpy_convenience_variable (PyObject *self, PyObject *args);
 PyObject *gdbpy_set_convenience_variable (PyObject *self, PyObject *args);
 PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
-PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
+const char *gdbpy_frame_stop_reason_string (gdbpy_borrowed_ref<> args,
+					    gdbpy_opt_borrowed_ref<> kw);
 gdbpy_ref<> gdbpy_lookup_symbol (gdbpy_borrowed_ref<> args,
 				 gdbpy_opt_borrowed_ref<> kw);
 gdbpy_ref<> gdbpy_lookup_global_symbol (gdbpy_borrowed_ref<> args,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 72c2d7bb10d..84e7c21377a 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -3165,9 +3165,9 @@ Return the newest frame object."),
   noargs_function<gdbpy_selected_frame> ("selected_frame",
     "selected_frame () -> gdb.Frame.\n\
 Return the selected frame object."),
-  { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
+  varargs_function<gdbpy_frame_stop_reason_string> ("frame_stop_reason_string",
     "stop_reason_string (Integer) -> String.\n\
-Return a string explaining unwind stop reason." },
+Return a string explaining unwind stop reason."),
 
   { "start_recording", gdbpy_start_recording, METH_VARARGS,
     "start_recording ([method] [, format]) -> gdb.Record.\n\
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
index aebec651964..30692ed6dd8 100644
--- a/gdb/testsuite/gdb.python/py-frame.exp
+++ b/gdb/testsuite/gdb.python/py-frame.exp
@@ -137,6 +137,9 @@ gdb_test "python print ('result = %s' % (f0.type () == gdb.NORMAL_FRAME))" " = T
 gdb_test "python print ('result = %s' % (f0.unwind_stop_reason () == gdb.FRAME_UNWIND_NO_REASON))" \
     " = True" "test Frame.unwind_stop_reason"
 gdb_test "python print ('result = %s' % gdb.frame_stop_reason_string (gdb.FRAME_UNWIND_INNER_ID))" " = previous frame inner to this frame \\(corrupt stack\\?\\)" "test gdb.frame_stop_reason_string"
+gdb_test "python print ('result = %s' % gdb.frame_stop_reason_string (reason=gdb.FRAME_UNWIND_INNER_ID))" \
+    " = previous frame inner to this frame \\(corrupt stack\\?\\)" \
+    "test gdb.frame_stop_reason_string with keyword"
 gdb_test "python print ('result = %s' % f0.pc ())" " = ${::decimal}" "test Frame.pc"
 gdb_test "python print ('result = %s' % (f0.older () == f1))" " = True" "test Frame.older"
 gdb_test "python print ('result = %s' % (f1.newer () == f0))" " = True" "test Frame.newer"

-- 
2.49.0


  parent reply	other threads:[~2026-08-19 23:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-19 23:54 [PATCH 0/4] Convert py-frame.c to Python " Tom Tromey
2026-08-19 23:54 ` [PATCH 1/4] Basic safety conversion of py-frame.c Tom Tromey
2026-08-19 23:54 ` [PATCH 2/4] Convert gdbpy_newest_frame and gdbpy_selected_frame to safety API Tom Tromey
2026-08-19 23:54 ` Tom Tromey [this message]
2026-08-19 23:54 ` [PATCH 4/4] Convert frapy_richcompare " 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=20260819-python-safety-frame-v1-3-563cb6b9e7a6@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