[RFA 2/3] Small event ownership clean up in Python layer

Tom Tromey
2017-09-09T00:34:04Z

It seems cleaner to me for functions like create_thread_event_object,
which pass object ownership to their callers, to directly return a
gdb_ref<>. This way the ownership transfer is part of the API. This
patch makes this change.

2017-09-08 Tom Tromey <tom@tromey.com>

* python/py-threadevent.c (create_thread_event_object): Return
gdbpy_ref.
* python/py-stopevent.h (create_stop_event_object)
(create_breakpoint_event_object, create_signal_event_object):
Update.
* python/py-stopevent.c (create_stop_event_object): Return
gdbpy_ref.
(emit_stop_event): Update.
* python/py-signalevent.c (create_signal_event_object): Return
gdbpy_ref.
* python/py-infevents.c (create_inferior_call_event_object):
Update.
* python/py-event.h (create_event_object)
(create_thread_event_object): Update.
* python/py-event.c (create_event_object): Return gdbpy_ref.
* python/py-continueevent.c: Return gdbpy_ref.
* python/py-bpevent.c (create_breakpoint_event_object): Return
gdbpy_ref.
---
gdb/ChangeLog | 21 +++++++++++++++++++++
gdb/python/py-bpevent.c | 4 ++--
gdb/python/py-continueevent.c | 2 +-
gdb/python/py-event.c | 4 ++--
gdb/python/py-event.h | 6 +++---
gdb/python/py-infevents.c | 4 ++--
gdb/python/py-signalevent.c | 4 ++--
gdb/python/py-stopevent.c | 10 +++++-----
gdb/python/py-stopevent.h | 8 ++++----
gdb/python/py-threadevent.c | 4 ++--
10 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 88520ea..f49b428 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,26 @@
2017-09-08 Tom Tromey <tom@tromey.com>

+ * python/py-threadevent.c (create_thread_event_object): Return
+ gdbpy_ref.
+ * python/py-stopevent.h (create_stop_event_object)
+ (create_breakpoint_event_object, create_signal_event_object):
+ Update.
+ * python/py-stopevent.c (create_stop_event_object): Return
+ gdbpy_ref.
+ (emit_stop_event): Update.
+ * python/py-signalevent.c (create_signal_event_object): Return
+ gdbpy_ref.
+ * python/py-infevents.c (create_inferior_call_event_object):
+ Update.
+ * python/py-event.h (create_event_object)
+ (create_thread_event_object): Update.
+ * python/py-event.c (create_event_object): Return gdbpy_ref.
+ * python/py-continueevent.c: Return gdbpy_ref.
+ * python/py-bpevent.c (create_breakpoint_event_object): Return
+ gdbpy_ref.
+
+2017-09-08 Tom Tromey <tom@tromey.com>
+
PR python/15622:
* NEWS: Add entry.
* python/python.c (do_start_initialization): Initialize new event
diff --git a/gdb/python/py-bpevent.c b/gdb/python/py-bpevent.c
index c5a85b6..021c83a 100644
--- a/gdb/python/py-bpevent.c
+++ b/gdb/python/py-bpevent.c
@@ -27,7 +27,7 @@ extern PyTypeObject breakpoint_event_object_type
/* Create and initialize a BreakpointEvent object. This acquires new
references to BREAKPOINT_LIST and FIRST_BP. */

-PyObject *
+gdbpy_ref<>
create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
{
gdbpy_ref<> breakpoint_event_obj
@@ -45,7 +45,7 @@ create_breakpoint_event_object (PyObject *breakpoint_list, PyObject *first_bp)
breakpoint_list) < 0)
return NULL;

- return breakpoint_event_obj.release ();
+ return breakpoint_event_obj;
}

GDBPY_NEW_EVENT_TYPE (breakpoint,
diff --git a/gdb/python/py-continueevent.c b/gdb/python/py-continueevent.c
index 8be28c2..ca2927f 100644
--- a/gdb/python/py-continueevent.c
+++ b/gdb/python/py-continueevent.c
@@ -24,7 +24,7 @@
extern PyTypeObject continue_event_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");

-static PyObject *
+static gdbpy_ref<>
create_continue_event_object (void)
{
return create_thread_event_object (&continue_event_object_type);
diff --git a/gdb/python/py-event.c b/gdb/python/py-event.c
index b349891..9d84e33 100644
--- a/gdb/python/py-event.c
+++ b/gdb/python/py-event.c
@@ -27,7 +27,7 @@ evpy_dealloc (PyObject *self)
Py_TYPE (self)->tp_free (self);
}

-PyObject *
+gdbpy_ref<>
create_event_object (PyTypeObject *py_type)
{
gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
@@ -38,7 +38,7 @@ create_event_object (PyTypeObject *py_type)
if (!event_obj->dict)
return NULL;

- return (PyObject*) event_obj.release ();
+ return gdbpy_ref<> ((PyObject *) event_obj.release ());
}

/* Add the attribute ATTR to the event object EVENT. In
diff --git a/gdb/python/py-event.h b/gdb/python/py-event.h
index 2f02c5f..27db828 100644
--- a/gdb/python/py-event.h
+++ b/gdb/python/py-event.h
@@ -124,9 +124,9 @@ extern int emit_memory_changed_event (CORE_ADDR addr, ssize_t len);
extern int evpy_emit_event (PyObject *event,
eventregistry_object *registry);

-extern PyObject *create_event_object (PyTypeObject *py_type);
-extern PyObject *create_thread_event_object (PyTypeObject *py_type,
- PyObject *thread = nullptr);
+extern gdbpy_ref<> create_event_object (PyTypeObject *py_type);
+extern gdbpy_ref<> create_thread_event_object (PyTypeObject *py_type,
+ PyObject *thread = nullptr);
extern int emit_new_objfile_event (struct objfile *objfile);
extern int emit_clear_objfiles_event (void);

diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index 6e3c8c8..8a63b79 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -43,10 +43,10 @@ create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid,
switch (flag)
{
case INFERIOR_CALL_PRE:
- event.reset (create_event_object (&inferior_call_pre_event_object_type));
+ event = create_event_object (&inferior_call_pre_event_object_type);
break;
case INFERIOR_CALL_POST:
- event.reset (create_event_object (&inferior_call_post_event_object_type));
+ event = create_event_object (&inferior_call_post_event_object_type);
break;
default:
gdb_assert_not_reached ("invalid inferior_call_kind");
diff --git a/gdb/python/py-signalevent.c b/gdb/python/py-signalevent.c
index a8f41c2..c2391de 100644
--- a/gdb/python/py-signalevent.c
+++ b/gdb/python/py-signalevent.c
@@ -23,7 +23,7 @@
extern PyTypeObject signal_event_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");

-PyObject *
+gdbpy_ref<>
create_signal_event_object (enum gdb_signal stop_signal)
{
const char *signal_name;
@@ -43,7 +43,7 @@ create_signal_event_object (enum gdb_signal stop_signal)
signal_name_obj.get ()) < 0)
return NULL;

- return signal_event_obj.release ();
+ return signal_event_obj;
}

GDBPY_NEW_EVENT_TYPE (signal,
diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c
index cfa4591..a0be5f2 100644
--- a/gdb/python/py-stopevent.c
+++ b/gdb/python/py-stopevent.c
@@ -20,7 +20,7 @@
#include "defs.h"
#include "py-stopevent.h"

-PyObject *
+gdbpy_ref<>
create_stop_event_object (PyTypeObject *py_type)
{
return create_thread_event_object (py_type);
@@ -70,8 +70,8 @@ emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)

if (list != NULL)
{
- stop_event_obj.reset (create_breakpoint_event_object (list.get (),
- first_bp));
+ stop_event_obj = create_breakpoint_event_object (list.get (),
+ first_bp);
if (stop_event_obj == NULL)
return -1;
}
@@ -80,7 +80,7 @@ emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
if (stop_signal != GDB_SIGNAL_0
&& stop_signal != GDB_SIGNAL_TRAP)
{
- stop_event_obj.reset (create_signal_event_object (stop_signal));
+ stop_event_obj = create_signal_event_object (stop_signal);
if (stop_event_obj == NULL)
return -1;
}
@@ -89,7 +89,7 @@ emit_stop_event (struct bpstats *bs, enum gdb_signal stop_signal)
be known and this should eventually be unused. */
if (stop_event_obj == NULL)
{
- stop_event_obj.reset (create_stop_event_object (&stop_event_object_type));
+ stop_event_obj = create_stop_event_object (&stop_event_object_type);
if (stop_event_obj == NULL)
return -1;
}
diff --git a/gdb/python/py-stopevent.h b/gdb/python/py-stopevent.h
index a56ca7e..62c52d7 100644
--- a/gdb/python/py-stopevent.h
+++ b/gdb/python/py-stopevent.h
@@ -22,15 +22,15 @@

#include "py-event.h"

-extern PyObject *create_stop_event_object (PyTypeObject *py_type);
+extern gdbpy_ref<> create_stop_event_object (PyTypeObject *py_type);
extern void stop_evpy_dealloc (PyObject *self);

extern int emit_stop_event (struct bpstats *bs,
enum gdb_signal stop_signal);

-extern PyObject *create_breakpoint_event_object (PyObject *breakpoint_list,
- PyObject *first_bp);
+extern gdbpy_ref<> create_breakpoint_event_object (PyObject *breakpoint_list,
+ PyObject *first_bp);

-extern PyObject *create_signal_event_object (enum gdb_signal stop_signal);
+extern gdbpy_ref<> create_signal_event_object (enum gdb_signal stop_signal);

#endif /* GDB_PY_STOPEVENT_H */
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index 7211fa2..8b742ca 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -47,7 +47,7 @@ get_event_thread (void)
return thread;
}

-PyObject *
+gdbpy_ref<>
create_thread_event_object (PyTypeObject *py_type, PyObject *thread)
{
gdbpy_ref<> thread_event_obj (create_event_object (py_type));
@@ -66,7 +66,7 @@ create_thread_event_object (PyTypeObject *py_type, PyObject *thread)
thread) < 0)
return NULL;

- return thread_event_obj.release ();
+ return thread_event_obj;
}

GDBPY_NEW_EVENT_TYPE (thread,
--
2.9.4