qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Luiz Capitulino <lcapitulino@redhat.com>
To: "Daniel P. Berrange" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] Hack qmp.py to support reading a JSON multi-line response
Date: Mon, 20 Aug 2012 15:03:11 -0300	[thread overview]
Message-ID: <20120820150311.5363c46e@doriath.home> (raw)
In-Reply-To: <1345471297-21459-1-git-send-email-berrange@redhat.com>

On Mon, 20 Aug 2012 15:01:37 +0100
"Daniel P. Berrange" <berrange@redhat.com> wrote:

> From: "Daniel P. Berrange" <berrange@redhat.com>
> 
> The qmp-shell code assumes the JSON response is only on a single
> line. If the QEMU monitor is configured in "pretty print" mode
> the JSON response can be multi-line. The basic Python JSON APIs
> do not appear to support a streaming mode, so the simple hack
> here is to try parsing a line, and if it fails, then read another
> line, append it, and try parsing again. Keep reading lines until
> we can successfully parse
> 
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>  QMP/qmp.py | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/QMP/qmp.py b/QMP/qmp.py
> index 36ecc1d..464a01a 100644
> --- a/QMP/qmp.py
> +++ b/QMP/qmp.py
> @@ -61,10 +61,18 @@ class QEMUMonitorProtocol:
>  
>      def __json_read(self, only_event=False):
>          while True:
> -            data = self.__sockfile.readline()
> -            if not data:
> -                return
> -            resp = json.loads(data)
> +            data = ""
> +            while True:
> +                moredata = self.__sockfile.readline()
> +                if not moredata:
> +                    return
> +                data = data + moredata
> +                try:
> +                    resp = json.loads(data)
> +                    break
> +                except ValueError:
> +                    pass
> +

I'm reluctant about this, because it makes it impossible to detect bad
json from qemu (not that qmp-shell handles this gracefully today).

At the same time I can't think of anything simpler than your hack. What
about calling json.loads() when closing } matches the number of opening ones?
QMP responses are always dictionaries.

>              if 'event' in resp:
>                  self.__events.append(resp)
>                  if not only_event:

  reply	other threads:[~2012-08-20 18:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-20 14:01 [Qemu-devel] [PATCH] Hack qmp.py to support reading a JSON multi-line response Daniel P. Berrange
2012-08-20 18:03 ` Luiz Capitulino [this message]
2012-08-20 18:25   ` Daniel P. Berrange
2012-08-20 18:28     ` Luiz Capitulino

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=20120820150311.5363c46e@doriath.home \
    --to=lcapitulino@redhat.com \
    --cc=berrange@redhat.com \
    --cc=qemu-devel@nongnu.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 NNTP newsgroup(s).