Replace usage of str() with six.text_type

Replace using of str with six.text_type as it's able to
handle unicode data.

Change-Id: I38e4378c490d1dad8020312308dd3b6dad8772c0
This commit is contained in:
Aaron Rosen 2014-06-02 15:35:57 -07:00
parent 3f9fc44bc6
commit 0102aa96e6
7 changed files with 18 additions and 17 deletions

View File

@ -183,9 +183,7 @@ def unpack_context(conf, msg):
"""Unpack context from msg.""" """Unpack context from msg."""
context_dict = {} context_dict = {}
for key in list(msg.keys()): for key in list(msg.keys()):
# NOTE(vish): Some versions of Python don't like unicode keys key = six.text_type(key)
# in kwargs.
key = str(key)
if key.startswith('_context_'): if key.startswith('_context_'):
value = msg.pop(key) value = msg.pop(key)
context_dict[key[9:]] = value context_dict[key[9:]] = value

View File

@ -199,8 +199,8 @@ def serialize_remote_exception(failure_info, log_failure=True):
# NOTE(matiu): With cells, it's possible to re-raise remote, remote # NOTE(matiu): With cells, it's possible to re-raise remote, remote
# exceptions. Lets turn it back into the original exception type. # exceptions. Lets turn it back into the original exception type.
cls_name = str(failure.__class__.__name__) cls_name = six.text_type(failure.__class__.__name__)
mod_name = str(failure.__class__.__module__) mod_name = six.text_type(failure.__class__.__module__)
if (cls_name.endswith(_REMOTE_POSTFIX) and if (cls_name.endswith(_REMOTE_POSTFIX) and
mod_name.endswith(_REMOTE_POSTFIX)): mod_name.endswith(_REMOTE_POSTFIX)):
cls_name = cls_name[:-len(_REMOTE_POSTFIX)] cls_name = cls_name[:-len(_REMOTE_POSTFIX)]
@ -221,7 +221,7 @@ def serialize_remote_exception(failure_info, log_failure=True):
def deserialize_remote_exception(data, allowed_remote_exmods): def deserialize_remote_exception(data, allowed_remote_exmods):
failure = jsonutils.loads(str(data)) failure = jsonutils.loads(six.text_type(data))
trace = failure.get('tb', []) trace = failure.get('tb', [])
message = failure.get('message', "") + "\n" + "\n".join(trace) message = failure.get('message', "") + "\n" + "\n".join(trace)

View File

@ -506,10 +506,10 @@ class Connection(object):
self.connection.open() self.connection.open()
def _register_consumer(self, consumer): def _register_consumer(self, consumer):
self.consumers[str(consumer.get_receiver())] = consumer self.consumers[six.text_type(consumer.get_receiver())] = consumer
def _lookup_consumer(self, receiver): def _lookup_consumer(self, receiver):
return self.consumers[str(receiver)] return self.consumers[six.text_type(receiver)]
def _disconnect(self): def _disconnect(self):
# Close the session if necessary # Close the session if necessary

View File

@ -151,7 +151,7 @@ class ConsumerBase(object):
passed in here as a dictionary. passed in here as a dictionary.
""" """
self.callback = callback self.callback = callback
self.tag = str(tag) self.tag = six.text_type(tag)
self.kwargs = kwargs self.kwargs = kwargs
self.queue = None self.queue = None
self.reconnect(channel) self.reconnect(channel)
@ -208,7 +208,7 @@ class ConsumerBase(object):
self.queue.cancel(self.tag) self.queue.cancel(self.tag)
except KeyError as e: except KeyError as e:
# NOTE(comstud): Kludge to get around a amqplib bug # NOTE(comstud): Kludge to get around a amqplib bug
if str(e) != "u'%s'" % self.tag: if six.text_type(e) != "u'%s'" % self.tag:
raise raise
self.queue = None self.queue = None
@ -617,7 +617,7 @@ class Connection(object):
# a protocol response. (See paste link in LP888621) # a protocol response. (See paste link in LP888621)
# So, we check all exceptions for 'timeout' in them # So, we check all exceptions for 'timeout' in them
# and try to reconnect in this case. # and try to reconnect in this case.
if 'timeout' not in str(e): if 'timeout' not in six.text_type(e):
raise raise
log_info = {} log_info = {}
@ -670,7 +670,7 @@ class Connection(object):
# a protocol response. (See paste link in LP888621) # a protocol response. (See paste link in LP888621)
# So, we check all exceptions for 'timeout' in them # So, we check all exceptions for 'timeout' in them
# and try to reconnect in this case. # and try to reconnect in this case.
if 'timeout' not in str(e): if 'timeout' not in six.text_type(e):
raise raise
if error_callback: if error_callback:
error_callback(e) error_callback(e)

View File

@ -783,7 +783,8 @@ def fanout_cast(conf, context, topic, msg, **kwargs):
"""Send a message to all listening and expect no reply.""" """Send a message to all listening and expect no reply."""
# NOTE(ewindisch): fanout~ is used because it avoid splitting on . # NOTE(ewindisch): fanout~ is used because it avoid splitting on .
# and acts as a non-subtle hint to the matchmaker and ZmqProxy. # and acts as a non-subtle hint to the matchmaker and ZmqProxy.
_multi_send(_cast, context, 'fanout~' + str(topic), msg, **kwargs) _multi_send(_cast, context, 'fanout~' + six.text_type(topic),
msg, **kwargs)
def notify(conf, context, topic, msg, envelope): def notify(conf, context, topic, msg, envelope):

View File

@ -16,6 +16,8 @@
__all__ = ['MessagingException', 'MessagingTimeout', 'MessageDeliveryFailure', __all__ = ['MessagingException', 'MessagingTimeout', 'MessageDeliveryFailure',
'InvalidTarget'] 'InvalidTarget']
import six
class MessagingException(Exception): class MessagingException(Exception):
"""Base class for exceptions.""" """Base class for exceptions."""
@ -33,6 +35,6 @@ class InvalidTarget(MessagingException, ValueError):
"""Raised if a target does not meet certain pre-conditions.""" """Raised if a target does not meet certain pre-conditions."""
def __init__(self, msg, target): def __init__(self, msg, target):
msg = msg + ":" + str(target) msg = msg + ":" + six.text_type(target)
super(InvalidTarget, self).__init__(msg) super(InvalidTarget, self).__init__(msg)
self.target = target self.target = target

View File

@ -62,7 +62,7 @@ class Notifier(object):
Notification messages follow the following format:: Notification messages follow the following format::
{'message_id': str(uuid.uuid4()), {'message_id': six.text_type(uuid.uuid4()),
'publisher_id': 'compute.host1', 'publisher_id': 'compute.host1',
'timestamp': timeutils.utcnow(), 'timestamp': timeutils.utcnow(),
'priority': 'WARN', 'priority': 'WARN',
@ -163,12 +163,12 @@ class Notifier(object):
payload = self._serializer.serialize_entity(ctxt, payload) payload = self._serializer.serialize_entity(ctxt, payload)
ctxt = self._serializer.serialize_context(ctxt) ctxt = self._serializer.serialize_context(ctxt)
msg = dict(message_id=str(uuid.uuid4()), msg = dict(message_id=six.text_type(uuid.uuid4()),
publisher_id=publisher_id or self.publisher_id, publisher_id=publisher_id or self.publisher_id,
event_type=event_type, event_type=event_type,
priority=priority, priority=priority,
payload=payload, payload=payload,
timestamp=str(timeutils.utcnow())) timestamp=six.text_type(timeutils.utcnow()))
def do_notify(ext): def do_notify(ext):
try: try: