blob: 78beda773c06cedc5c393e283318aaf02c3a3d0f [file] [log] [blame]
Guido van Rossumaa3828a1999-03-25 22:38:49 +00001import cPickle
Fred Drake694ed092001-12-19 16:42:15 +00002import unittest
Jeremy Hylton66426532001-10-15 21:38:56 +00003from cStringIO import StringIO
Neal Norwitzcdb79482006-03-24 08:58:38 +00004from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Jeremy Hylton66426532001-10-15 21:38:56 +00006
7class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
Tim Peterse0c446b2001-10-18 21:57:37 +00008
Jeremy Hylton66426532001-10-15 21:38:56 +00009 def setUp(self):
10 self.dumps = cPickle.dumps
11 self.loads = cPickle.loads
12
13 error = cPickle.BadPickleGet
14 module = cPickle
15
16class cPicklePicklerTests(AbstractPickleTests):
17
Tim Petersdcaa24e2003-01-28 22:26:28 +000018 def dumps(self, arg, proto=0):
Jeremy Hylton66426532001-10-15 21:38:56 +000019 f = StringIO()
Tim Petersdcaa24e2003-01-28 22:26:28 +000020 p = cPickle.Pickler(f, proto)
Jeremy Hylton66426532001-10-15 21:38:56 +000021 p.dump(arg)
22 f.seek(0)
23 return f.read()
24
25 def loads(self, buf):
26 f = StringIO(buf)
27 p = cPickle.Unpickler(f)
28 return p.load()
29
30 error = cPickle.BadPickleGet
31
32class cPickleListPicklerTests(AbstractPickleTests):
33
Tim Petersdcaa24e2003-01-28 22:26:28 +000034 def dumps(self, arg, proto=0):
35 p = cPickle.Pickler(proto)
Jeremy Hylton66426532001-10-15 21:38:56 +000036 p.dump(arg)
37 return p.getvalue()
38
39 def loads(self, *args):
40 f = StringIO(args[0])
41 p = cPickle.Unpickler(f)
42 return p.load()
43
44 error = cPickle.BadPickleGet
45
46class cPickleFastPicklerTests(AbstractPickleTests):
47
Tim Petersdcaa24e2003-01-28 22:26:28 +000048 def dumps(self, arg, proto=0):
Jeremy Hylton66426532001-10-15 21:38:56 +000049 f = StringIO()
Tim Petersdcaa24e2003-01-28 22:26:28 +000050 p = cPickle.Pickler(f, proto)
Jeremy Hylton66426532001-10-15 21:38:56 +000051 p.fast = 1
52 p.dump(arg)
53 f.seek(0)
54 return f.read()
55
56 def loads(self, *args):
57 f = StringIO(args[0])
58 p = cPickle.Unpickler(f)
59 return p.load()
60
61 error = cPickle.BadPickleGet
62
63 def test_recursive_list(self):
64 self.assertRaises(ValueError,
65 AbstractPickleTests.test_recursive_list,
66 self)
67
68 def test_recursive_inst(self):
69 self.assertRaises(ValueError,
70 AbstractPickleTests.test_recursive_inst,
71 self)
72
73 def test_recursive_dict(self):
74 self.assertRaises(ValueError,
75 AbstractPickleTests.test_recursive_dict,
76 self)
77
78 def test_recursive_multi(self):
79 self.assertRaises(ValueError,
80 AbstractPickleTests.test_recursive_multi,
81 self)
82
Barry Warsaw52acb492001-12-21 20:04:22 +000083 def test_nonrecursive_deep(self):
Tim Peters7107a7f2003-02-21 20:14:35 +000084 # If it's not cyclic, it should pickle OK even if the nesting
85 # depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be
86 # 50 today. Jack Jansen reported stack overflow on Mac OS 9
87 # at 64.
Barry Warsaw52acb492001-12-21 20:04:22 +000088 a = []
Tim Peters7107a7f2003-02-21 20:14:35 +000089 for i in range(60):
Barry Warsaw52acb492001-12-21 20:04:22 +000090 a = [a]
91 b = self.loads(self.dumps(a))
92 self.assertEqual(a, b)
93
Fred Drake694ed092001-12-19 16:42:15 +000094def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000095 test_support.run_unittest(
96 cPickleTests,
97 cPicklePicklerTests,
98 cPickleListPicklerTests,
99 cPickleFastPicklerTests
100 )
Fred Drake694ed092001-12-19 16:42:15 +0000101
Jeremy Hylton66426532001-10-15 21:38:56 +0000102if __name__ == "__main__":
Fred Drake694ed092001-12-19 16:42:15 +0000103 test_main()