Skip to content

Commit 3449a9a

Browse files
guyco-redisdanni-m
authored andcommitted
Adapting mockredis to support futurize's types and python 3
1 parent 3acb994 commit 3449a9a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

mockredis/client.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import time
1212
import fnmatch
13+
from future.builtins import bytes as newbytes
1314

1415
from mockredis.clock import SystemClock
1516
from mockredis.lock import MockRedisLock
@@ -22,6 +23,7 @@
2223
long = int
2324
xrange = range
2425
basestring = str
26+
unicode = str
2527
from functools import reduce
2628

2729

@@ -165,8 +167,15 @@ def keys(self, pattern='*'):
165167
regex = fnmatch.translate(pattern)
166168
regex = re.compile(re.sub(r'(^|[^\\])\.', r'\1[^/]', regex))
167169

170+
keys = []
171+
168172
# Find every key that matches the pattern
169-
return [key for key in self.redis.keys() if regex.match(key.decode('utf-8'))]
173+
for key in self.redis.keys():
174+
decoded_key = key if isinstance(key, unicode) else key.decode('utf-8')
175+
if regex.match(decoded_key):
176+
keys.append(key)
177+
178+
return keys
170179

171180
def delete(self, *keys):
172181
"""Emulate delete."""
@@ -1303,7 +1312,8 @@ def script_kill(self):
13031312

13041313
def script_load(self, script):
13051314
"""Emulate script_load"""
1306-
sha_digest = sha1(script.encode("utf-8")).hexdigest()
1315+
encoded_script = script if isinstance(script, bytes) else script.encode("utf-8")
1316+
sha_digest = sha1(encoded_script).hexdigest()
13071317
self.shas[sha_digest] = script
13081318
return sha_digest
13091319

@@ -1521,9 +1531,9 @@ def _score_inclusive(self, score):
15211531
return True, float(score)
15221532

15231533
def _encode(self, value):
1524-
"Return a bytestring representation of the value. Taken from redis-py connection.py"
1525-
if isinstance(value, bytes):
1526-
return value
1534+
"Return a bytestring representation of the value. Originally taken from redis-py connection.py"
1535+
if isinstance(value, (newbytes, bytes)):
1536+
return str(value)
15271537
elif isinstance(value, (int, long)):
15281538
value = str(value).encode('utf-8')
15291539
elif isinstance(value, float):
@@ -1532,6 +1542,10 @@ def _encode(self, value):
15321542
value = str(value).encode('utf-8')
15331543
else:
15341544
value = value.encode('utf-8', 'strict')
1545+
1546+
if isinstance(value, bytes):
1547+
value = value.decode('utf-8', 'strict')
1548+
15351549
return value
15361550

15371551
def _log(self, level, msg):

mockredis/script.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
except ImportError:
88
# Python 3
99
izip = zip
10+
basestring = str
11+
long = int
12+
1013
from mockredis.exceptions import ResponseError
1114

1215
LuaLock = threading.Lock()

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
license='Apache2',
1616
packages=find_packages(exclude=['*.tests']),
1717
setup_requires=[
18-
'nose'
18+
'nose',
19+
'future'
1920
],
2021
extras_require={
2122
'lua': ['lunatic-python-bugfix==1.1.1'],

0 commit comments

Comments
 (0)