Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1 | import unittest |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 2 | import pickle |
Tim Peters | 31f119e | 2003-02-03 16:20:13 +0000 | [diff] [blame] | 3 | import pickletools |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 4 | import copy_reg |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 6 | from test.test_support import TestFailed, have_unicode, TESTFN |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 7 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 8 | # Tests that try a number of pickle protocols should have a |
| 9 | # for proto in protocols: |
| 10 | # kind of outer loop. Bump the 3 to 4 if/when protocol 3 is invented. |
| 11 | protocols = range(3) |
| 12 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 13 | |
| 14 | # Return True if opcode code appears in the pickle, else False. |
| 15 | def opcode_in_pickle(code, pickle): |
| 16 | for op, dummy, dummy in pickletools.genops(pickle): |
| 17 | if op.code == code: |
| 18 | return True |
| 19 | return False |
| 20 | |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 21 | # We can't very well test the extension registry without putting known stuff |
| 22 | # in it, but we have to be careful to restore its original state. Code |
| 23 | # should do this: |
| 24 | # |
| 25 | # e = ExtensionSaver(extension_code) |
| 26 | # try: |
| 27 | # fiddle w/ the extension registry's stuff for extension_code |
| 28 | # finally: |
| 29 | # e.restore() |
| 30 | |
| 31 | class ExtensionSaver: |
| 32 | # Remember current registration for code (if any), and remove it (if |
| 33 | # there is one). |
| 34 | def __init__(self, code): |
| 35 | self.code = code |
| 36 | if code in copy_reg._inverted_registry: |
| 37 | self.pair = copy_reg._inverted_registry[code] |
| 38 | copy_reg.remove_extension(self.pair[0], self.pair[1], code) |
| 39 | else: |
| 40 | self.pair = None |
| 41 | |
| 42 | # Restore previous registration for code. |
| 43 | def restore(self): |
| 44 | code = self.code |
| 45 | curpair = copy_reg._inverted_registry.get(code) |
| 46 | if curpair is not None: |
| 47 | copy_reg.remove_extension(curpair[0], curpair[1], code) |
| 48 | pair = self.pair |
| 49 | if pair is not None: |
| 50 | copy_reg.add_extension(pair[0], pair[1], code) |
| 51 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 52 | class C: |
| 53 | def __cmp__(self, other): |
| 54 | return cmp(self.__dict__, other.__dict__) |
| 55 | |
| 56 | import __main__ |
| 57 | __main__.C = C |
| 58 | C.__module__ = "__main__" |
| 59 | |
| 60 | class myint(int): |
| 61 | def __init__(self, x): |
| 62 | self.str = str(x) |
| 63 | |
| 64 | class initarg(C): |
Guido van Rossum | 1444f67 | 2001-12-19 16:38:29 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 66 | def __init__(self, a, b): |
| 67 | self.a = a |
| 68 | self.b = b |
| 69 | |
| 70 | def __getinitargs__(self): |
| 71 | return self.a, self.b |
| 72 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 73 | class metaclass(type): |
| 74 | pass |
| 75 | |
| 76 | class use_metaclass(object): |
| 77 | __metaclass__ = metaclass |
| 78 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 79 | # DATA0 .. DATA2 are the pickles we expect under the various protocols, for |
| 80 | # the object returned by create_data(). |
| 81 | # XXX DATA2 doesn't exist yet, as it's not fully implemented in cPickle. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 82 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 83 | # break into multiple strings to avoid confusing font-lock-mode |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 84 | DATA0 = """(lp1 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 85 | I0 |
| 86 | aL1L |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 87 | aF2 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 88 | ac__builtin__ |
| 89 | complex |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 90 | p2 |
| 91 | """ + \ |
| 92 | """(F3 |
| 93 | F0 |
| 94 | tRp3 |
| 95 | aI1 |
| 96 | aI-1 |
| 97 | aI255 |
| 98 | aI-255 |
| 99 | aI-256 |
| 100 | aI65535 |
| 101 | aI-65535 |
| 102 | aI-65536 |
| 103 | aI2147483647 |
| 104 | aI-2147483647 |
| 105 | aI-2147483648 |
| 106 | a""" + \ |
| 107 | """(S'abc' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 108 | p4 |
| 109 | g4 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 110 | """ + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 111 | """(i__main__ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 112 | C |
| 113 | p5 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 114 | """ + \ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 115 | """(dp6 |
| 116 | S'foo' |
| 117 | p7 |
| 118 | I1 |
| 119 | sS'bar' |
| 120 | p8 |
| 121 | I2 |
| 122 | sbg5 |
| 123 | tp9 |
| 124 | ag9 |
| 125 | aI5 |
| 126 | a. |
| 127 | """ |
| 128 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 129 | # Disassembly of DATA0. |
| 130 | DATA0_DIS = """\ |
| 131 | 0: ( MARK |
| 132 | 1: l LIST (MARK at 0) |
| 133 | 2: p PUT 1 |
| 134 | 5: I INT 0 |
| 135 | 8: a APPEND |
| 136 | 9: L LONG 1L |
| 137 | 13: a APPEND |
| 138 | 14: F FLOAT 2.0 |
| 139 | 17: a APPEND |
| 140 | 18: c GLOBAL '__builtin__ complex' |
| 141 | 39: p PUT 2 |
| 142 | 42: ( MARK |
| 143 | 43: F FLOAT 3.0 |
| 144 | 46: F FLOAT 0.0 |
| 145 | 49: t TUPLE (MARK at 42) |
| 146 | 50: R REDUCE |
| 147 | 51: p PUT 3 |
| 148 | 54: a APPEND |
| 149 | 55: I INT 1 |
| 150 | 58: a APPEND |
| 151 | 59: I INT -1 |
| 152 | 63: a APPEND |
| 153 | 64: I INT 255 |
| 154 | 69: a APPEND |
| 155 | 70: I INT -255 |
| 156 | 76: a APPEND |
| 157 | 77: I INT -256 |
| 158 | 83: a APPEND |
| 159 | 84: I INT 65535 |
| 160 | 91: a APPEND |
| 161 | 92: I INT -65535 |
| 162 | 100: a APPEND |
| 163 | 101: I INT -65536 |
| 164 | 109: a APPEND |
| 165 | 110: I INT 2147483647 |
| 166 | 122: a APPEND |
| 167 | 123: I INT -2147483647 |
| 168 | 136: a APPEND |
| 169 | 137: I INT -2147483648 |
| 170 | 150: a APPEND |
| 171 | 151: ( MARK |
| 172 | 152: S STRING 'abc' |
| 173 | 159: p PUT 4 |
| 174 | 162: g GET 4 |
| 175 | 165: ( MARK |
| 176 | 166: i INST '__main__ C' (MARK at 165) |
| 177 | 178: p PUT 5 |
| 178 | 181: ( MARK |
| 179 | 182: d DICT (MARK at 181) |
| 180 | 183: p PUT 6 |
| 181 | 186: S STRING 'foo' |
| 182 | 193: p PUT 7 |
| 183 | 196: I INT 1 |
| 184 | 199: s SETITEM |
| 185 | 200: S STRING 'bar' |
| 186 | 207: p PUT 8 |
| 187 | 210: I INT 2 |
| 188 | 213: s SETITEM |
| 189 | 214: b BUILD |
| 190 | 215: g GET 5 |
| 191 | 218: t TUPLE (MARK at 151) |
| 192 | 219: p PUT 9 |
| 193 | 222: a APPEND |
| 194 | 223: g GET 9 |
| 195 | 226: a APPEND |
| 196 | 227: I INT 5 |
| 197 | 230: a APPEND |
| 198 | 231: . STOP |
| 199 | highest protocol among opcodes = 0 |
| 200 | """ |
| 201 | |
| 202 | DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' |
| 203 | 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' |
| 204 | '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' |
| 205 | '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' |
| 206 | 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' |
| 207 | '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' |
| 208 | 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' |
| 209 | '\x06tq\nh\nK\x05e.' |
| 210 | ) |
| 211 | |
| 212 | # Disassembly of DATA1. |
| 213 | DATA1_DIS = """\ |
| 214 | 0: ] EMPTY_LIST |
| 215 | 1: q BINPUT 1 |
| 216 | 3: ( MARK |
| 217 | 4: K BININT1 0 |
| 218 | 6: L LONG 1L |
| 219 | 10: G BINFLOAT 2.0 |
| 220 | 19: c GLOBAL '__builtin__ complex' |
| 221 | 40: q BINPUT 2 |
| 222 | 42: ( MARK |
| 223 | 43: G BINFLOAT 3.0 |
| 224 | 52: G BINFLOAT 0.0 |
| 225 | 61: t TUPLE (MARK at 42) |
| 226 | 62: R REDUCE |
| 227 | 63: q BINPUT 3 |
| 228 | 65: K BININT1 1 |
| 229 | 67: J BININT -1 |
| 230 | 72: K BININT1 255 |
| 231 | 74: J BININT -255 |
| 232 | 79: J BININT -256 |
| 233 | 84: M BININT2 65535 |
| 234 | 87: J BININT -65535 |
| 235 | 92: J BININT -65536 |
| 236 | 97: J BININT 2147483647 |
| 237 | 102: J BININT -2147483647 |
| 238 | 107: J BININT -2147483648 |
| 239 | 112: ( MARK |
| 240 | 113: U SHORT_BINSTRING 'abc' |
| 241 | 118: q BINPUT 4 |
| 242 | 120: h BINGET 4 |
| 243 | 122: ( MARK |
| 244 | 123: c GLOBAL '__main__ C' |
| 245 | 135: q BINPUT 5 |
| 246 | 137: o OBJ (MARK at 122) |
| 247 | 138: q BINPUT 6 |
| 248 | 140: } EMPTY_DICT |
| 249 | 141: q BINPUT 7 |
| 250 | 143: ( MARK |
| 251 | 144: U SHORT_BINSTRING 'foo' |
| 252 | 149: q BINPUT 8 |
| 253 | 151: K BININT1 1 |
| 254 | 153: U SHORT_BINSTRING 'bar' |
| 255 | 158: q BINPUT 9 |
| 256 | 160: K BININT1 2 |
| 257 | 162: u SETITEMS (MARK at 143) |
| 258 | 163: b BUILD |
| 259 | 164: h BINGET 6 |
| 260 | 166: t TUPLE (MARK at 112) |
| 261 | 167: q BINPUT 10 |
| 262 | 169: h BINGET 10 |
| 263 | 171: K BININT1 5 |
| 264 | 173: e APPENDS (MARK at 3) |
| 265 | 174: . STOP |
| 266 | highest protocol among opcodes = 1 |
| 267 | """ |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 268 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 269 | def create_data(): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 270 | c = C() |
| 271 | c.foo = 1 |
| 272 | c.bar = 2 |
| 273 | x = [0, 1L, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 274 | # Append some integer test cases at cPickle.c's internal size |
| 275 | # cutoffs. |
| 276 | uint1max = 0xff |
| 277 | uint2max = 0xffff |
| 278 | int4max = 0x7fffffff |
| 279 | x.extend([1, -1, |
| 280 | uint1max, -uint1max, -uint1max-1, |
| 281 | uint2max, -uint2max, -uint2max-1, |
| 282 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 283 | y = ('abc', 'abc', c, c) |
| 284 | x.append(y) |
| 285 | x.append(y) |
| 286 | x.append(5) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 287 | return x |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 288 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 289 | class AbstractPickleTests(unittest.TestCase): |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 290 | # Subclass must define self.dumps, self.loads, self.error. |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 291 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 292 | _testdata = create_data() |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 293 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 294 | def setUp(self): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 295 | pass |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 296 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 297 | def test_misc(self): |
| 298 | # test various datatypes not tested by testdata |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 299 | for proto in protocols: |
| 300 | x = myint(4) |
| 301 | s = self.dumps(x, proto) |
| 302 | y = self.loads(s) |
| 303 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 304 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 305 | x = (1, ()) |
| 306 | s = self.dumps(x, proto) |
| 307 | y = self.loads(s) |
| 308 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 309 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 310 | x = initarg(1, x) |
| 311 | s = self.dumps(x, proto) |
| 312 | y = self.loads(s) |
| 313 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 314 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 315 | # XXX test __reduce__ protocol? |
| 316 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 317 | def test_roundtrip_equality(self): |
| 318 | expected = self._testdata |
| 319 | for proto in protocols: |
| 320 | s = self.dumps(expected, proto) |
| 321 | got = self.loads(s) |
| 322 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 323 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 324 | def test_load_from_canned_string(self): |
| 325 | expected = self._testdata |
| 326 | for canned in DATA0, DATA1: |
| 327 | got = self.loads(canned) |
| 328 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 329 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 330 | # There are gratuitous differences between pickles produced by |
| 331 | # pickle and cPickle, largely because cPickle starts PUT indices at |
| 332 | # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- |
| 333 | # there's a comment with an exclamation point there whose meaning |
| 334 | # is a mystery. cPickle also suppresses PUT for objects with a refcount |
| 335 | # of 1. |
| 336 | def dont_test_disassembly(self): |
| 337 | from cStringIO import StringIO |
| 338 | from pickletools import dis |
| 339 | |
| 340 | for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): |
| 341 | s = self.dumps(self._testdata, proto) |
| 342 | filelike = StringIO() |
| 343 | dis(s, out=filelike) |
| 344 | got = filelike.getvalue() |
| 345 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 346 | |
| 347 | def test_recursive_list(self): |
| 348 | l = [] |
| 349 | l.append(l) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 350 | for proto in protocols: |
| 351 | s = self.dumps(l, proto) |
| 352 | x = self.loads(s) |
| 353 | self.assertEqual(x, l) |
| 354 | self.assertEqual(x, x[0]) |
| 355 | self.assertEqual(id(x), id(x[0])) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 356 | |
| 357 | def test_recursive_dict(self): |
| 358 | d = {} |
| 359 | d[1] = d |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 360 | for proto in protocols: |
| 361 | s = self.dumps(d, proto) |
| 362 | x = self.loads(s) |
| 363 | self.assertEqual(x, d) |
| 364 | self.assertEqual(x[1], x) |
| 365 | self.assertEqual(id(x[1]), id(x)) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 366 | |
| 367 | def test_recursive_inst(self): |
| 368 | i = C() |
| 369 | i.attr = i |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 370 | for proto in protocols: |
| 371 | s = self.dumps(i, 2) |
| 372 | x = self.loads(s) |
| 373 | self.assertEqual(x, i) |
| 374 | self.assertEqual(x.attr, x) |
| 375 | self.assertEqual(id(x.attr), id(x)) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 376 | |
| 377 | def test_recursive_multi(self): |
| 378 | l = [] |
| 379 | d = {1:l} |
| 380 | i = C() |
| 381 | i.attr = d |
| 382 | l.append(i) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 383 | for proto in protocols: |
| 384 | s = self.dumps(l, proto) |
| 385 | x = self.loads(s) |
| 386 | self.assertEqual(x, l) |
| 387 | self.assertEqual(x[0], i) |
| 388 | self.assertEqual(x[0].attr, d) |
| 389 | self.assertEqual(x[0].attr[1], x) |
| 390 | self.assertEqual(x[0].attr[1][0], i) |
| 391 | self.assertEqual(x[0].attr[1][0].attr, d) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 392 | |
| 393 | def test_garyp(self): |
| 394 | self.assertRaises(self.error, self.loads, 'garyp') |
| 395 | |
| 396 | def test_insecure_strings(self): |
| 397 | insecure = ["abc", "2 + 2", # not quoted |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 398 | #"'abc' + 'def'", # not a single quoted string |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 399 | "'abc", # quote is not closed |
| 400 | "'abc\"", # open quote and close quote don't match |
| 401 | "'abc' ?", # junk after close quote |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 402 | "'\\'", # trailing backslash |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 403 | # some tests of the quoting rules |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 404 | #"'abc\"\''", |
| 405 | #"'\\\\a\'\'\'\\\'\\\\\''", |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 406 | ] |
| 407 | for s in insecure: |
| 408 | buf = "S" + s + "\012p0\012." |
| 409 | self.assertRaises(ValueError, self.loads, buf) |
| 410 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 411 | if have_unicode: |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 412 | def test_unicode(self): |
| 413 | endcases = [unicode(''), unicode('<\\u>'), unicode('<\\\u1234>'), |
| 414 | unicode('<\n>'), unicode('<\\>')] |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 415 | for proto in protocols: |
| 416 | for u in endcases: |
| 417 | p = self.dumps(u, proto) |
| 418 | u2 = self.loads(p) |
| 419 | self.assertEqual(u2, u) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 420 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 421 | def test_ints(self): |
| 422 | import sys |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 423 | for proto in protocols: |
| 424 | n = sys.maxint |
| 425 | while n: |
| 426 | for expected in (-n, n): |
| 427 | s = self.dumps(expected, proto) |
| 428 | n2 = self.loads(s) |
| 429 | self.assertEqual(expected, n2) |
| 430 | n = n >> 1 |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 431 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 432 | def test_maxint64(self): |
| 433 | maxint64 = (1L << 63) - 1 |
| 434 | data = 'I' + str(maxint64) + '\n.' |
| 435 | got = self.loads(data) |
| 436 | self.assertEqual(got, maxint64) |
| 437 | |
| 438 | # Try too with a bogus literal. |
| 439 | data = 'I' + str(maxint64) + 'JUNK\n.' |
| 440 | self.assertRaises(ValueError, self.loads, data) |
| 441 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 442 | def test_long(self): |
| 443 | for proto in protocols: |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 444 | # 256 bytes is where LONG4 begins. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 445 | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
| 446 | nbase = 1L << nbits |
| 447 | for npos in nbase-1, nbase, nbase+1: |
| 448 | for n in npos, -npos: |
| 449 | pickle = self.dumps(n, proto) |
| 450 | got = self.loads(pickle) |
| 451 | self.assertEqual(n, got) |
| 452 | # Try a monster. This is quadratic-time in protos 0 & 1, so don't |
| 453 | # bother with those. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 454 | nbase = long("deadbeeffeedface", 16) |
| 455 | nbase += nbase << 1000000 |
| 456 | for n in nbase, -nbase: |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 457 | p = self.dumps(n, 2) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 458 | got = self.loads(p) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 459 | self.assertEqual(n, got) |
| 460 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 461 | def test_reduce(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 462 | pass |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 463 | |
| 464 | def test_getinitargs(self): |
| 465 | pass |
| 466 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 467 | def test_metaclass(self): |
| 468 | a = use_metaclass() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 469 | for proto in protocols: |
| 470 | s = self.dumps(a, proto) |
| 471 | b = self.loads(s) |
| 472 | self.assertEqual(a.__class__, b.__class__) |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 473 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 474 | def test_structseq(self): |
| 475 | import time |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 476 | import os |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 477 | |
| 478 | t = time.localtime() |
| 479 | for proto in protocols: |
| 480 | s = self.dumps(t, proto) |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 481 | u = self.loads(s) |
| 482 | self.assertEqual(t, u) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 483 | if hasattr(os, "stat"): |
| 484 | t = os.stat(os.curdir) |
| 485 | s = self.dumps(t, proto) |
| 486 | u = self.loads(s) |
| 487 | self.assertEqual(t, u) |
| 488 | if hasattr(os, "statvfs"): |
| 489 | t = os.statvfs(os.curdir) |
| 490 | s = self.dumps(t, proto) |
| 491 | u = self.loads(s) |
| 492 | self.assertEqual(t, u) |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 494 | # Tests for protocol 2 |
| 495 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 496 | def test_proto(self): |
| 497 | build_none = pickle.NONE + pickle.STOP |
| 498 | for proto in protocols: |
| 499 | expected = build_none |
| 500 | if proto >= 2: |
| 501 | expected = pickle.PROTO + chr(proto) + expected |
| 502 | p = self.dumps(None, proto) |
| 503 | self.assertEqual(p, expected) |
| 504 | |
| 505 | oob = protocols[-1] + 1 # a future protocol |
| 506 | badpickle = pickle.PROTO + chr(oob) + build_none |
| 507 | try: |
| 508 | self.loads(badpickle) |
| 509 | except ValueError, detail: |
| 510 | self.failUnless(str(detail).startswith( |
| 511 | "unsupported pickle protocol")) |
| 512 | else: |
| 513 | self.fail("expected bad protocol number to raise ValueError") |
| 514 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 515 | def test_long1(self): |
| 516 | x = 12345678910111213141516178920L |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 517 | for proto in protocols: |
| 518 | s = self.dumps(x, proto) |
| 519 | y = self.loads(s) |
| 520 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 521 | self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 522 | |
| 523 | def test_long4(self): |
| 524 | x = 12345678910111213141516178920L << (256*8) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 525 | for proto in protocols: |
| 526 | s = self.dumps(x, proto) |
| 527 | y = self.loads(s) |
| 528 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 529 | self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 531 | def test_short_tuples(self): |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 532 | # Map (proto, len(tuple)) to expected opcode. |
| 533 | expected_opcode = {(0, 0): pickle.TUPLE, |
| 534 | (0, 1): pickle.TUPLE, |
| 535 | (0, 2): pickle.TUPLE, |
| 536 | (0, 3): pickle.TUPLE, |
| 537 | (0, 4): pickle.TUPLE, |
| 538 | |
| 539 | (1, 0): pickle.EMPTY_TUPLE, |
| 540 | (1, 1): pickle.TUPLE, |
| 541 | (1, 2): pickle.TUPLE, |
| 542 | (1, 3): pickle.TUPLE, |
| 543 | (1, 4): pickle.TUPLE, |
| 544 | |
| 545 | (2, 0): pickle.EMPTY_TUPLE, |
| 546 | (2, 1): pickle.TUPLE1, |
| 547 | (2, 2): pickle.TUPLE2, |
| 548 | (2, 3): pickle.TUPLE3, |
| 549 | (2, 4): pickle.TUPLE, |
| 550 | } |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 551 | a = () |
Guido van Rossum | 025bc2f | 2003-01-28 04:20:02 +0000 | [diff] [blame] | 552 | b = (1,) |
| 553 | c = (1, 2) |
| 554 | d = (1, 2, 3) |
| 555 | e = (1, 2, 3, 4) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 556 | for proto in protocols: |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 557 | for x in a, b, c, d, e: |
| 558 | s = self.dumps(x, proto) |
| 559 | y = self.loads(s) |
| 560 | self.assertEqual(x, y, (proto, x, s, y)) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 561 | expected = expected_opcode[proto, len(x)] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 562 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 564 | def test_singletons(self): |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 565 | # Map (proto, singleton) to expected opcode. |
| 566 | expected_opcode = {(0, None): pickle.NONE, |
| 567 | (1, None): pickle.NONE, |
| 568 | (2, None): pickle.NONE, |
| 569 | |
| 570 | (0, True): pickle.INT, |
| 571 | (1, True): pickle.INT, |
| 572 | (2, True): pickle.NEWTRUE, |
| 573 | |
| 574 | (0, False): pickle.INT, |
| 575 | (1, False): pickle.INT, |
| 576 | (2, False): pickle.NEWFALSE, |
| 577 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 578 | for proto in protocols: |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 579 | for x in None, False, True: |
| 580 | s = self.dumps(x, proto) |
| 581 | y = self.loads(s) |
| 582 | self.assert_(x is y, (proto, x, s, y)) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 583 | expected = expected_opcode[proto, x] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 584 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 585 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 586 | def test_newobj_tuple(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 587 | x = MyTuple([1, 2, 3]) |
| 588 | x.foo = 42 |
| 589 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 590 | for proto in protocols: |
| 591 | s = self.dumps(x, proto) |
| 592 | y = self.loads(s) |
| 593 | self.assertEqual(tuple(x), tuple(y)) |
| 594 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 595 | |
| 596 | def test_newobj_list(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 597 | x = MyList([1, 2, 3]) |
| 598 | x.foo = 42 |
| 599 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 600 | for proto in protocols: |
| 601 | s = self.dumps(x, proto) |
| 602 | y = self.loads(s) |
| 603 | self.assertEqual(list(x), list(y)) |
| 604 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 605 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 606 | def test_newobj_generic(self): |
Tim Peters | 5013bd9 | 2003-02-03 22:28:41 +0000 | [diff] [blame] | 607 | for proto in protocols: |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 608 | for C in myclasses: |
| 609 | B = C.__base__ |
| 610 | x = C(C.sample) |
| 611 | x.foo = 42 |
| 612 | s = self.dumps(x, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 613 | y = self.loads(s) |
| 614 | detail = (proto, C, B, x, y, type(y)) |
| 615 | self.assertEqual(B(x), B(y), detail) |
| 616 | self.assertEqual(x.__dict__, y.__dict__, detail) |
| 617 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 618 | # Register a type with copy_reg, with extension code extcode. Pickle |
| 619 | # an object of that type. Check that the resulting pickle uses opcode |
| 620 | # (EXT[124]) under proto 2, and not in proto 1. |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 621 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 622 | def produce_global_ext(self, extcode, opcode): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 623 | e = ExtensionSaver(extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 624 | try: |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 625 | copy_reg.add_extension(__name__, "MyList", extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 626 | x = MyList([1, 2, 3]) |
| 627 | x.foo = 42 |
| 628 | x.bar = "hello" |
| 629 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 630 | # Dump using protocol 1 for comparison. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 631 | s1 = self.dumps(x, 1) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 632 | self.assert_(__name__ in s1) |
| 633 | self.assert_("MyList" in s1) |
| 634 | self.assertEqual(opcode_in_pickle(opcode, s1), False) |
| 635 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 636 | y = self.loads(s1) |
| 637 | self.assertEqual(list(x), list(y)) |
| 638 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 639 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 640 | # Dump using protocol 2 for test. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 641 | s2 = self.dumps(x, 2) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 642 | self.assert_(__name__ not in s2) |
| 643 | self.assert_("MyList" not in s2) |
| 644 | self.assertEqual(opcode_in_pickle(opcode, s2), True) |
| 645 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 646 | y = self.loads(s2) |
| 647 | self.assertEqual(list(x), list(y)) |
| 648 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 649 | |
| 650 | finally: |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 651 | e.restore() |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 652 | |
| 653 | def test_global_ext1(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 654 | self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code |
| 655 | self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 656 | |
| 657 | def test_global_ext2(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 658 | self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code |
| 659 | self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code |
| 660 | self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 661 | |
| 662 | def test_global_ext4(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 663 | self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code |
| 664 | self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code |
| 665 | self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness |
| 666 | |
Tim Peters | 731098b | 2003-02-04 20:56:09 +0000 | [diff] [blame] | 667 | |
| 668 | # XXX Temporary hack, so long as the C implementation of pickle protocol |
| 669 | # XXX 2 isn't ready. When it is, move the methods in TempAbstractPickleTests |
| 670 | # XXX into AbstractPickleTests above, and get rid of TempAbstractPickleTests |
| 671 | # XXX along with the references to it in test_pickle.py. |
| 672 | class TempAbstractPickleTests(unittest.TestCase): |
| 673 | |
| 674 | def test_newobj_list_slots(self): |
| 675 | x = SlotList([1, 2, 3]) |
| 676 | x.foo = 42 |
| 677 | x.bar = "hello" |
| 678 | s = self.dumps(x, 2) |
| 679 | y = self.loads(s) |
| 680 | self.assertEqual(list(x), list(y)) |
| 681 | self.assertEqual(x.__dict__, y.__dict__) |
| 682 | self.assertEqual(x.foo, y.foo) |
| 683 | self.assertEqual(x.bar, y.bar) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 685 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 686 | class MyInt(int): |
| 687 | sample = 1 |
| 688 | |
| 689 | class MyLong(long): |
| 690 | sample = 1L |
| 691 | |
| 692 | class MyFloat(float): |
| 693 | sample = 1.0 |
| 694 | |
| 695 | class MyComplex(complex): |
| 696 | sample = 1.0 + 0.0j |
| 697 | |
| 698 | class MyStr(str): |
| 699 | sample = "hello" |
| 700 | |
| 701 | class MyUnicode(unicode): |
| 702 | sample = u"hello \u1234" |
| 703 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 704 | class MyTuple(tuple): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 705 | sample = (1, 2, 3) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 706 | |
| 707 | class MyList(list): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 708 | sample = [1, 2, 3] |
| 709 | |
| 710 | class MyDict(dict): |
| 711 | sample = {"a": 1, "b": 2} |
| 712 | |
| 713 | myclasses = [MyInt, MyLong, MyFloat, |
| 714 | # MyComplex, # XXX complex somehow doesn't work here :-( |
| 715 | MyStr, MyUnicode, |
| 716 | MyTuple, MyList, MyDict] |
| 717 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | c8d6ef5 | 2003-01-28 22:02:31 +0000 | [diff] [blame] | 719 | class SlotList(MyList): |
| 720 | __slots__ = ["foo"] |
| 721 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 722 | class AbstractPickleModuleTests(unittest.TestCase): |
| 723 | |
| 724 | def test_dump_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 725 | import os |
| 726 | f = open(TESTFN, "w") |
| 727 | try: |
| 728 | f.close() |
| 729 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 730 | finally: |
| 731 | os.remove(TESTFN) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 732 | |
| 733 | def test_load_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 734 | import os |
| 735 | f = open(TESTFN, "w") |
| 736 | try: |
| 737 | f.close() |
| 738 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 739 | finally: |
| 740 | os.remove(TESTFN) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 741 | |
| 742 | class AbstractPersistentPicklerTests(unittest.TestCase): |
| 743 | |
| 744 | # This class defines persistent_id() and persistent_load() |
| 745 | # functions that should be used by the pickler. All even integers |
| 746 | # are pickled using persistent ids. |
| 747 | |
| 748 | def persistent_id(self, object): |
| 749 | if isinstance(object, int) and object % 2 == 0: |
| 750 | self.id_count += 1 |
| 751 | return str(object) |
| 752 | else: |
| 753 | return None |
| 754 | |
| 755 | def persistent_load(self, oid): |
| 756 | self.load_count += 1 |
| 757 | object = int(oid) |
| 758 | assert object % 2 == 0 |
| 759 | return object |
| 760 | |
| 761 | def test_persistence(self): |
| 762 | self.id_count = 0 |
| 763 | self.load_count = 0 |
| 764 | L = range(10) |
| 765 | self.assertEqual(self.loads(self.dumps(L)), L) |
| 766 | self.assertEqual(self.id_count, 5) |
| 767 | self.assertEqual(self.load_count, 5) |
| 768 | |
| 769 | def test_bin_persistence(self): |
| 770 | self.id_count = 0 |
| 771 | self.load_count = 0 |
| 772 | L = range(10) |
| 773 | self.assertEqual(self.loads(self.dumps(L, 1)), L) |
| 774 | self.assertEqual(self.id_count, 5) |
| 775 | self.assertEqual(self.load_count, 5) |