From e36cafb253b56c7165d73091b874b78111a2ecf2 Mon Sep 17 00:00:00 2001 From: Liang Chen Date: Mon, 9 Nov 2015 15:21:17 +0800 Subject: [PATCH] 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 f5e792a9833e5139ee1f66b2ac1e55b251c42533) --- oslo_versionedobjects/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/oslo_versionedobjects/base.py b/oslo_versionedobjects/base.py index 402f1dd3..fc2200e3 100644 --- a/oslo_versionedobjects/base.py +++ b/oslo_versionedobjects/base.py @@ -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):