diff options
author | andy <andy@whiskeymedia.com> | 2012-10-07 22:32:50 -0700 |
---|---|---|
committer | andy <andy@whiskeymedia.com> | 2012-10-07 22:32:50 -0700 |
commit | 520ddbd47c96055a88c09d58e490328634dd59cb (patch) | |
tree | 8b6b5cfe76cbc11b0f03a5b7dafb9aa8eff61c9b | |
parent | 6e7a710fe480816d2abe27263a9975332c939c6f (diff) | |
download | redis-py-520ddbd47c96055a88c09d58e490328634dd59cb.tar.gz |
High precision floating point values are now properly sent to the Redis server.
Fixes #227
-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) |