| 1 | import unittest
|
|---|
| 2 | import pickle
|
|---|
| 3 | import cPickle
|
|---|
| 4 | import pickletools
|
|---|
| 5 | import copy_reg
|
|---|
| 6 |
|
|---|
| 7 | from test.test_support import TestFailed, have_unicode, TESTFN, \
|
|---|
| 8 | run_with_locale
|
|---|
| 9 |
|
|---|
| 10 | # Tests that try a number of pickle protocols should have a
|
|---|
| 11 | # for proto in protocols:
|
|---|
| 12 | # kind of outer loop.
|
|---|
| 13 | assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
|
|---|
| 14 | protocols = range(pickle.HIGHEST_PROTOCOL + 1)
|
|---|
| 15 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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
|
|---|
| 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)
|
|---|
| 50 | else:
|
|---|
| 51 | self.pair = None
|
|---|
| 52 |
|
|---|
| 53 | # Restore previous registration for code.
|
|---|
| 54 | def restore(self):
|
|---|
| 55 | code = self.code
|
|---|
| 56 | curpair = copy_reg._inverted_registry.get(code)
|
|---|
| 57 | if curpair is not None:
|
|---|
| 58 | copy_reg.remove_extension(curpair[0], curpair[1], code)
|
|---|
| 59 | pair = self.pair
|
|---|
| 60 | if pair is not None:
|
|---|
| 61 | copy_reg.add_extension(pair[0], pair[1], code)
|
|---|
| 62 |
|
|---|
| 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):
|
|---|
| 76 |
|
|---|
| 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 |
|
|---|
| 84 | class metaclass(type):
|
|---|
| 85 | pass
|
|---|
| 86 |
|
|---|
| 87 | class use_metaclass(object):
|
|---|
| 88 | __metaclass__ = metaclass
|
|---|
| 89 |
|
|---|
| 90 | # DATA0 .. DATA2 are the pickles we expect under the various protocols, for
|
|---|
| 91 | # the object returned by create_data().
|
|---|
| 92 |
|
|---|
| 93 | # break into multiple strings to avoid confusing font-lock-mode
|
|---|
| 94 | DATA0 = """(lp1
|
|---|
| 95 | I0
|
|---|
| 96 | aL1L
|
|---|
| 97 | aF2
|
|---|
| 98 | ac__builtin__
|
|---|
| 99 | complex
|
|---|
| 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'
|
|---|
| 118 | p4
|
|---|
| 119 | g4
|
|---|
| 120 | """ + \
|
|---|
| 121 | """(i__main__
|
|---|
| 122 | C
|
|---|
| 123 | p5
|
|---|
| 124 | """ + \
|
|---|
| 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 |
|
|---|
| 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 | """
|
|---|
| 278 |
|
|---|
| 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 |
|
|---|
| 344 | def create_data():
|
|---|
| 345 | c = C()
|
|---|
| 346 | c.foo = 1
|
|---|
| 347 | c.bar = 2
|
|---|
| 348 | x = [0, 1L, 2.0, 3.0+0j]
|
|---|
| 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])
|
|---|
| 358 | y = ('abc', 'abc', c, c)
|
|---|
| 359 | x.append(y)
|
|---|
| 360 | x.append(y)
|
|---|
| 361 | x.append(5)
|
|---|
| 362 | return x
|
|---|
| 363 |
|
|---|
| 364 | class AbstractPickleTests(unittest.TestCase):
|
|---|
| 365 | # Subclass must define self.dumps, self.loads, self.error.
|
|---|
| 366 |
|
|---|
| 367 | _testdata = create_data()
|
|---|
| 368 |
|
|---|
| 369 | def setUp(self):
|
|---|
| 370 | pass
|
|---|
| 371 |
|
|---|
| 372 | def test_misc(self):
|
|---|
| 373 | # test various datatypes not tested by testdata
|
|---|
| 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)
|
|---|
| 379 |
|
|---|
| 380 | x = (1, ())
|
|---|
| 381 | s = self.dumps(x, proto)
|
|---|
| 382 | y = self.loads(s)
|
|---|
| 383 | self.assertEqual(x, y)
|
|---|
| 384 |
|
|---|
| 385 | x = initarg(1, x)
|
|---|
| 386 | s = self.dumps(x, proto)
|
|---|
| 387 | y = self.loads(s)
|
|---|
| 388 | self.assertEqual(x, y)
|
|---|
| 389 |
|
|---|
| 390 | # XXX test __reduce__ protocol?
|
|---|
| 391 |
|
|---|
| 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)
|
|---|
| 398 |
|
|---|
| 399 | def test_load_from_canned_string(self):
|
|---|
| 400 | expected = self._testdata
|
|---|
| 401 | for canned in DATA0, DATA1, DATA2:
|
|---|
| 402 | got = self.loads(canned)
|
|---|
| 403 | self.assertEqual(expected, got)
|
|---|
| 404 |
|
|---|
| 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)
|
|---|
| 421 |
|
|---|
| 422 | def test_recursive_list(self):
|
|---|
| 423 | l = []
|
|---|
| 424 | l.append(l)
|
|---|
| 425 | for proto in protocols:
|
|---|
| 426 | s = self.dumps(l, proto)
|
|---|
| 427 | x = self.loads(s)
|
|---|
| 428 | self.assertEqual(len(x), 1)
|
|---|
| 429 | self.assert_(x is x[0])
|
|---|
| 430 |
|
|---|
| 431 | def test_recursive_dict(self):
|
|---|
| 432 | d = {}
|
|---|
| 433 | d[1] = d
|
|---|
| 434 | for proto in protocols:
|
|---|
| 435 | s = self.dumps(d, proto)
|
|---|
| 436 | x = self.loads(s)
|
|---|
| 437 | self.assertEqual(x.keys(), [1])
|
|---|
| 438 | self.assert_(x[1] is x)
|
|---|
| 439 |
|
|---|
| 440 | def test_recursive_inst(self):
|
|---|
| 441 | i = C()
|
|---|
| 442 | i.attr = i
|
|---|
| 443 | for proto in protocols:
|
|---|
| 444 | s = self.dumps(i, 2)
|
|---|
| 445 | x = self.loads(s)
|
|---|
| 446 | self.assertEqual(dir(x), dir(i))
|
|---|
| 447 | self.assert_(x.attr is x)
|
|---|
| 448 |
|
|---|
| 449 | def test_recursive_multi(self):
|
|---|
| 450 | l = []
|
|---|
| 451 | d = {1:l}
|
|---|
| 452 | i = C()
|
|---|
| 453 | i.attr = d
|
|---|
| 454 | l.append(i)
|
|---|
| 455 | for proto in protocols:
|
|---|
| 456 | s = self.dumps(l, proto)
|
|---|
| 457 | x = self.loads(s)
|
|---|
| 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)
|
|---|
| 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
|
|---|
| 468 | #"'abc' + 'def'", # not a single quoted string
|
|---|
| 469 | "'abc", # quote is not closed
|
|---|
| 470 | "'abc\"", # open quote and close quote don't match
|
|---|
| 471 | "'abc' ?", # junk after close quote
|
|---|
| 472 | "'\\'", # trailing backslash
|
|---|
| 473 | # some tests of the quoting rules
|
|---|
| 474 | #"'abc\"\''",
|
|---|
| 475 | #"'\\\\a\'\'\'\\\'\\\\\''",
|
|---|
| 476 | ]
|
|---|
| 477 | for s in insecure:
|
|---|
| 478 | buf = "S" + s + "\012p0\012."
|
|---|
| 479 | self.assertRaises(ValueError, self.loads, buf)
|
|---|
| 480 |
|
|---|
| 481 | if have_unicode:
|
|---|
| 482 | def test_unicode(self):
|
|---|
| 483 | endcases = [unicode(''), unicode('<\\u>'), unicode('<\\\u1234>'),
|
|---|
| 484 | unicode('<\n>'), unicode('<\\>')]
|
|---|
| 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)
|
|---|
| 490 |
|
|---|
| 491 | def test_ints(self):
|
|---|
| 492 | import sys
|
|---|
| 493 | for proto in protocols:
|
|---|
| 494 | n = sys.maxint
|
|---|
| 495 | while n:
|
|---|
| 496 | for expected in (-n, n):
|
|---|
| 497 | s = self.dumps(expected, proto)
|
|---|
| 498 | n2 = self.loads(s)
|
|---|
| 499 | self.assertEqual(expected, n2)
|
|---|
| 500 | n = n >> 1
|
|---|
| 501 |
|
|---|
| 502 | def test_maxint64(self):
|
|---|
| 503 | maxint64 = (1L << 63) - 1
|
|---|
| 504 | data = 'I' + str(maxint64) + '\n.'
|
|---|
| 505 | got = self.loads(data)
|
|---|
| 506 | self.assertEqual(got, maxint64)
|
|---|
| 507 |
|
|---|
| 508 | # Try too with a bogus literal.
|
|---|
| 509 | data = 'I' + str(maxint64) + 'JUNK\n.'
|
|---|
| 510 | self.assertRaises(ValueError, self.loads, data)
|
|---|
| 511 |
|
|---|
| 512 | def test_long(self):
|
|---|
| 513 | for proto in protocols:
|
|---|
| 514 | # 256 bytes is where LONG4 begins.
|
|---|
| 515 | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257:
|
|---|
| 516 | nbase = 1L << nbits
|
|---|
| 517 | for npos in nbase-1, nbase, nbase+1:
|
|---|
| 518 | for n in npos, -npos:
|
|---|
| 519 | pickle = self.dumps(n, proto)
|
|---|
| 520 | got = self.loads(pickle)
|
|---|
| 521 | self.assertEqual(n, got)
|
|---|
| 522 | # Try a monster. This is quadratic-time in protos 0 & 1, so don't
|
|---|
| 523 | # bother with those.
|
|---|
| 524 | nbase = long("deadbeeffeedface", 16)
|
|---|
| 525 | nbase += nbase << 1000000
|
|---|
|
|---|