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