PyXR

c:\python24\lib \ test \ test_cpickle.py



0001 import cPickle
0002 import unittest
0003 from cStringIO import StringIO
0004 from pickletester import AbstractPickleTests, AbstractPickleModuleTests
0005 from test import test_support
0006 
0007 class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
0008 
0009     def setUp(self):
0010         self.dumps = cPickle.dumps
0011         self.loads = cPickle.loads
0012 
0013     error = cPickle.BadPickleGet
0014     module = cPickle
0015 
0016 class cPicklePicklerTests(AbstractPickleTests):
0017 
0018     def dumps(self, arg, proto=0):
0019         f = StringIO()
0020         p = cPickle.Pickler(f, proto)
0021         p.dump(arg)
0022         f.seek(0)
0023         return f.read()
0024 
0025     def loads(self, buf):
0026         f = StringIO(buf)
0027         p = cPickle.Unpickler(f)
0028         return p.load()
0029 
0030     error = cPickle.BadPickleGet
0031 
0032 class cPickleListPicklerTests(AbstractPickleTests):
0033 
0034     def dumps(self, arg, proto=0):
0035         p = cPickle.Pickler(proto)
0036         p.dump(arg)
0037         return p.getvalue()
0038 
0039     def loads(self, *args):
0040         f = StringIO(args[0])
0041         p = cPickle.Unpickler(f)
0042         return p.load()
0043 
0044     error = cPickle.BadPickleGet
0045 
0046 class cPickleFastPicklerTests(AbstractPickleTests):
0047 
0048     def dumps(self, arg, proto=0):
0049         f = StringIO()
0050         p = cPickle.Pickler(f, proto)
0051         p.fast = 1
0052         p.dump(arg)
0053         f.seek(0)
0054         return f.read()
0055 
0056     def loads(self, *args):
0057         f = StringIO(args[0])
0058         p = cPickle.Unpickler(f)
0059         return p.load()
0060 
0061     error = cPickle.BadPickleGet
0062 
0063     def test_recursive_list(self):
0064         self.assertRaises(ValueError,
0065                           AbstractPickleTests.test_recursive_list,
0066                           self)
0067 
0068     def test_recursive_inst(self):
0069         self.assertRaises(ValueError,
0070                           AbstractPickleTests.test_recursive_inst,
0071                           self)
0072 
0073     def test_recursive_dict(self):
0074         self.assertRaises(ValueError,
0075                           AbstractPickleTests.test_recursive_dict,
0076                           self)
0077 
0078     def test_recursive_multi(self):
0079         self.assertRaises(ValueError,
0080                           AbstractPickleTests.test_recursive_multi,
0081                           self)
0082 
0083     def test_nonrecursive_deep(self):
0084         # If it's not cyclic, it should pickle OK even if the nesting
0085         # depth exceeds PY_CPICKLE_FAST_LIMIT.  That happens to be
0086         # 50 today.  Jack Jansen reported stack overflow on Mac OS 9
0087         # at 64.
0088         a = []
0089         for i in range(60):
0090             a = [a]
0091         b = self.loads(self.dumps(a))
0092         self.assertEqual(a, b)
0093 
0094 def test_main():
0095     test_support.run_unittest(
0096         cPickleTests,
0097         cPicklePicklerTests,
0098         cPickleListPicklerTests,
0099         cPickleFastPicklerTests
0100     )
0101 
0102 if __name__ == "__main__":
0103     test_main()
0104 

Generated by PyXR 0.9.4
SourceForge.net Logo