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