unicode strings should not raise an error if used in repr.

Python 2 doesn't allow unicode objects in __repr__ methods. Therefore we need to encode them as utf-8 bytes.
This commit is contained in:
Dave Halter
2015-03-25 23:42:52 +01:00
parent 4bb41b6096
commit 72fd190149
4 changed files with 30 additions and 15 deletions
+18
View File
@@ -180,3 +180,21 @@ def no_unicode_pprint(dct):
import pprint
s = pprint.pformat(dct)
print(re.sub("u'", "'", s))
def utf8_repr(func):
"""
``__repr__`` methods in Python 2 don't allow unicode objects to be
returned. Therefore cast them to utf-8 bytes in this decorator.
"""
def wrapper(self):
result = func(self)
if isinstance(result, unicode):
return result.encode('utf-8')
else:
return result
if is_py3:
return func
else:
return wrapper