From 88b6faea9953505e9e8a7a77995c7db5dfb6ac19 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 11 Jan 2017 16:28:43 -0700 Subject: Use gdbpy_ref to simplify some logic This uses the new gdbpy_ref template to simplify logic in various parts of the Python layer; for example removing repeated error code or removing gotos. gdb/ChangeLog 2017-02-10 Tom Tromey * 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. --- gdb/python/py-progspace.c | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'gdb/python/py-progspace.c') diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index b0d9458..1e06a75 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -24,6 +24,7 @@ #include "objfiles.h" #include "language.h" #include "arch-utils.h" +#include "py-ref.h" typedef struct { @@ -128,18 +129,15 @@ pspy_initialize (pspace_object *self) static PyObject * pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords) { - pspace_object *self = (pspace_object *) type->tp_alloc (type, 0); + gdbpy_ref 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 * @@ -323,7 +321,6 @@ pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) 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 @@ -336,8 +333,8 @@ py_free_pspace (struct program_space *pspace, void *datum) struct gdbarch *arch = target_gdbarch (); gdbpy_enter enter_py (arch, current_language); + gdbpy_ref object ((pspace_object *) datum); object->pspace = NULL; - Py_DECREF ((PyObject *) object); } /* Return a borrowed reference to the Python object of type Pspace @@ -348,26 +345,22 @@ py_free_pspace (struct program_space *pspace, void *datum) 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 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 -- cgit v1.1