Adopt @six.python_2_unicode_compatible decorator

On Python 3, the decorator does nothing. On Python 2, it aliases the
__str__ method to __unicode__ and creates a new __str__ method that
returns the result of __unicode__() encoded with UTF-8.

All instances of __unicode__ have been removed with the
aforementioned decorator in place instead.

Note: There are some instances of __str__ which aren't handled but
will be once we have actual python34 tests running in the gate.

partial blueprint heat-python34-support

Change-Id: I7271a2581e1c2bbc282933c7da73db810c7e09db
This commit is contained in:
Sirushti Murugesan 2015-07-09 12:44:03 +05:30
parent d48b7176f1
commit 2c99f0090c
8 changed files with 19 additions and 59 deletions

View File

@ -93,6 +93,7 @@ def wrap_exception(notifier=None, publisher_id=None, event_type=None,
return inner
@six.python_2_unicode_compatible
class HeatException(Exception):
"""Base Heat Exception
@ -121,9 +122,6 @@ class HeatException(Exception):
raise_(exc_info[0], exc_info[1], exc_info[2])
def __str__(self):
return six.text_type(self.message).encode('UTF-8')
def __unicode__(self):
return six.text_type(self.message)
def __deepcopy__(self, memo):

View File

@ -25,6 +25,7 @@ class CircularDependencyException(exception.HeatException):
msg_fmt = _("Circular Dependency Found: %(cycle)s")
@six.python_2_unicode_compatible
class Node(object):
'''A node in a dependency graph.'''
@ -94,18 +95,14 @@ class Node(object):
def __str__(self):
'''Return a human-readable string representation of the node.'''
text = '{%s}' % ', '.join(str(n) for n in self)
return encodeutils.safe_encode(text)
def __unicode__(self):
'''Return a human-readable string representation of the node.'''
text = '{%s}' % ', '.join(six.text_type(n) for n in self)
return encodeutils.safe_decode(text)
return six.text_type(text)
def __repr__(self):
'''Return a string representation of the node.'''
return repr(self.require)
@six.python_2_unicode_compatible
class Graph(collections.defaultdict):
'''A mutable mapping of objects to nodes in a dependency graph.'''
@ -153,14 +150,7 @@ class Graph(collections.defaultdict):
'''Convert the graph to a human-readable string.'''
pairs = ('%s: %s' % (str(k), str(v)) for k, v in six.iteritems(self))
text = '{%s}' % ', '.join(pairs)
return encodeutils.safe_encode(text)
def __unicode__(self):
'''Convert the graph to a human-readable string.'''
pairs = ('%s: %s' % (six.text_type(k), six.text_type(v))
for k, v in six.iteritems(self))
text = '{%s}' % ', '.join(pairs)
return encodeutils.safe_decode(text)
return six.text_type(text)
@staticmethod
def toposort(graph):
@ -181,6 +171,7 @@ class Graph(collections.defaultdict):
raise CircularDependencyException(cycle=six.text_type(graph))
@six.python_2_unicode_compatible
class Dependencies(object):
'''Helper class for calculating a dependency graph.'''
@ -280,12 +271,6 @@ class Dependencies(object):
return type(self)(tuple(map(transform_key, e)) for e in edges)
def __str__(self):
'''
Return a human-readable string representation of the dependency graph
'''
return str(self._graph)
def __unicode__(self):
'''
Return a human-readable string representation of the dependency graph
'''

View File

@ -21,7 +21,6 @@ import warnings
from oslo_config import cfg
from oslo_log import log
from oslo_utils import encodeutils
import six
from heat.common import environment_format as env_fmt
@ -266,7 +265,7 @@ class ResourceRegistry(object):
if isinstance(info, ClassResourceInfo):
if info.value.support_status.status != support.SUPPORTED:
if info.value.support_status.message is not None:
warnings.warn(encodeutils.safe_encode(
warnings.warn(six.text_type(
info.value.support_status.message))
info.user_resource = (self.global_registry is not None)

View File

@ -171,6 +171,7 @@ class Schema(constr.Schema):
return super(Schema, self).__getitem__(key)
@six.python_2_unicode_compatible
class Parameter(object):
'''A template parameter.'''
@ -283,9 +284,9 @@ class Parameter(object):
'''Return a string representation of the parameter.'''
value = self.value()
if self.hidden():
return '******'
return six.text_type('******')
else:
return encodeutils.safe_encode(six.text_type(value))
return six.text_type(value)
class NumberParam(Parameter):

View File

@ -18,7 +18,6 @@ import weakref
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import encodeutils
from oslo_utils import excutils
import six
@ -93,6 +92,7 @@ class UpdateInProgress(Exception):
super(Exception, self).__init__(six.text_type(msg))
@six.python_2_unicode_compatible
class Resource(object):
ACTIONS = (
INIT, CREATE, DELETE, UPDATE, ROLLBACK,
@ -461,18 +461,6 @@ class Resource(object):
return dict((k, after_props.get(k)) for k in changed_properties_set)
def __str__(self):
if self.stack.id:
if self.resource_id:
text = '%s "%s" [%s] %s' % (self.__class__.__name__, self.name,
self.resource_id, str(self.stack))
else:
text = '%s "%s" %s' % (self.__class__.__name__, self.name,
str(self.stack))
else:
text = '%s "%s"' % (self.__class__.__name__, self.name)
return encodeutils.safe_encode(text)
def __unicode__(self):
if self.stack.id:
if self.resource_id:
text = '%s "%s" [%s] %s' % (self.__class__.__name__, self.name,
@ -483,7 +471,7 @@ class Resource(object):
six.text_type(self.stack))
else:
text = '%s "%s"' % (self.__class__.__name__, self.name)
return encodeutils.safe_decode(text)
return six.text_type(text)
def dep_attrs(self, resource_name):
return self.t.dep_attrs(resource_name)

View File

@ -120,6 +120,7 @@ class TimedCancel(Timeout):
return False
@six.python_2_unicode_compatible
class ExceptionGroup(Exception):
'''
Container for multiple exceptions.
@ -137,13 +138,10 @@ class ExceptionGroup(Exception):
self.exceptions = list(exceptions)
def __str__(self):
return six.text_type([six.text_type(ex).encode('utf-8')
for ex in self.exceptions]).encode('utf-8')
def __unicode__(self):
return six.text_type(map(six.text_type, self.exceptions))
return six.text_type([six.text_type(ex) for ex in self.exceptions])
@six.python_2_unicode_compatible
class TaskRunner(object):
"""
Wrapper for a resumable task (co-routine).
@ -170,12 +168,7 @@ class TaskRunner(object):
def __str__(self):
"""Return a human-readable string representation of the task."""
text = 'Task %s' % self.name
return encodeutils.safe_encode(text)
def __unicode__(self):
"""Return a human-readable string representation of the task."""
text = 'Task %s' % self.name
return encodeutils.safe_decode(text)
return six.text_type(text)
def _sleep(self, wait_time):
"""Sleep for the specified number of seconds."""

View File

@ -65,6 +65,7 @@ class ForcedCancel(BaseException):
return "Operation cancelled"
@six.python_2_unicode_compatible
class Stack(collections.Mapping):
ACTIONS = (
@ -556,12 +557,7 @@ class Stack(collections.Mapping):
def __str__(self):
'''Return a human-readable string representation of the stack.'''
text = 'Stack "%s" [%s]' % (self.name, self.id)
return encodeutils.safe_encode(text)
def __unicode__(self):
'''Return a human-readable string representation of the stack.'''
text = 'Stack "%s" [%s]' % (self.name, self.id)
return encodeutils.safe_encode(text)
return six.text_type(text)
def resource_by_refid(self, refid):
'''

View File

@ -187,7 +187,7 @@ class ExceptionGroupTest(common.HeatTestCase):
ex2 = Exception("ex 2")
exception_group = scheduler.ExceptionGroup([ex1, ex2])
self.assertEqual("['ex 1', 'ex 2']", str(exception_group))
self.assertEqual("[u'ex 1', u'ex 2']", str(exception_group))
class DependencyTaskGroupTest(common.HeatTestCase):