Skip to contentSkip to conversation

scripts: Python 3 compatibility fixes

PATCH In review Eduardo Habkost 3 participants 10 messages Mar 12, 2018 – Mar 13, 2018

lore
Share & tools
lore
#0 lore raw

cover scripts: Python 3 compatibility fixes

Fix a few incompatibilities, which should be enough to allow device-crash-test to work with Python 3.

Eduardo Habkost (3):
qemu.py: Use items() instead of iteritems()
qmp.py: Encode json data before sending
device-crash-test: Use 'python' binary

 scripts/qemu.py           | 2 +-
 scripts/qmp/qmp.py        | 2 +-
 scripts/device-crash-test | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/3 qemu.py: Use items() instead of iteritems()

items() is less efficient on Python 2.x, but makes the code work on both Python 2 and Python 3.

Cc Lukáš Doktor <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Cleber Rosa <[email protected]>
---
 scripts/qemu.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qemu.py +1 -1
index 305a946562..08a3e9af5a 100644
@@ -277,7 +277,7 @@ class QEMUMachine(object):
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the response dict'''
         qmp_args = dict()
-        for key, value in args.iteritems():
+        for key, value in args.items():
             if conv_keys:
                 qmp_args[key.replace('_', '-')] = value
             else:
Signature · 1 line
2.14.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu.py: Use items() instead of iteritems()

Quoted 16 lines · Eduardo Habkost

items() is less efficient on Python 2.x, but makes the code work on both Python 2 and Python 3.

Cc Lukáš Doktor <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Cleber Rosa <[email protected]>
---
 scripts/qemu.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qemu.py +0 -0
index 305a946562..08a3e9af5a 100644

scripts/qemu.py class QEMUMachine(object): · line 283

Quoted 8 lines
scripts/qemu.py +1 -1
@@ -277,7 +277,7 @@ class QEMUMachine(object):
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the response dict'''
         qmp_args = dict()
-        for key, value in args.iteritems():
+        for key, value in args.items():
             if conv_keys:
                 qmp_args[key.replace('_', '-')] = value
             else:

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu.py: Use items() instead of iteritems()

In reply to Eduardo Habkost
Quoted 17 lines · Eduardo Habkost

items() is less efficient on Python 2.x, but makes the code work on both Python 2 and Python 3.

Cc Lukáš Doktor <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Cleber Rosa <[email protected]>
---
 scripts/qemu.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qemu.py +0 -0
index 305a946562..08a3e9af5a 100644

scripts/qemu.py class QEMUMachine(object): · line 283

Quoted 8 lines
scripts/qemu.py +1 -1
@@ -277,7 +277,7 @@ class QEMUMachine(object):
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the response dict'''
         qmp_args = dict()
-        for key, value in args.iteritems():
+        for key, value in args.items():
             if conv_keys:
                 qmp_args[key.replace('_', '-')] = value
             else:

http://lists.nongnu.org/archive/html/qemu-devel/2017-12/msg04046.html

;)

#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu.py: Use items() instead of iteritems()

Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Eduardo Habkost

items() is less efficient on Python 2.x, but makes the code work on both Python 2 and Python 3.

Cc Lukáš Doktor <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
Cc Cleber Rosa <[email protected]>
---
 scripts/qemu.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines
scripts/qemu.py +0 -0
index 305a946562..08a3e9af5a 100644

scripts/qemu.py class QEMUMachine(object): · line 283

Quoted 8 lines
scripts/qemu.py +1 -1
@@ -277,7 +277,7 @@ class QEMUMachine(object):
     def qmp(self, cmd, conv_keys=True, **args):
         '''Invoke a QMP command and return the response dict'''
         qmp_args = dict()
-        for key, value in args.iteritems():
+        for key, value in args.items():
             if conv_keys:
                 qmp_args[key.replace('_', '-')] = value
             else:

http://lists.nongnu.org/archive/html/qemu-devel/2017-12/msg04046.html

Oops, that series had fallen through the cracks, sorry!

I remember merging a series with fixes for Python 3, but now I see there were two separate series: "Support building with py2 or py3¨ (already merged), and yours ("iotests: python3 compatibility¨).

Signature · 1 line
Eduardo
#5 lore raw

PATCH 2/3 qmp.py: Encode json data before sending

In reply to Eduardo Habkost

On Python 3, json.dumps() return a str object, which can't be sent directly through a socket and must be encoded into a bytes object. Use .encode('utf-8'), which will work on both Python 2 and Python 3.

---
 scripts/qmp/qmp.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qmp/qmp.py +1 -1
index 07c9632e9e..5c8cf6a056 100644
@@ -166,7 +166,7 @@ class QEMUMonitorProtocol(object):
         """
         self.logger.debug(">>> %s", qmp_cmd)
         try:
-            self.__sock.sendall(json.dumps(qmp_cmd))
+            self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
         except socket.error as err:
             if err[0] == errno.EPIPE:
                 return
Signature · 1 line
2.14.3
Reviewed-by on v1 2/3
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] qmp.py: Encode json data before sending

Quoted 15 lines · Eduardo Habkost

On Python 3, json.dumps() return a str object, which can't be sent directly through a socket and must be encoded into a bytes object. Use .encode('utf-8'), which will work on both Python 2 and Python 3.

---
 scripts/qmp/qmp.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qmp/qmp.py +0 -0
index 07c9632e9e..5c8cf6a056 100644

scripts/qmp/qmp.py class QEMUMonitorProtocol(object): · line 172

Quoted 8 lines
scripts/qmp/qmp.py +1 -1
@@ -166,7 +166,7 @@ class QEMUMonitorProtocol(object):
         """
         self.logger.debug(">>> %s", qmp_cmd)
         try:
-            self.__sock.sendall(json.dumps(qmp_cmd))
+            self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
         except socket.error as err:
             if err[0] == errno.EPIPE:
                 return

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] qmp.py: Encode json data before sending

In reply to Eduardo Habkost
Quoted 16 lines · Eduardo Habkost

On Python 3, json.dumps() return a str object, which can't be sent directly through a socket and must be encoded into a bytes object. Use .encode('utf-8'), which will work on both Python 2 and Python 3.

---
 scripts/qmp/qmp.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qmp/qmp.py +0 -0
index 07c9632e9e..5c8cf6a056 100644

scripts/qmp/qmp.py class QEMUMonitorProtocol(object): · line 172

Quoted 8 lines
scripts/qmp/qmp.py +1 -1
@@ -166,7 +166,7 @@ class QEMUMonitorProtocol(object):
         """
         self.logger.debug(">>> %s", qmp_cmd)
         try:
-            self.__sock.sendall(json.dumps(qmp_cmd))
+            self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8'))
         except socket.error as err:
             if err[0] == errno.EPIPE:
                 return

http://lists.nongnu.org/archive/html/qemu-devel/2017-12/msg04047.html

;)

#8 lore raw

PATCH 3/3 device-crash-test: Use 'python' binary

In reply to Eduardo Habkost

Now the script works with Python 3, so we can use the 'python' binary provided by the system.

---
 scripts/device-crash-test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/device-crash-test +1 -1
index 36194e4347..7e79a8d885 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python
 #
 #  Copyright (c) 2017 Red Hat Inc
 #
Signature · 1 line
2.14.3
Reviewed-by on v1 3/3
#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] device-crash-test: Use 'python' binary

Quoted 13 lines · Eduardo Habkost

Now the script works with Python 3, so we can use the 'python' binary provided by the system.

---
 scripts/device-crash-test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/device-crash-test +0 -0
index 36194e4347..7e79a8d885 100755

scripts/device-crash-test line 4

Quoted 5 lines
scripts/device-crash-test +1 -1
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python
 #
 #  Copyright (c) 2017 Red Hat Inc
 #

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|