Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | import cPickle |
2 | import unittest | |
3 | from cStringIO import StringIO | |
4 | from pickletester import AbstractPickleTests, AbstractPickleModuleTests | |
5 | from test import test_support | |
6 | ||
7 | class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests): | |
8 | ||
9 | def setUp(self): | |
10 | self.dumps = cPickle.dumps | |
11 | self.loads = cPickle.loads | |
12 | ||
13 | error = cPickle.BadPickleGet | |
14 | module = cPickle | |
15 | ||
16 | class cPicklePicklerTests(AbstractPickleTests): | |
17 | ||
18 | def dumps(self, arg, proto=0): | |
19 | f = StringIO() | |
20 | p = cPickle.Pickler(f, proto) | |
21 | 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 | ||
32 | class cPickleListPicklerTests(AbstractPickleTests): | |
33 | ||
34 | def dumps(self, arg, proto=0): | |
35 | p = cPickle.Pickler(proto) | |
36 | 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 | ||
46 | class cPickleFastPicklerTests(AbstractPickleTests): | |
47 | ||
48 | def dumps(self, arg, proto=0): | |
49 | f = StringIO() | |
50 | p = cPickle.Pickler(f, proto) | |
51 | 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 | ||
83 | def test_nonrecursive_deep(self): | |
84 | # 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. | |
88 | a = [] | |
89 | for i in range(60): | |
90 | a = [a] | |
91 | b = self.loads(self.dumps(a)) | |
92 | self.assertEqual(a, b) | |
93 | ||
94 | def test_main(): | |
95 | test_support.run_unittest( | |
96 | cPickleTests, | |
97 | cPicklePicklerTests, | |
98 | cPickleListPicklerTests, | |
99 | cPickleFastPicklerTests | |
100 | ) | |
101 | ||
102 | if __name__ == "__main__": | |
103 | test_main() |