Skip to contentSkip to conversation

scripts/qmp: python3 support for qmp.py

PATCH Stefan Hajnoczi 1 participant 1 message Mar 28, 2017 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 31 lines · Joannah Nanjekye

From: nanjekyejoannah <[email protected]>

The patch provides python 3 support for one of the scripts in scripts/qmp that is to say qmp.py. This is not a port to python 3 but rather the patch ensures that the script runs fine for both python 2 and 3.

Minimum Python Versions supported:

Python 2 : python 2.6 +
Python 3 : python 3.3 +

The two new imports future and builtins introduced refer to the future pip-installable package on PyPI.

---
 scripts/qmp/qmp.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
scripts/qmp/qmp.py +1 -1
index 62d3651..57ac86b 100644
@@ -7,7 +7,7 @@
 #
 # This work is licensed under the terms of the GNU GPL, version 2.  See
 # the COPYING file in the top-level directory.
-
+from __future__ import absolute_import, print_function, unicode_literals
 import json
 import errno
 import socket

scripts/qmp/qmp.py class QEMUMonitorProtocol: · line 59

Quoted 6 lines
scripts/qmp/qmp.py +1 -2
@@ -56,13 +56,13 @@ class QEMUMonitorProtocol:
 
     def __negotiate_capabilities(self):
         greeting = self.__json_read()
-        if greeting is None or not greeting.has_key('QMP'):
-            raise QMPConnectError
+        if greeting is None or not 'QMP' in list(greeting):

list() is not necessary. The "key in dict" expression already operates on just the dict's keys.

Quoted 11 lines
scripts/qmp/qmp.py +2 -1
+            raise QMPConnectError()
         # Greeting seems ok, negotiate capabilities
         resp = self.cmd('qmp_capabilities')
         if "return" in resp:
             return greeting
-        raise QMPCapabilitiesError
+        raise QMPCapabilitiesError()
 
     def __json_read(self, only_event=False):
         while True:

scripts/qmp/qmp.py class QEMUMonitorProtocol: · line 75

Quoted 5 lines
scripts/qmp/qmp.py +1 -1
@@ -72,7 +72,7 @@ class QEMUMonitorProtocol:
             resp = json.loads(data)
             if 'event' in resp:
                 if self._debug:
-                    print >>sys.stderr, "QMP:<<< %s" % resp
+                    print(u"QMP:<<< %s" % resp, file=sys.stderr) 

unicode_literals already makes all string literals unicode. There is no need to explicitly add u"".

scripts/qmp/qmp.py class QEMUMonitorProtocol: · line 116

Quoted 9 lines
scripts/qmp/qmp.py +1 -1
                 self.__events.append(resp)
                 if not only_event:
                     continue
@@ -113,7 +113,7 @@ class QEMUMonitorProtocol:
             except socket.timeout:
                 raise QMPTimeoutError("Timeout waiting for event")
             except:
-                raise QMPConnectError("Error while reading from socket")
+                raise(QMPConnectError("Error while reading from socket"))

Is there a reason for this change?

scripts/qmp/qmp.py class QEMUMonitorProtocol: · line 160

Quoted 12 lines
scripts/qmp/qmp.py +2 -2
             if ret is None:
                 raise QMPConnectError("Error while reading from socket")
             self.__sock.settimeout(None)
@@ -155,16 +155,16 @@ class QEMUMonitorProtocol:
                 been closed
         """
         if self._debug:
-            print >>sys.stderr, "QMP:>>> %s" % qmp_cmd
+            print(u"QMP:>>> %s" % qmp_cmd, file=sys.stderr) 
         try:
-            self.__sock.sendall(json.dumps(qmp_cmd))
+            self.__sock.sendall((json.dumps(qmp_cmd)).encode('latin-1'))

The socket's encoding should be 'utf-8'.

You didn't specify an encoding in __json_read() where self.__sockfile.readline() is called. It will use the default encoding and could therefore hit decoding errors. We need to specify 'utf-8' for reading too.

I think that is a little tricky to do in a Python 2/3 compatible way because Python 2.6 socket and makefile do not take an encoding argument. The simplest solution might be to read 1 byte at a time in __json_read() until '\n' is encountered instead of calling readline(). That way you can call line.decode('utf-8') on the raw bytes.

scripts/qmp/qmp.py class QEMUMonitorProtocol: · line 164

Quoted 5 lines
scripts/qmp/qmp.py +1 -1
         except socket.error as err:
             if err[0] == errno.EPIPE:
                 return
-            raise socket.error(err)
+            raise (socket.error(), err)

We're re-raising the exception. There is shorter syntax for this:

except socket.error as err:

      if err[0] == errno.EPIPE:
          return
      raise