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