Author: gwilson
Date: 2007-07-15 01:24:54 -0500 (Sun, 15 Jul 2007)
New Revision: 5703

Modified:
   django/trunk/AUTHORS
   django/trunk/django/core/cache/backends/locmem.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
Fixed #3012 -- Changed the locmem cache backend to use pickle instead of 
deepcopy to make it compatible with iterators (which cannot be copied).  Patch 
from Sundance.


Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2007-07-15 05:11:06 UTC (rev 5702)
+++ django/trunk/AUTHORS        2007-07-15 06:24:54 UTC (rev 5703)
@@ -237,6 +237,7 @@
     Vasiliy Stavenko <[EMAIL PROTECTED]>
     Thomas Steinacher <http://www.eggdrop.ch/>
     nowell strite
+    Sundance
     Radek Švarz <http://www.svarz.cz/translate/>
     Swaroop C H <http://www.swaroopch.info>
     Aaron Swartz <http://www.aaronsw.com/>

Modified: django/trunk/django/core/cache/backends/locmem.py
===================================================================
--- django/trunk/django/core/cache/backends/locmem.py   2007-07-15 05:11:06 UTC 
(rev 5702)
+++ django/trunk/django/core/cache/backends/locmem.py   2007-07-15 06:24:54 UTC 
(rev 5703)
@@ -2,7 +2,11 @@
 
 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
 from django.utils.synch import RWLock
-import copy, time
+import time
+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
 
 class CacheClass(SimpleCacheClass):
     def __init__(self, host, params):
@@ -20,7 +24,10 @@
             elif exp < now:
                 should_delete = True
             else:
-                return copy.deepcopy(self._cache[key])
+                try:
+                    return pickle.loads(self._cache[key])
+                except pickle.PickleError:
+                    return default
         finally:
             self._lock.reader_leaves()
         if should_delete:
@@ -35,7 +42,10 @@
     def set(self, key, value, timeout=None):
         self._lock.writer_enters()
         try:
-            SimpleCacheClass.set(self, key, value, timeout)
+            try:
+                super(CacheClass, self).set(key, pickle.dumps(value), timeout)
+            except pickle.PickleError:
+                pass
         finally:
             self._lock.writer_leaves()
 

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2007-07-15 05:11:06 UTC 
(rev 5702)
+++ django/trunk/tests/regressiontests/cache/tests.py   2007-07-15 06:24:54 UTC 
(rev 5703)
@@ -4,7 +4,7 @@
 from django.core.cache import cache
 import time, unittest
 
-# functions/classes for complex data type tests        
+# functions/classes for complex data type tests
 def f():
     return 42
 class C:
@@ -46,13 +46,12 @@
         self.assertEqual(cache.has_key("hello"), True)
         self.assertEqual(cache.has_key("goodbye"), False)
 
-    def test_in(self): 
-        cache.set("hello", "goodbye") 
-        self.assertEqual("hello" in cache, True) 
-        self.assertEqual("goodbye" in cache, False) 
+    def test_in(self):
+        cache.set("hello", "goodbye")
+        self.assertEqual("hello" in cache, True)
+        self.assertEqual("goodbye" in cache, False)
 
     def test_data_types(self):
-        # test data types
         stuff = {
             'string'    : 'this is a string',
             'int'       : 42,
@@ -61,11 +60,12 @@
             'dict'      : {'A': 1, 'B' : 2},
             'function'  : f,
             'class'     : C,
+            'iter'      : iter([1, 2 ,3]),
         }
         for (key, value) in stuff.items():
             cache.set(key, value)
             self.assertEqual(cache.get(key), value)
-    
+
     def test_expiration(self):
         # expiration
         cache.set('expire', 'very quickly', 1)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to