[PATCH 2/4] Change evpy_add_attribute to accept gdbpy_borrowed_ref

Tom Tromey <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <20260808-python-safety-events-simple-v1-2-132aea40c801@tromey.com>
This changes evpy_add_attribute to take gdbpy_borrowed_ref arguments.
This allows removing various calls to "get".
---
 gdb/python/py-bpevent.c         |  7 ++-----
 gdb/python/py-connection.c      |  2 +-
 gdb/python/py-corefile.c        |  3 +--
 gdb/python/py-event.c           |  3 ++-
 gdb/python/py-event.h           |  4 ++--
 gdb/python/py-exitedevent.c     |  8 +++-----
 gdb/python/py-inferior.c        | 12 +++++-------
 gdb/python/py-infevents.c       | 12 ++++++------
 gdb/python/py-newobjfileevent.c | 13 +++++--------
 gdb/python/py-progspace.c       |  9 +++------
 gdb/python/py-signalevent.c     |  5 ++---
 gdb/python/py-stopevent.c       |  2 +-
 gdb/python/py-threadevent.c     |  4 +---
 gdb/python/py-tui.c             |  2 +-
 gdb/python/python.c             |  2 +-
 15 files changed, 36 insertions(+), 52 deletions(-)

diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c
index ae8e6e1594f..20a7745e4cc 100644
--- a/gdb/python/py-bpevent.c
+++ b/gdb/python/py-bpevent.c
@@ -32,12 +32,9 @@ create_breakpoint_event_object (const gdbpy_ref<> &dict,
   if (breakpoint_event_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (breakpoint_event_obj.get (),
-			  "breakpoint",
-			  first_bp) < 0)
+  if (evpy_add_attribute (breakpoint_event_obj, "breakpoint", first_bp) < 0)
     return NULL;
-  if (evpy_add_attribute (breakpoint_event_obj.get (),
-			  "breakpoints",
+  if (evpy_add_attribute (breakpoint_event_obj, "breakpoints",
 			  breakpoint_list) < 0)
     return NULL;
 
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index 99bed238b6c..d2085960be4 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -143,7 +143,7 @@ emit_connection_event (process_stratum_target *target,
     return -1;
 
   gdbpy_ref<> conn = target_to_connection_object (target);
-  if (evpy_add_attribute (event_obj.get (), "connection", conn.get ()) < 0)
+  if (evpy_add_attribute (event_obj, "connection", conn) < 0)
     return -1;
 
   return evpy_emit_event (event_obj, registry);
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 542bc05f560..a1dee384edf 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -342,8 +342,7 @@ emit_corefile_changed_event (inferior *inf)
 
   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
   if (inf_obj == nullptr
-      || evpy_add_attribute (event_obj.get (), "inferior",
-			     inf_obj.get ()) < 0)
+      || evpy_add_attribute (event_obj, "inferior", inf_obj) < 0)
     return -1;
 
   return evpy_emit_event (event_obj, gdb_py_events.corefile_changed);
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index 6dd6f3bf356..815e815149c 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -45,7 +45,8 @@ create_event_object (PyTypeObject *py_type)
    function acquires a new reference to ATTR.  */
 
 int
-evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
+evpy_add_attribute (gdbpy_borrowed_ref<> event, const char *name,
+		    gdbpy_borrowed_ref<> attr)
 {
   return PyObject_SetAttrString (event, name, attr);
 }
diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h
index c8fbbf3add6..2c724c9a084 100644
--- a/gdb/python/py-event.h
+++ b/gdb/python/py-event.h
@@ -81,7 +81,7 @@ extern int emit_free_objfile_event (struct objfile *objfile);
 extern int emit_clear_objfiles_event (program_space *pspace);
 
 extern void evpy_dealloc (PyObject *self);
-extern int evpy_add_attribute (PyObject *event,
-			       const char *name, PyObject *attr);
+extern int evpy_add_attribute (gdbpy_borrowed_ref<> event,
+			       const char *name, gdbpy_borrowed_ref<> attr);
 
 #endif /* GDB_PYTHON_PY_EVENT_H */
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 866adb7ff8b..83ccebb9127 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -33,15 +33,13 @@ create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 
       if (exit_code_obj == NULL)
 	return NULL;
-      if (evpy_add_attribute (exited_event.get (), "exit_code",
-			      exit_code_obj.get ()) < 0)
+      if (evpy_add_attribute (exited_event, "exit_code", exit_code_obj) < 0)
 	return NULL;
     }
 
   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
-  if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
-					     "inferior",
-					     (PyObject *) inf_obj.get ()) < 0)
+  if (inf_obj == nullptr
+      || evpy_add_attribute (exited_event, "inferior", inf_obj) < 0)
     return NULL;
 
   return exited_event;
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 5f0019b7b86..ab3a95da1bf 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -255,8 +255,7 @@ python_new_inferior (struct inferior *inf)
 
   gdbpy_ref<> event = create_event_object (&new_inferior_event_object_type);
   if (event == NULL
-      || evpy_add_attribute (event.get (), "inferior",
-			     (PyObject *) inf_obj.get ()) < 0
+      || evpy_add_attribute (event, "inferior", inf_obj) < 0
       || evpy_emit_event (event, gdb_py_events.new_inferior) < 0)
     gdbpy_print_stack ();
 }
@@ -283,8 +282,7 @@ python_inferior_deleted (struct inferior *inf)
 
   gdbpy_ref<> event = create_event_object (&inferior_deleted_event_object_type);
   if (event == NULL
-      || evpy_add_attribute (event.get (), "inferior",
-			     (PyObject *) inf_obj.get ()) < 0
+      || evpy_add_attribute (event, "inferior", inf_obj) < 0
       || evpy_emit_event (event, gdb_py_events.inferior_deleted) < 0)
     gdbpy_print_stack ();
 }
@@ -1037,9 +1035,9 @@ python_context_changed (user_selected_what selection)
   gdbpy_ref<> event
     = create_event_object (&selected_context_event_object_type);
   if (event == nullptr
-      || evpy_add_attribute (event.get (), "inferior", inf_obj.get ()) < 0
-      || evpy_add_attribute (event.get (), "thread", thr_obj.get ()) < 0
-      || evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0
+      || evpy_add_attribute (event, "inferior", inf_obj) < 0
+      || evpy_add_attribute (event, "thread", thr_obj) < 0
+      || evpy_add_attribute (event, "frame", frame_obj) < 0
       || evpy_emit_event (event, gdb_py_events.selected_context) < 0)
     gdbpy_print_stack ();
 }
diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index db77f4593d6..b472c809197 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -44,14 +44,14 @@ create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
   if (ptid_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "ptid", ptid_obj) < 0)
     return NULL;
 
   gdbpy_ref<> addr_obj = gdb_py_object_from_ulongest (addr);
   if (addr_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "address", addr_obj) < 0)
     return NULL;
 
   return event;
@@ -72,14 +72,14 @@ create_register_changed_event_object (const frame_info_ptr &frame,
   if (frame_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "frame", frame_obj) < 0)
     return NULL;
 
   gdbpy_ref<> regnum_obj = gdb_py_object_from_longest (regnum);
   if (regnum_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "regnum", regnum_obj) < 0)
     return NULL;
 
   return event;
@@ -100,14 +100,14 @@ create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
   if (addr_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "address", addr_obj) < 0)
     return NULL;
 
   gdbpy_ref<> len_obj = gdb_py_object_from_longest (len);
   if (len_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0)
+  if (evpy_add_attribute (event, "length", len_obj) < 0)
     return NULL;
 
   return event;
diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index e72e9ae67e3..cac49b2a39c 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -28,9 +28,8 @@ create_new_objfile_event_object (struct objfile *objfile)
     return NULL;
 
   gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
-  if (py_objfile == NULL || evpy_add_attribute (objfile_event.get (),
-						"new_objfile",
-						py_objfile.get ()) < 0)
+  if (py_objfile == nullptr
+      || evpy_add_attribute (objfile_event, "new_objfile", py_objfile) < 0)
     return NULL;
 
   return objfile_event;
@@ -65,8 +64,7 @@ create_free_objfile_event_object (struct objfile *objfile)
 
   gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
   if (py_objfile == nullptr
-      || evpy_add_attribute (objfile_event.get (), "objfile",
-			     py_objfile.get ()) < 0)
+      || evpy_add_attribute (objfile_event, "objfile", py_objfile) < 0)
     return nullptr;
 
   return objfile_event;
@@ -100,9 +98,8 @@ create_clear_objfiles_event_object (program_space *pspace)
     return NULL;
 
   gdbpy_ref<> py_progspace = pspace_to_pspace_object (pspace);
-  if (py_progspace == NULL || evpy_add_attribute (objfile_event.get (),
-						  "progspace",
-						  py_progspace.get ()) < 0)
+  if (py_progspace == nullptr
+      || evpy_add_attribute (objfile_event, "progspace", py_progspace) < 0)
     return NULL;
 
   return objfile_event;
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 31ea3ad8fc8..a88535bdca9 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -638,14 +638,12 @@ emit_executable_changed_event (eventregistry_object *registry,
 
   gdbpy_ref<> py_pspace = pspace_to_pspace_object (pspace);
   if (py_pspace == nullptr
-      || evpy_add_attribute (event_obj.get (), "progspace",
-			     py_pspace.get ()) < 0)
+      || evpy_add_attribute (event_obj, "progspace", py_pspace) < 0)
     return -1;
 
   gdbpy_ref<> py_reload_p (PyBool_FromLong (reload_p ? 1 : 0));
   if (py_reload_p == nullptr
-      || evpy_add_attribute (event_obj.get (), "reload",
-			     py_reload_p.get ()) < 0)
+      || evpy_add_attribute (event_obj, "reload", py_reload_p) < 0)
     return -1;
 
   return evpy_emit_event (event_obj, registry);
@@ -706,8 +704,7 @@ gdbpy_program_space_event (program_space *pspace, bool adding_p)
 
   gdbpy_ref<> event = create_event_object (event_type);
   if (event == nullptr
-      || evpy_add_attribute (event.get (), "progspace",
-			     pspace_obj.get ()) < 0
+      || evpy_add_attribute (event, "progspace", pspace_obj) < 0
       || evpy_emit_event (event, registry) < 0)
     gdbpy_print_stack ();
 }
diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c
index dc8639bb4d8..3be6ab31c6f 100644
--- a/gdb/python/py-signalevent.c
+++ b/gdb/python/py-signalevent.c
@@ -34,9 +34,8 @@ create_signal_event_object (const gdbpy_ref<> &dict,
   gdbpy_ref<> signal_name_obj (PyUnicode_FromString (signal_name));
   if (signal_name_obj == NULL)
     return NULL;
-  if (evpy_add_attribute (signal_event_obj.get (),
-			  "stop_signal",
-			  signal_name_obj.get ()) < 0)
+  if (evpy_add_attribute (signal_event_obj, "stop_signal",
+			  signal_name_obj) < 0)
     return NULL;
 
   return signal_event_obj;
diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c
index 77db754140c..3f07f8a29fb 100644
--- a/gdb/python/py-stopevent.c
+++ b/gdb/python/py-stopevent.c
@@ -32,7 +32,7 @@ create_stop_event_object (PyTypeObject *py_type, const gdbpy_ref<> &dict)
   if (result == nullptr)
     return nullptr;
 
-  if (evpy_add_attribute (result.get (), "details", dict.get ()) < 0)
+  if (evpy_add_attribute (result, "details", dict) < 0)
     return nullptr;
 
   return result;
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index d2998d92304..9b0a963d0ba 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -45,9 +45,7 @@ create_thread_event_object (PyTypeObject *py_type, PyObject *thread)
   if (thread_event_obj == NULL)
     return NULL;
 
-  if (evpy_add_attribute (thread_event_obj.get (),
-			  "inferior_thread",
-			  thread) < 0)
+  if (evpy_add_attribute (thread_event_obj, "inferior_thread", thread) < 0)
     return NULL;
 
   return thread_event_obj;
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 0046c66d570..625de3bf87b 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -605,7 +605,7 @@ gdbpy_tui_enabled (bool state)
     }
 
   gdbpy_ref<> code (PyBool_FromLong (state));
-  if (evpy_add_attribute (event_obj.get (), "enabled", code.get ()) < 0
+  if (evpy_add_attribute (event_obj, "enabled", code) < 0
       || evpy_emit_event (event_obj, gdb_py_events.tui_enabled) < 0)
     gdbpy_print_stack ();
 }
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 5312cdbdb01..83923a2059c 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2500,7 +2500,7 @@ emit_exiting_event (int exit_code)
     return -1;
 
   gdbpy_ref<> code = gdb_py_object_from_longest (exit_code);
-  if (evpy_add_attribute (event_obj.get (), "exit_code", code.get ()) < 0)
+  if (evpy_add_attribute (event_obj, "exit_code", code) < 0)
     return -1;
 
   return evpy_emit_event (event_obj, gdb_py_events.gdb_exiting);

-- 
2.49.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.