move sections
[python/dscho.git] / Lib / test / test_cpickle.py
blobc240669873a1a5734a00fcd78cf1151d249df0e2
1 import cPickle, unittest
2 from cStringIO import StringIO
3 from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
4 from test.pickletester import AbstractPicklerUnpicklerObjectTests
5 from test import test_support
7 class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
9 def setUp(self):
10 self.dumps = cPickle.dumps
11 self.loads = cPickle.loads
13 error = cPickle.BadPickleGet
14 module = cPickle
16 class cPicklePicklerTests(AbstractPickleTests):
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()
25 def loads(self, buf):
26 f = StringIO(buf)
27 p = cPickle.Unpickler(f)
28 return p.load()
30 error = cPickle.BadPickleGet
32 class cPickleListPicklerTests(AbstractPickleTests):
34 def dumps(self, arg, proto=0):
35 p = cPickle.Pickler(proto)
36 p.dump(arg)
37 return p.getvalue()
39 def loads(self, *args):
40 f = StringIO(args[0])
41 p = cPickle.Unpickler(f)
42 return p.load()
44 error = cPickle.BadPickleGet
46 class cPickleFastPicklerTests(AbstractPickleTests):
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()
56 def loads(self, *args):
57 f = StringIO(args[0])
58 p = cPickle.Unpickler(f)
59 return p.load()
61 error = cPickle.BadPickleGet
63 def test_recursive_list(self):
64 self.assertRaises(ValueError,
65 AbstractPickleTests.test_recursive_list,
66 self)
68 def test_recursive_tuple(self):
69 self.assertRaises(ValueError,
70 AbstractPickleTests.test_recursive_tuple,
71 self)
73 def test_recursive_inst(self):
74 self.assertRaises(ValueError,
75 AbstractPickleTests.test_recursive_inst,
76 self)
78 def test_recursive_dict(self):
79 self.assertRaises(ValueError,
80 AbstractPickleTests.test_recursive_dict,
81 self)
83 def test_recursive_multi(self):
84 self.assertRaises(ValueError,
85 AbstractPickleTests.test_recursive_multi,
86 self)
88 def test_nonrecursive_deep(self):
89 # If it's not cyclic, it should pickle OK even if the nesting
90 # depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be
91 # 50 today. Jack Jansen reported stack overflow on Mac OS 9
92 # at 64.
93 a = []
94 for i in range(60):
95 a = [a]
96 b = self.loads(self.dumps(a))
97 self.assertEqual(a, b)
99 class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
101 pickler_class = cPickle.Pickler
102 unpickler_class = cPickle.Unpickler
105 class Node(object):
106 pass
108 class cPickleDeepRecursive(unittest.TestCase):
109 def test_issue2702(self):
110 # This should raise a RecursionLimit but in some
111 # platforms (FreeBSD, win32) sometimes raises KeyError instead,
112 # or just silently terminates the interpreter (=crashes).
113 nodes = [Node() for i in range(500)]
114 for n in nodes:
115 n.connections = list(nodes)
116 n.connections.remove(n)
117 self.assertRaises((AttributeError, RuntimeError), cPickle.dumps, n)
119 def test_issue3179(self):
120 # Safe test, because I broke this case when fixing the
121 # behaviour for the previous test.
122 res=[]
123 for x in range(1,2000):
124 res.append(dict(doc=x, similar=[]))
125 cPickle.dumps(res)
128 def test_main():
129 test_support.run_unittest(
130 cPickleTests,
131 cPicklePicklerTests,
132 cPickleListPicklerTests,
133 cPickleFastPicklerTests,
134 cPickleDeepRecursive,
135 cPicklePicklerUnpicklerObjectTests,
138 if __name__ == "__main__":
139 test_main()