From 19d48d81ce3227d5f9544072c9c6915133b0f75c Mon Sep 17 00:00:00 2001 From: Eric Du Date: Mon, 4 May 2015 10:28:24 +0800 Subject: [PATCH] fix: Connection.encode complains when value is an object having unicode characters in its printable representation --- redis/connection.py | 2 +- tests/test_encoding.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/redis/connection.py b/redis/connection.py index 790997b8b4..0229d28eed 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -588,7 +588,7 @@ def encode(self, value): elif isinstance(value, float): value = b(repr(value)) elif not isinstance(value, basestring): - value = str(value) + value = unicode(value) if isinstance(value, unicode): value = value.encode(self.encoding, self.encoding_errors) return value diff --git a/tests/test_encoding.py b/tests/test_encoding.py index b1df0a56c2..f0c67bec47 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -23,6 +23,13 @@ def test_list_encoding(self, r): r.rpush('a', *result) assert r.lrange('a', 0, -1) == result + def test_object_value(self, r): + unicode_string = unichr(3456) + u('abcd') + unichr(3421) + r['unicode-string'] = Exception(unicode_string) + cached_val = r['unicode-string'] + assert isinstance(cached_val, unicode) + assert unicode_string == cached_val + class TestCommandsAndTokensArentEncoded(object): @pytest.fixture()