Ensure__repr__ return value is encoded

When __repr__ returns an unicode object, python will
try to encode it with the default encoding scheme
which is ascii in general. So __repr__ needs to make
sure its return value is properly encoded.

Change-Id: Ifdb8c073474a917207a48fc5af5a340e87f66fc4
Closes-Bug: #1514325
(cherry picked from commit f5e792a983)
This commit is contained in:
Liang Chen
2015-11-09 15:21:17 +08:00
parent 1361211239
commit e36cafb253

View File

@@ -20,6 +20,7 @@ import copy
import logging
import oslo_messaging as messaging
from oslo_utils import encodeutils
from oslo_utils import excutils
import six
@@ -294,13 +295,16 @@ class VersionedObject(object):
setattr(self, key, kwargs[key])
def __repr__(self):
return '%s(%s)' % (
repr_str = '%s(%s)' % (
self.obj_name(),
','.join(['%s=%s' % (name,
(self.obj_attr_is_set(name) and
field.stringify(getattr(self, name)) or
'<?>'))
for name, field in sorted(self.fields.items())]))
if not six.PY3:
repr_str = encodeutils.safe_encode(repr_str, incoming='utf-8')
return repr_str
@classmethod
def obj_name(cls):