Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>, Tom Tromey <tom@tromey.com>
Cc: Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v2 1/9] gdb: switch tuple object helpers to Python limited API equivalents
Date: Tue, 3 Mar 2026 16:16:51 +0000	[thread overview]
Message-ID: <20260303161659.397427-2-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260303161659.397427-1-matthieu.longo@arm.com>

* PyTuple_GET_ITEM -> PyTuple_GetItem
* PyTuple_SET_ITEM -> PyTuple_SetItem
* PyTuple_GET_SIZE -> PyTuple_Size

Unlike PyTuple_SET_ITEM(), PyTuple_SetItem() returns an integer: 0 on
success and -1 on error (e.g. IndexError). The existing code must therefore
be updated to handle this new behaviour.

Since processing now stops when PyTuple_SetItem() returns an error, some
resources (such as newly allocated tuples) must be properly deallocated in
error paths. To address this, this patch replaces the use of raw 'PyObject *'
pointers with gdbpy_ref<>, a reference-counted wrapper around 'PyObject *',
which automatically decrements the reference count on early exit.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
---
 gdb/python/py-color.c     |  7 ++++---
 gdb/python/py-function.c  |  3 ++-
 gdb/python/py-inferior.c  | 16 +++++++---------
 gdb/python/py-infthread.c |  7 ++++---
 gdb/python/py-symbol.c    | 11 +++++++----
 gdb/python/py-type.c      |  5 +++--
 gdb/python/py-unwind.c    |  8 +++++---
 gdb/python/py-xmethods.c  | 24 ++++++++++++++++++------
 gdb/python/python.c       |  5 +++--
 9 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c
index 0bec51b1e52..24589dba265 100644
--- a/gdb/python/py-color.c
+++ b/gdb/python/py-color.c
@@ -119,14 +119,15 @@ get_attr (PyObject *obj, PyObject *attr_name)
 	    return nullptr;
 	}
 
-      PyObject *comp = PyTuple_New (3);
+      gdbpy_ref<> comp (PyTuple_New (3));
       if (comp == nullptr)
 	return nullptr;
 
       for (int i = 0; i < 3; ++i)
-	PyTuple_SET_ITEM (comp, i, rgb_objects[i].release ());
+	if (PyTuple_SetItem (comp.get (), i, rgb_objects[i].release ()) < 0)
+	  return nullptr;
 
-      return comp;
+      return comp.release ();
     }
 
   return PyObject_GenericGetAttr (obj, attr_name);
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 3bb81527a9c..23e0be0ea43 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -48,7 +48,8 @@ convert_values_to_python (int argc, struct value **argv)
       gdbpy_ref<> elt = value_to_value_object (argv[i]);
       if (elt == NULL)
 	return NULL;
-      PyTuple_SetItem (result.get (), i, elt.release ());
+      if (PyTuple_SetItem (result.get (), i, elt.release ()) < 0)
+	return nullptr;
     }
   return result;
 }
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index ab948d28ebe..ae309620e1f 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -382,7 +382,6 @@ infpy_threads (PyObject *self, PyObject *args)
 {
   int i = 0;
   inferior_object *inf_obj = (inferior_object *) self;
-  PyObject *tuple;
 
   INFPY_REQUIRE_VALID (inf_obj);
 
@@ -395,19 +394,18 @@ infpy_threads (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  tuple = PyTuple_New (inf_obj->threads->size ());
-  if (!tuple)
-    return NULL;
+  gdbpy_ref<> tuple (PyTuple_New (inf_obj->threads->size ()));
+  if (tuple == nullptr)
+    return nullptr;
 
   for (const thread_map_t::value_type &entry : *inf_obj->threads)
     {
-      PyObject *thr = (PyObject *) entry.second.get ();
-      Py_INCREF (thr);
-      PyTuple_SET_ITEM (tuple, i, thr);
-      i = i + 1;
+      auto thr = gdbpy_ref<>::new_reference ((PyObject *) entry.second.get ());
+      if (PyTuple_SetItem (tuple.get (), i++, thr.release ()) < 0)
+	return nullptr;
     }
 
-  return tuple;
+  return tuple.release ();
 }
 
 static PyObject *
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index d75742360d4..652355990ee 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -384,9 +384,10 @@ gdbpy_create_ptid_object (ptid_t ptid)
     return nullptr;
 
   /* Note that these steal references, hence the use of 'release'.  */
-  PyTuple_SET_ITEM (ret.get (), 0, pid_obj.release ());
-  PyTuple_SET_ITEM (ret.get (), 1, lwp_obj.release ());
-  PyTuple_SET_ITEM (ret.get (), 2, tid_obj.release ());
+  if (PyTuple_SetItem (ret.get (), 0, pid_obj.release ()) < 0
+      || PyTuple_SetItem (ret.get (), 1, lwp_obj.release ()) < 0
+      || PyTuple_SetItem (ret.get (), 2, tid_obj.release ()) < 0)
+    return nullptr;
 
   return ret;
 }
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 15fcbeea9be..fe4d6dac000 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -402,7 +402,7 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
   const char *name;
   static const char *keywords[] = { "name", "block", "domain", NULL };
   struct symbol *symbol = NULL;
-  PyObject *block_obj = NULL, *bool_obj;
+  PyObject *block_obj = NULL;
   const struct block *block = NULL;
 
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
@@ -450,10 +450,13 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
     }
   else
     sym_obj = gdbpy_ref<>::new_reference (Py_None);
-  PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj.release ());
 
-  bool_obj = PyBool_FromLong (is_a_field_of_this.type != NULL);
-  PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
+  if (PyTuple_SetItem (ret_tuple.get (), 0, sym_obj.release ()) < 0)
+    return nullptr;
+
+  gdbpy_ref<> bool_obj (PyBool_FromLong (is_a_field_of_this.type != NULL));
+  if (PyTuple_SetItem (ret_tuple.get (), 1, bool_obj.release ()) < 0)
+    return nullptr;
 
   return ret_tuple.release ();
 }
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index b6807801f7e..7ba77ad1d4a 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -249,8 +249,9 @@ make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind)
 	gdbpy_ref<> item (PyTuple_New (2));
 	if (item == NULL)
 	  return NULL;
-	PyTuple_SET_ITEM (item.get (), 0, key.release ());
-	PyTuple_SET_ITEM (item.get (), 1, value.release ());
+	if (PyTuple_SetItem (item.get (), 0, key.release ()) < 0
+	    || PyTuple_SetItem (item.get (), 1, value.release ()) < 0)
+	  return nullptr;
 	return item;
       }
     case iter_keys:
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 09af3e7ca2c..9ffa382d093 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -899,11 +899,12 @@ frame_unwind_python::sniff (const frame_info_ptr &this_frame,
 
   /* Verify the return value of _execute_unwinders is a tuple of size 2.  */
   gdb_assert (PyTuple_Check (pyo_execute_ret.get ()));
-  gdb_assert (PyTuple_GET_SIZE (pyo_execute_ret.get ()) == 2);
+  gdb_assert (PyTuple_Size (pyo_execute_ret.get ()) == 2);
 
   if (pyuw_debug)
     {
-      PyObject *pyo_unwinder_name = PyTuple_GET_ITEM (pyo_execute_ret.get (), 1);
+      PyObject *pyo_unwinder_name = PyTuple_GetItem (pyo_execute_ret.get (), 1);
+      gdb_assert (pyo_unwinder_name != nullptr);
       gdb::unique_xmalloc_ptr<char> name
 	= python_string_to_host_string (pyo_unwinder_name);
 
@@ -919,7 +920,8 @@ frame_unwind_python::sniff (const frame_info_ptr &this_frame,
     }
 
   /* Received UnwindInfo, cache data.  */
-  PyObject *pyo_unwind_info = PyTuple_GET_ITEM (pyo_execute_ret.get (), 0);
+  PyObject *pyo_unwind_info = PyTuple_GetItem (pyo_execute_ret.get (), 0);
+  gdb_assert (pyo_unwind_info != nullptr);
   if (!PyObject_TypeCheck (pyo_unwind_info, &unwind_info_object_type))
     error (_("an Unwinder should return gdb.UnwindInfo, not %s."),
 	   Py_TYPE (pyo_unwind_info)->tp_name);
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index 1fafafb4d24..24c8f48e1bd 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -458,9 +458,13 @@ python_xmethod_worker::do_get_result_type (value *obj,
       return EXT_LANG_RC_ERROR;
     }
 
-  /* PyTuple_SET_ITEM steals the reference of the element, hence the
+  /* PyTuple_SetItem steals the reference of the element, hence the
      release.  */
-  PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
+  if (PyTuple_SetItem (py_arg_tuple.get (), 0, py_value_obj.release ()) < 0)
+    {
+      gdbpy_print_stack ();
+      return EXT_LANG_RC_ERROR;
+    }
 
   for (i = 0; i < args.size (); i++)
     {
@@ -471,7 +475,12 @@ python_xmethod_worker::do_get_result_type (value *obj,
 	  gdbpy_print_stack ();
 	  return EXT_LANG_RC_ERROR;
 	}
-      PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg.release ());
+      if (PyTuple_SetItem (py_arg_tuple.get (), i + 1,
+			   py_value_arg.release ()) < 0)
+	{
+	  gdbpy_print_stack ();
+	  return EXT_LANG_RC_ERROR;
+	}
     }
 
   gdbpy_ref<> py_result_type
@@ -543,9 +552,10 @@ python_xmethod_worker::invoke (struct value *obj,
       error (_("Error while executing Python code."));
     }
 
-  /* PyTuple_SET_ITEM steals the reference of the element, hence the
+  /* PyTuple_SetItem steals the reference of the element, hence the
      release.  */
-  PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
+  if (PyTuple_SetItem (py_arg_tuple.get (), 0, py_value_obj.release ()) < 0)
+    return nullptr;
 
   for (i = 0; i < args.size (); i++)
     {
@@ -557,7 +567,9 @@ python_xmethod_worker::invoke (struct value *obj,
 	  error (_("Error while executing Python code."));
 	}
 
-      PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg.release ());
+      if (PyTuple_SetItem (py_arg_tuple.get (), i + 1,
+			   py_value_arg.release ()) < 0)
+	return nullptr;
     }
 
   gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 8739864a861..5474b8d644f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1043,8 +1043,9 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
   else
     unparsed = gdbpy_ref<>::new_reference (Py_None);
 
-  PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
-  PyTuple_SetItem (return_result.get (), 1, result.release ());
+  if (PyTuple_SetItem (return_result.get (), 0, unparsed.release ()) < 0
+      || PyTuple_SetItem (return_result.get (), 1, result.release ()) < 0)
+    return nullptr;
 
   return return_result.release ();
 }
-- 
2.53.0


  reply	other threads:[~2026-03-03 16:26 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-03 16:16 [PATCH v2 0/9] gdb: more fixes for Python limited C API support Matthieu Longo
2026-03-03 16:16 ` Matthieu Longo [this message]
2026-03-03 18:09   ` [PATCH v2 1/9] gdb: switch tuple object helpers to Python limited API equivalents Tom Tromey
2026-03-03 16:16 ` [PATCH v2 2/9] gdb: introduce rgb_color type to simplify existing code Matthieu Longo
2026-03-03 18:16   ` Tom Tromey
2026-03-04 16:30     ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 3/9] gdb: switch bytes object helpers to Python limited API equivalents Matthieu Longo
2026-03-03 18:03   ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 4/9] gdb: add new helpers for retrieving a type's fully qualified name Matthieu Longo
2026-03-03 18:59   ` Tom Tromey
2026-03-06 17:49     ` Matthieu Longo
2026-03-06 19:45       ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 5/9] gdb/python: allow ref_ptr<T, Policy>::new_reference to accept subclasses of T Matthieu Longo
2026-03-03 18:18   ` Tom Tromey
2026-03-04 16:56     ` Matthieu Longo
2026-03-04 18:55       ` Tom Tromey
2026-03-06 11:37     ` Matthieu Longo
2026-03-06 11:43       ` Matthieu Longo
2026-03-06 16:47       ` Tom Tromey
2026-03-09 11:38         ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 6/9] gdb/python: flatten functions calling PyObject_New and use gdbpy_ref Matthieu Longo
2026-03-03 18:22   ` Tom Tromey
2026-03-09 11:41     ` Matthieu Longo
2026-03-03 18:22   ` Tom Tromey
2026-03-03 16:16 ` [PATCH v2 7/9] gdb/python: accept gdbpy_ref in init helpers and return bool Matthieu Longo
2026-03-03 18:24   ` Tom Tromey
2026-03-09 13:25     ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 8/9] gdb/python: add gdbpy_dict_wrapper:allocate_dict helper Matthieu Longo
2026-03-03 18:30   ` Tom Tromey
2026-03-06 12:03     ` Matthieu Longo
2026-03-03 16:16 ` [PATCH v2 9/9] gdb/python: add accessor helpers for __dict__ in Python extension objects Matthieu Longo
2026-03-03 19:02   ` Tom Tromey
2026-03-06 14:33     ` Matthieu Longo
2026-03-06 16:04       ` 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=20260303161659.397427-2-matthieu.longo@arm.com \
    --to=matthieu.longo@arm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /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