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