Hello there,
I was experiencing some problems with Django's caching system on 1.2.X
(1.2.5, to be more specific) when using either the database or the
file-based backends.
Both use pickle and call pickle.dumps(..., pickle.HIGHEST_PROTOCOL) when
serializing the data after being called by UpdateCacheMiddleware.
However, it looks like pickle does not play nice with SimpleCookie-based
classes [1]. In the first request (still not cached), the csrf token is
set correctly as follows:
Set-Cookie: csrftoken=XX; Max-Age=31449600; Path=/
When the same view is requested again, though, the cookie is retrieved
incorrectly from the cache by FetchFromCacheMiddleware:
Set-Cookie: csrftoken="Set-Cookie: csrftoken=XX Max-Age=31449600\073 Path=/"
The locmem, dummy and memcached backends do not present this problem:
locmem does not specify the protocol version when calling pickle.dumps,
which means protocol version 0 will be used; dummy does not do anything
and memcached does not use pickle. Pickle protocol versions 0 and 1 work
fine.
The following patch to the unit tests should break both
FileBasedCacheTests and DBCacheTests. I only tested it against the 1.2.X
git branch, but 1.3.X should present the same behaviour, and both Python
2.7.1 and Python 3.2 fail.
diff --git a/tests/regressiontests/cache/tests.py
b/tests/regressiontests/cache/tests.py
index 0581e4e..5611eef 100644
--- a/tests/regressiontests/cache/tests.py
+++ b/tests/regressiontests/cache/tests.py
@@ -285,6 +285,22 @@ class BaseCacheTests(object):
self.assertEqual(self.cache.get("expire2"), "newvalue")
self.assertEqual(self.cache.has_key("expire3"), False)
+ def test_cookie_caching(self):
+ try:
+ from Cookie import SimpleCookie
+ except ImportError:
+ from http.cookies import SimpleCookie
+
+ test_cookie = SimpleCookie()
+ test_cookie['key'] = 'some value'
+
+ self.cache.set('some_cookie', test_cookie)
+
+ cached_cookie = self.cache.get('some_cookie')
+
+ self.assertEqual(cached_cookie['key'].value,
+ test_cookie['key'].value)
+
def test_unicode(self):
# Unicode values can be cached
stuff = {
[1] http://bugs.python.org/issue826897
--
Raphael Kubo da Costa
ProFUSION embedded systems
http://profusion.mobi
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.