Home | History | Annotate | Download | only in test
      1 import cPickle
      2 import cStringIO
      3 import io
      4 import unittest
      5 from test.pickletester import (AbstractPickleTests,
      6                                AbstractPickleModuleTests,
      7                                AbstractPicklerUnpicklerObjectTests,
      8                                BigmemPickleTests)
      9 from test import test_support
     10 
     11 class cStringIOMixin:
     12     output = input = cStringIO.StringIO
     13 
     14     def close(self, f):
     15         pass
     16 
     17 class BytesIOMixin:
     18     output = input = io.BytesIO
     19 
     20     def close(self, f):
     21         pass
     22 
     23 class FileIOMixin:
     24 
     25     def output(self):
     26         return open(test_support.TESTFN, 'wb+')
     27 
     28     def input(self, data):
     29         f = open(test_support.TESTFN, 'wb+')
     30         try:
     31             f.write(data)
     32             f.seek(0)
     33             return f
     34         except:
     35             f.close()
     36             raise
     37 
     38     def close(self, f):
     39         f.close()
     40         test_support.unlink(test_support.TESTFN)
     41 
     42 
     43 class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
     44 
     45     def setUp(self):
     46         self.dumps = cPickle.dumps
     47         self.loads = cPickle.loads
     48 
     49     error = cPickle.BadPickleGet
     50     module = cPickle
     51 
     52 class cPicklePicklerTests(AbstractPickleTests):
     53 
     54     def dumps(self, arg, proto=0):
     55         f = self.output()
     56         try:
     57             p = cPickle.Pickler(f, proto)
     58             p.dump(arg)
     59             f.seek(0)
     60             return f.read()
     61         finally:
     62             self.close(f)
     63 
     64     def loads(self, buf):
     65         f = self.input(buf)
     66         try:
     67             p = cPickle.Unpickler(f)
     68             return p.load()
     69         finally:
     70             self.close(f)
     71 
     72     error = cPickle.BadPickleGet
     73 
     74 class cStringIOCPicklerTests(cStringIOMixin, cPicklePicklerTests):
     75     pass
     76 
     77 class BytesIOCPicklerTests(BytesIOMixin, cPicklePicklerTests):
     78     pass
     79 
     80 class FileIOCPicklerTests(FileIOMixin, cPicklePicklerTests):
     81     pass
     82 
     83 
     84 class cPickleListPicklerTests(AbstractPickleTests):
     85 
     86     def dumps(self, arg, proto=0):
     87         p = cPickle.Pickler(proto)
     88         p.dump(arg)
     89         return p.getvalue()
     90 
     91     def loads(self, *args):
     92         f = self.input(args[0])
     93         try:
     94             p = cPickle.Unpickler(f)
     95             return p.load()
     96         finally:
     97             self.close(f)
     98 
     99     error = cPickle.BadPickleGet
    100 
    101 class cStringIOCPicklerListTests(cStringIOMixin, cPickleListPicklerTests):
    102     pass
    103 
    104 class BytesIOCPicklerListTests(BytesIOMixin, cPickleListPicklerTests):
    105     pass
    106 
    107 class FileIOCPicklerListTests(FileIOMixin, cPickleListPicklerTests):
    108     pass
    109 
    110 
    111 class cPickleFastPicklerTests(AbstractPickleTests):
    112 
    113     def dumps(self, arg, proto=0):
    114         f = self.output()
    115         try:
    116             p = cPickle.Pickler(f, proto)
    117             p.fast = 1
    118             p.dump(arg)
    119             f.seek(0)
    120             return f.read()
    121         finally:
    122             self.close(f)
    123 
    124     def loads(self, *args):
    125         f = self.input(args[0])
    126         try:
    127             p = cPickle.Unpickler(f)
    128             return p.load()
    129         finally:
    130             self.close(f)
    131 
    132     error = cPickle.BadPickleGet
    133 
    134     def test_recursive_list(self):
    135         self.assertRaises(ValueError,
    136                           AbstractPickleTests.test_recursive_list,
    137                           self)
    138 
    139     def test_recursive_tuple(self):
    140         self.assertRaises(ValueError,
    141                           AbstractPickleTests.test_recursive_tuple,
    142                           self)
    143 
    144     def test_recursive_inst(self):
    145         self.assertRaises(ValueError,
    146                           AbstractPickleTests.test_recursive_inst,
    147                           self)
    148 
    149     def test_recursive_dict(self):
    150         self.assertRaises(ValueError,
    151                           AbstractPickleTests.test_recursive_dict,
    152                           self)
    153 
    154     def test_recursive_multi(self):
    155         self.assertRaises(ValueError,
    156                           AbstractPickleTests.test_recursive_multi,
    157                           self)
    158 
    159     def test_nonrecursive_deep(self):
    160         # If it's not cyclic, it should pickle OK even if the nesting
    161         # depth exceeds PY_CPICKLE_FAST_LIMIT.  That happens to be
    162         # 50 today.  Jack Jansen reported stack overflow on Mac OS 9
    163         # at 64.
    164         a = []
    165         for i in range(60):
    166             a = [a]
    167         b = self.loads(self.dumps(a))
    168         self.assertEqual(a, b)
    169 
    170 class cStringIOCPicklerFastTests(cStringIOMixin, cPickleFastPicklerTests):
    171     pass
    172 
    173 class BytesIOCPicklerFastTests(BytesIOMixin, cPickleFastPicklerTests):
    174     pass
    175 
    176 class FileIOCPicklerFastTests(FileIOMixin, cPickleFastPicklerTests):
    177     pass
    178 
    179 
    180 class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests):
    181 
    182     pickler_class = cPickle.Pickler
    183     unpickler_class = cPickle.Unpickler
    184 
    185 class cPickleBigmemPickleTests(BigmemPickleTests):
    186 
    187     def dumps(self, arg, proto=0, fast=0):
    188         # Ignore fast
    189         return cPickle.dumps(arg, proto)
    190 
    191     def loads(self, buf):
    192         # Ignore fast
    193         return cPickle.loads(buf)
    194 
    195 
    196 class Node(object):
    197     pass
    198 
    199 class cPickleDeepRecursive(unittest.TestCase):
    200     def test_issue2702(self):
    201         # This should raise a RecursionLimit but in some
    202         # platforms (FreeBSD, win32) sometimes raises KeyError instead,
    203         # or just silently terminates the interpreter (=crashes).
    204         nodes = [Node() for i in range(500)]
    205         for n in nodes:
    206             n.connections = list(nodes)
    207             n.connections.remove(n)
    208         self.assertRaises((AttributeError, RuntimeError), cPickle.dumps, n)
    209 
    210     def test_issue3179(self):
    211         # Safe test, because I broke this case when fixing the
    212         # behaviour for the previous test.
    213         res=[]
    214         for x in range(1,2000):
    215             res.append(dict(doc=x, similar=[]))
    216         cPickle.dumps(res)
    217 
    218 
    219 def test_main():
    220     test_support.run_unittest(
    221         cPickleTests,
    222         cStringIOCPicklerTests,
    223         BytesIOCPicklerTests,
    224         FileIOCPicklerTests,
    225         cStringIOCPicklerListTests,
    226         BytesIOCPicklerListTests,
    227         FileIOCPicklerListTests,
    228         cStringIOCPicklerFastTests,
    229         BytesIOCPicklerFastTests,
    230         FileIOCPicklerFastTests,
    231         cPickleDeepRecursive,
    232         cPicklePicklerUnpicklerObjectTests,
    233         cPickleBigmemPickleTests,
    234     )
    235 
    236 if __name__ == "__main__":
    237     test_main()
    238