Skip to contentSkip to conversation

Provided python3 support for qmp.py

PATCH Stefan Hajnoczi 1 participant 2 messages Mar 15, 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 4 lines · Joannah Nanjekye

From: Joannah Najekye <[email protected]>

---

Text below '---' is not included as part of the commit description. I think the following lines should be included, they describe the purpose of the patch. Please the text above '---'.

Quoted 10 lines

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.

PyPI packages can be used if necessary but dependencies should be minimized. Comments on the new dependencies below:

Quoted 8 lines
 scripts/qmp/qmp.py | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
scripts/qmp/qmp.py +0 -0
index 62d3651..6dcb870 100644

scripts/qmp/qmp.py line 11

Quoted 6 lines
scripts/qmp/qmp.py +2 -1
@@ -7,7 +7,9 @@
 #
 # 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
+from future.utils import raise_from

Why is raise_from() needed? My understanding is that it provides exception chaining, which this code doesn't use. Therefore plain 'raise' is enough. The dependency can be dropped.

Quoted 20 lines
scripts/qmp/qmp.py +3 -2
+from builtins import str 
 import json
 import errno
 import socket
@@ -57,12 +59,12 @@ class QEMUMonitorProtocol:
     def __negotiate_capabilities(self):
         greeting = self.__json_read()
         if greeting is None or not greeting.has_key('QMP'):
-            raise QMPConnectError
+            raise_from(QMPConnectError())
         # Greeting seems ok, negotiate capabilities
         resp = self.cmd('qmp_capabilities')
         if "return" in resp:
             return greeting
-        raise QMPCapabilitiesError
+        raise_from(QMPCapabilitiesError())
 
     def __json_read(self, only_event=False):
         while True:

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

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

Why is str() necessary? resp is a dict and the %s format specifier already produces a string representation.

The same applies to the str(qmp_cmd) and str(resp) changes below.

Quoted 41 lines
scripts/qmp/qmp.py +6 -6
                 self.__events.append(resp)
                 if not only_event:
                     continue
@@ -111,11 +113,11 @@ class QEMUMonitorProtocol:
             try:
                 ret = self.__json_read(only_event=True)
             except socket.timeout:
-                raise QMPTimeoutError("Timeout waiting for event")
+                raise_from(QMPTimeoutError("Timeout waiting for event"))
             except:
-                raise QMPConnectError("Error while reading from socket")
+                raise_from(QMPConnectError("Error while reading from socket"))
             if ret is None:
-                raise QMPConnectError("Error while reading from socket")
+                raise_from(QMPConnectError("Error while reading from socket"))
             self.__sock.settimeout(None)
 
     def connect(self, negotiate=True):
@@ -155,16 +157,16 @@ class QEMUMonitorProtocol:
                 been closed
         """
         if self._debug:
-            print >>sys.stderr, "QMP:>>> %s" % qmp_cmd
+            print(u"QMP:>>> %s" % str(qmp_cmd), file=sys.stderr) 
         try:
             self.__sock.sendall(json.dumps(qmp_cmd))
         except socket.error as err:
             if err[0] == errno.EPIPE:
                 return
-            raise socket.error(err)
+            raise_from(socket.error(), err)
         resp = self.__json_read()
         if self._debug:
-            print >>sys.stderr, "QMP:<<< %s" % resp
+            print(u"QMP:<<< %s" % str(resp), file=sys.stderr)  
         return resp
 
     def cmd(self, name, args=None, id=None):
Signature · 1 line
2.7.4
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Joannah Nanjekye

From: Joannah Najekye <[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:

Quoted 8 lines

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 | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

Did you test your patch with Python 3? Launch QEMU like this:

  $ qemu-system-x86_64 -qmp unix:/tmp/foo.sock,server

In another terminal you can launch Python and interactively test the module like I did above.

I still get the following Python 3 exception with your patch:

$ python3
Python 3.5.2 (default, Sep 14 2016, 11:28:32)
[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

import qmp
q = qmp.QEMUMonitorProtocol('/tmp/foo.sock')
q.connect()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "qemu/scripts/qmp/qmp.py", line 135, in connect
return self.__negotiate_capabilities()
File "qemu/scripts/qmp/qmp.py", line 61, in __negotiate_capabilities
if greeting is None or not greeting.has_key('QMP'):
AttributeError: 'dict' object has no attribute 'has_key'

In Python 3 the dict object lacks the has_key() method. Please convert the code to use 'key' in dict instead of dict.has_key('key').