[2/5] Remove more uses of explicit reference counting in Python

Message ID 20181227192637.17862-3-tom@tromey.com
State New
Headers show
Series
  • Reference counting improvements in Python
Related show

Commit Message

Tom Tromey Dec. 27, 2018, 7:26 p.m.
This changes some more place in the Python code to use gdbpy_ref
rather than explicit reference counting.  While doing this I found a
latent bug in typy_fields_items -- it was not checking for errors in
one spot.  I also changed valpy_dealloc to use Py_XDECREF rather than
an explicit "if".

gdb/ChangeLog
2018-12-27  Tom Tromey  <tom@tromey.com>

	* python/py-value.c (valpy_dealloc): Use Py_XDECREF.
	* python/py-type.c (typy_fields_items): Use gdbpy_ref.
	* python/py-progspace.c (pspy_set_printers): Use gdbpy_ref.
	(pspy_set_frame_filters, pspy_set_frame_unwinders)
	(pspy_set_type_printers): Likewise.
	* python/py-function.c (fnpy_init): Use gdbpy_ref.
	* python/py-cmd.c (cmdpy_init): Use gdbpy_ref.
	* python/py-objfile.c (objfpy_set_printers): Use gdbpy_ref.
	(objfpy_set_frame_filters, objfpy_set_frame_unwinders)
	(objfpy_set_type_printers): Likewise.
---
 gdb/ChangeLog             | 13 +++++++++++++
 gdb/python/py-cmd.c       |  5 ++---
 gdb/python/py-function.c  | 11 +++++------
 gdb/python/py-objfile.c   | 16 ++++------------
 gdb/python/py-progspace.c | 16 ++++------------
 gdb/python/py-type.c      | 22 +++++++++-------------
 gdb/python/py-value.c     | 13 ++-----------
 7 files changed, 39 insertions(+), 57 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 0e39730a4c..1f873c0c28 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -540,7 +540,7 @@  cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
   if (! docstring)
     docstring = xstrdup (_("This command is not documented."));
 
-  Py_INCREF (self);
+  gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
 
   TRY
     {
@@ -566,7 +566,7 @@  cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
       cmd->destroyer = cmdpy_destroyer;
 
       obj->command = cmd;
-      set_cmd_context (cmd, self);
+      set_cmd_context (cmd, self_ref.release ());
       set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
 			       : completers[completetype].completer));
       if (completetype == -1)
@@ -578,7 +578,6 @@  cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
       xfree (cmd_name);
       xfree (docstring);
       xfree (pfx_name);
-      Py_DECREF (self);
       PyErr_Format (except.reason == RETURN_QUIT
 		    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
 		    "%s", except.message);
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index cf5e68a054..7c33f17d22 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -109,7 +109,8 @@  fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
 
   if (! PyArg_ParseTuple (args, "s", &name))
     return -1;
-  Py_INCREF (self);
+
+  gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
 
   if (PyObject_HasAttrString (self, "__doc__"))
     {
@@ -120,17 +121,15 @@  fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
 	    {
 	      docstring = python_string_to_host_string (ds_obj.get ());
 	      if (docstring == NULL)
-		{
-		  Py_DECREF (self);
-		  return -1;
-		}
+		return -1;
 	    }
 	}
     }
   if (! docstring)
     docstring.reset (xstrdup (_("This function is not documented.")));
 
-  add_internal_function (name, docstring.release (), fnpy_call, self);
+  add_internal_function (name, docstring.release (), fnpy_call,
+			 self_ref.release ());
   return 0;
 }
 
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index d15d5eccae..fa4147b145 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -240,7 +240,6 @@  objfpy_get_printers (PyObject *o, void *ignore)
 static int
 objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
 {
-  PyObject *tmp;
   objfile_object *self = (objfile_object *) o;
 
   if (! value)
@@ -258,10 +257,9 @@  objfpy_set_printers (PyObject *o, PyObject *value, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->printers;
+  gdbpy_ref<> tmp (self->printers);
   Py_INCREF (value);
   self->printers = value;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -281,7 +279,6 @@  objfpy_get_frame_filters (PyObject *o, void *ignore)
 static int
 objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
 {
-  PyObject *tmp;
   objfile_object *self = (objfile_object *) o;
 
   if (! filters)
@@ -299,10 +296,9 @@  objfpy_set_frame_filters (PyObject *o, PyObject *filters, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->frame_filters;
+  gdbpy_ref<> tmp (self->frame_filters);
   Py_INCREF (filters);
   self->frame_filters = filters;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -323,7 +319,6 @@  objfpy_get_frame_unwinders (PyObject *o, void *ignore)
 static int
 objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
 {
-  PyObject *tmp;
   objfile_object *self = (objfile_object *) o;
 
   if (!unwinders)
@@ -341,10 +336,9 @@  objfpy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->frame_unwinders;
+  gdbpy_ref<> tmp (self->frame_unwinders);
   Py_INCREF (unwinders);
   self->frame_unwinders = unwinders;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -376,7 +370,6 @@  objfpy_get_xmethods (PyObject *o, void *ignore)
 static int
 objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
 {
-  PyObject *tmp;
   objfile_object *self = (objfile_object *) o;
 
   if (! value)
@@ -394,10 +387,9 @@  objfpy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->type_printers;
+  gdbpy_ref<> tmp (self->type_printers);
   Py_INCREF (value);
   self->type_printers = value;
-  Py_XDECREF (tmp);
 
   return 0;
 }
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index bd195a54c1..6600edef4e 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -164,7 +164,6 @@  pspy_get_printers (PyObject *o, void *ignore)
 static int
 pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
 {
-  PyObject *tmp;
   pspace_object *self = (pspace_object *) o;
 
   if (! value)
@@ -182,10 +181,9 @@  pspy_set_printers (PyObject *o, PyObject *value, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->printers;
+  gdbpy_ref<> tmp (self->printers);
   Py_INCREF (value);
   self->printers = value;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -205,7 +203,6 @@  pspy_get_frame_filters (PyObject *o, void *ignore)
 static int
 pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
 {
-  PyObject *tmp;
   pspace_object *self = (pspace_object *) o;
 
   if (! frame)
@@ -223,10 +220,9 @@  pspy_set_frame_filters (PyObject *o, PyObject *frame, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->frame_filters;
+  gdbpy_ref<> tmp (self->frame_filters);
   Py_INCREF (frame);
   self->frame_filters = frame;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -247,7 +243,6 @@  pspy_get_frame_unwinders (PyObject *o, void *ignore)
 static int
 pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
 {
-  PyObject *tmp;
   pspace_object *self = (pspace_object *) o;
 
   if (!unwinders)
@@ -265,10 +260,9 @@  pspy_set_frame_unwinders (PyObject *o, PyObject *unwinders, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->frame_unwinders;
+  gdbpy_ref<> tmp (self->frame_unwinders);
   Py_INCREF (unwinders);
   self->frame_unwinders = unwinders;
-  Py_XDECREF (tmp);
 
   return 0;
 }
@@ -300,7 +294,6 @@  pspy_get_xmethods (PyObject *o, void *ignore)
 static int
 pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
 {
-  PyObject *tmp;
   pspace_object *self = (pspace_object *) o;
 
   if (! value)
@@ -318,10 +311,9 @@  pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore)
     }
 
   /* Take care in case the LHS and RHS are related somehow.  */
-  tmp = self->type_printers;
+  gdbpy_ref<> tmp (self->type_printers);
   Py_INCREF (value);
   self->type_printers = value;
-  Py_XDECREF (tmp);
 
   return 0;
 }
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index afc7635235..a3584e20d3 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -319,7 +319,6 @@  static PyObject *
 typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
 {
   PyObject *py_type = self;
-  PyObject *result = NULL, *iter = NULL;
   struct type *type = ((type_object *) py_type)->type;
   struct type *checked_type = type;
 
@@ -333,22 +332,19 @@  typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
     }
   END_CATCH
 
+  gdbpy_ref<> type_holder;
   if (checked_type != type)
-    py_type = type_to_type_object (checked_type);
-  iter = typy_make_iter (py_type, kind);
-  if (checked_type != type)
-    {
-      /* Need to wrap this in braces because Py_DECREF isn't wrapped
-	 in a do{}while(0).  */
-      Py_DECREF (py_type);
-    }
-  if (iter != NULL)
     {
-      result = PySequence_List (iter);
-      Py_DECREF (iter);
+      type_holder.reset (type_to_type_object (checked_type));
+      if (type_holder == nullptr)
+	return nullptr;
+      py_type = type_holder.get ();
     }
+  gdbpy_ref<> iter (typy_make_iter (py_type, kind));
+  if (iter == nullptr)
+    return nullptr;
 
-  return result;
+  return PySequence_List (iter.get ());
 }
 
 /* Return a sequence of all fields.  Each field is a gdb.Field object.  */
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 66cfa59026..ae34b28576 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -90,17 +90,8 @@  valpy_dealloc (PyObject *obj)
 
   value_decref (self->value);
 
-  if (self->address)
-    /* Use braces to appease gcc warning.  *sigh*  */
-    {
-      Py_DECREF (self->address);
-    }
-
-  if (self->type)
-    {
-      Py_DECREF (self->type);
-    }
-
+  Py_XDECREF (self->address);
+  Py_XDECREF (self->type);
   Py_XDECREF (self->dynamic_type);
 
   Py_TYPE (self)->tp_free (self);