summaryrefslogtreecommitdiff
path: root/redis/_compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'redis/_compat.py')
-rw-r--r--redis/_compat.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/redis/_compat.py b/redis/_compat.py
index c7859b5..38d767d 100644
--- a/redis/_compat.py
+++ b/redis/_compat.py
@@ -12,6 +12,16 @@ if sys.version_info[0] < 3:
except ImportError:
from StringIO import StringIO as BytesIO
+ # special unicode handling for python2 to avoid UnicodeDecodeError
+ def safe_unicode(obj, *args):
+ """ return the unicode representation of obj """
+ try:
+ return unicode(obj, *args)
+ except UnicodeDecodeError:
+ # obj is byte string
+ ascii_text = str(obj).encode('string_escape')
+ return unicode(ascii_text)
+
iteritems = lambda x: x.iteritems()
iterkeys = lambda x: x.iterkeys()
itervalues = lambda x: x.itervalues()
@@ -48,6 +58,7 @@ else:
xrange = range
basestring = str
unicode = str
+ safe_unicode = str
bytes = bytes
long = int