Author: jezdez
Date: 2011-07-05 02:10:58 -0700 (Tue, 05 Jul 2011)
New Revision: 16511

Modified:
   django/trunk/django/core/cache/__init__.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
Fixed #16410 -- Fixed get_cache to behave gracefully when given a string that 
can't be split. Thanks, jedie.

Modified: django/trunk/django/core/cache/__init__.py
===================================================================
--- django/trunk/django/core/cache/__init__.py  2011-07-04 21:53:17 UTC (rev 
16510)
+++ django/trunk/django/core/cache/__init__.py  2011-07-05 09:10:58 UTC (rev 
16511)
@@ -126,12 +126,12 @@
         location = args.pop('LOCATION', '')
         return backend, location, args
     else:
-        # Trying to import the given backend, in case it's a dotted path
-        mod_path, cls_name = backend.rsplit('.', 1)
         try:
+            # Trying to import the given backend, in case it's a dotted path
+            mod_path, cls_name = backend.rsplit('.', 1)
             mod = importlib.import_module(mod_path)
             backend_cls = getattr(mod, cls_name)
-        except (AttributeError, ImportError):
+        except (AttributeError, ImportError, ValueError):
             raise InvalidCacheBackendError("Could not find backend '%s'" % 
backend)
         location = kwargs.pop('LOCATION', '')
         return backend, location, kwargs

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2011-07-04 21:53:17 UTC 
(rev 16510)
+++ django/trunk/tests/regressiontests/cache/tests.py   2011-07-05 09:10:58 UTC 
(rev 16511)
@@ -12,7 +12,7 @@
 from django.conf import settings
 from django.core import management
 from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
-from django.core.cache.backends.base import CacheKeyWarning
+from django.core.cache.backends.base import CacheKeyWarning, 
InvalidCacheBackendError
 from django.db import connections, router
 from django.http import HttpResponse, HttpRequest, QueryDict
 from django.middleware.cache import FetchFromCacheMiddleware, 
UpdateCacheMiddleware, CacheMiddleware
@@ -938,6 +938,23 @@
         cache.set(key, val)
         self.assertEqual(cache.get(key), val)
 
+
+class GetCacheTests(unittest.TestCase):
+
+    def test_simple(self):
+        cache = get_cache('locmem://')
+        from django.core.cache.backends.locmem import LocMemCache
+        self.assertTrue(isinstance(cache, LocMemCache))
+
+        from django.core.cache import cache
+        self.assertTrue(isinstance(cache, get_cache('default').__class__))
+
+        cache = get_cache(
+            'django.core.cache.backends.dummy.DummyCache', **{'TIMEOUT': 120})
+        self.assertEqual(cache.default_timeout, 120)
+
+        self.assertRaises(InvalidCacheBackendError, get_cache, 
'does_not_exist')
+
 class CacheUtils(unittest.TestCase):
     """TestCase for django.utils.cache functions."""
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to