]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Return gdbpy_ref<> from gdbpy_call_method
authorTom Tromey <tromey@adacore.com>
Tue, 11 Jun 2024 20:10:08 +0000 (14:10 -0600)
committerTom Tromey <tromey@adacore.com>
Thu, 13 Jun 2024 16:13:36 +0000 (10:13 -0600)
This changes gdbpy_call_method to return a gdbpy_ref<>.  This is
slightly safer because it makes it simpler to correctly handle
reference counts.

Reviewed-By: Tom de Vries <tdevries@suse.de>
gdb/python/py-breakpoint.c
gdb/python/py-disasm.c
gdb/python/py-finishbreakpoint.c
gdb/python/py-framefilter.c
gdb/python/py-tui.c
gdb/python/python-internal.h

index 1b8c7174d344d23f0a3831f679f59a71b95f82a6..fdc24832315071638dbee5e766be1884313ff67e 100644 (file)
@@ -1174,7 +1174,7 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
 
   if (PyObject_HasAttrString (py_bp, stop_func))
     {
-      gdbpy_ref<> result (gdbpy_call_method (py_bp, stop_func));
+      gdbpy_ref<> result = gdbpy_call_method (py_bp, stop_func);
 
       stop = 1;
       if (result != NULL)
index 5206c7628f562ee551ce3f5d735e72bfe414e8da..87fea26c2b275ef2280b428c09e161a5c360c5ce 100644 (file)
@@ -855,8 +855,8 @@ gdbpy_disassembler::read_memory_func (bfd_vma memaddr, gdb_byte *buff,
 
   /* Now call the DisassembleInfo.read_memory method.  This might have been
      overridden by the user.  */
-  gdbpy_ref<> result_obj (gdbpy_call_method ((PyObject *) obj, "read_memory",
-                                            len, offset));
+  gdbpy_ref<> result_obj gdbpy_call_method ((PyObject *) obj, "read_memory",
+                                             len, offset);
 
   /* Handle any exceptions.  */
   if (result_obj == nullptr)
index 1b620e6d30ed8dd68aa09d6873b0ed81d5de7eea..78030be9abfaad474377c569839fbc7c3e1fc5c9 100644 (file)
@@ -344,7 +344,7 @@ bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
       && PyObject_HasAttrString (py_obj, outofscope_func))
     {
-      gdbpy_ref<> meth_result (gdbpy_call_method (py_obj, outofscope_func));
+      gdbpy_ref<> meth_result = gdbpy_call_method (py_obj, outofscope_func);
       if (meth_result == NULL)
        gdbpy_print_stack ();
     }
index 4ae583b4331b23375b5ee78dadbc1ce5eaa42bcb..89695ff5ac5d260ba50fd6adc17ef80b1903baf3 100644 (file)
@@ -59,7 +59,7 @@ extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name,
             struct symbol **sym, const struct block **sym_block,
             const struct language_defn **language)
 {
-  gdbpy_ref<> result (gdbpy_call_method (obj, "symbol"));
+  gdbpy_ref<> result = gdbpy_call_method (obj, "symbol");
 
   if (result == NULL)
     return EXT_LANG_BT_ERROR;
@@ -130,7 +130,7 @@ extract_value (PyObject *obj, struct value **value)
 {
   if (PyObject_HasAttrString (obj, "value"))
     {
-      gdbpy_ref<> vresult (gdbpy_call_method (obj, "value"));
+      gdbpy_ref<> vresult = gdbpy_call_method (obj, "value");
 
       if (vresult == NULL)
        return EXT_LANG_BT_ERROR;
@@ -264,7 +264,7 @@ get_py_iter_from_func (PyObject *filter, const char *func)
 {
   if (PyObject_HasAttrString (filter, func))
     {
-      gdbpy_ref<> result (gdbpy_call_method (filter, func));
+      gdbpy_ref<> result = gdbpy_call_method (filter, func);
 
       if (result != NULL)
        {
@@ -790,7 +790,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
   /* Get the underlying frame.  This is needed to determine GDB
   architecture, and also, in the cases of frame variables/arguments to
   read them if they returned filter object requires us to do so.  */
-  gdbpy_ref<> py_inf_frame (gdbpy_call_method (filter, "inferior_frame"));
+  gdbpy_ref<> py_inf_frame = gdbpy_call_method (filter, "inferior_frame");
   if (py_inf_frame == NULL)
     return EXT_LANG_BT_ERROR;
 
@@ -830,7 +830,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
         address printing.  */
       if (PyObject_HasAttrString (filter, "address"))
        {
-         gdbpy_ref<> paddr (gdbpy_call_method (filter, "address"));
+         gdbpy_ref<> paddr = gdbpy_call_method (filter, "address");
 
          if (paddr == NULL)
            return EXT_LANG_BT_ERROR;
@@ -905,7 +905,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
       /* Print frame function name.  */
       if (PyObject_HasAttrString (filter, "function"))
        {
-         gdbpy_ref<> py_func (gdbpy_call_method (filter, "function"));
+         gdbpy_ref<> py_func = gdbpy_call_method (filter, "function");
          const char *function = NULL;
 
          if (py_func == NULL)
@@ -969,7 +969,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
 
       if (PyObject_HasAttrString (filter, "filename"))
        {
-         gdbpy_ref<> py_fn (gdbpy_call_method (filter, "filename"));
+         gdbpy_ref<> py_fn = gdbpy_call_method (filter, "filename");
 
          if (py_fn == NULL)
            return EXT_LANG_BT_ERROR;
@@ -993,7 +993,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
 
       if (PyObject_HasAttrString (filter, "line"))
        {
-         gdbpy_ref<> py_line (gdbpy_call_method (filter, "line"));
+         gdbpy_ref<> py_line = gdbpy_call_method (filter, "line");
          int line;
 
          if (py_line == NULL)
index 9df86df8c40638b9b4f210b004fbf5916831215a..901f6fe67434538f30574a05b4eda5fcf329d994 100644 (file)
@@ -164,7 +164,7 @@ tui_py_window::~tui_py_window ()
   if (m_window != nullptr
       && PyObject_HasAttrString (m_window.get (), "close"))
     {
-      gdbpy_ref<> result (gdbpy_call_method (m_window.get (), "close"));
+      gdbpy_ref<> result = gdbpy_call_method (m_window.get (), "close");
       if (result == nullptr)
        gdbpy_print_stack ();
     }
@@ -197,7 +197,7 @@ tui_py_window::rerender ()
 
   if (PyObject_HasAttrString (m_window.get (), "render"))
     {
-      gdbpy_ref<> result (gdbpy_call_method (m_window.get (), "render"));
+      gdbpy_ref<> result = gdbpy_call_method (m_window.get (), "render");
       if (result == nullptr)
        gdbpy_print_stack ();
     }
@@ -210,8 +210,8 @@ tui_py_window::do_scroll_horizontal (int num_to_scroll)
 
   if (PyObject_HasAttrString (m_window.get (), "hscroll"))
     {
-      gdbpy_ref<> result (gdbpy_call_method (m_window.get (), "hscroll",
-                                            num_to_scroll));
+      gdbpy_ref<> result = gdbpy_call_method (m_window.get(), "hscroll",
+                                             num_to_scroll);
       if (result == nullptr)
        gdbpy_print_stack ();
     }
@@ -224,8 +224,8 @@ tui_py_window::do_scroll_vertical (int num_to_scroll)
 
   if (PyObject_HasAttrString (m_window.get (), "vscroll"))
     {
-      gdbpy_ref<> result (gdbpy_call_method (m_window.get (), "vscroll",
-                                            num_to_scroll));
+      gdbpy_ref<> result gdbpy_call_method (m_window.get (), "vscroll",
+                                             num_to_scroll);
       if (result == nullptr)
        gdbpy_print_stack ();
     }
@@ -246,8 +246,8 @@ tui_py_window::click (int mouse_x, int mouse_y, int mouse_button)
 
   if (PyObject_HasAttrString (m_window.get (), "click"))
     {
-      gdbpy_ref<> result (gdbpy_call_method (m_window.get (), "click",
-                                            mouse_x, mouse_y, mouse_button));
+      gdbpy_ref<> result gdbpy_call_method (m_window.get (), "click",
+                                             mouse_x, mouse_y, mouse_button);
       if (result == nullptr)
        gdbpy_print_stack ();
     }
index 1ebb5315a2aa40701fd0157347a1ec99d9e882f3..caf85f5f9b997f668203d6913d215984c536bdcf 100644 (file)
@@ -176,14 +176,14 @@ gdbpy_make_fmt ()
 
    This variant accepts no arguments.  */
 
-static inline PyObject *
+static inline gdbpy_ref<>
 gdbpy_call_method (PyObject *o, const char *method)
 {
   /* PyObject_CallMethod's 'method' and 'format' parameters were missing the
      'const' qualifier before Python 3.4.  */
-  return PyObject_CallMethod (o,
-                             const_cast<char *> (method),
-                             nullptr);
+  return gdbpy_ref<> (PyObject_CallMethod (o,
+                                          const_cast<char *> (method),
+                                          nullptr));
 }
 
 /* Typesafe wrapper around PyObject_CallMethod.
@@ -193,7 +193,7 @@ gdbpy_call_method (PyObject *o, const char *method)
    mismatches are impossible.  */
 
 template<typename Arg, typename... Args>
-static inline PyObject *
+static inline gdbpy_ref<>
 gdbpy_call_method (PyObject *o, const char *method,
                   Arg arg, Args... args)
 {
@@ -201,10 +201,10 @@ gdbpy_call_method (PyObject *o, const char *method,
 
   /* PyObject_CallMethod's 'method' and 'format' parameters were missing the
      'const' qualifier before Python 3.4.  */
-  return PyObject_CallMethod (o,
-                             const_cast<char *> (method),
-                             const_cast<char *> (fmt.data ()),
-                             arg, args...);
+  return gdbpy_ref<> (PyObject_CallMethod (o,
+                                          const_cast<char *> (method),
+                                          const_cast<char *> (fmt.data ()),
+                                          arg, args...));
 }
 
 /* Poison PyObject_CallMethod.  The typesafe wrapper gdbpy_call_method should be