public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@fit.cvut.cz>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@fit.cvut.cz>
Subject: [RFC 5/8] mi/python: Polish MI output of python commands
Date: Thu, 18 Apr 2019 16:12:00 -0000	[thread overview]
Message-ID: <20190418152337.6376-6-jan.vrany@fit.cvut.cz> (raw)
In-Reply-To: <20190418152337.6376-1-jan.vrany@fit.cvut.cz>

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  <jan.vrany@fit.cvut.cz>
+
+	* 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  <jan.vrany@fit.cvut.cz>
 
 	* 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<char> 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<char> 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

  parent reply	other threads:[~2019-04-18 16:12 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-18 15:23 [RFC 0/8] Create MI commands using python Jan Vrany
2019-04-18 15:23 ` [RFC 1/8] Use std::map for MI commands in mi-cmds.c Jan Vrany
2019-04-25 19:13   ` Tom Tromey
2019-04-25 19:15   ` Tom Tromey
2019-04-25 19:34     ` Jan Vrany
2019-05-03 22:40   ` Simon Marchi
2019-05-03 22:45     ` Simon Marchi
2019-04-18 15:24 ` [RFC 8/8] mi/python: Allow redefinition of python MI commands Jan Vrany
2019-04-25 19:50   ` Tom Tromey
2019-05-03 15:26     ` Jan Vrany
2019-05-06 21:40       ` Tom Tromey
2019-05-07 11:26         ` Jan Vrany
2019-05-07 13:09           ` Simon Marchi
2019-05-07 13:19             ` Jan Vrany
2019-05-08  0:10               ` Simon Marchi
2019-05-08 18:00                 ` Tom Tromey
2019-04-18 15:24 ` [RFC 3/8] Create MI commands using python Jan Vrany
2019-04-25 19:42   ` Tom Tromey
2019-04-18 15:24 ` [RFC 6/8] mi/python: Handle python exception when executiong python-defined MI commands Jan Vrany
2019-04-25 19:46   ` Tom Tromey
2019-04-26 10:19     ` Jan Vrany
2019-04-18 15:24 ` [RFC 4/8] mi/python: C++ify python MI command handling code Jan Vrany
2019-04-25 19:43   ` Tom Tromey
2019-04-18 15:24 ` [RFC 7/8] mi/python: Add tests for python-defined MI commands Jan Vrany
2019-04-25 19:48   ` Tom Tromey
2019-04-18 15:24 ` [RFC 2/8] Use classes to represent MI Command instead of structures Jan Vrany
2019-04-25 19:25   ` Tom Tromey
2019-05-03 22:49     ` Simon Marchi
2019-05-03 22:57       ` Simon Marchi
2019-04-18 16:03 ` [RFC 0/8] Create MI commands using python Simon Marchi
2019-04-20  7:20   ` Jan Vrany
2019-04-18 16:12 ` Jan Vrany [this message]
2019-04-25 19:50   ` [RFC 5/8] mi/python: Polish MI output of python commands Tom Tromey
2019-04-25 18:03 ` [RFC 0/8] Create MI commands using python Tom Tromey
2019-04-25 19:00   ` Simon Marchi
2019-04-25 19:01     ` Simon Marchi
2019-05-14 11:24 ` [PATCH v2 5/5] mi/python: Add tests for python-defined MI commands Jan Vrany
2019-05-14 11:24 ` [PATCH v2 2/5] Use classes to represent MI Command instead of structures Jan Vrany
2019-05-17  3:12   ` Simon Marchi
2019-05-14 11:24 ` [PATCH v2 3/5] Create MI commands using python Jan Vrany
2019-05-17  4:29   ` Simon Marchi
2019-05-28 20:35     ` Jan Vrany
2019-05-14 11:24 ` [PATCH v2 1/5] Use std::map for MI commands in mi-cmds.c Jan Vrany
2019-05-14 11:24 ` [PATCH v2 0/5] Create MI commands using python Jan Vrany
2019-05-14 11:57 ` [PATCH v2 4/5] mi/python: Allow redefinition of python MI commands Jan Vrany

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190418152337.6376-6-jan.vrany@fit.cvut.cz \
    --to=jan.vrany@fit.cvut.cz \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).