From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:41628) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T3Wh6-00008S-Oy for qemu-devel@nongnu.org; Mon, 20 Aug 2012 14:27:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T3Wh5-0004qt-Kk for qemu-devel@nongnu.org; Mon, 20 Aug 2012 14:27:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38643) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T3Wh5-0004qS-Co for qemu-devel@nongnu.org; Mon, 20 Aug 2012 14:27:19 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q7KIRHWH026446 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 20 Aug 2012 14:27:18 -0400 Date: Mon, 20 Aug 2012 15:28:01 -0300 From: Luiz Capitulino Message-ID: <20120820152801.57241c6c@doriath.home> In-Reply-To: <20120820182502.GB4801@redhat.com> References: <1345471297-21459-1-git-send-email-berrange@redhat.com> <20120820150311.5363c46e@doriath.home> <20120820182502.GB4801@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] Hack qmp.py to support reading a JSON multi-line response List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" Cc: qemu-devel@nongnu.org On Mon, 20 Aug 2012 19:25:02 +0100 "Daniel P. Berrange" wrote: > On Mon, Aug 20, 2012 at 03:03:11PM -0300, Luiz Capitulino wrote: > > On Mon, 20 Aug 2012 15:01:37 +0100 > > "Daniel P. Berrange" wrote: > > > > > From: "Daniel P. Berrange" > > > > > > 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 > > > --- > > > 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). > > Yeah, it is slightly unpleasant. > > > 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. > > How about making multi-line processing an optional feature, by adding > a '-m' flag to qmp-shell. That way we still get error checking by > default Good idea. > > > Daniel