diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2014-12-05 13:03:38 -0500 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2014-12-05 13:03:38 -0500 |
commit | 38b19cd3098f0c21510a5565dae349bd1caef6dc (patch) | |
tree | e135fe34d53cb9319fd18767f5de72c4dbade30f /redis/_compat.py | |
parent | fa714e08ef59d37b145616cb60939eedafda0bd4 (diff) | |
parent | eddf49d774945986325a04e134b0ba42e0046e70 (diff) | |
download | redis-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.py | 11 |
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 |