10
10
import sys
11
11
import time
12
12
import fnmatch
13
+ from future .builtins import bytes as newbytes
13
14
14
15
from mockredis .clock import SystemClock
15
16
from mockredis .lock import MockRedisLock
22
23
long = int
23
24
xrange = range
24
25
basestring = str
26
+ unicode = str
25
27
from functools import reduce
26
28
27
29
@@ -165,8 +167,15 @@ def keys(self, pattern='*'):
165
167
regex = fnmatch .translate (pattern )
166
168
regex = re .compile (re .sub (r'(^|[^\\])\.' , r'\1[^/]' , regex ))
167
169
170
+ keys = []
171
+
168
172
# 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
170
179
171
180
def delete (self , * keys ):
172
181
"""Emulate delete."""
@@ -1303,7 +1312,8 @@ def script_kill(self):
1303
1312
1304
1313
def script_load (self , script ):
1305
1314
"""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 ()
1307
1317
self .shas [sha_digest ] = script
1308
1318
return sha_digest
1309
1319
@@ -1521,9 +1531,9 @@ def _score_inclusive(self, score):
1521
1531
return True , float (score )
1522
1532
1523
1533
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 )
1527
1537
elif isinstance (value , (int , long )):
1528
1538
value = str (value ).encode ('utf-8' )
1529
1539
elif isinstance (value , float ):
@@ -1532,6 +1542,10 @@ def _encode(self, value):
1532
1542
value = str (value ).encode ('utf-8' )
1533
1543
else :
1534
1544
value = value .encode ('utf-8' , 'strict' )
1545
+
1546
+ if isinstance (value , bytes ):
1547
+ value = value .decode ('utf-8' , 'strict' )
1548
+
1535
1549
return value
1536
1550
1537
1551
def _log (self , level , msg ):
0 commit comments