]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Remove more uses of explicit reference counting in Python
authorTom Tromey <tom@tromey.com>
Thu, 27 Dec 2018 18:53:20 +0000 (11:53 -0700)
committerTom Tromey <tom@tromey.com>
Thu, 3 Jan 2019 21:49:18 +0000 (14:49 -0700)
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
2019-01-03  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
gdb/python/py-cmd.c
gdb/python/py-function.c
gdb/python/py-objfile.c
gdb/python/py-progspace.c
gdb/python/py-type.c
gdb/python/py-value.c

index af27de2e4324b4c1c2542e8d823a1ffc9e694105..a01c1491ff1eef1a528e3a019bb3b81db9840ce1 100644 (file)
@@ -1,3 +1,16 @@
+2019-01-03  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.
+
 2019-01-03  Tom Tromey  <tom@tromey.com>
 
        * python/python.c (gdbpy_enter, ~gdbpy_enter): Update.
index 1087a9cd211efd5deb4225634b81ab6d511a3664..bc18cec436d2a7a0e5123ec981e707047958e718 100644 (file)
@@ -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);
       gdbpy_convert_exception (except);
       return -1;
     }
index b254ab7c0dfa0d91123455e8b968d8390a60973f..dbd5649bbb527696348a6b7691d6985ee54d85f5 100644 (file)
@@ -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;
 }
 
index c5212b59d677d417533b9705bb7a9a961f24e0aa..5f61b734afe07131e1cc57497640ab9473a75724 100644 (file)
@@ -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;
 }
index c55c432bb8801cb8e856a9b193fb3c8bed29648c..4d0297b23d78992884ae937010e2ad7cf61bfa79 100644 (file)
@@ -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;
 }
index 88770fcc9f392923f381a66e0cc9f2cf984dbcfd..601d29fee76804f19e30679d818437b1764a1d61 100644 (file)
@@ -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.  */
index 9cc56e6cd1d1494a83b91cdcb9831cf14d8d271f..ebd7ef1a47e8ed95902048a31cf8473ed18c8471 100644 (file)
@@ -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);