From patchwork Thu Dec 27 19:26:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 30867 Received: (qmail 16113 invoked by alias); 27 Dec 2018 19:26:50 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 15958 invoked by uid 89); 27 Dec 2018 19:26:49 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1819, spot X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.231) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 27 Dec 2018 19:26:45 +0000 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway34.websitewelcome.com (Postfix) with ESMTP id 184DD620C8F for ; Thu, 27 Dec 2018 13:26:44 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id cbIqguXQdiQercbIqgyTj9; Thu, 27 Dec 2018 13:26:44 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=3I5iGHnWewp1hyv+UKMeOeM7q4kLy9iyAUP1fUAZn78=; b=mhM5MJj+APFX42QoeXKZJjkhL2 MogFaA1kph9k+NYlobjBVEJNUqmPZhNK0Y8NKtOKT7PlQmfVlpesJckFmR+n+H/II2lq2cG0BZZVk MBKHpLThlEisQHCJAc/1akZxB; Received: from 75-166-72-210.hlrn.qwest.net ([75.166.72.210]:47364 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1gcbIp-0004pC-Iv; Thu, 27 Dec 2018 13:26:43 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 2/5] Remove more uses of explicit reference counting in Python Date: Thu, 27 Dec 2018 12:26:34 -0700 Message-Id: <20181227192637.17862-3-tom@tromey.com> In-Reply-To: <20181227192637.17862-1-tom@tromey.com> References: <20181227192637.17862-1-tom@tromey.com> 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 * 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(-) 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);