1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #33361 -- Fixed Redis cache backend crash on booleans.

This commit is contained in:
Jeremy Lainé 2021-12-13 17:09:13 +01:00 committed by Mariusz Felisiak
parent c7902612ca
commit 2f33217ea2
3 changed files with 21 additions and 4 deletions

View File

@ -10,8 +10,14 @@ from django.utils.module_loading import import_string
class RedisSerializer(PickleSerializer):
"""
Similar to PickSerializer, except integers are serialized as native Redis
integers for better incr() and decr() atomicity.
"""
def dumps(self, obj):
if isinstance(obj, int):
# Only skip pickling for integers, a int subclasses as bool should be
# pickled.
if type(obj) is int:
return obj
return super().dumps(obj)

View File

@ -12,3 +12,6 @@ Bugfixes
* Fixed a regression in Django 4.0 that caused a crash of
:meth:`~django.test.SimpleTestCase.assertFormsetError` on a formset named
``form`` (:ticket:`33346`).
* Fixed a bug in Django 4.0 that caused a crash on booleans with the
``RedisCache`` backend (:ticket:`33361`).

14
tests/cache/tests.py vendored
View File

@ -402,17 +402,20 @@ class BaseCacheTests:
def test_data_types(self):
# Many different data types can be cached
stuff = {
tests = {
'string': 'this is a string',
'int': 42,
'bool': True,
'list': [1, 2, 3, 4],
'tuple': (1, 2, 3, 4),
'dict': {'A': 1, 'B': 2},
'function': f,
'class': C,
}
cache.set("stuff", stuff)
self.assertEqual(cache.get("stuff"), stuff)
for key, value in tests.items():
with self.subTest(key=key):
cache.set(key, value)
self.assertEqual(cache.get(key), value)
def test_cache_read_for_model_instance(self):
# Don't want fields with callable as default to be called on cache read
@ -1713,6 +1716,11 @@ class RedisCacheTests(BaseCacheTests, TestCase):
def test_get_client(self):
self.assertIsInstance(cache._cache.get_client(), self.lib.Redis)
def test_serializer_dumps(self):
self.assertEqual(cache._cache._serializer.dumps(123), 123)
self.assertIsInstance(cache._cache._serializer.dumps(True), bytes)
self.assertIsInstance(cache._cache._serializer.dumps('abc'), bytes)
class FileBasedCachePathLibTests(FileBasedCacheTests):
def mkdtemp(self):