From: Tom Tromey Date: Thu, 27 Dec 2018 18:53:20 +0000 (-0700) Subject: Remove more uses of explicit reference counting in Python X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=2a3c71d68d452bb6b06d199d0eb7bc0cbc2b9b25;p=binutils-gdb.git Remove more uses of explicit reference counting in Python 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 * 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. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index af27de2e432..a01c1491ff1 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,16 @@ +2019-01-03 Tom Tromey + + * 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 * python/python.c (gdbpy_enter, ~gdbpy_enter): Update. diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 1087a9cd211..bc18cec436d 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); gdbpy_convert_exception (except); return -1; } diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c index b254ab7c0df..dbd5649bbb5 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 c5212b59d67..5f61b734afe 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 c55c432bb88..4d0297b23d7 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 88770fcc9f3..601d29fee76 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 9cc56e6cd1d..ebd7ef1a47e 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);