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
):
10 self
.dumps
= cPickle
.dumps
11 self
.loads
= cPickle
.loads
13 error
= cPickle
.BadPickleGet
16 class cPicklePicklerTests(AbstractPickleTests
):
18 def dumps(self
, arg
, proto
=0):
20 p
= cPickle
.Pickler(f
, proto
)
27 p
= cPickle
.Unpickler(f
)
30 error
= cPickle
.BadPickleGet
32 class cPickleListPicklerTests(AbstractPickleTests
):
34 def dumps(self
, arg
, proto
=0):
35 p
= cPickle
.Pickler(proto
)
39 def loads(self
, *args
):
41 p
= cPickle
.Unpickler(f
)
44 error
= cPickle
.BadPickleGet
46 class cPickleFastPicklerTests(AbstractPickleTests
):
48 def dumps(self
, arg
, proto
=0):
50 p
= cPickle
.Pickler(f
, proto
)
56 def loads(self
, *args
):
58 p
= cPickle
.Unpickler(f
)
61 error
= cPickle
.BadPickleGet
63 def test_recursive_list(self
):
64 self
.assertRaises(ValueError,
65 AbstractPickleTests
.test_recursive_list
,
68 def test_recursive_tuple(self
):
69 self
.assertRaises(ValueError,
70 AbstractPickleTests
.test_recursive_tuple
,
73 def test_recursive_inst(self
):
74 self
.assertRaises(ValueError,
75 AbstractPickleTests
.test_recursive_inst
,
78 def test_recursive_dict(self
):
79 self
.assertRaises(ValueError,
80 AbstractPickleTests
.test_recursive_dict
,
83 def test_recursive_multi(self
):
84 self
.assertRaises(ValueError,
85 AbstractPickleTests
.test_recursive_multi
,
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
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
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)]
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.
123 for x
in range(1,2000):
124 res
.append(dict(doc
=x
, similar
=[]))
129 test_support
.run_unittest(
132 cPickleListPicklerTests
,
133 cPickleFastPicklerTests
,
134 cPickleDeepRecursive
,
135 cPicklePicklerUnpicklerObjectTests
,
138 if __name__
== "__main__":