Fix unicode errors in json.load method

HTTPConnection returns bytes in Python 2. An explicit
decoding is needed in Python 3 to ensure compatibility.
pull/21111/merge
Esteban Echeverry 8 years ago committed by Toshio Kuratomi
parent cfa8bb4255
commit 66040e0c9d

@ -27,23 +27,22 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import socket
import ssl
from ansible.module_utils.urls import generic_urlparse from ansible.module_utils.urls import generic_urlparse
from ansible.module_utils.six.moves.urllib.parse import urlparse from ansible.module_utils.six.moves.urllib.parse import urlparse
from ansible.module_utils.six.moves import http_client
try: from ansible.module_utils.six import PY2
import json
except ImportError:
import simplejson as json
# httplib/http.client connection using unix domain socket # httplib/http.client connection using unix domain socket
import socket HTTPConnection = http_client.HTTPConnection
import ssl HTTPSConnection = http_client.HTTPSConnection
try: try:
from httplib import HTTPConnection, HTTPSConnection import json
except ImportError: except ImportError:
# Python 3 import simplejson as json
from http.client import HTTPConnection, HTTPSConnection
class UnixHTTPConnection(HTTPConnection): class UnixHTTPConnection(HTTPConnection):
@ -110,7 +109,10 @@ class LXDClient(object):
body = json.dumps(body_json) body = json.dumps(body_json)
self.connection.request(method, url, body=body) self.connection.request(method, url, body=body)
resp = self.connection.getresponse() resp = self.connection.getresponse()
resp_json = json.loads(resp.read()) resp_data = resp.read()
if not PY2:
resp_data = resp_data.decode('utf-8')
resp_json = json.loads(resp_data)
self.logs.append({ self.logs.append({
'type': 'sent request', 'type': 'sent request',
'request': {'method': method, 'url': url, 'json': body_json, 'timeout': timeout}, 'request': {'method': method, 'url': url, 'json': body_json, 'timeout': timeout},

Loading…
Cancel
Save