[lxc-devel] [pylxd/master] Support python 3.4+

sergiusens on Github lxc-bot at linuxcontainers.org
Wed Feb 3 18:02:43 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 363 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160203/7f5aca2b/attachment.bin>
-------------- next part --------------
From 90810294587ba638a4f67524d524c6b2f6695eb8 Mon Sep 17 00:00:00 2001
From: Sergio Schvezov <sergio.schvezov at canonical.com>
Date: Wed, 3 Feb 2016 14:32:23 -0300
Subject: [PATCH] Support python 3.4+

Signed-off-by: Sergio Schvezov <sergio.schvezov at canonical.com>
---
 pylxd/connection.py            | 16 ++++++++++++----
 pylxd/tests/test_connection.py |  9 +++++++--
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/pylxd/connection.py b/pylxd/connection.py
index 74ce925..2fb6286 100644
--- a/pylxd/connection.py
+++ b/pylxd/connection.py
@@ -17,6 +17,7 @@
 import copy
 import json
 import os
+import six
 import socket
 import ssl
 import threading
@@ -49,9 +50,13 @@ class UnixHTTPConnection(http_client.HTTPConnection):
 
     def __init__(self, path, host='localhost', port=None, strict=None,
                  timeout=None):
-        http_client.HTTPConnection.__init__(self, host, port=port,
-                                            strict=strict,
-                                            timeout=timeout)
+        if six.PY2:
+            http_client.HTTPConnection.__init__(self, host, port=port,
+   	                                        strict=strict,
+                                                timeout=timeout)
+        else:
+            http_client.HTTPConnection.__init__(self, host, port=port,
+                                                timeout=timeout)
 
         self.path = path
 
@@ -152,7 +157,10 @@ def _request(self, *args, **kwargs):
         status = response.status
         raw_body = response.read()
         try:
-            body = json.loads(raw_body)
+            if six.PY2:
+                body = json.loads(raw_body)
+            else:
+                body = json.loads(raw_body.decode())
         except ValueError:
             body = None
 
diff --git a/pylxd/tests/test_connection.py b/pylxd/tests/test_connection.py
index dcffa30..f15ae7f 100644
--- a/pylxd/tests/test_connection.py
+++ b/pylxd/tests/test_connection.py
@@ -15,6 +15,7 @@
 from ddt import ddt
 import inspect
 import mock
+import six
 from six.moves import cStringIO
 from six.moves import http_client
 import socket
@@ -32,8 +33,12 @@ class LXDInitConnectionTest(unittest.TestCase):
     @mock.patch.object(http_client.HTTPConnection, '__init__')
     def test_http_connection(self, mc, ms):
         conn = connection.UnixHTTPConnection('/', 'host', 1234)
-        mc.assert_called_once_with(
-            conn, 'host', port=1234, strict=None, timeout=None)
+        if six.PY2:
+            mc.assert_called_once_with(
+                conn, 'host', port=1234, strict=None, timeout=None)
+        else:
+            mc.assert_called_once_with(
+                conn, 'host', port=1234, timeout=None)
         conn.connect()
         ms.assert_called_once_with(socket.AF_UNIX, socket.SOCK_STREAM)
         ms.return_value.connect.assert_called_once_with('/')


More information about the lxc-devel mailing list