Ensure the json result type is bytes on Python 3

Since the json.dumps() returns Unicode string On Python 3, to
ensure that the result type is bytes on Python 2 and Python 3
that if the result is used for the message body, let's replace
    json.dumps() /  oslo_serialization.jsonutils.dumps()
with
    oslo_serialization.jsoutils.dump_as_bytes()

Change-Id: I0e0f6b715ffc4a9ad82be52e55696d032b6d0976
This commit is contained in:
Javeme 2016-02-22 15:22:06 +08:00
parent 3288c4d7e2
commit bd81d09c02
7 changed files with 10 additions and 9 deletions

View File

@ -197,13 +197,13 @@ def serialize_remote_exception(failure_info, log_failure=True):
'kwargs': kwargs
}
json_data = jsonutils.dumps(data)
json_data = jsonutils.dump_as_bytes(data)
return json_data
def deserialize_remote_exception(data, allowed_remote_exmods):
failure = jsonutils.loads(six.text_type(data))
failure = jsonutils.loads(data)
trace = failure.get('tb', [])
message = failure.get('message', "") + "\n" + "\n".join(trace)
@ -284,7 +284,7 @@ def serialize_msg(raw_msg):
# NOTE(russellb) See the docstring for _RPC_ENVELOPE_VERSION for more
# information about this format.
msg = {_VERSION_KEY: _RPC_ENVELOPE_VERSION,
_MESSAGE_KEY: jsonutils.dumps(raw_msg)}
_MESSAGE_KEY: jsonutils.dump_as_bytes(raw_msg)}
return msg

View File

@ -126,7 +126,7 @@ class Connection(object):
def _send_and_retry(self, message, topic, retry):
current_retry = 0
if not isinstance(message, str):
message = jsonutils.dumps(message)
message = jsonutils.dump_as_bytes(message)
while message is not None:
try:
self._send(message, topic)

View File

@ -56,7 +56,7 @@ def marshal_response(reply=None, failure=None):
data = {"failure": failure}
else:
data = {"response": reply}
msg.body = jsonutils.dumps(data)
msg.body = jsonutils.dump_as_bytes(data)
return msg
@ -80,7 +80,7 @@ def marshal_request(request, context, envelope):
"request": request,
"context": context
}
msg.body = jsonutils.dumps(data)
msg.body = jsonutils.dump_as_bytes(data)
return msg

View File

@ -40,6 +40,7 @@ class LogDriver(notifier.Driver):
message['event_type']))
method = getattr(logger, priority.lower(), None)
if method:
# NOTE: The logger needs json formatted string instead of bytes
method(jsonutils.dumps(strutils.mask_dict_password(message)))
else:
warnings.warn('Unable to log message as notify cannot find a '

View File

@ -145,7 +145,7 @@ class TestKafkaConnection(test_utils.BaseTestCase):
conn.consumer = mock.MagicMock()
conn.consumer.fetch_messages = mock.MagicMock(
return_value=iter([jsonutils.dumps(fake_message)]))
return_value=iter([jsonutils.dump_as_bytes(fake_message)]))
self.assertEqual(fake_message, jsonutils.loads(conn.consume()[0]))
self.assertEqual(1, len(conn.consumer.fetch_messages.mock_calls))

View File

@ -919,7 +919,7 @@ class TestReplyWireFormat(test_utils.BaseTestCase):
'_reply_q': 'reply_' + uuid.uuid4().hex,
})
msg['oslo.message'] = jsonutils.dumps(msg['oslo.message'])
msg['oslo.message'] = jsonutils.dump_as_bytes(msg['oslo.message'])
producer.publish(msg)

View File

@ -293,7 +293,7 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
'kwargs': self.kwargs,
}
serialized = jsonutils.dumps(failure)
serialized = jsonutils.dump_as_bytes(failure)
ex = exceptions.deserialize_remote_exception(serialized, self.allowed)