Skip to content

Commit

Permalink
[backends]: Consul: Do not encode, but decode a byte() object (celery…
Browse files Browse the repository at this point in the history
…#4416)

We want a string and it's already a byte() code array, so we should
decode it instead of encoding it.

While doing so fix another case where PY3 was used on the same file

To do so use kombu.utils.encoding.bytes_to_str and add a test for this

Signed-off-by: Wido den Hollander <[email protected]>
  • Loading branch information
wido authored and Omer Katz committed Dec 9, 2017
1 parent b8550a0 commit ebb99a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 5 additions & 6 deletions celery/backends/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"""
from __future__ import absolute_import, unicode_literals

from kombu.utils.encoding import bytes_to_str
from kombu.utils.url import parse_url

from celery.backends.base import PY3, KeyValueStoreBackend
from celery.backends.base import KeyValueStoreBackend
from celery.exceptions import ImproperlyConfigured
from celery.utils.log import get_logger

Expand Down Expand Up @@ -53,8 +54,7 @@ def _init_from_params(self, hostname, port, virtual_host, **params):
consistency=self.consistency)

def _key_to_consul_key(self, key):
if PY3:
key = key.encode('utf-8')
key = bytes_to_str(key)
return key if self.path is None else '{0}/{1}'.format(self.path, key)

def get(self, key):
Expand All @@ -81,9 +81,8 @@ def set(self, key, value):
If the session expires it will remove the key so that results
can auto expire from the K/V store
"""
session_name = key
if PY3:
session_name = key.decode('utf-8')
session_name = bytes_to_str(key)

key = self._key_to_consul_key(key)

logger.debug('Trying to create Consul session %s with TTL %d',
Expand Down
5 changes: 5 additions & 0 deletions t/unit/backends/test_consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ def test_get(self):
self.backend.client = Mock(name='c.client')
self.backend.client.kv.get.return_value = (index, data)
assert self.backend.get(data['Key']) == 'mypayload'

def test_index_bytes_key(self):
key = 'test-consul-2'
assert self.backend._key_to_consul_key(key) == key
assert self.backend._key_to_consul_key(key.encode('utf-8')) == key

0 comments on commit ebb99a1

Please sign in to comment.