]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_cpickle.py
AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python...
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_cpickle.py
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
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_tuple(self):
69 self.assertRaises(ValueError,
70 AbstractPickleTests.test_recursive_tuple,
71 self)
72
73 def test_recursive_inst(self):
74 self.assertRaises(ValueError,
75 AbstractPickleTests.test_recursive_inst,
76 self)
77
78 def test_recursive_dict(self):
79 self.assertRaises(ValueError,
80 AbstractPickleTests.test_recursive_dict,
81 self)
82
83 def test_recursive_multi(self):
84 self.assertRaises(ValueError,
85 AbstractPickleTests.test_recursive_multi,
86 self)
87
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)
98
99 class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
100
101 pickler_class = cPickle.Pickler
102 unpickler_class = cPickle.Unpickler
103
104
105 class Node(object):
106 pass
107
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)
118
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)
126
127
128 def test_main():
129 test_support.run_unittest(
130 cPickleTests,
131 cPicklePicklerTests,
132 cPickleListPicklerTests,
133 cPickleFastPicklerTests,
134 cPickleDeepRecursive,
135 cPicklePicklerUnpicklerObjectTests,
136 )
137
138 if __name__ == "__main__":
139 test_main()