Commit 68898f82 authored by Douglas's avatar Douglas

Ensure that every string key and value of the json response from the network...

Ensure that every string key and value of the json response from the network cache server is a str object.

This method was added mostly because the NetworkcacheClient gets a json response from the server and all textual keys and values after json.loads are unicode objects, which may result in a error later when we try to join the url and the parameters of the package to use the networkcache.
parent 88c67f9b
......@@ -49,6 +49,17 @@ class short_exc_info(tuple):
l = ['%s:%s\n' % (filename, lineno)]
l += traceback.format_exception_only(t, v)
return ''.join(l).rstrip()
def byteify(input):
'''Transform every unicode string inside a list or dict into normal strings. '''
if isinstance(input, dict):
return dict([(byteify(key), byteify(value)) for key, value in input.iteritems()])
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
class NetworkcacheClient(object):
......@@ -263,7 +274,7 @@ class NetworkcacheClient(object):
data_list = self.select_generic(key, self.signature_certificate_list)
for information_json, signature in data_list:
try:
information_dict = json.loads(information_json)
information_dict = byteify(json.loads(information_json))
except Exception:
logger.info('Failed to parse json-in-json response (%r)',
information_json)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment