From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7441 invoked by alias); 19 Apr 2013 14:32:46 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 7430 invoked by uid 89); 19 Apr 2013 14:32:46 -0000 X-Spam-SWARE-Status: No, score=-6.8 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Fri, 19 Apr 2013 14:32:45 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r3JEWiJC009879 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 19 Apr 2013 10:32:44 -0400 Received: from barimba (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r3JEWgEa026405 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Fri, 19 Apr 2013 10:32:43 -0400 From: Tom Tromey To: gdb-patches@sourceware.org Subject: [PATCH 11/28] use iterator protocol and avoid refcount bugs References: <87ehe638ww.fsf@fleche.redhat.com> Date: Fri, 19 Apr 2013 14:41:00 -0000 In-Reply-To: <87ehe638ww.fsf@fleche.redhat.com> (Tom Tromey's message of "Fri, 19 Apr 2013 08:13:51 -0600") Message-ID: <87ppxqzj3p.fsf@fleche.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-SW-Source: 2013-04/txt/msg00594.txt.bz2 The checker pointed out some refcounting bugs in cmdpy_completer. And, while looking at it, I noticed that it was not using the iterator protocol. I couldn't think of a reason why not; and using an iterator cleaned up the code, so this patch does that. * python/py-cmd.c (cmdpy_completer): Use iterator protocol. Correctly decref. --- gdb/python/py-cmd.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 6516e1f..26823c7 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -247,26 +247,40 @@ cmdpy_completer (struct cmd_list_element *command, make_cleanup_py_decref (resultobj); result = NULL; - if (PySequence_Check (resultobj)) + if (PyInt_Check (resultobj)) { - Py_ssize_t i, len = PySequence_Size (resultobj); - Py_ssize_t out; + /* User code may also return one of the completion constants, + thus requesting that sort of completion. */ + long value; + + if (! gdb_py_int_as_long (resultobj, &value)) + { + /* Ignore. */ + PyErr_Clear (); + } + else if (value >= 0 && value < (long) N_COMPLETERS) + result = completers[value].completer (command, text, word); + } + else + { + PyObject *iter = PyObject_GetIter (resultobj); + PyObject *elt; - if (len < 0) + if (iter == NULL) goto done; - for (i = out = 0; i < len; ++i) + while ((elt = PyIter_Next (iter)) != NULL) { - PyObject *elt = PySequence_GetItem (resultobj, i); char *item; - if (elt == NULL || ! gdbpy_is_string (elt)) + if (! gdbpy_is_string (elt)) { /* Skip problem elements. */ - PyErr_Clear (); + Py_DECREF (elt); continue; } item = python_string_to_host_string (elt); + Py_DECREF (elt); if (item == NULL) { /* Skip problem elements. */ @@ -275,20 +289,13 @@ cmdpy_completer (struct cmd_list_element *command, } VEC_safe_push (char_ptr, result, item); } - } - else if (PyInt_Check (resultobj)) - { - /* User code may also return one of the completion constants, - thus requesting that sort of completion. */ - long value; - if (! gdb_py_int_as_long (resultobj, &value)) - { - /* Ignore. */ - PyErr_Clear (); - } - else if (value >= 0 && value < (long) N_COMPLETERS) - result = completers[value].completer (command, text, word); + Py_DECREF (iter); + + /* If we got some results, ignore problems. Otherwise, report + the problem. */ + if (result != NULL && PyErr_Occurred ()) + PyErr_Clear (); } done: -- 1.8.1.4