From 6f16b5bad2f714b88639ebb90d30fd6cef686cfc Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 26 Jul 2008 03:58:31 +0000 Subject: [PATCH] Fixed #7967 -- Make sure the __contains__ method in the cache backends call the right has_key() method for the subclass. Patch from Marty Alchin. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8084 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/cache/backends/base.py | 9 ++++++++- tests/regressiontests/cache/tests.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py index ff4223bf86..58e166d655 100644 --- a/django/core/cache/backends/base.py +++ b/django/core/cache/backends/base.py @@ -63,4 +63,11 @@ class BaseCache(object): """ return self.get(key) is not None - __contains__ = has_key + def __contains__(self, key): + """ + Returns True if the key is in the cache and has not expired. + """ + # This is a separate method, rather than just a copy of has_key(), + # so that it always has the same functionality as has_key(), even + # if a subclass overrides it. + return self.has_key(key) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index f050348c77..703fbe5534 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -56,11 +56,15 @@ class Cache(unittest.TestCase): cache.set("hello1", "goodbye1") self.assertEqual(cache.has_key("hello1"), True) self.assertEqual(cache.has_key("goodbye1"), False) + cache.set("empty", 'fred') + self.assertEqual(cache.has_key("empty"), True) def test_in(self): cache.set("hello2", "goodbye2") self.assertEqual("hello2" in cache, True) self.assertEqual("goodbye2" in cache, False) + cache.set("empty", 'fred') + self.assertEqual("empty" in cache, True) def test_data_types(self): stuff = {