annotate Lib/test/test_cpickle.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 9a7317ce6db9
children aac26de8b5d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47811
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
1 import cPickle, unittest
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
2 from cStringIO import StringIO
37195
24363301a299 Use absolute import.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28917
diff changeset
3 from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
4 from test.pickletester import AbstractPicklerUnpicklerObjectTests
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 21522
diff changeset
5 from test import test_support
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
6
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
7 class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20444
diff changeset
8
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
9 def setUp(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
10 self.dumps = cPickle.dumps
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
11 self.loads = cPickle.loads
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
12
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
13 error = cPickle.BadPickleGet
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
14 module = cPickle
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
15
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
16 class cPicklePicklerTests(AbstractPickleTests):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
17
27497
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
18 def dumps(self, arg, proto=0):
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
19 f = StringIO()
27497
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
20 p = cPickle.Pickler(f, proto)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
21 p.dump(arg)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
22 f.seek(0)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
23 return f.read()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
24
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
25 def loads(self, buf):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
26 f = StringIO(buf)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
27 p = cPickle.Unpickler(f)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
28 return p.load()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
29
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
30 error = cPickle.BadPickleGet
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
31
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
32 class cPickleListPicklerTests(AbstractPickleTests):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
33
27497
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
34 def dumps(self, arg, proto=0):
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
35 p = cPickle.Pickler(proto)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
36 p.dump(arg)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
37 return p.getvalue()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
38
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
39 def loads(self, *args):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
40 f = StringIO(args[0])
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
41 p = cPickle.Unpickler(f)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
42 return p.load()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
43
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
44 error = cPickle.BadPickleGet
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
45
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
46 class cPickleFastPicklerTests(AbstractPickleTests):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
47
27497
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
48 def dumps(self, arg, proto=0):
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
49 f = StringIO()
27497
e905b22e392b Renamed "bin" arguments to "proto". Note that this test currently
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
50 p = cPickle.Pickler(f, proto)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
51 p.fast = 1
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
52 p.dump(arg)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
53 f.seek(0)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
54 return f.read()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
55
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
56 def loads(self, *args):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
57 f = StringIO(args[0])
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
58 p = cPickle.Unpickler(f)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
59 return p.load()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
60
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
61 error = cPickle.BadPickleGet
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
62
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
63 def test_recursive_list(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
64 self.assertRaises(ValueError,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
65 AbstractPickleTests.test_recursive_list,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
66 self)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
67
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
68 def test_recursive_inst(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
69 self.assertRaises(ValueError,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
70 AbstractPickleTests.test_recursive_inst,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
71 self)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
72
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
73 def test_recursive_dict(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
74 self.assertRaises(ValueError,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
75 AbstractPickleTests.test_recursive_dict,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
76 self)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
77
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
78 def test_recursive_multi(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
79 self.assertRaises(ValueError,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
80 AbstractPickleTests.test_recursive_multi,
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
81 self)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
82
21522
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
83 def test_nonrecursive_deep(self):
28015
79ad6156d6c4 SF bug 690622: test_cpickle overflows stack on MacOS9.
Tim Peters <tim.peters@gmail.com>
parents: 27497
diff changeset
84 # If it's not cyclic, it should pickle OK even if the nesting
79ad6156d6c4 SF bug 690622: test_cpickle overflows stack on MacOS9.
Tim Peters <tim.peters@gmail.com>
parents: 27497
diff changeset
85 # depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be
79ad6156d6c4 SF bug 690622: test_cpickle overflows stack on MacOS9.
Tim Peters <tim.peters@gmail.com>
parents: 27497
diff changeset
86 # 50 today. Jack Jansen reported stack overflow on Mac OS 9
79ad6156d6c4 SF bug 690622: test_cpickle overflows stack on MacOS9.
Tim Peters <tim.peters@gmail.com>
parents: 27497
diff changeset
87 # at 64.
21522
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
88 a = []
28015
79ad6156d6c4 SF bug 690622: test_cpickle overflows stack on MacOS9.
Tim Peters <tim.peters@gmail.com>
parents: 27497
diff changeset
89 for i in range(60):
21522
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
90 a = [a]
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
91 b = self.loads(self.dumps(a))
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
92 self.assertEqual(a, b)
cb55f15b2730 Merge of the release22 branch changes back into the trunk.
Barry Warsaw <barry@python.org>
parents: 21482
diff changeset
93
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
94 class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
95
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
96 pickler_class = cPickle.Pickler
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
97 unpickler_class = cPickle.Unpickler
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
98
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
99
47811
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
100 class Node(object):
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
101 pass
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
102
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
103 class cPickleDeepRecursive(unittest.TestCase):
47856
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
104 def test_issue2702(self):
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
105 # This should raise a RecursionLimit but in some
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
106 # platforms (FreeBSD, win32) sometimes raises KeyError instead,
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
107 # or just silently terminates the interpreter (=crashes).
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
108 nodes = [Node() for i in range(500)]
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
109 for n in nodes:
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
110 n.connections = list(nodes)
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
111 n.connections.remove(n)
52275
1f72ac909408 AttributeError can be thrown during recursion errors
Benjamin Peterson <benjamin@python.org>
parents: 47856
diff changeset
112 self.assertRaises((AttributeError, RuntimeError), cPickle.dumps, n)
47828
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
113
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
114 def test_issue3179(self):
47856
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
115 # Safe test, because I broke this case when fixing the
e14739c58401 Fix #2702, with a correct accounting of recursion.
Facundo Batista <facundobatista@gmail.com>
parents: 47828
diff changeset
116 # behaviour for the previous test.
47828
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
117 res=[]
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
118 for x in range(1,2000):
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
119 res.append(dict(doc=x, similar=[]))
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
120 cPickle.dumps(res)
bde2ab943593 Reverting the patch from #3165, as it broke other
Facundo Batista <facundobatista@gmail.com>
parents: 47811
diff changeset
121
47811
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
122
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
123 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28015
diff changeset
124 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28015
diff changeset
125 cPickleTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28015
diff changeset
126 cPicklePicklerTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28015
diff changeset
127 cPickleListPicklerTests,
47811
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
128 cPickleFastPicklerTests,
a9ad99d0c722 Fixing the problem stated in issue 2702 with the patch submitted
Facundo Batista <facundobatista@gmail.com>
parents: 45468
diff changeset
129 cPickleDeepRecursive,
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 52275
diff changeset
130 cPicklePicklerUnpicklerObjectTests,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28015
diff changeset
131 )
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
132
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16133
diff changeset
133 if __name__ == "__main__":
21482
76225434a6fe Fix the test control support for the pickle & cPickle tests so the tests run
Fred Drake <fdrake@acm.org>
parents: 20547
diff changeset
134 test_main()