From a5a456630dbd19e64c75fc86fcdf843c912faa2e Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 14:52:23 +0200 Subject: [PATCH 1/7] Use string values for django-chache keys. When django cache backend tests the key provided by django_celery_results, It wants to have str type value. byte values result in the following exception being raised: File "/opt/exaboard/eXaboard/lib/python3.6/site-packages/django/core/cache/backends/base.py", line 287, in memcache_key_warnings if ord(char) < 33 or ord(char) == 127: TypeError: ord() expected string of length 1, but int found --- django_celery_results/backends/cache.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/django_celery_results/backends/cache.py b/django_celery_results/backends/cache.py index 43c3745a..4c9387a7 100644 --- a/django_celery_results/backends/cache.py +++ b/django_celery_results/backends/cache.py @@ -1,26 +1,30 @@ """Celery cache backend using the Django Cache Framework.""" +from __future__ import absolute_import, unicode_literals +from django.core.cache import cache as default_cache, caches +from kombu.utils.encoding import bytes_to_str from celery.backends.base import KeyValueStoreBackend -from django.core.cache import cache as default_cache -from django.core.cache import caches class CacheBackend(KeyValueStoreBackend): """Backend using the Django cache framework to store task metadata.""" def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + super(CacheBackend, self).__init__(*args, **kwargs) # Must make sure backend doesn't convert exceptions to dict. self.serializer = 'pickle' def get(self, key): + key = bytes_to_str(key) return self.cache_backend.get(key) def set(self, key, value): + key = bytes_to_str(key) self.cache_backend.set(key, value, self.expires) def delete(self, key): + key = bytes_to_str(key) self.cache_backend.delete(key) def encode(self, data): From e227772915c016471d919b60e807a565187d8c4f Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 14:54:10 +0200 Subject: [PATCH 2/7] Remove old super call argument --- django_celery_results/backends/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_celery_results/backends/cache.py b/django_celery_results/backends/cache.py index 4c9387a7..60e09a1f 100644 --- a/django_celery_results/backends/cache.py +++ b/django_celery_results/backends/cache.py @@ -10,7 +10,7 @@ class CacheBackend(KeyValueStoreBackend): """Backend using the Django cache framework to store task metadata.""" def __init__(self, *args, **kwargs): - super(CacheBackend, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) # Must make sure backend doesn't convert exceptions to dict. self.serializer = 'pickle' From 21e2fa6623f3a9a89158b4bc87e0740ee9a8f6c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Oct 2021 12:57:32 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_celery_results/backends/cache.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/django_celery_results/backends/cache.py b/django_celery_results/backends/cache.py index 60e09a1f..a40e1b41 100644 --- a/django_celery_results/backends/cache.py +++ b/django_celery_results/backends/cache.py @@ -1,9 +1,9 @@ """Celery cache backend using the Django Cache Framework.""" -from __future__ import absolute_import, unicode_literals -from django.core.cache import cache as default_cache, caches -from kombu.utils.encoding import bytes_to_str from celery.backends.base import KeyValueStoreBackend +from django.core.cache import cache as default_cache +from django.core.cache import caches +from kombu.utils.encoding import bytes_to_str class CacheBackend(KeyValueStoreBackend): From 9e878965fc4b335d7f0edc820b7bb4be1d71d3b4 Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 16:26:07 +0200 Subject: [PATCH 4/7] Add test that saves key value in byte format --- t/unit/backends/test_cache.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/unit/backends/test_cache.py b/t/unit/backends/test_cache.py index 346ea971..54761721 100644 --- a/t/unit/backends/test_cache.py +++ b/t/unit/backends/test_cache.py @@ -4,6 +4,7 @@ import pytest from billiard.einfo import ExceptionInfo from celery import result, states, uuid +from kombu.utils.encoding import bytes_to_str from django_celery_results.backends.cache import CacheBackend @@ -59,6 +60,17 @@ def test_is_pickled(self): assert rindb.get('foo') == 'baz' assert rindb.get('bar').data == 12345 + def test_convert_key_from_byte_to_str(self): + """ Tests that key in byte form passed into cache are succesfully retrieved """ + tid = str_to_bytes(uuid()) + + assert self.b.get_status(tid) == states.PENDING + assert self.b.get_result(tid) is None + + self.b.mark_as_done(tid, 42) + assert self.b.get_status(tid) == states.SUCCESS + assert self.b.get_result(tid) == 42 + def test_mark_as_failure(self): einfo = None tid3 = uuid() From 66d0bf33c413fc7a5c44812e1d75d7c3e6227e48 Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 16:29:15 +0200 Subject: [PATCH 5/7] Syntax fix for test_convert_key_from_byte_to_str --- t/unit/backends/test_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/unit/backends/test_cache.py b/t/unit/backends/test_cache.py index 54761721..7026a219 100644 --- a/t/unit/backends/test_cache.py +++ b/t/unit/backends/test_cache.py @@ -60,7 +60,7 @@ def test_is_pickled(self): assert rindb.get('foo') == 'baz' assert rindb.get('bar').data == 12345 - def test_convert_key_from_byte_to_str(self): + def test_convert_key_from_byte_to_str(self): """ Tests that key in byte form passed into cache are succesfully retrieved """ tid = str_to_bytes(uuid()) From c09fa99534b031c0b9c9a4ac65e73907e278732c Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 16:31:56 +0200 Subject: [PATCH 6/7] Fixes based on flake8 failures --- t/unit/backends/test_cache.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/t/unit/backends/test_cache.py b/t/unit/backends/test_cache.py index 7026a219..78652a02 100644 --- a/t/unit/backends/test_cache.py +++ b/t/unit/backends/test_cache.py @@ -61,8 +61,9 @@ def test_is_pickled(self): assert rindb.get('bar').data == 12345 def test_convert_key_from_byte_to_str(self): - """ Tests that key in byte form passed into cache are succesfully retrieved """ - tid = str_to_bytes(uuid()) + """ Tests that key in byte form passed into cache + are succesfully completed """ + tid = bytes_to_str(uuid()) assert self.b.get_status(tid) == states.PENDING assert self.b.get_result(tid) is None @@ -70,7 +71,7 @@ def test_convert_key_from_byte_to_str(self): self.b.mark_as_done(tid, 42) assert self.b.get_status(tid) == states.SUCCESS assert self.b.get_result(tid) == 42 - + def test_mark_as_failure(self): einfo = None tid3 = uuid() From 1bdc35fa2076d4ea01943f771cb2531f4bf5b5f1 Mon Sep 17 00:00:00 2001 From: badeendjuh Date: Mon, 11 Oct 2021 16:35:55 +0200 Subject: [PATCH 7/7] Fix t/unit/backends/test_cache.py:64:50: W291 trailing whitespace --- t/unit/backends/test_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/unit/backends/test_cache.py b/t/unit/backends/test_cache.py index 78652a02..1cc70401 100644 --- a/t/unit/backends/test_cache.py +++ b/t/unit/backends/test_cache.py @@ -61,7 +61,7 @@ def test_is_pickled(self): assert rindb.get('bar').data == 12345 def test_convert_key_from_byte_to_str(self): - """ Tests that key in byte form passed into cache + """ Tests that key in byte form passed into cache are succesfully completed """ tid = bytes_to_str(uuid())