Skip to content

Commit

Permalink
Resolve a redis-py bug with decode_responses=True
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed Dec 16, 2015
1 parent 31316e0 commit dfd271c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions limpyd/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ def has_scripting(self):
except:
self._has_scripting = False
return self._has_scripting


class Lock(redis.client.Lock):
"""
Override the default Lock class to manage the fact that we use ``decode_responses=True``
but it is not taken into account in python 3 with redis 2.10, as the lock token in
``threading.local`` is saved as ``bytes`` but the value retrieved from redis is decoded.
So the equal check doesn't work.
So we decode the value in ``threading.local`` to make this check work.
See https://github.com/andymccurdy/redis-py/issues/694
"""

def do_release(self, expected_token):

if isinstance(expected_token, bytes) and \
self.redis.connection_pool.connection_kwargs.get('decode_responses', False):
expected_token = expected_token.decode()

super(Lock, self).do_release(expected_token)
2 changes: 1 addition & 1 deletion limpyd/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from copy import copy

from redis.exceptions import RedisError
from redis.client import Lock

from limpyd.database import Lock
from limpyd.utils import make_key, normalize
from limpyd.exceptions import *

Expand Down

0 comments on commit dfd271c

Please sign in to comment.