+2017-02-10 Tom Tromey <tom@tromey.com>
+
+ * python/py-cmd.c (cmdpy_destroyer): Use gdbpy_ref.
+ * python/py-breakpoint.c (gdbpy_breakpoint_deleted): Use
+ gdbpy_ref.
+ * python/py-type.c (field_new): Use gdbpy_ref.
+ * python/py-symtab.c (symtab_and_line_to_sal_object): Use
+ gdbpy_ref.
+ * python/py-progspace.c (pspy_new): Use gdbpy_ref.
+ (py_free_pspace): Likewise.
+ (pspace_to_pspace_object): Likewise.
+ * python/py-objfile.c (objfpy_new): Use gdbpy_ref.
+ (py_free_objfile): Likewise.
+ (objfile_to_objfile_object): Likewise.
+ * python/py-inferior.c (delete_thread_object): Use
+ gdbpy_ref.
+ (infpy_read_memory): Likewise.
+ (py_free_inferior): Likewise.
+ * python/py-evtregistry.c (create_eventregistry_object): Use
+ gdbpy_ref.
+ * python/py-event.c (create_event_object): Use gdbpy_ref.
+
2017-02-10 Tom Tromey <tom@tromey.com>
* python/py-ref.h (gdbpy_ref_policy): Now a template.
int num = b->number;
PyGILState_STATE state;
struct breakpoint *bp = NULL;
- gdbpy_breakpoint_object *bp_obj;
state = PyGILState_Ensure ();
bp = get_breakpoint (num);
if (bp)
{
- bp_obj = bp->py_bp_object;
- if (bp_obj)
+ gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
+ if (bp_obj != NULL)
{
if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
{
- if (evpy_emit_event ((PyObject *) bp_obj,
+ if (evpy_emit_event ((PyObject *) bp_obj.get (),
gdb_py_events.breakpoint_deleted) < 0)
gdbpy_print_stack ();
}
bp_obj->bp = NULL;
--bppy_live;
- Py_DECREF (bp_obj);
}
}
PyGILState_Release (state);
static void
cmdpy_destroyer (struct cmd_list_element *self, void *context)
{
- cmdpy_object *cmd;
-
gdbpy_enter enter_py (get_current_arch (), current_language);
/* Release our hold on the command object. */
- cmd = (cmdpy_object *) context;
+ gdbpy_ref<cmdpy_object> cmd ((cmdpy_object *) context);
cmd->command = NULL;
- Py_DECREF (cmd);
/* We allocated the name, doc string, and perhaps the prefix
name. */
PyObject *
create_event_object (PyTypeObject *py_type)
{
- event_object *event_obj;
-
- event_obj = PyObject_New (event_object, py_type);
- if (!event_obj)
- goto fail;
+ gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
+ if (event_obj == NULL)
+ return NULL;
event_obj->dict = PyDict_New ();
if (!event_obj->dict)
- goto fail;
-
- return (PyObject*) event_obj;
+ return NULL;
- fail:
- Py_XDECREF (event_obj);
- return NULL;
+ return (PyObject*) event_obj.release ();
}
/* Add the attribute ATTR to the event object EVENT. In
#include "defs.h"
#include "command.h"
#include "py-events.h"
+#include "py-ref.h"
events_object gdb_py_events;
eventregistry_object *
create_eventregistry_object (void)
{
- eventregistry_object *eventregistry_obj;
+ gdbpy_ref<eventregistry_object>
+ eventregistry_obj (PyObject_New (eventregistry_object,
+ &eventregistry_object_type));
- eventregistry_obj = PyObject_New (eventregistry_object,
- &eventregistry_object_type);
-
- if (!eventregistry_obj)
+ if (eventregistry_obj == NULL)
return NULL;
eventregistry_obj->callbacks = PyList_New (0);
if (!eventregistry_obj->callbacks)
- {
- Py_DECREF (eventregistry_obj);
- return NULL;
- }
+ return NULL;
- return eventregistry_obj;
+ return eventregistry_obj.release ();
}
static void
#include "symfile.h"
#include "objfiles.h"
#include "user-regs.h"
+#include "py-ref.h"
typedef struct {
PyObject_HEAD
PyObject *
frame_info_to_frame_object (struct frame_info *frame)
{
- frame_object *frame_obj;
-
- frame_obj = PyObject_New (frame_object, &frame_object_type);
+ gdbpy_ref<frame_object> frame_obj (PyObject_New (frame_object,
+ &frame_object_type));
if (frame_obj == NULL)
return NULL;
}
CATCH (except, RETURN_MASK_ALL)
{
- Py_DECREF (frame_obj);
gdbpy_convert_exception (except);
return NULL;
}
END_CATCH
- return (PyObject *) frame_obj;
+ return (PyObject *) frame_obj.release ();
}
/* Implementation of gdb.Frame.older (self) -> gdb.Frame.
static void
delete_thread_object (struct thread_info *tp, int ignore)
{
- inferior_object *inf_obj;
struct threadlist_entry **entry, *tmp;
if (!gdb_python_initialized)
gdbpy_enter enter_py (python_gdbarch, python_language);
- inf_obj
- = (inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid));
- if (!inf_obj)
+ gdbpy_ref<inferior_object> inf_obj
+ ((inferior_object *) find_inferior_object (ptid_get_pid (tp->ptid)));
+ if (inf_obj == NULL)
return;
/* Find thread entry in its inferior's thread_list. */
break;
if (!*entry)
- {
- Py_DECREF (inf_obj);
- return;
- }
+ return;
tmp = *entry;
tmp->thread_obj->thread = NULL;
inf_obj->nthreads--;
Py_DECREF (tmp->thread_obj);
- Py_DECREF (inf_obj);
xfree (tmp);
}
{
CORE_ADDR addr, length;
gdb_byte *buffer = NULL;
- membuf_object *membuf_obj;
PyObject *addr_obj, *length_obj, *result;
static char *keywords[] = { "address", "length", NULL };
}
END_CATCH
- membuf_obj = PyObject_New (membuf_object, &membuf_object_type);
+ gdbpy_ref<membuf_object> membuf_obj (PyObject_New (membuf_object,
+ &membuf_object_type));
if (membuf_obj == NULL)
{
xfree (buffer);
membuf_obj->length = length;
#ifdef IS_PY3K
- result = PyMemoryView_FromObject ((PyObject *) membuf_obj);
+ result = PyMemoryView_FromObject ((PyObject *) membuf_obj.get ());
#else
- result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
+ result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj.get (), 0,
Py_END_OF_BUFFER);
#endif
- Py_DECREF (membuf_obj);
return result;
}
static void
py_free_inferior (struct inferior *inf, void *datum)
{
- inferior_object *inf_obj = (inferior_object *) datum;
+ gdbpy_ref<inferior_object> inf_obj ((inferior_object *) datum);
struct threadlist_entry *th_entry, *th_tmp;
if (!gdb_python_initialized)
}
inf_obj->nthreads = 0;
-
- Py_DECREF ((PyObject *) inf_obj);
}
/* Implementation of gdb.selected_inferior() -> gdb.Inferior.
#include "language.h"
#include "build-id.h"
#include "symtab.h"
+#include "py-ref.h"
typedef struct
{
static PyObject *
objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
{
- objfile_object *self = (objfile_object *) type->tp_alloc (type, 0);
+ gdbpy_ref<objfile_object> self ((objfile_object *) type->tp_alloc (type, 0));
- if (self)
+ if (self != NULL)
{
- if (!objfpy_initialize (self))
- {
- Py_DECREF (self);
- return NULL;
- }
+ if (!objfpy_initialize (self.get ()))
+ return NULL;
}
- return (PyObject *) self;
+ return (PyObject *) self.release ();
}
PyObject *
static void
py_free_objfile (struct objfile *objfile, void *datum)
{
- objfile_object *object = (objfile_object *) datum;
-
gdbpy_enter enter_py (get_objfile_arch (objfile), current_language);
+ gdbpy_ref<objfile_object> object ((objfile_object *) datum);
object->objfile = NULL;
- Py_DECREF ((PyObject *) object);
}
/* Return a borrowed reference to the Python object of type Objfile
PyObject *
objfile_to_objfile_object (struct objfile *objfile)
{
- objfile_object *object;
-
- object = (objfile_object *) objfile_data (objfile, objfpy_objfile_data_key);
- if (!object)
+ gdbpy_ref<objfile_object> object
+ ((objfile_object *) objfile_data (objfile, objfpy_objfile_data_key));
+ if (object == NULL)
{
- object = PyObject_New (objfile_object, &objfile_object_type);
- if (object)
+ object.reset (PyObject_New (objfile_object, &objfile_object_type));
+ if (object != NULL)
{
- if (!objfpy_initialize (object))
- {
- Py_DECREF (object);
- return NULL;
- }
+ if (!objfpy_initialize (object.get ()))
+ return NULL;
object->objfile = objfile;
- set_objfile_data (objfile, objfpy_objfile_data_key, object);
+ set_objfile_data (objfile, objfpy_objfile_data_key, object.get ());
}
}
- return (PyObject *) object;
+ return (PyObject *) object.release ();
}
int
#include "objfiles.h"
#include "language.h"
#include "arch-utils.h"
+#include "py-ref.h"
typedef struct
{
static PyObject *
pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
{
- pspace_object *self = (pspace_object *) type->tp_alloc (type, 0);
+ gdbpy_ref<pspace_object> self ((pspace_object *) type->tp_alloc (type, 0));
- if (self)
+ if (self != NULL)
{
- if (!pspy_initialize (self))
- {
- Py_DECREF (self);
- return NULL;
- }
+ if (!pspy_initialize (self.get ()))
+ return NULL;
}
- return (PyObject *) self;
+ return (PyObject *) self.release ();
}
PyObject *
static void
py_free_pspace (struct program_space *pspace, void *datum)
{
- pspace_object *object = (pspace_object *) datum;
/* This is a fiction, but we're in a nasty spot: The pspace is in the
process of being deleted, we can't rely on anything in it. Plus
this is one time when the current program space and current inferior
struct gdbarch *arch = target_gdbarch ();
gdbpy_enter enter_py (arch, current_language);
+ gdbpy_ref<pspace_object> object ((pspace_object *) datum);
object->pspace = NULL;
- Py_DECREF ((PyObject *) object);
}
/* Return a borrowed reference to the Python object of type Pspace
PyObject *
pspace_to_pspace_object (struct program_space *pspace)
{
- pspace_object *object;
-
- object = (pspace_object *) program_space_data (pspace, pspy_pspace_data_key);
- if (!object)
+ gdbpy_ref<pspace_object> object
+ ((pspace_object *) program_space_data (pspace, pspy_pspace_data_key));
+ if (object == NULL)
{
- object = PyObject_New (pspace_object, &pspace_object_type);
- if (object)
+ object.reset (PyObject_New (pspace_object, &pspace_object_type));
+ if (object != NULL)
{
- if (!pspy_initialize (object))
- {
- Py_DECREF (object);
- return NULL;
- }
+ if (!pspy_initialize (object.get ()))
+ return NULL;
object->pspace = pspace;
- set_program_space_data (pspace, pspy_pspace_data_key, object);
+ set_program_space_data (pspace, pspy_pspace_data_key, object.get ());
}
}
- return (PyObject *) object;
+ return (PyObject *) object.release ();
}
int
#include "python-internal.h"
#include "objfiles.h"
#include "block.h"
+#include "py-ref.h"
typedef struct stpy_symtab_object {
PyObject_HEAD
PyObject *
symtab_and_line_to_sal_object (struct symtab_and_line sal)
{
- sal_object *sal_obj;
-
- sal_obj = PyObject_New (sal_object, &sal_object_type);
- if (sal_obj)
+ gdbpy_ref<sal_object> sal_obj (PyObject_New (sal_object, &sal_object_type));
+ if (sal_obj != NULL)
{
- if (set_sal (sal_obj, sal) < 0)
- {
- Py_DECREF (sal_obj);
- return NULL;
- }
+ if (set_sal (sal_obj.get (), sal) < 0)
+ return NULL;
}
- return (PyObject *) sal_obj;
+ return (PyObject *) sal_obj.release ();
}
/* Return struct symtab_and_line reference that is wrapped by this
static PyObject *
field_new (void)
{
- field_object *result = PyObject_New (field_object, &field_object_type);
+ gdbpy_ref<field_object> result (PyObject_New (field_object,
+ &field_object_type));
- if (result)
+ if (result != NULL)
{
result->dict = PyDict_New ();
if (!result->dict)
- {
- Py_DECREF (result);
- result = NULL;
- }
+ return NULL;
}
- return (PyObject *) result;
+ return (PyObject *) result.release ();
}
\f