diff options
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | redis/connection.py | 6 | ||||
-rw-r--r-- | tests/server_commands.py | 9 |
3 files changed, 15 insertions, 2 deletions
@@ -14,6 +14,8 @@ Redis 2.6+ servers. * Added PEXPIRE/PEXPIREAT/PTTL commands. Thanks Luper Rouch. * Added INCRBYFLOAT/HINCRBYFLOAT commands. Thanks Nikita Uvarov. + * High precision floating point values won't lose their precision when + being sent to the Redis server. Thanks Jason Oster and Oleg Pudeyev. * 2.6.2 * `from_url` is now available as a classmethod on client classes. Thanks Jon Parise for the patch. diff --git a/redis/connection.py b/redis/connection.py index 0322feb..118e99f 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -4,7 +4,7 @@ import socket import sys from redis._compat import (b, xrange, imap, byte_to_chr, unicode, bytes, long, - BytesIO, nativestr) + BytesIO, nativestr, basestring) from redis.exceptions import ( RedisError, ConnectionError, @@ -313,7 +313,9 @@ class Connection(object): "Return a bytestring representation of the value" if isinstance(value, bytes): return value - if not isinstance(value, unicode): + if isinstance(value, float): + value = repr(value) + if not isinstance(value, basestring): value = str(value) if isinstance(value, unicode): value = value.encode(self.encoding, self.encoding_errors) diff --git a/tests/server_commands.py b/tests/server_commands.py index d3616eb..dd32b19 100644 --- a/tests/server_commands.py +++ b/tests/server_commands.py @@ -1635,3 +1635,12 @@ class ServerCommandsTestCase(unittest.TestCase): data = ''.join(data) self.client.set('a', data) self.assertEquals(self.client.get('a'), b(data)) + + def test_floating_point_encoding(self): + """ + High precision floating point values sent to the server should keep + precision. + """ + timestamp = 1349673917.939762 + self.client.zadd('a', 'aaa', timestamp) + self.assertEquals(self.client.zscore('a', 'aaa'), timestamp) |