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