Hi Raphael,

Yes, SimpleCookie is known to be an unpickleable class. We shouldn't
be directly pickling it anywhere in Django. In your code, you should
probably turn the cookie into a string before caching it. I'm not
clear if the bug you're experiencing is happening in Django's code or
something your application is doing directly with SimpleCookie.

The last memcached backend I looked at pickled things using the
highest protocol. I don't remember which one it was. It's very odd
that you're not seeing the same error using memcached.

Locmem should use the highest version of pickle, to more closely
mirror the behavior of memcached. I'll open a ticket about that.
Django shouldn't switch to the earlier versions of pickle for
performance reasons.

I think that your provided test case is trying to do something that is
explicitly not supported, but I'm unclear on whether or not there is
an issue in Django-provided code. Could you provide a little more
information?

Thanks,
-Paul

On Fri, Apr 15, 2011 at 1:23 PM, Raphael Kubo da Costa
<[email protected]> wrote:
> 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.
>
>

-- 
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.

Reply via email to