summaryrefslogtreecommitdiff
path: root/redis/_compat.py
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2014-12-05 13:03:38 -0500
committerAndy McCurdy <andy@andymccurdy.com>2014-12-05 13:03:38 -0500
commit38b19cd3098f0c21510a5565dae349bd1caef6dc (patch)
treee135fe34d53cb9319fd18767f5de72c4dbade30f /redis/_compat.py
parentfa714e08ef59d37b145616cb60939eedafda0bd4 (diff)
parenteddf49d774945986325a04e134b0ba42e0046e70 (diff)
downloadredis-py-38b19cd3098f0c21510a5565dae349bd1caef6dc.tar.gz
Merge pull request #564 from hendrik-cliqz/fix-unicode-conversion-in-exception-handling
Fix UnicodeDecodeError in annotate_excpetion
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