Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 1 | import cPickle |
| 2 | import cStringIO |
| 3 | import io |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 4 | import functools |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 5 | import unittest |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 6 | from test.pickletester import (AbstractUnpickleTests, |
| 7 | AbstractPickleTests, |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 8 | AbstractPickleModuleTests, |
| 9 | AbstractPicklerUnpicklerObjectTests, |
| 10 | BigmemPickleTests) |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 11 | from test import test_support |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 12 | |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 13 | class cStringIOMixin: |
| 14 | output = input = cStringIO.StringIO |
| 15 | |
| 16 | def close(self, f): |
| 17 | pass |
| 18 | |
| 19 | class BytesIOMixin: |
| 20 | output = input = io.BytesIO |
| 21 | |
| 22 | def close(self, f): |
| 23 | pass |
| 24 | |
| 25 | class FileIOMixin: |
| 26 | |
| 27 | def output(self): |
Benjamin Peterson | bebf75f | 2013-03-17 21:28:29 -0700 | [diff] [blame] | 28 | return open(test_support.TESTFN, 'wb+') |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 29 | |
| 30 | def input(self, data): |
Benjamin Peterson | bebf75f | 2013-03-17 21:28:29 -0700 | [diff] [blame] | 31 | f = open(test_support.TESTFN, 'wb+') |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 32 | try: |
| 33 | f.write(data) |
| 34 | f.seek(0) |
| 35 | return f |
| 36 | except: |
| 37 | f.close() |
| 38 | raise |
| 39 | |
| 40 | def close(self, f): |
| 41 | f.close() |
| 42 | test_support.unlink(test_support.TESTFN) |
| 43 | |
| 44 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 45 | class cPickleTests(AbstractUnpickleTests, AbstractPickleTests, |
| 46 | AbstractPickleModuleTests): |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 47 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 48 | def setUp(self): |
| 49 | self.dumps = cPickle.dumps |
| 50 | self.loads = cPickle.loads |
| 51 | |
| 52 | error = cPickle.BadPickleGet |
| 53 | module = cPickle |
| 54 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 55 | class cPickleUnpicklerTests(AbstractUnpickleTests): |
| 56 | |
| 57 | def loads(self, buf): |
| 58 | f = self.input(buf) |
| 59 | try: |
| 60 | p = cPickle.Unpickler(f) |
| 61 | return p.load() |
| 62 | finally: |
| 63 | self.close(f) |
| 64 | |
| 65 | error = cPickle.BadPickleGet |
| 66 | |
| 67 | class cStringIOCUnpicklerTests(cStringIOMixin, cPickleUnpicklerTests): |
| 68 | pass |
| 69 | |
| 70 | class BytesIOCUnpicklerTests(BytesIOMixin, cPickleUnpicklerTests): |
| 71 | pass |
| 72 | |
| 73 | class FileIOCUnpicklerTests(FileIOMixin, cPickleUnpicklerTests): |
| 74 | pass |
| 75 | |
| 76 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 77 | class cPicklePicklerTests(AbstractPickleTests): |
| 78 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 79 | def dumps(self, arg, proto=0): |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 80 | f = self.output() |
| 81 | try: |
| 82 | p = cPickle.Pickler(f, proto) |
| 83 | p.dump(arg) |
| 84 | f.seek(0) |
| 85 | return f.read() |
| 86 | finally: |
| 87 | self.close(f) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 88 | |
| 89 | def loads(self, buf): |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 90 | f = self.input(buf) |
| 91 | try: |
| 92 | p = cPickle.Unpickler(f) |
| 93 | return p.load() |
| 94 | finally: |
| 95 | self.close(f) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 96 | |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 97 | class cStringIOCPicklerTests(cStringIOMixin, cPicklePicklerTests): |
| 98 | pass |
| 99 | |
| 100 | class BytesIOCPicklerTests(BytesIOMixin, cPicklePicklerTests): |
| 101 | pass |
| 102 | |
| 103 | class FileIOCPicklerTests(FileIOMixin, cPicklePicklerTests): |
| 104 | pass |
| 105 | |
| 106 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 107 | class cPickleListPicklerTests(AbstractPickleTests): |
| 108 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 109 | def dumps(self, arg, proto=0): |
| 110 | p = cPickle.Pickler(proto) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 111 | p.dump(arg) |
| 112 | return p.getvalue() |
| 113 | |
| 114 | def loads(self, *args): |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 115 | f = self.input(args[0]) |
| 116 | try: |
| 117 | p = cPickle.Unpickler(f) |
| 118 | return p.load() |
| 119 | finally: |
| 120 | self.close(f) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 121 | |
| 122 | error = cPickle.BadPickleGet |
| 123 | |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 124 | class cStringIOCPicklerListTests(cStringIOMixin, cPickleListPicklerTests): |
| 125 | pass |
| 126 | |
| 127 | class BytesIOCPicklerListTests(BytesIOMixin, cPickleListPicklerTests): |
| 128 | pass |
| 129 | |
| 130 | class FileIOCPicklerListTests(FileIOMixin, cPickleListPicklerTests): |
| 131 | pass |
| 132 | |
| 133 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 134 | class cPickleFastPicklerTests(AbstractPickleTests): |
| 135 | |
Tim Peters | dcaa24e | 2003-01-28 22:26:28 +0000 | [diff] [blame] | 136 | def dumps(self, arg, proto=0): |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 137 | f = self.output() |
| 138 | try: |
| 139 | p = cPickle.Pickler(f, proto) |
| 140 | p.fast = 1 |
| 141 | p.dump(arg) |
| 142 | f.seek(0) |
| 143 | return f.read() |
| 144 | finally: |
| 145 | self.close(f) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 146 | |
| 147 | def loads(self, *args): |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 148 | f = self.input(args[0]) |
| 149 | try: |
| 150 | p = cPickle.Unpickler(f) |
| 151 | return p.load() |
| 152 | finally: |
| 153 | self.close(f) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 154 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 155 | def test_nonrecursive_deep(self): |
Tim Peters | 7107a7f | 2003-02-21 20:14:35 +0000 | [diff] [blame] | 156 | # If it's not cyclic, it should pickle OK even if the nesting |
| 157 | # depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be |
| 158 | # 50 today. Jack Jansen reported stack overflow on Mac OS 9 |
| 159 | # at 64. |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 160 | a = [] |
Tim Peters | 7107a7f | 2003-02-21 20:14:35 +0000 | [diff] [blame] | 161 | for i in range(60): |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 162 | a = [a] |
| 163 | b = self.loads(self.dumps(a)) |
| 164 | self.assertEqual(a, b) |
| 165 | |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 166 | for name in dir(AbstractPickleTests): |
| 167 | if name.startswith('test_recursive_'): |
| 168 | func = getattr(AbstractPickleTests, name) |
| 169 | if '_subclass' in name and '_and_inst' not in name: |
| 170 | assert_args = RuntimeError, 'maximum recursion depth exceeded' |
| 171 | else: |
| 172 | assert_args = ValueError, "can't pickle cyclic objects" |
| 173 | def wrapper(self, func=func, assert_args=assert_args): |
| 174 | with self.assertRaisesRegexp(*assert_args): |
| 175 | func(self) |
| 176 | functools.update_wrapper(wrapper, func) |
| 177 | setattr(cPickleFastPicklerTests, name, wrapper) |
| 178 | |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 179 | class cStringIOCPicklerFastTests(cStringIOMixin, cPickleFastPicklerTests): |
| 180 | pass |
| 181 | |
| 182 | class BytesIOCPicklerFastTests(BytesIOMixin, cPickleFastPicklerTests): |
| 183 | pass |
| 184 | |
| 185 | class FileIOCPicklerFastTests(FileIOMixin, cPickleFastPicklerTests): |
| 186 | pass |
| 187 | |
| 188 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 189 | class cPicklePicklerUnpicklerObjectTests(AbstractPicklerUnpicklerObjectTests): |
| 190 | |
| 191 | pickler_class = cPickle.Pickler |
| 192 | unpickler_class = cPickle.Unpickler |
| 193 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 194 | class cPickleBigmemPickleTests(BigmemPickleTests): |
| 195 | |
| 196 | def dumps(self, arg, proto=0, fast=0): |
| 197 | # Ignore fast |
| 198 | return cPickle.dumps(arg, proto) |
| 199 | |
| 200 | def loads(self, buf): |
| 201 | # Ignore fast |
| 202 | return cPickle.loads(buf) |
| 203 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 204 | |
Facundo Batista | 9da18b3 | 2008-06-22 23:19:14 +0000 | [diff] [blame] | 205 | class Node(object): |
| 206 | pass |
| 207 | |
| 208 | class cPickleDeepRecursive(unittest.TestCase): |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 209 | def test_issue2702(self): |
| 210 | # This should raise a RecursionLimit but in some |
| 211 | # platforms (FreeBSD, win32) sometimes raises KeyError instead, |
| 212 | # or just silently terminates the interpreter (=crashes). |
| 213 | nodes = [Node() for i in range(500)] |
| 214 | for n in nodes: |
| 215 | n.connections = list(nodes) |
| 216 | n.connections.remove(n) |
Benjamin Peterson | 704dc26 | 2009-03-22 22:24:58 +0000 | [diff] [blame] | 217 | self.assertRaises((AttributeError, RuntimeError), cPickle.dumps, n) |
Facundo Batista | 68dc052 | 2008-06-25 19:24:53 +0000 | [diff] [blame] | 218 | |
| 219 | def test_issue3179(self): |
Facundo Batista | 763d309 | 2008-06-30 01:10:55 +0000 | [diff] [blame] | 220 | # Safe test, because I broke this case when fixing the |
| 221 | # behaviour for the previous test. |
Facundo Batista | 68dc052 | 2008-06-25 19:24:53 +0000 | [diff] [blame] | 222 | res=[] |
| 223 | for x in range(1,2000): |
| 224 | res.append(dict(doc=x, similar=[])) |
| 225 | cPickle.dumps(res) |
| 226 | |
Facundo Batista | 9da18b3 | 2008-06-22 23:19:14 +0000 | [diff] [blame] | 227 | |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 228 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 229 | test_support.run_unittest( |
| 230 | cPickleTests, |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 231 | cStringIOCUnpicklerTests, |
| 232 | BytesIOCUnpicklerTests, |
| 233 | FileIOCUnpicklerTests, |
Serhiy Storchaka | 2556c83 | 2013-03-14 20:59:09 +0200 | [diff] [blame] | 234 | cStringIOCPicklerTests, |
| 235 | BytesIOCPicklerTests, |
| 236 | FileIOCPicklerTests, |
| 237 | cStringIOCPicklerListTests, |
| 238 | BytesIOCPicklerListTests, |
| 239 | FileIOCPicklerListTests, |
| 240 | cStringIOCPicklerFastTests, |
| 241 | BytesIOCPicklerFastTests, |
| 242 | FileIOCPicklerFastTests, |
Facundo Batista | 9da18b3 | 2008-06-22 23:19:14 +0000 | [diff] [blame] | 243 | cPickleDeepRecursive, |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 244 | cPicklePicklerUnpicklerObjectTests, |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 245 | cPickleBigmemPickleTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 246 | ) |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 247 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 248 | if __name__ == "__main__": |
Fred Drake | 694ed09 | 2001-12-19 16:42:15 +0000 | [diff] [blame] | 249 | test_main() |