From 82be474efa81e5f2e127d711045a2ec06b206a8e Mon Sep 17 00:00:00 2001 From: "Dmitry S..ky / skype: dvska-at-skype" Date: Wed, 22 Jun 2016 21:28:43 +0300 Subject: [PATCH] Fixed #26792 -- Allowed None for the value of cache.get_or_set(). --- django/core/cache/backends/base.py | 6 ++---- tests/cache/tests.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py index a07a34e3b5..9cbfe0be1d 100644 --- a/django/core/cache/backends/base.py +++ b/django/core/cache/backends/base.py @@ -147,7 +147,7 @@ class BaseCache(object): d[k] = val return d - def get_or_set(self, key, default=None, timeout=DEFAULT_TIMEOUT, version=None): + def get_or_set(self, key, default, timeout=DEFAULT_TIMEOUT, version=None): """ Fetch a given key from the cache. If the key does not exist, the key is added and set to the default value. The default value can @@ -156,10 +156,8 @@ class BaseCache(object): Return the value of the key stored or retrieved. """ - if default is None: - raise ValueError('You need to specify a value.') val = self.get(key, version=version) - if val is None: + if val is None and default is not None: if callable(default): default = default() self.add(key, default, timeout=timeout, version=version) diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 9099402f43..dc4c69cd43 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -213,12 +213,14 @@ class DummyCacheTests(SimpleTestCase): def test_get_or_set(self): self.assertEqual(cache.get_or_set('mykey', 'default'), 'default') + self.assertEqual(cache.get_or_set('mykey', None), None) def test_get_or_set_callable(self): def my_callable(): return 'default' self.assertEqual(cache.get_or_set('mykey', my_callable), 'default') + self.assertEqual(cache.get_or_set('mykey', my_callable()), 'default') def custom_key_func(key, key_prefix, version): @@ -918,18 +920,25 @@ class BaseCacheTests(object): self.assertIsNone(cache.get('projector')) self.assertEqual(cache.get_or_set('projector', 42), 42) self.assertEqual(cache.get('projector'), 42) + self.assertEqual(cache.get_or_set('null', None), None) def test_get_or_set_callable(self): def my_callable(): return 'value' self.assertEqual(cache.get_or_set('mykey', my_callable), 'value') + self.assertEqual(cache.get_or_set('mykey', my_callable()), 'value') def test_get_or_set_version(self): + msg = ( + "get_or_set() missing 1 required positional argument: 'default'" + if six.PY3 + else 'get_or_set() takes at least 3 arguments' + ) cache.get_or_set('brian', 1979, version=2) - with self.assertRaisesMessage(ValueError, 'You need to specify a value.'): + with self.assertRaisesMessage(TypeError, msg): cache.get_or_set('brian') - with self.assertRaisesMessage(ValueError, 'You need to specify a value.'): + with self.assertRaisesMessage(TypeError, msg): cache.get_or_set('brian', version=1) self.assertIsNone(cache.get('brian', version=1)) self.assertEqual(cache.get_or_set('brian', 42, version=1), 42)