PageRenderTime 253ms CodeModel.GetById 90ms app.highlight 11ms RepoModel.GetById 150ms app.codeStats 0ms

/Lib/test/test_cpickle.py

http://unladen-swallow.googlecode.com/
Python | 141 lines | 126 code | 13 blank | 2 comment | 0 complexity | 7b99ae1db4819f71b3e21b280581dda3 MD5 | raw file
  1import cPickle, unittest
  2from cStringIO import StringIO
  3from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
  4from test.pickletester import AbstractPicklerUnpicklerObjectTests
  5from test import test_support
  6
  7class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
  8
  9    def setUp(self):
 10        self.dumps = cPickle.dumps
 11        self.loads = cPickle.loads
 12        self.load = cPickle.load
 13        self.dump = cPickle.dump
 14
 15    error = cPickle.BadPickleGet
 16    module = cPickle
 17
 18class cPickleAbstractPicklerTests(AbstractPickleTests):
 19
 20    """Common methods for TestCase classes testing cPickle.Pickler variants."""
 21
 22    error = cPickle.BadPickleGet
 23
 24    def loads(self, buf):
 25        f = StringIO(buf)
 26        p = cPickle.Unpickler(f)
 27        return p.load()
 28
 29    def load(self, buf):
 30        p = cPickle.Unpickler(buf)
 31        return p.load()
 32
 33class cPickleFilePicklerTests(cPickleAbstractPicklerTests):
 34
 35    """Test pickling to a file"""
 36
 37    def dumps(self, arg, proto=0):
 38        f = StringIO()
 39        p = cPickle.Pickler(f, proto)
 40        p.dump(arg)
 41        f.seek(0)
 42        return f.read()
 43
 44class cPickleInternalBufferPicklerTests(cPickleAbstractPicklerTests):
 45
 46    """Test pickling to the Pickler's internal buffer."""
 47
 48    def dumps(self, arg, proto=0):
 49        p = cPickle.Pickler(proto)
 50        p.dump(arg)
 51        return p.getvalue()
 52
 53class cPickleFastPicklerTests(cPickleAbstractPicklerTests):
 54
 55    """Test pickling in fast mode (ie, no recursive data structures)."""
 56
 57    def dumps(self, arg, proto=0):
 58        f = StringIO()
 59        p = cPickle.Pickler(f, proto)
 60        p.fast = True
 61        p.dump(arg)
 62        f.seek(0)
 63        return f.read()
 64
 65    def test_recursive_list(self):
 66        self.assertRaises(ValueError,
 67                          AbstractPickleTests.test_recursive_list,
 68                          self)
 69
 70    def test_recursive_tuple(self):
 71        self.assertRaises(ValueError,
 72                          AbstractPickleTests.test_recursive_tuple,
 73                          self)
 74
 75    def test_recursive_inst(self):
 76        self.assertRaises(ValueError,
 77                          AbstractPickleTests.test_recursive_inst,
 78                          self)
 79
 80    def test_recursive_dict(self):
 81        self.assertRaises(ValueError,
 82                          AbstractPickleTests.test_recursive_dict,
 83                          self)
 84
 85    def test_recursive_multi(self):
 86        self.assertRaises(ValueError,
 87                          AbstractPickleTests.test_recursive_multi,
 88                          self)
 89
 90    def test_nonrecursive_deep(self):
 91        # If it's not cyclic, it should pickle OK even if the nesting
 92        # depth exceeds PY_CPICKLE_FAST_LIMIT.  That happens to be
 93        # 50 today.  Jack Jansen reported stack overflow on Mac OS 9
 94        # at 64.
 95        a = []
 96        for i in range(60):
 97            a = [a]
 98        b = self.loads(self.dumps(a))
 99        self.assertEqual(a, b)
100
101class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
102
103    pickler_class = cPickle.Pickler
104    unpickler_class = cPickle.Unpickler
105
106
107class Node(object):
108    pass
109
110class cPickleDeepRecursive(unittest.TestCase):
111    def test_issue2702(self):
112        # This should raise a RecursionLimit but in some
113        # platforms (FreeBSD, win32) sometimes raises KeyError instead,
114        # or just silently terminates the interpreter (=crashes).
115        nodes = [Node() for i in range(500)]
116        for n in nodes:
117            n.connections = list(nodes)
118            n.connections.remove(n)
119        self.assertRaises(RuntimeError, cPickle.dumps, n)
120
121    def test_issue3179(self):
122        # Safe test, because I broke this case when fixing the
123        # behaviour for the previous test.
124        res=[]
125        for x in range(1,2000):
126            res.append(dict(doc=x, similar=[]))
127        cPickle.dumps(res)
128
129
130def test_main():
131    test_support.run_unittest(
132        cPickleTests,
133        cPickleFilePicklerTests,
134        cPickleInternalBufferPicklerTests,
135        cPickleFastPicklerTests,
136        cPickleDeepRecursive,
137        cPicklePicklerUnpicklerObjectTests,
138    )
139
140if __name__ == "__main__":
141    test_main()