[py-svn] r57097 - in py/branch/event/py/test2/rsession: . testing

hpk at codespeak.net hpk at codespeak.net
Fri Aug 8 13:29:19 CEST 2008


Author: hpk
Date: Fri Aug  8 13:29:18 2008
New Revision: 57097

Modified:
   py/branch/event/py/test2/rsession/pickle.py
   py/branch/event/py/test2/rsession/testing/test_pickle.py
Log:
allow for protocols other than 0, extend hack


Modified: py/branch/event/py/test2/rsession/pickle.py
==============================================================================
--- py/branch/event/py/test2/rsession/pickle.py	(original)
+++ py/branch/event/py/test2/rsession/pickle.py	Fri Aug  8 13:29:18 2008
@@ -1,20 +1,26 @@
 
-from cPickle import Pickler, Unpickler
 from cStringIO import StringIO
+from cPickle import Pickler, Unpickler
 
 class ImmutablePickler:
-    def __init__(self):
+    def __init__(self, protocol=0):
         self.picklememo = {}
         self.unpicklememo = {}
+        self.protocol = protocol
 
     def dumps(self, obj):
         f = StringIO()
-        pickler = Pickler(f)
+        pickler = Pickler(f, self.protocol)
         pickler.memo = self.picklememo
         pickler.dump(obj)
-        self.unpicklememo.update(dict(
+        if self.protocol == 0:
+            self.unpicklememo.update(dict(
                 [(str(x), y) 
-                    for x, y in self.picklememo.values()]))
+                    for x, y in self.picklememo.itervalues()]))
+        else:
+            self.unpicklememo.update(dict(
+                [(x, y) 
+                    for x, y in self.picklememo.itervalues()]))
         return f.getvalue()
 
     def loads(self, string):

Modified: py/branch/event/py/test2/rsession/testing/test_pickle.py
==============================================================================
--- py/branch/event/py/test2/rsession/testing/test_pickle.py	(original)
+++ py/branch/event/py/test2/rsession/testing/test_pickle.py	Fri Aug  8 13:29:18 2008
@@ -6,10 +6,10 @@
     pass
 
 def test_pickle_and_back_IS_same():
-    p1 = ImmutablePickler()
-    p2 = ImmutablePickler()
 
-    def check(obj):
+    def pickle_band_back_IS_same(obj, proto):
+        p1 = ImmutablePickler(protocol=proto)
+        p2 = ImmutablePickler(protocol=proto)
         s1 = p1.dumps(obj)
         d2 = p2.loads(s1)
         s2 = p2.dumps(d2)
@@ -19,5 +19,6 @@
     a1 = A()
     a2 = A()
     a2.a1 = a1
-    for obj in {1:2}, [1,2,3], a1, a2:
-        yield check, obj 
+    for proto in 0,1,2,-1:
+        for obj in {1:2}, [1,2,3], a1, a2:
+            yield pickle_band_back_IS_same, obj, proto



More information about the pytest-commit mailing list