diff mbox

[RFA,02/14] Change event code to use gdbpy_reference

Message ID 1478497656-11832-3-git-send-email-tom@tromey.com
State New
Headers show

Commit Message

Tom Tromey Nov. 7, 2016, 5:47 a.m. UTC
This changes the event code in the Python layer to use
gdbpy_reference, simplifying the logic in many places.

It also changes evpy_emit_event not to steal a reference to its
argument.  This is simpler to do now that gdbpy_reference is in use;
it's also a reasonable cleanup in its own right.  While doing this I
realized that evpy_emit_event should not be calling gdbpy_print_stack
(all the outermost callers do this if needed), so I removed this as
well.

2016-11-06  Tom Tromey  <tom@tromey.com>

	* python/py-threadevent.c (create_thread_event_object): Use
	gdbpy_reference.
	* python/py-stopevent.c (create_stop_event_object): Simplify.
	(emit_stop_event): Use gdbpy_reference.
	* python/py-signalevent.c (create_signal_event_object): Use
	gdbpy_reference.
	* python/py-newobjfileevent.c (create_new_objfile_event_object)
	(emit_new_objfile_event, create_clear_objfiles_event_object)
	(emit_clear_objfiles_event): Use gdbpy_reference.
	* python/py-infevents.c (create_inferior_call_event_object)
	(create_register_changed_event_object)
	(create_memory_changed_event_object, emit_inferior_call_event)
	(emit_memory_changed_event, emit_register_changed_event): Use
	gdbpy_reference.
	* python/py-exitedevent.c (create_exited_event_object)
	(emit_exited_event): Use gdbpy_reference.
	* python/py-event.h (evpy_emit_event): Remove
	CPYCHECKER_STEALS_REFERENCE_TO_ARG annotation.
	* python/py-event.c (evpy_emit_event): Use gdbpy_reference.
	* python/py-continueevent.c (emit_continue_event): Use
	gdbpy_reference.
	* python/py-breakpoint.c (gdbpy_breakpoint_created)
	(gdbpy_breakpoint_deleted, gdbpy_breakpoint_modified): Use
	gdbpy_reference.
	* python/py-bpevent.c (create_breakpoint_event_object): Use
	gdbpy_reference.
---
 gdb/ChangeLog                   |  29 +++++++++
 gdb/python/py-bpevent.c         |  23 +++----
 gdb/python/py-breakpoint.c      |   7 +-
 gdb/python/py-continueevent.c   |   9 ++-
 gdb/python/py-event.c           |  29 +++------
 gdb/python/py-event.h           |   4 +-
 gdb/python/py-exitedevent.c     |  50 +++++---------
 gdb/python/py-infevents.c       | 141 ++++++++++++----------------------------
 gdb/python/py-newobjfileevent.c |  52 ++++++---------
 gdb/python/py-signalevent.c     |  27 +++-----
 gdb/python/py-stopevent.c       |  54 ++++++---------
 gdb/python/py-threadevent.c     |  19 ++----
 12 files changed, 172 insertions(+), 272 deletions(-)

Comments

Pedro Alves Nov. 11, 2016, 12:09 a.m. UTC | #1
On 11/07/2016 05:47 AM, Tom Tromey wrote:
>  PyObject *
>  create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
>  {
> -  PyObject *breakpoint_event_obj =
> -      create_stop_event_object (&breakpoint_event_object_type);
> +  gdbpy_reference breakpoint_event_obj
> +    (create_stop_event_object (&breakpoint_event_object_type));
>  

> - fail:
> -  Py_XDECREF (breakpoint_event_obj);
> -  return NULL;
> +  return breakpoint_event_obj.release ();
>  }

Wouldn't it better to make these create_foo functions return a
gdbpy_reference ?  Then at the call site you'd use std::move instead
of reset (when assigning to an existing lval):

      stop_event_obj = std::move (create_breakpoint_event_object (list.get (),
							          first_bp));

The advantage of following such a principle is that it makes it impossible
for callers to forget to manage the result with a gdbpy_reference, since
assigning the return to a raw PyObject * won't compile.

Thanks,
Pedro Alves
Pedro Alves Nov. 11, 2016, 12:50 a.m. UTC | #2
On 11/11/2016 12:09 AM, Pedro Alves wrote:
> On 11/07/2016 05:47 AM, Tom Tromey wrote:
>>  PyObject *
>>  create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
>>  {
>> -  PyObject *breakpoint_event_obj =
>> -      create_stop_event_object (&breakpoint_event_object_type);
>> +  gdbpy_reference breakpoint_event_obj
>> +    (create_stop_event_object (&breakpoint_event_object_type));
>>  
> 
>> - fail:
>> -  Py_XDECREF (breakpoint_event_obj);
>> -  return NULL;
>> +  return breakpoint_event_obj.release ();
>>  }
> 
> Wouldn't it better to make these create_foo functions return a
> gdbpy_reference ?  Then at the call site you'd use std::move instead
> of reset (when assigning to an existing lval):
> 
>       stop_event_obj = std::move (create_breakpoint_event_object (list.get (),
> 							          first_bp));

Err, not even std::move would be necessary, of course, 
since the function's return would be an rvalue in this context already.

So you'd just do:

  stop_event_obj = create_breakpoint_event_object (list.get (), first_bp));

That could be done after this series lands, in any case.

> The advantage of following such a principle is that it makes it impossible
> for callers to forget to manage the result with a gdbpy_reference, since
> assigning the return to a raw PyObject * won't compile.

Thanks,
Pedro Alves
diff mbox

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index b46e26a..a827e37 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,34 @@ 
 2016-11-06  Tom Tromey  <tom@tromey.com>
 
+	* python/py-threadevent.c (create_thread_event_object): Use
+	gdbpy_reference.
+	* python/py-stopevent.c (create_stop_event_object): Simplify.
+	(emit_stop_event): Use gdbpy_reference.
+	* python/py-signalevent.c (create_signal_event_object): Use
+	gdbpy_reference.
+	* python/py-newobjfileevent.c (create_new_objfile_event_object)
+	(emit_new_objfile_event, create_clear_objfiles_event_object)
+	(emit_clear_objfiles_event): Use gdbpy_reference.
+	* python/py-infevents.c (create_inferior_call_event_object)
+	(create_register_changed_event_object)
+	(create_memory_changed_event_object, emit_inferior_call_event)
+	(emit_memory_changed_event, emit_register_changed_event): Use
+	gdbpy_reference.
+	* python/py-exitedevent.c (create_exited_event_object)
+	(emit_exited_event): Use gdbpy_reference.
+	* python/py-event.h (evpy_emit_event): Remove
+	CPYCHECKER_STEALS_REFERENCE_TO_ARG annotation.
+	* python/py-event.c (evpy_emit_event): Use gdbpy_reference.
+	* python/py-continueevent.c (emit_continue_event): Use
+	gdbpy_reference.
+	* python/py-breakpoint.c (gdbpy_breakpoint_created)
+	(gdbpy_breakpoint_deleted, gdbpy_breakpoint_modified): Use
+	gdbpy_reference.
+	* python/py-bpevent.c (create_breakpoint_event_object): Use
+	gdbpy_reference.
+
+2016-11-06  Tom Tromey  <tom@tromey.com>
+
 	* python/py-ref.h: New file.
 
 2016-10-29  Manish Goregaokar  <manish@mozilla.com>
diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c
index f5ff2ef..787f927 100644
--- a/gdb/python/py-bpevent.c
+++ b/gdb/python/py-bpevent.c
@@ -19,6 +19,7 @@ 
 
 #include "defs.h"
 #include "py-stopevent.h"
+#include "py-ref.h"
 
 extern PyTypeObject breakpoint_event_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
@@ -29,26 +30,22 @@  extern PyTypeObject breakpoint_event_object_type
 PyObject *
 create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
 {
-  PyObject *breakpoint_event_obj =
-      create_stop_event_object (&breakpoint_event_object_type);
+  gdbpy_reference breakpoint_event_obj
+    (create_stop_event_object (&breakpoint_event_object_type));
 
-  if (!breakpoint_event_obj)
-    goto fail;
+  if (breakpoint_event_obj == NULL)
+    return NULL;
 
-  if (evpy_add_attribute (breakpoint_event_obj,
+  if (evpy_add_attribute (breakpoint_event_obj.get (),
                           "breakpoint",
                           first_bp) < 0)
-    goto fail;
-  if (evpy_add_attribute (breakpoint_event_obj,
+    return NULL;
+  if (evpy_add_attribute (breakpoint_event_obj.get (),
                           "breakpoints",
                           breakpoint_list) < 0)
-    goto fail;
+    return NULL;
 
-  return breakpoint_event_obj;
-
- fail:
-  Py_XDECREF (breakpoint_event_obj);
-  return NULL;
+  return breakpoint_event_obj.release ();
 }
 
 GDBPY_NEW_EVENT_TYPE (breakpoint,
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 80f5d1f..656b13a 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -919,7 +919,6 @@  gdbpy_breakpoint_created (struct breakpoint *bp)
 
   if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
     {
-      Py_INCREF (newbp);
       if (evpy_emit_event ((PyObject *) newbp,
 			   gdb_py_events.breakpoint_created) < 0)
 	gdbpy_print_stack ();
@@ -947,10 +946,7 @@  gdbpy_breakpoint_deleted (struct breakpoint *b)
 	{
 	  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
 	    {
-	      PyObject *bp_obj_alias = (PyObject *) bp_obj;
-
-	      Py_INCREF (bp_obj_alias);
-	      if (evpy_emit_event (bp_obj_alias,
+	      if (evpy_emit_event ((PyObject *) bp_obj,
 				   gdb_py_events.breakpoint_deleted) < 0)
 		gdbpy_print_stack ();
 	    }
@@ -982,7 +978,6 @@  gdbpy_breakpoint_modified (struct breakpoint *b)
 	{
 	  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
 	    {
-	      Py_INCREF (bp_obj);
 	      if (evpy_emit_event (bp_obj,
 				   gdb_py_events.breakpoint_modified) < 0)
 		gdbpy_print_stack ();
diff --git a/gdb/python/py-continueevent.c b/gdb/python/py-continueevent.c
index 2f21524..5bd0b39 100644
--- a/gdb/python/py-continueevent.c
+++ b/gdb/python/py-continueevent.c
@@ -19,6 +19,7 @@ 
 
 #include "defs.h"
 #include "py-event.h"
+#include "py-ref.h"
 
 extern PyTypeObject continue_event_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
@@ -36,14 +37,12 @@  create_continue_event_object (void)
 int
 emit_continue_event (ptid_t ptid)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.cont))
     return 0;
 
-  event = create_continue_event_object ();
-  if (event)
-    return evpy_emit_event (event, gdb_py_events.cont);
+  gdbpy_reference event (create_continue_event_object ());
+  if (event != NULL)
+    return evpy_emit_event (event.get (), gdb_py_events.cont);
   return -1;
 }
 
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index 0dd94b4..c973337 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -89,26 +89,25 @@  int
 evpy_emit_event (PyObject *event,
                  eventregistry_object *registry)
 {
-  PyObject *callback_list_copy = NULL;
   Py_ssize_t i;
 
   /* Create a copy of call back list and use that for
      notifying listeners to avoid skipping callbacks
      in the case of a callback being disconnected during
      a notification.  */
-  callback_list_copy = PySequence_List (registry->callbacks);
-  if (!callback_list_copy)
-    goto fail;
+  gdbpy_reference callback_list_copy (PySequence_List (registry->callbacks));
+  if (callback_list_copy == NULL)
+    return -1;
 
-  for (i = 0; i < PyList_Size (callback_list_copy); i++)
+  for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
     {
-      PyObject *func = PyList_GetItem (callback_list_copy, i);
-      PyObject *func_result;
+      PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
 
       if (func == NULL)
-	goto fail;
+	return -1;
 
-      func_result = PyObject_CallFunctionObjArgs (func, event, NULL);
+      gdbpy_reference
+	func_result (PyObject_CallFunctionObjArgs (func, event, NULL));
 
       if (func_result == NULL)
 	{
@@ -116,21 +115,9 @@  evpy_emit_event (PyObject *event,
 	     call all of the callbacks even if one is broken.  */
 	  gdbpy_print_stack ();
 	}
-      else
-	{
-	  Py_DECREF (func_result);
-	}
     }
 
-  Py_XDECREF (callback_list_copy);
-  Py_XDECREF (event);
   return 0;
-
- fail:
-  gdbpy_print_stack ();
-  Py_XDECREF (callback_list_copy);
-  Py_XDECREF (event);
-  return -1;
 }
 
 static PyGetSetDef event_object_getset[] =
diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h
index 998fc84..786a963 100644
--- a/gdb/python/py-event.h
+++ b/gdb/python/py-event.h
@@ -24,6 +24,7 @@ 
 #include "command.h"
 #include "python-internal.h"
 #include "inferior.h"
+#include "py-ref.h"
 
 /* This macro creates the following functions:
 
@@ -121,8 +122,7 @@  extern int emit_register_changed_event (struct frame_info *frame,
 				        int regnum);
 extern int emit_memory_changed_event (CORE_ADDR addr, ssize_t len);
 extern int evpy_emit_event (PyObject *event,
-                            eventregistry_object *registry)
-  CPYCHECKER_STEALS_REFERENCE_TO_ARG (1);
+                            eventregistry_object *registry);
 
 extern PyObject *create_event_object (PyTypeObject *py_type);
 extern PyObject *create_thread_event_object (PyTypeObject *py_type);
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 87eedf8..6000dee 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -26,42 +26,30 @@  extern PyTypeObject exited_event_object_type
 static PyObject *
 create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 {
-  PyObject *exited_event;
-  PyObject *inf_obj = NULL;
+  gdbpy_reference exited_event
+    (create_event_object (&exited_event_object_type));
 
-  exited_event = create_event_object (&exited_event_object_type);
-
-  if (!exited_event)
-    goto fail;
+  if (exited_event == NULL)
+    return NULL;
 
   if (exit_code)
     {
-      PyObject *exit_code_obj = PyLong_FromLongLong (*exit_code);
-      int failed;
+      gdbpy_reference exit_code_obj (PyLong_FromLongLong (*exit_code));
 
       if (exit_code_obj == NULL)
-	goto fail;
-
-      failed = evpy_add_attribute (exited_event, "exit_code",
-				   exit_code_obj) < 0;
-      Py_DECREF (exit_code_obj);
-      if (failed)
-	goto fail;
+	return NULL;
+      if (evpy_add_attribute (exited_event.get (), "exit_code",
+			      exit_code_obj.get ()) < 0)
+	return NULL;
     }
 
-  inf_obj = inferior_to_inferior_object (inf);
-  if (!inf_obj || evpy_add_attribute (exited_event,
-                                      "inferior",
-                                      inf_obj) < 0)
-    goto fail;
-  Py_DECREF (inf_obj);
+  gdbpy_reference inf_obj (inferior_to_inferior_object (inf));
+  if (inf_obj == NULL || evpy_add_attribute (exited_event.get (),
+					     "inferior",
+					     inf_obj.get ()) < 0)
+    return NULL;
 
-  return exited_event;
-
- fail:
-  Py_XDECREF (inf_obj);
-  Py_XDECREF (exited_event);
-  return NULL;
+  return exited_event.release ();
 }
 
 /* Callback that is used when an exit event occurs.  This function
@@ -70,15 +58,13 @@  create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
 int
 emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.exited))
     return 0;
 
-  event = create_exited_event_object (exit_code, inf);
+  gdbpy_reference event (create_exited_event_object (exit_code, inf));
 
-  if (event)
-    return evpy_emit_event (event, gdb_py_events.exited);
+  if (event != NULL)
+    return evpy_emit_event (event.get (), gdb_py_events.exited);
 
   return -1;
 }
diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index 33d994c..34b9dc9 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -19,6 +19,7 @@ 
 
 #include "defs.h"
 #include "py-event.h"
+#include "py-ref.h"
 
 extern PyTypeObject inferior_call_pre_event_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
@@ -36,52 +37,36 @@  static PyObject *
 create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
 				   CORE_ADDR addr)
 {
-  PyObject *event;
-  PyObject *ptid_obj = NULL;
-  PyObject *addr_obj = NULL;
+  gdbpy_reference event;
   int failed;
-  struct cleanup *cleanups;
-  struct cleanup *member_cleanups;
 
   switch (flag)
     {
     case INFERIOR_CALL_PRE:
-      event = create_event_object (&inferior_call_pre_event_object_type);
+      event.reset (create_event_object (&inferior_call_pre_event_object_type));
       break;
     case INFERIOR_CALL_POST:
-      event = create_event_object (&inferior_call_post_event_object_type);
+      event.reset (create_event_object (&inferior_call_post_event_object_type));
       break;
     default:
-      return NULL;
+      gdb_assert_not_reached ("invalid inferior_call_kind");
     }
 
-  cleanups = make_cleanup_py_decref (event);
-
-  ptid_obj = gdbpy_create_ptid_object (ptid);
+  gdbpy_reference ptid_obj (gdbpy_create_ptid_object (ptid));
   if (ptid_obj == NULL)
-    goto fail;
-  member_cleanups = make_cleanup_py_decref (ptid_obj);
+    return NULL;
 
-  failed = evpy_add_attribute (event, "ptid", ptid_obj) < 0;
-  if (failed)
-    goto fail;
+  if (evpy_add_attribute (event.get (), "ptid", ptid_obj.get ()) < 0)
+    return NULL;
 
-  addr_obj = PyLong_FromLongLong (addr);
+  gdbpy_reference addr_obj (PyLong_FromLongLong (addr));
   if (addr_obj == NULL)
-    goto fail;
-  make_cleanup_py_decref (addr_obj);
-
-  failed = evpy_add_attribute (event, "address", addr_obj) < 0;
-  if (failed)
-    goto fail;
+    return NULL;
 
-  do_cleanups (member_cleanups);
-  discard_cleanups (cleanups);
-  return event;
+  if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+    return NULL;
 
- fail:
-  do_cleanups (cleanups);
-  return NULL;
+  return event.release ();
 }
 
 /* Construct a gdb.RegisterChangedEvent containing the affected
@@ -91,44 +76,26 @@  static PyObject *
 create_register_changed_event_object (struct frame_info *frame, 
 				      int regnum)
 {
-  PyObject *event;
-  PyObject *frame_obj = NULL;
-  PyObject *regnum_obj = NULL;
-  int failed;
-  struct cleanup *cleanups;
-  struct cleanup *member_cleanups;
-
-  event = create_event_object (&register_changed_event_object_type);
+  gdbpy_reference event
+    (create_event_object (&register_changed_event_object_type));
   if (event == NULL)
     return NULL;
 
-  cleanups = make_cleanup_py_decref (event);
-
-  frame_obj = frame_info_to_frame_object (frame);
+  gdbpy_reference frame_obj (frame_info_to_frame_object (frame));
   if (frame_obj == NULL)
-    goto fail;
-  member_cleanups = make_cleanup_py_decref (frame_obj);
+    return NULL;
 
-  failed = evpy_add_attribute (event, "frame", frame_obj) < 0;
-  if (failed)
-    goto fail;
+  if (evpy_add_attribute (event.get (), "frame", frame_obj.get ()) < 0)
+    return NULL;
 
-  regnum_obj = PyLong_FromLongLong (regnum);
+  gdbpy_reference regnum_obj (PyLong_FromLongLong (regnum));
   if (regnum_obj == NULL)
-    goto fail;
-  make_cleanup_py_decref (regnum_obj);
-
-  failed = evpy_add_attribute (event, "regnum", regnum_obj) < 0;
-  if (failed)
-    goto fail;
+    return NULL;
 
-  do_cleanups (member_cleanups);
-  discard_cleanups (cleanups);
-  return event;
+  if (evpy_add_attribute (event.get (), "regnum", regnum_obj.get ()) < 0)
+    return NULL;
 
- fail:
-  do_cleanups (cleanups);
-  return NULL;
+  return event.release ();
 }
 
 /* Construct a gdb.MemoryChangedEvent describing the extent of the
@@ -137,44 +104,27 @@  create_register_changed_event_object (struct frame_info *frame,
 static PyObject *
 create_memory_changed_event_object (CORE_ADDR addr, ssize_t len)
 {
-  PyObject *event;
-  PyObject *addr_obj = NULL;
-  PyObject *len_obj = NULL;
-  int failed;
-  struct cleanup *cleanups;
-  struct cleanup *member_cleanups;
-
-  event = create_event_object (&memory_changed_event_object_type);
+  gdbpy_reference event
+    (create_event_object (&memory_changed_event_object_type));
 
   if (event == NULL)
     return NULL;
-  cleanups = make_cleanup_py_decref (event);
 
-  addr_obj = PyLong_FromLongLong (addr);
+  gdbpy_reference addr_obj (PyLong_FromLongLong (addr));
   if (addr_obj == NULL)
-    goto fail;
-  member_cleanups = make_cleanup_py_decref (addr_obj);
+    return NULL;
 
-  failed = evpy_add_attribute (event, "address", addr_obj) < 0;
-  if (failed)
-    goto fail;
+  if (evpy_add_attribute (event.get (), "address", addr_obj.get ()) < 0)
+    return NULL;
 
-  len_obj = PyLong_FromLong (len);
+  gdbpy_reference len_obj (PyLong_FromLong (len));
   if (len_obj == NULL)
-    goto fail;
-  make_cleanup_py_decref (len_obj);
-
-  failed = evpy_add_attribute (event, "length", len_obj) < 0;
-  if (failed)
-    goto fail;
+    return NULL;
 
-  do_cleanups (member_cleanups);
-  discard_cleanups (cleanups);
-  return event;
+  if (evpy_add_attribute (event.get (), "length", len_obj.get ()) < 0)
+    return NULL;
 
- fail:
-  do_cleanups (cleanups);
-  return NULL;
+  return event.release ();
 }
 
 /* Callback function which notifies observers when an event occurs which
@@ -186,14 +136,13 @@  int
 emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
 			  CORE_ADDR addr)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
     return 0;
 
-  event = create_inferior_call_event_object (flag, thread, addr);
+  gdbpy_reference event (create_inferior_call_event_object (flag, thread,
+							    addr));
   if (event != NULL)
-    return evpy_emit_event (event, gdb_py_events.inferior_call);
+    return evpy_emit_event (event.get (), gdb_py_events.inferior_call);
   return -1;
 }
 
@@ -203,14 +152,12 @@  emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
 int
 emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
     return 0;
 
-  event = create_memory_changed_event_object (addr, len);
+  gdbpy_reference event (create_memory_changed_event_object (addr, len));
   if (event != NULL)
-    return evpy_emit_event (event, gdb_py_events.memory_changed);
+    return evpy_emit_event (event.get (), gdb_py_events.memory_changed);
   return -1;
 }
 
@@ -220,14 +167,12 @@  emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
 int
 emit_register_changed_event (struct frame_info* frame, int regnum)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.register_changed))
     return 0;
 
-  event = create_register_changed_event_object (frame, regnum);
+  gdbpy_reference event (create_register_changed_event_object (frame, regnum));
   if (event != NULL)
-    return evpy_emit_event (event, gdb_py_events.register_changed);
+    return evpy_emit_event (event.get (), gdb_py_events.register_changed);
   return -1;
 }
 
diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index 3dd2430b..a96e4f0 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -28,26 +28,22 @@  extern PyTypeObject clear_objfiles_event_object_type
 static PyObject *
 create_new_objfile_event_object (struct objfile *objfile)
 {
-  PyObject *objfile_event;
   PyObject *py_objfile;
 
-  objfile_event = create_event_object (&new_objfile_event_object_type);
-  if (!objfile_event)
-    goto fail;
+  gdbpy_reference objfile_event
+    (create_event_object (&new_objfile_event_object_type));
+  if (objfile_event == NULL)
+    return NULL;
 
   /* Note that objfile_to_objfile_object returns a borrowed reference,
      so we don't need a decref here.  */
   py_objfile = objfile_to_objfile_object (objfile);
-  if (!py_objfile || evpy_add_attribute (objfile_event,
+  if (!py_objfile || evpy_add_attribute (objfile_event.get (),
                                          "new_objfile",
                                          py_objfile) < 0)
-    goto fail;
+    return NULL;
 
-  return objfile_event;
-
- fail:
-  Py_XDECREF (objfile_event);
-  return NULL;
+  return objfile_event.release ();
 }
 
 /* Callback function which notifies observers when a new objfile event occurs.
@@ -57,14 +53,12 @@  create_new_objfile_event_object (struct objfile *objfile)
 int
 emit_new_objfile_event (struct objfile *objfile)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
     return 0;
 
-  event = create_new_objfile_event_object (objfile);
-  if (event)
-    return evpy_emit_event (event, gdb_py_events.new_objfile);
+  gdbpy_reference event (create_new_objfile_event_object (objfile));
+  if (event != NULL)
+    return evpy_emit_event (event.get (), gdb_py_events.new_objfile);
   return -1;
 }
 
@@ -79,26 +73,22 @@  GDBPY_NEW_EVENT_TYPE (new_objfile,
 static PyObject *
 create_clear_objfiles_event_object (void)
 {
-  PyObject *objfile_event;
   PyObject *py_progspace;
 
-  objfile_event = create_event_object (&clear_objfiles_event_object_type);
-  if (!objfile_event)
-    goto fail;
+  gdbpy_reference objfile_event
+    (create_event_object (&clear_objfiles_event_object_type));
+  if (objfile_event == NULL)
+    return NULL;
 
   /* Note that pspace_to_pspace_object returns a borrowed reference,
      so we don't need a decref here.  */
   py_progspace = pspace_to_pspace_object (current_program_space);
-  if (!py_progspace || evpy_add_attribute (objfile_event,
+  if (!py_progspace || evpy_add_attribute (objfile_event.get (),
 					   "progspace",
 					   py_progspace) < 0)
-    goto fail;
+    return NULL;
 
-  return objfile_event;
-
- fail:
-  Py_XDECREF (objfile_event);
-  return NULL;
+  return objfile_event.release ();
 }
 
 /* Callback function which notifies observers when the "clear objfiles"
@@ -109,14 +99,12 @@  create_clear_objfiles_event_object (void)
 int
 emit_clear_objfiles_event (void)
 {
-  PyObject *event;
-
   if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
     return 0;
 
-  event = create_clear_objfiles_event_object ();
-  if (event)
-    return evpy_emit_event (event, gdb_py_events.clear_objfiles);
+  gdbpy_reference event (create_clear_objfiles_event_object ());
+  if (event != NULL)
+    return evpy_emit_event (event.get (), gdb_py_events.clear_objfiles);
   return -1;
 }
 
diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c
index cd5f72c..d3d030a 100644
--- a/gdb/python/py-signalevent.c
+++ b/gdb/python/py-signalevent.c
@@ -27,30 +27,23 @@  PyObject *
 create_signal_event_object (enum gdb_signal stop_signal)
 {
   const char *signal_name;
-  PyObject *signal_name_obj = NULL;
-  PyObject *signal_event_obj =
-      create_stop_event_object (&signal_event_object_type);
+  gdbpy_reference signal_event_obj
+    (create_stop_event_object (&signal_event_object_type));
 
-  if (!signal_event_obj)
-    goto fail;
+  if (signal_event_obj == NULL)
+    return NULL;
 
   signal_name = gdb_signal_to_name (stop_signal);
 
-  signal_name_obj = PyString_FromString (signal_name);
+  gdbpy_reference signal_name_obj (PyString_FromString (signal_name));
   if (signal_name_obj == NULL)
-    goto fail;
-  if (evpy_add_attribute (signal_event_obj,
+    return NULL;
+  if (evpy_add_attribute (signal_event_obj.get (),
                           "stop_signal",
-                          signal_name_obj) < 0)
-    goto fail;
-  Py_DECREF (signal_name_obj);
+                          signal_name_obj.get ()) < 0)
+    return NULL;
 
-  return signal_event_obj;
-
- fail:
-  Py_XDECREF (signal_name_obj);
-  Py_XDECREF (signal_event_obj);
-  return NULL;
+  return signal_event_obj.release ();
 }
 
 GDBPY_NEW_EVENT_TYPE (signal,
diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c
index 47592c1..0516012 100644
--- a/gdb/python/py-stopevent.c
+++ b/gdb/python/py-stopevent.c
@@ -23,16 +23,7 @@ 
 PyObject *
 create_stop_event_object (PyTypeObject *py_type)
 {
-  PyObject *stop_event_obj = create_thread_event_object (py_type);
-
-  if (!stop_event_obj)
-    goto fail;
-
-  return stop_event_obj;
-
-  fail:
-   Py_XDECREF (stop_event_obj);
-   return NULL;
+  return create_thread_event_object (py_type);
 }
 
 /* Callback observers when a stop event occurs.  This function will create a
@@ -45,8 +36,8 @@  create_stop_event_object (PyTypeObject *py_type)
 int
 emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
 {
-  PyObject *stop_event_obj = NULL; /* Appease GCC warning.  */
-  PyObject *list = NULL;
+  gdbpy_reference stop_event_obj;
+  gdbpy_reference list;
   PyObject *first_bp = NULL;
   struct bpstats *current_bs;
 
@@ -64,13 +55,13 @@  emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
 
           if (list == NULL)
             {
-              list = PyList_New (0);
-              if (!list)
-                goto fail;
+              list.reset (PyList_New (0));
+              if (list == NULL)
+		return -1;
             }
 
-          if (PyList_Append (list, current_py_bp))
-            goto fail;
+          if (PyList_Append (list.get (), current_py_bp))
+	    return -1;
 
           if (first_bp == NULL)
             first_bp = current_py_bp;
@@ -79,36 +70,31 @@  emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
 
   if (list != NULL)
     {
-      stop_event_obj = create_breakpoint_event_object (list, first_bp);
-      if (!stop_event_obj)
-        goto fail;
-      Py_DECREF (list);
+      stop_event_obj.reset (create_breakpoint_event_object (list.get (),
+							    first_bp));
+      if (stop_event_obj == NULL)
+	return -1;
     }
 
   /* Check if the signal is "Signal 0" or "Trace/breakpoint trap".  */
   if (stop_signal != GDB_SIGNAL_0
       && stop_signal != GDB_SIGNAL_TRAP)
     {
-      stop_event_obj =
-	  create_signal_event_object (stop_signal);
-      if (!stop_event_obj)
-	goto fail;
+      stop_event_obj.reset (create_signal_event_object (stop_signal));
+      if (stop_event_obj == NULL)
+	return -1;
     }
 
   /* If all fails emit an unknown stop event.  All event types should
      be known and this should eventually be unused.  */
-  if (!stop_event_obj)
+  if (stop_event_obj == NULL)
     {
-      stop_event_obj = create_stop_event_object (&stop_event_object_type);
-      if (!stop_event_obj)
-        goto fail;
+      stop_event_obj.reset (create_stop_event_object (&stop_event_object_type));
+      if (stop_event_obj == NULL)
+	return -1;
     }
 
-  return evpy_emit_event (stop_event_obj, gdb_py_events.stop);
-
- fail:
-  Py_XDECREF (list);
-  return -1;
+  return evpy_emit_event (stop_event_obj.get (), gdb_py_events.stop);
 }
 
 GDBPY_NEW_EVENT_TYPE (stop,
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index 53291d3..e4c0afb 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -51,26 +51,21 @@  PyObject *
 create_thread_event_object (PyTypeObject *py_type)
 {
   PyObject *thread = NULL;
-  PyObject *thread_event_obj = NULL;
 
-  thread_event_obj = create_event_object (py_type);
-  if (!thread_event_obj)
-    goto fail;
+  gdbpy_reference thread_event_obj (create_event_object (py_type));
+  if (thread_event_obj == NULL)
+    return NULL;
 
   thread = get_event_thread ();
   if (!thread)
-    goto fail;
+    return NULL;
 
-  if (evpy_add_attribute (thread_event_obj,
+  if (evpy_add_attribute (thread_event_obj.get (),
                           "inferior_thread",
                           thread) < 0)
-    goto fail;
+    return NULL;
 
-  return thread_event_obj;
-
-  fail:
-   Py_XDECREF (thread_event_obj);
-   return NULL;
+  return thread_event_obj.release ();
 }
 
 GDBPY_NEW_EVENT_TYPE (thread,