From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21870 invoked by alias); 18 Apr 2019 16:12:41 -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 21861 invoked by uid 89); 18 Apr 2019 16:12:41 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.1 spammy=channel X-HELO: relay.fit.cvut.cz Received: from relay.fit.cvut.cz (HELO relay.fit.cvut.cz) (147.32.232.237) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 18 Apr 2019 16:12:39 +0000 Received: from imap.fit.cvut.cz (imap.fit.cvut.cz [147.32.232.238]) by relay.fit.cvut.cz (8.15.2/8.15.2) with ESMTPS id x3IFNuum093706 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Apr 2019 17:23:58 +0200 (CEST) (envelope-from jan.vrany@fit.cvut.cz) Received: from localhost (02d97c6d.bb.sky.com [2.217.124.109] (may be forged)) (authenticated bits=0 as user vranyj1) by imap.fit.cvut.cz (8.15.2/8.15.2) with ESMTPSA id x3IFNttD060313 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Thu, 18 Apr 2019 17:23:56 +0200 (CEST) (envelope-from jan.vrany@fit.cvut.cz) From: Jan Vrany To: gdb-patches@sourceware.org Cc: Jan Vrany Subject: [RFC 5/8] mi/python: Polish MI output of python commands Date: Thu, 18 Apr 2019 16:12:00 -0000 Message-Id: <20190418152337.6376-6-jan.vrany@fit.cvut.cz> In-Reply-To: <20190418152337.6376-1-jan.vrany@fit.cvut.cz> References: <20190418152337.6376-1-jan.vrany@fit.cvut.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SW-Source: 2019-04/txt/msg00342.txt.bz2 This commits makes MI representation of python-implemented MI command more "natural". Add support for tuples, iterators and generic sequences. gdb/Changelog: * python/py-micmd.c (parse_mi_result): Polish to make the output more "natural". Add support for tuples, iterators and general sequences. (py_mi_invoke): Do not call Py_DECREF on argument strings since PyList_SetItem borrows the reference. If python MI command returns None, do not print the result on MI output channel and just respond with ^done. --- gdb/ChangeLog | 7 ++++++ gdb/python/py-micmd.c | 54 +++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 692b937fee..8bbaaba5ad 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2018-12-10 Jan Vrany + + * python/py-micmd.c (parse_mi_result): Polish to make the output + more "natural". Add support for tuples, iterators and general sequences. + (py_mi_invoke): If python MI command returns None, do not print the + result on MI output channel and just respond with "^done". + 2018-12-11 Jan Vrany * python/py-micmd.c (parse_mi_result): Use gdb::unique_xmalloc_ptr<> diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c index 0683a02017..bbd746fa32 100644 --- a/gdb/python/py-micmd.c +++ b/gdb/python/py-micmd.c @@ -37,37 +37,47 @@ parse_mi_result (PyObject *result, const char *field_name) { struct ui_out *uiout = current_uiout; - if(!field_name) - field_name = "default"; - - if (PyString_Check(result)) - { - const char *string = gdbpy_obj_to_string (result).release (); - uiout->field_string (field_name, string); - xfree ( (void *)string); - } - else if (PyList_Check (result)) + if (PyString_Check (result)) { - PyObject *item; - Py_ssize_t i = 0; - ui_out_emit_list list_emitter (uiout, field_name); - for(i = 0; i < PyList_GET_SIZE (result); ++i) - { - ui_out_emit_tuple tuple_emitter (uiout, NULL); - item = PyList_GetItem (result, i); - parse_mi_result (item, NULL); - } + goto generic; } - else if ( PyDict_Check (result)) + else if (PyDict_Check (result)) { PyObject *key, *value; Py_ssize_t pos = 0; + ui_out_emit_tuple tuple_emitter (uiout, field_name); while ( PyDict_Next (result, &pos, &key, &value) ) { gdb::unique_xmalloc_ptr key_string (gdbpy_obj_to_string (key)); - parse_mi_result (value, key_string.get ()); + parse_mi_result (value, key_string.get ()); } } + else if (PySequence_Check (result)) + { + PyObject *item; + Py_ssize_t i = 0; + ui_out_emit_list list_emitter (uiout, field_name); + for(i = 0; i < PySequence_Size (result); ++i) + { + item = PySequence_GetItem (result, i); + parse_mi_result (item, NULL); + } + } + else if (PyIter_Check (result)) + { + gdbpy_ref<> item; + ui_out_emit_list list_emitter (uiout, field_name); + while (item.reset(PyIter_Next (result)) , item != nullptr) + { + parse_mi_result (item.get (), NULL); + } + } + else + { + generic: + gdb::unique_xmalloc_ptr string (gdbpy_obj_to_string (result)); + uiout->field_string (field_name, string.get ()); + } } /* Called from mi_command_py's invoke to invoke the command. */ @@ -111,7 +121,7 @@ py_mi_invoke (void *py_obj, char **argv, int argc) if (result != nullptr) { - parse_mi_result (result.get (), NULL); + if (Py_None != result) parse_mi_result (result.get (), "result"); } } -- 2.20.1