summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBo Bayles <bbayles@gmail.com>2018-11-20 21:47:49 -0600
committerBo Bayles <bbayles@gmail.com>2018-11-20 21:47:49 -0600
commit1df87fc82348a26cd638bd82b431bf902522a8c4 (patch)
tree93e32447a555e742ef594ae79e7ecfbd4505b278
parentc8936f7c713e333c21dd7a6d5ecfa582bcafb535 (diff)
downloadredis-py-1df87fc82348a26cd638bd82b431bf902522a8c4.tar.gz
Don't destroy data in GEORADIUS commands
-rwxr-xr-xredis/client.py15
-rw-r--r--tests/test_commands.py9
2 files changed, 18 insertions, 6 deletions
diff --git a/redis/client.py b/redis/client.py
index d62e20e..a357368 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -378,6 +378,17 @@ def parse_cluster_nodes(response, **options):
return dict(_parse_node_line(line) for line in raw_lines)
+def text_or_binary(response):
+ """
+ Return the response as text data if possible (decoded as utf-8), otherwise
+ return it unmodified.
+ """
+ try:
+ return response.decode('utf-8')
+ except UnicodeDecodeError:
+ return response
+
+
def parse_georadius_generic(response, **options):
if options['store'] or options['store_dist']:
# `store` and `store_diff` cant be combined
@@ -392,7 +403,7 @@ def parse_georadius_generic(response, **options):
if not options['withdist'] and not options['withcoord']\
and not options['withhash']:
# just a bunch of places
- return [nativestr(r) for r in response_list]
+ return [text_or_binary(r) for r in response_list]
cast = {
'withdist': float,
@@ -402,7 +413,7 @@ def parse_georadius_generic(response, **options):
# zip all output results with each casting functino to get
# the properly native Python value.
- f = [nativestr]
+ f = [text_or_binary]
f += [cast[o] for o in ['withdist', 'withhash', 'withcoord'] if options[o]]
return [
list(map(lambda fv: fv[0](fv[1]), zip(f, r))) for r in response_list
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 28b8813..4e72466 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1653,10 +1653,11 @@ class TestRedisCommands(object):
@skip_if_server_version_lt('3.2.0')
def test_georadius(self, r):
values = (2.1909389952632, 41.433791470673, 'place1') +\
- (2.1873744593677, 41.406342043777, 'place2')
+ (2.1873744593677, 41.406342043777, b'\x80place2')
r.geoadd('barcelona', *values)
assert r.georadius('barcelona', 2.191, 41.433, 1000) == ['place1']
+ assert r.georadius('barcelona', 2.187, 41.406, 1000) == [b'\x80place2']
@skip_if_server_version_lt('3.2.0')
def test_georadius_no_values(self, r):
@@ -1746,17 +1747,17 @@ class TestRedisCommands(object):
@skip_if_server_version_lt('3.2.0')
def test_georadiusmember(self, r):
values = (2.1909389952632, 41.433791470673, 'place1') +\
- (2.1873744593677, 41.406342043777, 'place2')
+ (2.1873744593677, 41.406342043777, b'\x80place2')
r.geoadd('barcelona', *values)
assert r.georadiusbymember('barcelona', 'place1', 4000) ==\
- ['place2', 'place1']
+ [b'\x80place2', 'place1']
assert r.georadiusbymember('barcelona', 'place1', 10) == ['place1']
assert r.georadiusbymember('barcelona', 'place1', 4000,
withdist=True, withcoord=True,
withhash=True) ==\
- [['place2', 3067.4157, 3471609625421029,
+ [[b'\x80place2', 3067.4157, 3471609625421029,
(2.187376320362091, 41.40634178640635)],
['place1', 0.0, 3471609698139488,
(2.1909382939338684, 41.433790281840835)]]