Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 2 | import unittest |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 3 | import pickle |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 4 | import cPickle |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 5 | import StringIO |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 6 | import cStringIO |
Tim Peters | 31f119e | 2003-02-03 16:20:13 +0000 | [diff] [blame] | 7 | import pickletools |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 8 | import copy_reg |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | d627e12 | 2013-03-30 10:36:31 -0400 | [diff] [blame] | 10 | from test.test_support import TestFailed, verbose, have_unicode, TESTFN |
| 11 | try: |
| 12 | from test.test_support import _2G, _1M, precisionbigmemtest |
| 13 | except ImportError: |
| 14 | # this import might fail when run on older Python versions by test_xpickle |
Benjamin Peterson | f3ad030 | 2013-03-30 15:30:28 -0400 | [diff] [blame] | 15 | _2G = _1M = 0 |
Benjamin Peterson | d627e12 | 2013-03-30 10:36:31 -0400 | [diff] [blame] | 16 | def precisionbigmemtest(*args, **kwargs): |
| 17 | return lambda self: None |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 18 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 19 | # Tests that try a number of pickle protocols should have a |
| 20 | # for proto in protocols: |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 21 | # kind of outer loop. |
| 22 | assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2 |
| 23 | protocols = range(pickle.HIGHEST_PROTOCOL + 1) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 24 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 25 | # Copy of test.test_support.run_with_locale. This is needed to support Python |
| 26 | # 2.4, which didn't include it. This is all to support test_xpickle, which |
| 27 | # bounces pickled objects through older Python versions to test backwards |
| 28 | # compatibility. |
| 29 | def run_with_locale(catstr, *locales): |
| 30 | def decorator(func): |
| 31 | def inner(*args, **kwds): |
| 32 | try: |
| 33 | import locale |
| 34 | category = getattr(locale, catstr) |
| 35 | orig_locale = locale.setlocale(category) |
| 36 | except AttributeError: |
| 37 | # if the test author gives us an invalid category string |
| 38 | raise |
| 39 | except: |
| 40 | # cannot retrieve original locale, so do nothing |
| 41 | locale = orig_locale = None |
| 42 | else: |
| 43 | for loc in locales: |
| 44 | try: |
| 45 | locale.setlocale(category, loc) |
| 46 | break |
| 47 | except: |
| 48 | pass |
| 49 | |
| 50 | # now run the function, resetting the locale on exceptions |
| 51 | try: |
| 52 | return func(*args, **kwds) |
| 53 | finally: |
| 54 | if locale and orig_locale: |
| 55 | locale.setlocale(category, orig_locale) |
| 56 | inner.func_name = func.func_name |
| 57 | inner.__doc__ = func.__doc__ |
| 58 | return inner |
| 59 | return decorator |
| 60 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 61 | |
| 62 | # Return True if opcode code appears in the pickle, else False. |
| 63 | def opcode_in_pickle(code, pickle): |
| 64 | for op, dummy, dummy in pickletools.genops(pickle): |
| 65 | if op.code == code: |
| 66 | return True |
| 67 | return False |
| 68 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 69 | # Return the number of times opcode code appears in pickle. |
| 70 | def count_opcode(code, pickle): |
| 71 | n = 0 |
| 72 | for op, dummy, dummy in pickletools.genops(pickle): |
| 73 | if op.code == code: |
| 74 | n += 1 |
| 75 | return n |
| 76 | |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 77 | # We can't very well test the extension registry without putting known stuff |
| 78 | # in it, but we have to be careful to restore its original state. Code |
| 79 | # should do this: |
| 80 | # |
| 81 | # e = ExtensionSaver(extension_code) |
| 82 | # try: |
| 83 | # fiddle w/ the extension registry's stuff for extension_code |
| 84 | # finally: |
| 85 | # e.restore() |
| 86 | |
| 87 | class ExtensionSaver: |
| 88 | # Remember current registration for code (if any), and remove it (if |
| 89 | # there is one). |
| 90 | def __init__(self, code): |
| 91 | self.code = code |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 92 | if code in copy_reg._inverted_registry: |
| 93 | self.pair = copy_reg._inverted_registry[code] |
| 94 | copy_reg.remove_extension(self.pair[0], self.pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 95 | else: |
| 96 | self.pair = None |
| 97 | |
| 98 | # Restore previous registration for code. |
| 99 | def restore(self): |
| 100 | code = self.code |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 101 | curpair = copy_reg._inverted_registry.get(code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 102 | if curpair is not None: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 103 | copy_reg.remove_extension(curpair[0], curpair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 104 | pair = self.pair |
| 105 | if pair is not None: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 106 | copy_reg.add_extension(pair[0], pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 107 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 108 | class C: |
| 109 | def __cmp__(self, other): |
| 110 | return cmp(self.__dict__, other.__dict__) |
| 111 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 112 | class D(C): |
| 113 | def __init__(self, arg): |
| 114 | pass |
| 115 | |
| 116 | class E(C): |
| 117 | def __getinitargs__(self): |
| 118 | return () |
| 119 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 120 | import __main__ |
| 121 | __main__.C = C |
| 122 | C.__module__ = "__main__" |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 123 | __main__.D = D |
| 124 | D.__module__ = "__main__" |
| 125 | __main__.E = E |
| 126 | E.__module__ = "__main__" |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 127 | |
| 128 | class myint(int): |
| 129 | def __init__(self, x): |
| 130 | self.str = str(x) |
| 131 | |
| 132 | class initarg(C): |
Guido van Rossum | 1444f67 | 2001-12-19 16:38:29 +0000 | [diff] [blame] | 133 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 134 | def __init__(self, a, b): |
| 135 | self.a = a |
| 136 | self.b = b |
| 137 | |
| 138 | def __getinitargs__(self): |
| 139 | return self.a, self.b |
| 140 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 141 | class metaclass(type): |
| 142 | pass |
| 143 | |
| 144 | class use_metaclass(object): |
| 145 | __metaclass__ = metaclass |
| 146 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 147 | class pickling_metaclass(type): |
| 148 | def __eq__(self, other): |
| 149 | return (type(self) == type(other) and |
| 150 | self.reduce_args == other.reduce_args) |
| 151 | |
| 152 | def __reduce__(self): |
| 153 | return (create_dynamic_class, self.reduce_args) |
| 154 | |
Ezio Melotti | 030aa35 | 2011-11-06 18:50:32 +0200 | [diff] [blame] | 155 | __hash__ = None |
| 156 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 157 | def create_dynamic_class(name, bases): |
| 158 | result = pickling_metaclass(name, bases, dict()) |
| 159 | result.reduce_args = (name, bases) |
| 160 | return result |
| 161 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 162 | # DATA0 .. DATA2 are the pickles we expect under the various protocols, for |
| 163 | # the object returned by create_data(). |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 164 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 165 | # break into multiple strings to avoid confusing font-lock-mode |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 166 | DATA0 = """(lp1 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 167 | I0 |
| 168 | aL1L |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 169 | aF2 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 170 | ac__builtin__ |
| 171 | complex |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 172 | p2 |
| 173 | """ + \ |
| 174 | """(F3 |
| 175 | F0 |
| 176 | tRp3 |
| 177 | aI1 |
| 178 | aI-1 |
| 179 | aI255 |
| 180 | aI-255 |
| 181 | aI-256 |
| 182 | aI65535 |
| 183 | aI-65535 |
| 184 | aI-65536 |
| 185 | aI2147483647 |
| 186 | aI-2147483647 |
| 187 | aI-2147483648 |
| 188 | a""" + \ |
| 189 | """(S'abc' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 190 | p4 |
| 191 | g4 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 192 | """ + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 193 | """(i__main__ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 194 | C |
| 195 | p5 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 196 | """ + \ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 197 | """(dp6 |
| 198 | S'foo' |
| 199 | p7 |
| 200 | I1 |
| 201 | sS'bar' |
| 202 | p8 |
| 203 | I2 |
| 204 | sbg5 |
| 205 | tp9 |
| 206 | ag9 |
| 207 | aI5 |
| 208 | a. |
| 209 | """ |
| 210 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 211 | # Disassembly of DATA0. |
| 212 | DATA0_DIS = """\ |
| 213 | 0: ( MARK |
| 214 | 1: l LIST (MARK at 0) |
| 215 | 2: p PUT 1 |
| 216 | 5: I INT 0 |
| 217 | 8: a APPEND |
| 218 | 9: L LONG 1L |
| 219 | 13: a APPEND |
| 220 | 14: F FLOAT 2.0 |
| 221 | 17: a APPEND |
| 222 | 18: c GLOBAL '__builtin__ complex' |
| 223 | 39: p PUT 2 |
| 224 | 42: ( MARK |
| 225 | 43: F FLOAT 3.0 |
| 226 | 46: F FLOAT 0.0 |
| 227 | 49: t TUPLE (MARK at 42) |
| 228 | 50: R REDUCE |
| 229 | 51: p PUT 3 |
| 230 | 54: a APPEND |
| 231 | 55: I INT 1 |
| 232 | 58: a APPEND |
| 233 | 59: I INT -1 |
| 234 | 63: a APPEND |
| 235 | 64: I INT 255 |
| 236 | 69: a APPEND |
| 237 | 70: I INT -255 |
| 238 | 76: a APPEND |
| 239 | 77: I INT -256 |
| 240 | 83: a APPEND |
| 241 | 84: I INT 65535 |
| 242 | 91: a APPEND |
| 243 | 92: I INT -65535 |
| 244 | 100: a APPEND |
| 245 | 101: I INT -65536 |
| 246 | 109: a APPEND |
| 247 | 110: I INT 2147483647 |
| 248 | 122: a APPEND |
| 249 | 123: I INT -2147483647 |
| 250 | 136: a APPEND |
| 251 | 137: I INT -2147483648 |
| 252 | 150: a APPEND |
| 253 | 151: ( MARK |
| 254 | 152: S STRING 'abc' |
| 255 | 159: p PUT 4 |
| 256 | 162: g GET 4 |
| 257 | 165: ( MARK |
| 258 | 166: i INST '__main__ C' (MARK at 165) |
| 259 | 178: p PUT 5 |
| 260 | 181: ( MARK |
| 261 | 182: d DICT (MARK at 181) |
| 262 | 183: p PUT 6 |
| 263 | 186: S STRING 'foo' |
| 264 | 193: p PUT 7 |
| 265 | 196: I INT 1 |
| 266 | 199: s SETITEM |
| 267 | 200: S STRING 'bar' |
| 268 | 207: p PUT 8 |
| 269 | 210: I INT 2 |
| 270 | 213: s SETITEM |
| 271 | 214: b BUILD |
| 272 | 215: g GET 5 |
| 273 | 218: t TUPLE (MARK at 151) |
| 274 | 219: p PUT 9 |
| 275 | 222: a APPEND |
| 276 | 223: g GET 9 |
| 277 | 226: a APPEND |
| 278 | 227: I INT 5 |
| 279 | 230: a APPEND |
| 280 | 231: . STOP |
| 281 | highest protocol among opcodes = 0 |
| 282 | """ |
| 283 | |
| 284 | DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' |
| 285 | 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' |
| 286 | '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' |
| 287 | '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' |
| 288 | 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' |
| 289 | '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' |
| 290 | 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' |
| 291 | '\x06tq\nh\nK\x05e.' |
| 292 | ) |
| 293 | |
| 294 | # Disassembly of DATA1. |
| 295 | DATA1_DIS = """\ |
| 296 | 0: ] EMPTY_LIST |
| 297 | 1: q BINPUT 1 |
| 298 | 3: ( MARK |
| 299 | 4: K BININT1 0 |
| 300 | 6: L LONG 1L |
| 301 | 10: G BINFLOAT 2.0 |
| 302 | 19: c GLOBAL '__builtin__ complex' |
| 303 | 40: q BINPUT 2 |
| 304 | 42: ( MARK |
| 305 | 43: G BINFLOAT 3.0 |
| 306 | 52: G BINFLOAT 0.0 |
| 307 | 61: t TUPLE (MARK at 42) |
| 308 | 62: R REDUCE |
| 309 | 63: q BINPUT 3 |
| 310 | 65: K BININT1 1 |
| 311 | 67: J BININT -1 |
| 312 | 72: K BININT1 255 |
| 313 | 74: J BININT -255 |
| 314 | 79: J BININT -256 |
| 315 | 84: M BININT2 65535 |
| 316 | 87: J BININT -65535 |
| 317 | 92: J BININT -65536 |
| 318 | 97: J BININT 2147483647 |
| 319 | 102: J BININT -2147483647 |
| 320 | 107: J BININT -2147483648 |
| 321 | 112: ( MARK |
| 322 | 113: U SHORT_BINSTRING 'abc' |
| 323 | 118: q BINPUT 4 |
| 324 | 120: h BINGET 4 |
| 325 | 122: ( MARK |
| 326 | 123: c GLOBAL '__main__ C' |
| 327 | 135: q BINPUT 5 |
| 328 | 137: o OBJ (MARK at 122) |
| 329 | 138: q BINPUT 6 |
| 330 | 140: } EMPTY_DICT |
| 331 | 141: q BINPUT 7 |
| 332 | 143: ( MARK |
| 333 | 144: U SHORT_BINSTRING 'foo' |
| 334 | 149: q BINPUT 8 |
| 335 | 151: K BININT1 1 |
| 336 | 153: U SHORT_BINSTRING 'bar' |
| 337 | 158: q BINPUT 9 |
| 338 | 160: K BININT1 2 |
| 339 | 162: u SETITEMS (MARK at 143) |
| 340 | 163: b BUILD |
| 341 | 164: h BINGET 6 |
| 342 | 166: t TUPLE (MARK at 112) |
| 343 | 167: q BINPUT 10 |
| 344 | 169: h BINGET 10 |
| 345 | 171: K BININT1 5 |
| 346 | 173: e APPENDS (MARK at 3) |
| 347 | 174: . STOP |
| 348 | highest protocol among opcodes = 1 |
| 349 | """ |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 350 | |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 351 | DATA2 = ('\x80\x02]q\x01(K\x00\x8a\x01\x01G@\x00\x00\x00\x00\x00\x00\x00' |
| 352 | 'c__builtin__\ncomplex\nq\x02G@\x08\x00\x00\x00\x00\x00\x00G\x00' |
| 353 | '\x00\x00\x00\x00\x00\x00\x00\x86Rq\x03K\x01J\xff\xff\xff\xffK' |
| 354 | '\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xff' |
| 355 | 'J\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00' |
| 356 | '\x80(U\x03abcq\x04h\x04(c__main__\nC\nq\x05oq\x06}q\x07(U\x03foo' |
| 357 | 'q\x08K\x01U\x03barq\tK\x02ubh\x06tq\nh\nK\x05e.') |
| 358 | |
| 359 | # Disassembly of DATA2. |
| 360 | DATA2_DIS = """\ |
| 361 | 0: \x80 PROTO 2 |
| 362 | 2: ] EMPTY_LIST |
| 363 | 3: q BINPUT 1 |
| 364 | 5: ( MARK |
| 365 | 6: K BININT1 0 |
| 366 | 8: \x8a LONG1 1L |
| 367 | 11: G BINFLOAT 2.0 |
| 368 | 20: c GLOBAL '__builtin__ complex' |
| 369 | 41: q BINPUT 2 |
| 370 | 43: G BINFLOAT 3.0 |
| 371 | 52: G BINFLOAT 0.0 |
| 372 | 61: \x86 TUPLE2 |
| 373 | 62: R REDUCE |
| 374 | 63: q BINPUT 3 |
| 375 | 65: K BININT1 1 |
| 376 | 67: J BININT -1 |
| 377 | 72: K BININT1 255 |
| 378 | 74: J BININT -255 |
| 379 | 79: J BININT -256 |
| 380 | 84: M BININT2 65535 |
| 381 | 87: J BININT -65535 |
| 382 | 92: J BININT -65536 |
| 383 | 97: J BININT 2147483647 |
| 384 | 102: J BININT -2147483647 |
| 385 | 107: J BININT -2147483648 |
| 386 | 112: ( MARK |
| 387 | 113: U SHORT_BINSTRING 'abc' |
| 388 | 118: q BINPUT 4 |
| 389 | 120: h BINGET 4 |
| 390 | 122: ( MARK |
| 391 | 123: c GLOBAL '__main__ C' |
| 392 | 135: q BINPUT 5 |
| 393 | 137: o OBJ (MARK at 122) |
| 394 | 138: q BINPUT 6 |
| 395 | 140: } EMPTY_DICT |
| 396 | 141: q BINPUT 7 |
| 397 | 143: ( MARK |
| 398 | 144: U SHORT_BINSTRING 'foo' |
| 399 | 149: q BINPUT 8 |
| 400 | 151: K BININT1 1 |
| 401 | 153: U SHORT_BINSTRING 'bar' |
| 402 | 158: q BINPUT 9 |
| 403 | 160: K BININT1 2 |
| 404 | 162: u SETITEMS (MARK at 143) |
| 405 | 163: b BUILD |
| 406 | 164: h BINGET 6 |
| 407 | 166: t TUPLE (MARK at 112) |
| 408 | 167: q BINPUT 10 |
| 409 | 169: h BINGET 10 |
| 410 | 171: K BININT1 5 |
| 411 | 173: e APPENDS (MARK at 5) |
| 412 | 174: . STOP |
| 413 | highest protocol among opcodes = 2 |
| 414 | """ |
| 415 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 416 | def create_data(): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 417 | c = C() |
| 418 | c.foo = 1 |
| 419 | c.bar = 2 |
| 420 | x = [0, 1L, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 421 | # Append some integer test cases at cPickle.c's internal size |
| 422 | # cutoffs. |
| 423 | uint1max = 0xff |
| 424 | uint2max = 0xffff |
| 425 | int4max = 0x7fffffff |
| 426 | x.extend([1, -1, |
| 427 | uint1max, -uint1max, -uint1max-1, |
| 428 | uint2max, -uint2max, -uint2max-1, |
| 429 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 430 | y = ('abc', 'abc', c, c) |
| 431 | x.append(y) |
| 432 | x.append(y) |
| 433 | x.append(5) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 434 | return x |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 435 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 436 | |
| 437 | class AbstractUnpickleTests(unittest.TestCase): |
| 438 | # Subclass must define self.loads, self.error. |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 439 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 440 | _testdata = create_data() |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 441 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 442 | def assert_is_copy(self, obj, objcopy, msg=None): |
| 443 | """Utility method to verify if two objects are copies of each others. |
| 444 | """ |
| 445 | if msg is None: |
| 446 | msg = "{!r} is not a copy of {!r}".format(obj, objcopy) |
| 447 | self.assertEqual(obj, objcopy, msg=msg) |
| 448 | self.assertIs(type(obj), type(objcopy), msg=msg) |
| 449 | if hasattr(obj, '__dict__'): |
| 450 | self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg) |
| 451 | self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg) |
| 452 | if hasattr(obj, '__slots__'): |
| 453 | self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg) |
| 454 | for slot in obj.__slots__: |
| 455 | self.assertEqual( |
| 456 | hasattr(obj, slot), hasattr(objcopy, slot), msg=msg) |
| 457 | self.assertEqual(getattr(obj, slot, None), |
| 458 | getattr(objcopy, slot, None), msg=msg) |
| 459 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 460 | def test_load_from_canned_string(self): |
| 461 | expected = self._testdata |
| 462 | for canned in DATA0, DATA1, DATA2: |
| 463 | got = self.loads(canned) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 464 | self.assert_is_copy(expected, got) |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 465 | |
| 466 | def test_garyp(self): |
| 467 | self.assertRaises(self.error, self.loads, 'garyp') |
| 468 | |
| 469 | def test_maxint64(self): |
| 470 | maxint64 = (1L << 63) - 1 |
| 471 | data = 'I' + str(maxint64) + '\n.' |
| 472 | got = self.loads(data) |
| 473 | self.assertEqual(got, maxint64) |
| 474 | |
| 475 | # Try too with a bogus literal. |
| 476 | data = 'I' + str(maxint64) + 'JUNK\n.' |
| 477 | self.assertRaises(ValueError, self.loads, data) |
| 478 | |
| 479 | def test_insecure_strings(self): |
| 480 | insecure = ["abc", "2 + 2", # not quoted |
| 481 | #"'abc' + 'def'", # not a single quoted string |
| 482 | "'abc", # quote is not closed |
| 483 | "'abc\"", # open quote and close quote don't match |
| 484 | "'abc' ?", # junk after close quote |
| 485 | "'\\'", # trailing backslash |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 486 | # issue #17710 |
| 487 | "'", '"', |
| 488 | "' ", '" ', |
| 489 | '\'"', '"\'', |
| 490 | " ''", ' ""', |
| 491 | ' ', |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 492 | # some tests of the quoting rules |
| 493 | #"'abc\"\''", |
| 494 | #"'\\\\a\'\'\'\\\'\\\\\''", |
| 495 | ] |
| 496 | for s in insecure: |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 497 | buf = "S" + s + "\n." |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 498 | self.assertRaises(ValueError, self.loads, buf) |
| 499 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 500 | def test_correctly_quoted_string(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 501 | goodpickles = [("S''\n.", ''), |
| 502 | ('S""\n.', ''), |
| 503 | ('S"\\n"\n.', '\n'), |
| 504 | ("S'\\n'\n.", '\n')] |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 505 | for p, expected in goodpickles: |
| 506 | self.assertEqual(self.loads(p), expected) |
| 507 | |
| 508 | def test_load_classic_instance(self): |
| 509 | # See issue5180. Test loading 2.x pickles that |
| 510 | # contain an instance of old style class. |
| 511 | for X, args in [(C, ()), (D, ('x',)), (E, ())]: |
| 512 | xname = X.__name__.encode('ascii') |
| 513 | # Protocol 0 (text mode pickle): |
| 514 | """ |
| 515 | 0: ( MARK |
| 516 | 1: i INST '__main__ X' (MARK at 0) |
| 517 | 13: p PUT 0 |
| 518 | 16: ( MARK |
| 519 | 17: d DICT (MARK at 16) |
| 520 | 18: p PUT 1 |
| 521 | 21: b BUILD |
| 522 | 22: . STOP |
| 523 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 524 | pickle0 = ("(i__main__\n" |
| 525 | "X\n" |
| 526 | "p0\n" |
| 527 | "(dp1\nb.").replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 528 | self.assert_is_copy(X(*args), self.loads(pickle0)) |
| 529 | |
| 530 | # Protocol 1 (binary mode pickle) |
| 531 | """ |
| 532 | 0: ( MARK |
| 533 | 1: c GLOBAL '__main__ X' |
| 534 | 13: q BINPUT 0 |
| 535 | 15: o OBJ (MARK at 0) |
| 536 | 16: q BINPUT 1 |
| 537 | 18: } EMPTY_DICT |
| 538 | 19: q BINPUT 2 |
| 539 | 21: b BUILD |
| 540 | 22: . STOP |
| 541 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 542 | pickle1 = ('(c__main__\n' |
| 543 | 'X\n' |
| 544 | 'q\x00oq\x01}q\x02b.').replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 545 | self.assert_is_copy(X(*args), self.loads(pickle1)) |
| 546 | |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 547 | # Protocol 2 (pickle2 = '\x80\x02' + pickle1) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 548 | """ |
| 549 | 0: \x80 PROTO 2 |
| 550 | 2: ( MARK |
| 551 | 3: c GLOBAL '__main__ X' |
| 552 | 15: q BINPUT 0 |
| 553 | 17: o OBJ (MARK at 2) |
| 554 | 18: q BINPUT 1 |
| 555 | 20: } EMPTY_DICT |
| 556 | 21: q BINPUT 2 |
| 557 | 23: b BUILD |
| 558 | 24: . STOP |
| 559 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 560 | pickle2 = ('\x80\x02(c__main__\n' |
| 561 | 'X\n' |
| 562 | 'q\x00oq\x01}q\x02b.').replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 563 | self.assert_is_copy(X(*args), self.loads(pickle2)) |
| 564 | |
| 565 | def test_pop_empty_stack(self): |
| 566 | # Test issue7455 |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 567 | s = '0' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 568 | self.assertRaises((cPickle.UnpicklingError, IndexError), self.loads, s) |
| 569 | |
| 570 | def test_load_str(self): |
| 571 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=0) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 572 | self.assertEqual(self.loads("S'a\\x00\\xa0'\n."), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 573 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=1) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 574 | self.assertEqual(self.loads('U\x03a\x00\xa0.'), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 575 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=2) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 576 | self.assertEqual(self.loads('\x80\x02U\x03a\x00\xa0.'), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 577 | |
| 578 | def test_load_unicode(self): |
| 579 | # From Python 2: pickle.dumps(u'Ï€', protocol=0) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 580 | self.assertEqual(self.loads('V\\u03c0\n.'), u'Ï€') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 581 | # From Python 2: pickle.dumps(u'Ï€', protocol=1) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 582 | self.assertEqual(self.loads('X\x02\x00\x00\x00\xcf\x80.'), u'Ï€') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 583 | # From Python 2: pickle.dumps(u'Ï€', protocol=2) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 584 | self.assertEqual(self.loads('\x80\x02X\x02\x00\x00\x00\xcf\x80.'), u'Ï€') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 585 | |
| 586 | def test_constants(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 587 | self.assertIsNone(self.loads('N.')) |
| 588 | self.assertIs(self.loads('\x88.'), True) |
| 589 | self.assertIs(self.loads('\x89.'), False) |
| 590 | self.assertIs(self.loads('I01\n.'), True) |
| 591 | self.assertIs(self.loads('I00\n.'), False) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 592 | |
| 593 | def test_misc_get(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 594 | self.assertRaises(self.error, self.loads, 'g0\np0\n') |
| 595 | self.assertRaises(self.error, self.loads, 'h\x00q\x00') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 596 | |
| 597 | def test_get(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 598 | pickled = '((lp100000\ng100000\nt.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 599 | unpickled = self.loads(pickled) |
| 600 | self.assertEqual(unpickled, ([],)*2) |
| 601 | self.assertIs(unpickled[0], unpickled[1]) |
| 602 | |
| 603 | def test_binget(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 604 | pickled = '(]q\xffh\xfft.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 605 | unpickled = self.loads(pickled) |
| 606 | self.assertEqual(unpickled, ([],)*2) |
| 607 | self.assertIs(unpickled[0], unpickled[1]) |
| 608 | |
| 609 | def test_long_binget(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 610 | pickled = '(]r\x00\x00\x01\x00j\x00\x00\x01\x00t.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 611 | unpickled = self.loads(pickled) |
| 612 | self.assertEqual(unpickled, ([],)*2) |
| 613 | self.assertIs(unpickled[0], unpickled[1]) |
| 614 | |
| 615 | def test_dup(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 616 | pickled = '((l2t.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 617 | unpickled = self.loads(pickled) |
| 618 | self.assertEqual(unpickled, ([],)*2) |
| 619 | self.assertIs(unpickled[0], unpickled[1]) |
| 620 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 621 | |
| 622 | class AbstractPickleTests(unittest.TestCase): |
| 623 | # Subclass must define self.dumps, self.loads. |
| 624 | |
| 625 | _testdata = AbstractUnpickleTests._testdata |
| 626 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 627 | def setUp(self): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 628 | pass |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 629 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 630 | def test_misc(self): |
| 631 | # test various datatypes not tested by testdata |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 632 | for proto in protocols: |
| 633 | x = myint(4) |
| 634 | s = self.dumps(x, proto) |
| 635 | y = self.loads(s) |
| 636 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 637 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 638 | x = (1, ()) |
| 639 | s = self.dumps(x, proto) |
| 640 | y = self.loads(s) |
| 641 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 642 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 643 | x = initarg(1, x) |
| 644 | s = self.dumps(x, proto) |
| 645 | y = self.loads(s) |
| 646 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 647 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 648 | # XXX test __reduce__ protocol? |
| 649 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 650 | def test_roundtrip_equality(self): |
| 651 | expected = self._testdata |
| 652 | for proto in protocols: |
| 653 | s = self.dumps(expected, proto) |
| 654 | got = self.loads(s) |
| 655 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 656 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 657 | # There are gratuitous differences between pickles produced by |
| 658 | # pickle and cPickle, largely because cPickle starts PUT indices at |
| 659 | # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- |
| 660 | # there's a comment with an exclamation point there whose meaning |
| 661 | # is a mystery. cPickle also suppresses PUT for objects with a refcount |
| 662 | # of 1. |
| 663 | def dont_test_disassembly(self): |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 664 | from pickletools import dis |
| 665 | |
| 666 | for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): |
| 667 | s = self.dumps(self._testdata, proto) |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 668 | filelike = cStringIO.StringIO() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 669 | dis(s, out=filelike) |
| 670 | got = filelike.getvalue() |
| 671 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 672 | |
| 673 | def test_recursive_list(self): |
| 674 | l = [] |
| 675 | l.append(l) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 676 | for proto in protocols: |
| 677 | s = self.dumps(l, proto) |
| 678 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 679 | self.assertEqual(len(x), 1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 680 | self.assertTrue(x is x[0]) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 681 | |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 682 | def test_recursive_tuple(self): |
| 683 | t = ([],) |
| 684 | t[0].append(t) |
| 685 | for proto in protocols: |
| 686 | s = self.dumps(t, proto) |
| 687 | x = self.loads(s) |
| 688 | self.assertEqual(len(x), 1) |
| 689 | self.assertEqual(len(x[0]), 1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 690 | self.assertTrue(x is x[0][0]) |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 691 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 692 | def test_recursive_dict(self): |
| 693 | d = {} |
| 694 | d[1] = d |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 695 | for proto in protocols: |
| 696 | s = self.dumps(d, proto) |
| 697 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 698 | self.assertEqual(x.keys(), [1]) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 699 | self.assertTrue(x[1] is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 700 | |
| 701 | def test_recursive_inst(self): |
| 702 | i = C() |
| 703 | i.attr = i |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 704 | for proto in protocols: |
Ezio Melotti | a84ecc6 | 2013-03-04 15:23:12 +0200 | [diff] [blame] | 705 | s = self.dumps(i, proto) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 706 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 707 | self.assertEqual(dir(x), dir(i)) |
Ezio Melotti | a84ecc6 | 2013-03-04 15:23:12 +0200 | [diff] [blame] | 708 | self.assertIs(x.attr, x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 709 | |
| 710 | def test_recursive_multi(self): |
| 711 | l = [] |
| 712 | d = {1:l} |
| 713 | i = C() |
| 714 | i.attr = d |
| 715 | l.append(i) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 716 | for proto in protocols: |
| 717 | s = self.dumps(l, proto) |
| 718 | x = self.loads(s) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 719 | self.assertEqual(len(x), 1) |
| 720 | self.assertEqual(dir(x[0]), dir(i)) |
| 721 | self.assertEqual(x[0].attr.keys(), [1]) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 722 | self.assertTrue(x[0].attr[1] is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 723 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 724 | if have_unicode: |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 725 | def test_unicode(self): |
Alexandre Vassalotti | e57e999 | 2008-12-27 10:02:59 +0000 | [diff] [blame] | 726 | endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>', |
| 727 | u'<\\>', u'<\\\U00012345>'] |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 728 | for proto in protocols: |
| 729 | for u in endcases: |
| 730 | p = self.dumps(u, proto) |
| 731 | u2 = self.loads(p) |
| 732 | self.assertEqual(u2, u) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 733 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 734 | def test_unicode_high_plane(self): |
| 735 | t = u'\U00012345' |
| 736 | for proto in protocols: |
| 737 | p = self.dumps(t, proto) |
| 738 | t2 = self.loads(p) |
| 739 | self.assertEqual(t2, t) |
| 740 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 741 | def test_ints(self): |
| 742 | import sys |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 743 | for proto in protocols: |
| 744 | n = sys.maxint |
| 745 | while n: |
| 746 | for expected in (-n, n): |
| 747 | s = self.dumps(expected, proto) |
| 748 | n2 = self.loads(s) |
| 749 | self.assertEqual(expected, n2) |
| 750 | n = n >> 1 |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 751 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 752 | def test_long(self): |
| 753 | for proto in protocols: |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 754 | # 256 bytes is where LONG4 begins. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 755 | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
| 756 | nbase = 1L << nbits |
| 757 | for npos in nbase-1, nbase, nbase+1: |
| 758 | for n in npos, -npos: |
| 759 | pickle = self.dumps(n, proto) |
| 760 | got = self.loads(pickle) |
| 761 | self.assertEqual(n, got) |
| 762 | # Try a monster. This is quadratic-time in protos 0 & 1, so don't |
| 763 | # bother with those. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 764 | nbase = long("deadbeeffeedface", 16) |
| 765 | nbase += nbase << 1000000 |
| 766 | for n in nbase, -nbase: |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 767 | p = self.dumps(n, 2) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 768 | got = self.loads(p) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 769 | self.assertEqual(n, got) |
| 770 | |
Mark Dickinson | a3ecd2c | 2009-01-24 16:40:29 +0000 | [diff] [blame] | 771 | def test_float(self): |
| 772 | test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5, |
| 773 | 3.14, 263.44582062374053, 6.022e23, 1e30] |
| 774 | test_values = test_values + [-x for x in test_values] |
| 775 | for proto in protocols: |
| 776 | for value in test_values: |
| 777 | pickle = self.dumps(value, proto) |
| 778 | got = self.loads(pickle) |
| 779 | self.assertEqual(value, got) |
| 780 | |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 781 | @run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
| 782 | def test_float_format(self): |
| 783 | # make sure that floats are formatted locale independent |
| 784 | self.assertEqual(self.dumps(1.2)[0:3], 'F1.') |
| 785 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 786 | def test_reduce(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 787 | pass |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 788 | |
| 789 | def test_getinitargs(self): |
| 790 | pass |
| 791 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 792 | def test_metaclass(self): |
| 793 | a = use_metaclass() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 794 | for proto in protocols: |
| 795 | s = self.dumps(a, proto) |
| 796 | b = self.loads(s) |
| 797 | self.assertEqual(a.__class__, b.__class__) |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 799 | def test_dynamic_class(self): |
| 800 | a = create_dynamic_class("my_dynamic_class", (object,)) |
| 801 | copy_reg.pickle(pickling_metaclass, pickling_metaclass.__reduce__) |
| 802 | for proto in protocols: |
| 803 | s = self.dumps(a, proto) |
| 804 | b = self.loads(s) |
| 805 | self.assertEqual(a, b) |
| 806 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 807 | def test_structseq(self): |
| 808 | import time |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 809 | import os |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 810 | |
| 811 | t = time.localtime() |
| 812 | for proto in protocols: |
| 813 | s = self.dumps(t, proto) |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 814 | u = self.loads(s) |
| 815 | self.assertEqual(t, u) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 816 | if hasattr(os, "stat"): |
| 817 | t = os.stat(os.curdir) |
| 818 | s = self.dumps(t, proto) |
| 819 | u = self.loads(s) |
| 820 | self.assertEqual(t, u) |
| 821 | if hasattr(os, "statvfs"): |
| 822 | t = os.statvfs(os.curdir) |
| 823 | s = self.dumps(t, proto) |
| 824 | u = self.loads(s) |
| 825 | self.assertEqual(t, u) |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 827 | # Tests for protocol 2 |
| 828 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 829 | def test_proto(self): |
| 830 | build_none = pickle.NONE + pickle.STOP |
| 831 | for proto in protocols: |
| 832 | expected = build_none |
| 833 | if proto >= 2: |
| 834 | expected = pickle.PROTO + chr(proto) + expected |
| 835 | p = self.dumps(None, proto) |
| 836 | self.assertEqual(p, expected) |
| 837 | |
| 838 | oob = protocols[-1] + 1 # a future protocol |
| 839 | badpickle = pickle.PROTO + chr(oob) + build_none |
| 840 | try: |
| 841 | self.loads(badpickle) |
| 842 | except ValueError, detail: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 843 | self.assertTrue(str(detail).startswith( |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 844 | "unsupported pickle protocol")) |
| 845 | else: |
| 846 | self.fail("expected bad protocol number to raise ValueError") |
| 847 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 848 | def test_long1(self): |
| 849 | x = 12345678910111213141516178920L |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 850 | for proto in protocols: |
| 851 | s = self.dumps(x, proto) |
| 852 | y = self.loads(s) |
| 853 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 854 | self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 855 | |
| 856 | def test_long4(self): |
| 857 | x = 12345678910111213141516178920L << (256*8) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 858 | for proto in protocols: |
| 859 | s = self.dumps(x, proto) |
| 860 | y = self.loads(s) |
| 861 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 862 | self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 863 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 864 | def test_short_tuples(self): |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 865 | # Map (proto, len(tuple)) to expected opcode. |
| 866 | expected_opcode = {(0, 0): pickle.TUPLE, |
| 867 | (0, 1): pickle.TUPLE, |
| 868 | (0, 2): pickle.TUPLE, |
| 869 | (0, 3): pickle.TUPLE, |
| 870 | (0, 4): pickle.TUPLE, |
| 871 | |
| 872 | (1, 0): pickle.EMPTY_TUPLE, |
| 873 | (1, 1): pickle.TUPLE, |
| 874 | (1, 2): pickle.TUPLE, |
| 875 | (1, 3): pickle.TUPLE, |
| 876 | (1, 4): pickle.TUPLE, |
| 877 | |
| 878 | (2, 0): pickle.EMPTY_TUPLE, |
| 879 | (2, 1): pickle.TUPLE1, |
| 880 | (2, 2): pickle.TUPLE2, |
| 881 | (2, 3): pickle.TUPLE3, |
| 882 | (2, 4): pickle.TUPLE, |
| 883 | } |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 884 | a = () |
Guido van Rossum | 025bc2f | 2003-01-28 04:20:02 +0000 | [diff] [blame] | 885 | b = (1,) |
| 886 | c = (1, 2) |
| 887 | d = (1, 2, 3) |
| 888 | e = (1, 2, 3, 4) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 889 | for proto in protocols: |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 890 | for x in a, b, c, d, e: |
| 891 | s = self.dumps(x, proto) |
| 892 | y = self.loads(s) |
| 893 | self.assertEqual(x, y, (proto, x, s, y)) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 894 | expected = expected_opcode[proto, len(x)] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 895 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 896 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 897 | def test_singletons(self): |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 898 | # Map (proto, singleton) to expected opcode. |
| 899 | expected_opcode = {(0, None): pickle.NONE, |
| 900 | (1, None): pickle.NONE, |
| 901 | (2, None): pickle.NONE, |
| 902 | |
| 903 | (0, True): pickle.INT, |
| 904 | (1, True): pickle.INT, |
| 905 | (2, True): pickle.NEWTRUE, |
| 906 | |
| 907 | (0, False): pickle.INT, |
| 908 | (1, False): pickle.INT, |
| 909 | (2, False): pickle.NEWFALSE, |
| 910 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 911 | for proto in protocols: |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 912 | for x in None, False, True: |
| 913 | s = self.dumps(x, proto) |
| 914 | y = self.loads(s) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 915 | self.assertTrue(x is y, (proto, x, s, y)) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 916 | expected = expected_opcode[proto, x] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 917 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 919 | def test_newobj_tuple(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 920 | x = MyTuple([1, 2, 3]) |
| 921 | x.foo = 42 |
| 922 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 923 | for proto in protocols: |
| 924 | s = self.dumps(x, proto) |
| 925 | y = self.loads(s) |
| 926 | self.assertEqual(tuple(x), tuple(y)) |
| 927 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 928 | |
| 929 | def test_newobj_list(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 930 | x = MyList([1, 2, 3]) |
| 931 | x.foo = 42 |
| 932 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 933 | for proto in protocols: |
| 934 | s = self.dumps(x, proto) |
| 935 | y = self.loads(s) |
| 936 | self.assertEqual(list(x), list(y)) |
| 937 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 938 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 939 | def test_newobj_generic(self): |
Tim Peters | 5013bd9 | 2003-02-03 22:28:41 +0000 | [diff] [blame] | 940 | for proto in protocols: |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 941 | for C in myclasses: |
| 942 | B = C.__base__ |
| 943 | x = C(C.sample) |
| 944 | x.foo = 42 |
| 945 | s = self.dumps(x, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 946 | y = self.loads(s) |
| 947 | detail = (proto, C, B, x, y, type(y)) |
| 948 | self.assertEqual(B(x), B(y), detail) |
| 949 | self.assertEqual(x.__dict__, y.__dict__, detail) |
| 950 | |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 951 | # Register a type with copy_reg, with extension code extcode. Pickle |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 952 | # an object of that type. Check that the resulting pickle uses opcode |
| 953 | # (EXT[124]) under proto 2, and not in proto 1. |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 954 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 955 | def produce_global_ext(self, extcode, opcode): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 956 | e = ExtensionSaver(extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 957 | try: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 958 | copy_reg.add_extension(__name__, "MyList", extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 959 | x = MyList([1, 2, 3]) |
| 960 | x.foo = 42 |
| 961 | x.bar = "hello" |
| 962 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 963 | # Dump using protocol 1 for comparison. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 964 | s1 = self.dumps(x, 1) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 965 | self.assertIn(__name__, s1) |
| 966 | self.assertIn("MyList", s1) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 967 | self.assertEqual(opcode_in_pickle(opcode, s1), False) |
| 968 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 969 | y = self.loads(s1) |
| 970 | self.assertEqual(list(x), list(y)) |
| 971 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 972 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 973 | # Dump using protocol 2 for test. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 974 | s2 = self.dumps(x, 2) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 975 | self.assertNotIn(__name__, s2) |
| 976 | self.assertNotIn("MyList", s2) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 977 | self.assertEqual(opcode_in_pickle(opcode, s2), True) |
| 978 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 979 | y = self.loads(s2) |
| 980 | self.assertEqual(list(x), list(y)) |
| 981 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 982 | |
| 983 | finally: |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 984 | e.restore() |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 985 | |
| 986 | def test_global_ext1(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 987 | self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code |
| 988 | self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 989 | |
| 990 | def test_global_ext2(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 991 | self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code |
| 992 | self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code |
| 993 | self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 994 | |
| 995 | def test_global_ext4(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 996 | self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code |
| 997 | self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code |
| 998 | self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness |
| 999 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 1000 | def test_list_chunking(self): |
| 1001 | n = 10 # too small to chunk |
| 1002 | x = range(n) |
| 1003 | for proto in protocols: |
| 1004 | s = self.dumps(x, proto) |
| 1005 | y = self.loads(s) |
| 1006 | self.assertEqual(x, y) |
| 1007 | num_appends = count_opcode(pickle.APPENDS, s) |
| 1008 | self.assertEqual(num_appends, proto > 0) |
| 1009 | |
| 1010 | n = 2500 # expect at least two chunks when proto > 0 |
| 1011 | x = range(n) |
| 1012 | for proto in protocols: |
| 1013 | s = self.dumps(x, proto) |
| 1014 | y = self.loads(s) |
| 1015 | self.assertEqual(x, y) |
| 1016 | num_appends = count_opcode(pickle.APPENDS, s) |
| 1017 | if proto == 0: |
| 1018 | self.assertEqual(num_appends, 0) |
| 1019 | else: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1020 | self.assertTrue(num_appends >= 2) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 1021 | |
| 1022 | def test_dict_chunking(self): |
| 1023 | n = 10 # too small to chunk |
| 1024 | x = dict.fromkeys(range(n)) |
| 1025 | for proto in protocols: |
| 1026 | s = self.dumps(x, proto) |
| 1027 | y = self.loads(s) |
| 1028 | self.assertEqual(x, y) |
| 1029 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 1030 | self.assertEqual(num_setitems, proto > 0) |
| 1031 | |
| 1032 | n = 2500 # expect at least two chunks when proto > 0 |
| 1033 | x = dict.fromkeys(range(n)) |
| 1034 | for proto in protocols: |
| 1035 | s = self.dumps(x, proto) |
| 1036 | y = self.loads(s) |
| 1037 | self.assertEqual(x, y) |
| 1038 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 1039 | if proto == 0: |
| 1040 | self.assertEqual(num_setitems, 0) |
| 1041 | else: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1042 | self.assertTrue(num_setitems >= 2) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1043 | |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1044 | def test_simple_newobj(self): |
| 1045 | x = object.__new__(SimpleNewObj) # avoid __init__ |
| 1046 | x.abc = 666 |
| 1047 | for proto in protocols: |
| 1048 | s = self.dumps(x, proto) |
| 1049 | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2) |
| 1050 | y = self.loads(s) # will raise TypeError if __init__ called |
| 1051 | self.assertEqual(y.abc, 666) |
| 1052 | self.assertEqual(x.__dict__, y.__dict__) |
| 1053 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1054 | def test_newobj_list_slots(self): |
| 1055 | x = SlotList([1, 2, 3]) |
| 1056 | x.foo = 42 |
| 1057 | x.bar = "hello" |
| 1058 | s = self.dumps(x, 2) |
| 1059 | y = self.loads(s) |
| 1060 | self.assertEqual(list(x), list(y)) |
| 1061 | self.assertEqual(x.__dict__, y.__dict__) |
| 1062 | self.assertEqual(x.foo, y.foo) |
| 1063 | self.assertEqual(x.bar, y.bar) |
| 1064 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1065 | def test_reduce_overrides_default_reduce_ex(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1066 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1067 | x = REX_one() |
| 1068 | self.assertEqual(x._reduce_called, 0) |
| 1069 | s = self.dumps(x, proto) |
| 1070 | self.assertEqual(x._reduce_called, 1) |
| 1071 | y = self.loads(s) |
| 1072 | self.assertEqual(y._reduce_called, 0) |
| 1073 | |
| 1074 | def test_reduce_ex_called(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1075 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1076 | x = REX_two() |
| 1077 | self.assertEqual(x._proto, None) |
| 1078 | s = self.dumps(x, proto) |
| 1079 | self.assertEqual(x._proto, proto) |
| 1080 | y = self.loads(s) |
| 1081 | self.assertEqual(y._proto, None) |
| 1082 | |
| 1083 | def test_reduce_ex_overrides_reduce(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1084 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1085 | x = REX_three() |
| 1086 | self.assertEqual(x._proto, None) |
| 1087 | s = self.dumps(x, proto) |
| 1088 | self.assertEqual(x._proto, proto) |
| 1089 | y = self.loads(s) |
| 1090 | self.assertEqual(y._proto, None) |
| 1091 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1092 | def test_reduce_ex_calls_base(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1093 | for proto in protocols: |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1094 | x = REX_four() |
| 1095 | self.assertEqual(x._proto, None) |
| 1096 | s = self.dumps(x, proto) |
| 1097 | self.assertEqual(x._proto, proto) |
| 1098 | y = self.loads(s) |
| 1099 | self.assertEqual(y._proto, proto) |
| 1100 | |
| 1101 | def test_reduce_calls_base(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1102 | for proto in protocols: |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1103 | x = REX_five() |
| 1104 | self.assertEqual(x._reduce_called, 0) |
| 1105 | s = self.dumps(x, proto) |
| 1106 | self.assertEqual(x._reduce_called, 1) |
| 1107 | y = self.loads(s) |
| 1108 | self.assertEqual(y._reduce_called, 1) |
| 1109 | |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1110 | def test_reduce_bad_iterator(self): |
| 1111 | # Issue4176: crash when 4th and 5th items of __reduce__() |
| 1112 | # are not iterators |
| 1113 | class C(object): |
| 1114 | def __reduce__(self): |
| 1115 | # 4th item is not an iterator |
| 1116 | return list, (), None, [], None |
| 1117 | class D(object): |
| 1118 | def __reduce__(self): |
| 1119 | # 5th item is not an iterator |
| 1120 | return dict, (), None, None, [] |
| 1121 | |
| 1122 | # Protocol 0 is less strict and also accept iterables. |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1123 | for proto in protocols: |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1124 | try: |
| 1125 | self.dumps(C(), proto) |
| 1126 | except (AttributeError, pickle.PickleError, cPickle.PickleError): |
| 1127 | pass |
| 1128 | try: |
| 1129 | self.dumps(D(), proto) |
| 1130 | except (AttributeError, pickle.PickleError, cPickle.PickleError): |
| 1131 | pass |
| 1132 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1133 | def test_many_puts_and_gets(self): |
| 1134 | # Test that internal data structures correctly deal with lots of |
| 1135 | # puts/gets. |
| 1136 | keys = ("aaa" + str(i) for i in xrange(100)) |
| 1137 | large_dict = dict((k, [4, 5, 6]) for k in keys) |
| 1138 | obj = [dict(large_dict), dict(large_dict), dict(large_dict)] |
| 1139 | |
| 1140 | for proto in protocols: |
| 1141 | dumped = self.dumps(obj, proto) |
| 1142 | loaded = self.loads(dumped) |
| 1143 | self.assertEqual(loaded, obj, |
| 1144 | "Failed protocol %d: %r != %r" |
| 1145 | % (proto, obj, loaded)) |
| 1146 | |
Antoine Pitrou | 7430989 | 2009-05-02 21:13:23 +0000 | [diff] [blame] | 1147 | def test_attribute_name_interning(self): |
| 1148 | # Test that attribute names of pickled objects are interned when |
| 1149 | # unpickling. |
| 1150 | for proto in protocols: |
| 1151 | x = C() |
| 1152 | x.foo = 42 |
| 1153 | x.bar = "hello" |
| 1154 | s = self.dumps(x, proto) |
| 1155 | y = self.loads(s) |
| 1156 | x_keys = sorted(x.__dict__) |
| 1157 | y_keys = sorted(y.__dict__) |
| 1158 | for x_key, y_key in zip(x_keys, y_keys): |
| 1159 | self.assertIs(x_key, y_key) |
| 1160 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1161 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1162 | # Test classes for reduce_ex |
| 1163 | |
| 1164 | class REX_one(object): |
| 1165 | _reduce_called = 0 |
| 1166 | def __reduce__(self): |
| 1167 | self._reduce_called = 1 |
| 1168 | return REX_one, () |
| 1169 | # No __reduce_ex__ here, but inheriting it from object |
| 1170 | |
| 1171 | class REX_two(object): |
| 1172 | _proto = None |
| 1173 | def __reduce_ex__(self, proto): |
| 1174 | self._proto = proto |
| 1175 | return REX_two, () |
| 1176 | # No __reduce__ here, but inheriting it from object |
| 1177 | |
| 1178 | class REX_three(object): |
| 1179 | _proto = None |
| 1180 | def __reduce_ex__(self, proto): |
| 1181 | self._proto = proto |
| 1182 | return REX_two, () |
| 1183 | def __reduce__(self): |
| 1184 | raise TestFailed, "This __reduce__ shouldn't be called" |
| 1185 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1186 | class REX_four(object): |
| 1187 | _proto = None |
| 1188 | def __reduce_ex__(self, proto): |
| 1189 | self._proto = proto |
| 1190 | return object.__reduce_ex__(self, proto) |
| 1191 | # Calling base class method should succeed |
| 1192 | |
| 1193 | class REX_five(object): |
| 1194 | _reduce_called = 0 |
| 1195 | def __reduce__(self): |
| 1196 | self._reduce_called = 1 |
| 1197 | return object.__reduce__(self) |
| 1198 | # This one used to fail with infinite recursion |
| 1199 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1200 | # Test classes for newobj |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1201 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1202 | class MyInt(int): |
| 1203 | sample = 1 |
| 1204 | |
| 1205 | class MyLong(long): |
| 1206 | sample = 1L |
| 1207 | |
| 1208 | class MyFloat(float): |
| 1209 | sample = 1.0 |
| 1210 | |
| 1211 | class MyComplex(complex): |
| 1212 | sample = 1.0 + 0.0j |
| 1213 | |
| 1214 | class MyStr(str): |
| 1215 | sample = "hello" |
| 1216 | |
| 1217 | class MyUnicode(unicode): |
| 1218 | sample = u"hello \u1234" |
| 1219 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1220 | class MyTuple(tuple): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1221 | sample = (1, 2, 3) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1222 | |
| 1223 | class MyList(list): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1224 | sample = [1, 2, 3] |
| 1225 | |
| 1226 | class MyDict(dict): |
| 1227 | sample = {"a": 1, "b": 2} |
| 1228 | |
| 1229 | myclasses = [MyInt, MyLong, MyFloat, |
Guido van Rossum | 206b9a7 | 2003-03-02 13:53:18 +0000 | [diff] [blame] | 1230 | MyComplex, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1231 | MyStr, MyUnicode, |
| 1232 | MyTuple, MyList, MyDict] |
| 1233 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | c8d6ef5 | 2003-01-28 22:02:31 +0000 | [diff] [blame] | 1235 | class SlotList(MyList): |
| 1236 | __slots__ = ["foo"] |
| 1237 | |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1238 | class SimpleNewObj(object): |
| 1239 | def __init__(self, a, b, c): |
| 1240 | # raise an error, to make sure this isn't called |
| 1241 | raise TypeError("SimpleNewObj.__init__() didn't expect to get called") |
| 1242 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1243 | class AbstractPickleModuleTests(unittest.TestCase): |
| 1244 | |
| 1245 | def test_dump_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1246 | import os |
| 1247 | f = open(TESTFN, "w") |
| 1248 | try: |
| 1249 | f.close() |
| 1250 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 1251 | finally: |
| 1252 | os.remove(TESTFN) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1253 | |
| 1254 | def test_load_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1255 | import os |
| 1256 | f = open(TESTFN, "w") |
| 1257 | try: |
| 1258 | f.close() |
| 1259 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 1260 | finally: |
| 1261 | os.remove(TESTFN) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1262 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1263 | def test_load_from_and_dump_to_file(self): |
| 1264 | stream = cStringIO.StringIO() |
| 1265 | data = [123, {}, 124] |
| 1266 | self.module.dump(data, stream) |
| 1267 | stream.seek(0) |
| 1268 | unpickled = self.module.load(stream) |
| 1269 | self.assertEqual(unpickled, data) |
| 1270 | |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1271 | def test_highest_protocol(self): |
| 1272 | # Of course this needs to be changed when HIGHEST_PROTOCOL changes. |
| 1273 | self.assertEqual(self.module.HIGHEST_PROTOCOL, 2) |
| 1274 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1275 | def test_callapi(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1276 | f = cStringIO.StringIO() |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1277 | # With and without keyword arguments |
| 1278 | self.module.dump(123, f, -1) |
| 1279 | self.module.dump(123, file=f, protocol=-1) |
| 1280 | self.module.dumps(123, -1) |
| 1281 | self.module.dumps(123, protocol=-1) |
| 1282 | self.module.Pickler(f, -1) |
| 1283 | self.module.Pickler(f, protocol=-1) |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1284 | |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 1285 | def test_incomplete_input(self): |
| 1286 | s = StringIO.StringIO("X''.") |
| 1287 | self.assertRaises(EOFError, self.module.load, s) |
| 1288 | |
Alexandre Vassalotti | 8b2d713 | 2009-11-24 17:53:23 +0000 | [diff] [blame] | 1289 | def test_restricted(self): |
| 1290 | # issue7128: cPickle failed in restricted mode |
| 1291 | builtins = {self.module.__name__: self.module, |
| 1292 | '__import__': __import__} |
| 1293 | d = {} |
| 1294 | teststr = "def f(): {0}.dumps(0)".format(self.module.__name__) |
| 1295 | exec teststr in {'__builtins__': builtins}, d |
| 1296 | d['f']() |
| 1297 | |
Antoine Pitrou | 0d423b8 | 2010-01-07 17:46:49 +0000 | [diff] [blame] | 1298 | def test_bad_input(self): |
| 1299 | # Test issue4298 |
| 1300 | s = '\x58\0\0\0\x54' |
| 1301 | self.assertRaises(EOFError, self.module.loads, s) |
| 1302 | # Test issue7455 |
| 1303 | s = '0' |
| 1304 | # XXX Why doesn't pickle raise UnpicklingError? |
| 1305 | self.assertRaises((IndexError, cPickle.UnpicklingError), |
| 1306 | self.module.loads, s) |
Alexandre Vassalotti | 8b2d713 | 2009-11-24 17:53:23 +0000 | [diff] [blame] | 1307 | |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1308 | class AbstractPersistentPicklerTests(unittest.TestCase): |
| 1309 | |
| 1310 | # This class defines persistent_id() and persistent_load() |
| 1311 | # functions that should be used by the pickler. All even integers |
| 1312 | # are pickled using persistent ids. |
| 1313 | |
| 1314 | def persistent_id(self, object): |
| 1315 | if isinstance(object, int) and object % 2 == 0: |
| 1316 | self.id_count += 1 |
| 1317 | return str(object) |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1318 | elif object == "test_false_value": |
| 1319 | self.false_count += 1 |
| 1320 | return "" |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1321 | else: |
| 1322 | return None |
| 1323 | |
| 1324 | def persistent_load(self, oid): |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1325 | if not oid: |
| 1326 | self.load_false_count += 1 |
| 1327 | return "test_false_value" |
| 1328 | else: |
| 1329 | self.load_count += 1 |
| 1330 | object = int(oid) |
| 1331 | assert object % 2 == 0 |
| 1332 | return object |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1333 | |
| 1334 | def test_persistence(self): |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1335 | L = range(10) + ["test_false_value"] |
| 1336 | for proto in protocols: |
| 1337 | self.id_count = 0 |
| 1338 | self.false_count = 0 |
| 1339 | self.load_false_count = 0 |
| 1340 | self.load_count = 0 |
| 1341 | self.assertEqual(self.loads(self.dumps(L, proto)), L) |
| 1342 | self.assertEqual(self.id_count, 5) |
| 1343 | self.assertEqual(self.false_count, 1) |
| 1344 | self.assertEqual(self.load_count, 5) |
| 1345 | self.assertEqual(self.load_false_count, 1) |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1346 | |
| 1347 | class AbstractPicklerUnpicklerObjectTests(unittest.TestCase): |
| 1348 | |
| 1349 | pickler_class = None |
| 1350 | unpickler_class = None |
| 1351 | |
| 1352 | def setUp(self): |
| 1353 | assert self.pickler_class |
| 1354 | assert self.unpickler_class |
| 1355 | |
| 1356 | def test_clear_pickler_memo(self): |
| 1357 | # To test whether clear_memo() has any effect, we pickle an object, |
| 1358 | # then pickle it again without clearing the memo; the two serialized |
| 1359 | # forms should be different. If we clear_memo() and then pickle the |
| 1360 | # object again, the third serialized form should be identical to the |
| 1361 | # first one we obtained. |
| 1362 | data = ["abcdefg", "abcdefg", 44] |
| 1363 | f = cStringIO.StringIO() |
| 1364 | pickler = self.pickler_class(f) |
| 1365 | |
| 1366 | pickler.dump(data) |
| 1367 | first_pickled = f.getvalue() |
| 1368 | |
| 1369 | # Reset StringIO object. |
| 1370 | f.seek(0) |
| 1371 | f.truncate() |
| 1372 | |
| 1373 | pickler.dump(data) |
| 1374 | second_pickled = f.getvalue() |
| 1375 | |
| 1376 | # Reset the Pickler and StringIO objects. |
| 1377 | pickler.clear_memo() |
| 1378 | f.seek(0) |
| 1379 | f.truncate() |
| 1380 | |
| 1381 | pickler.dump(data) |
| 1382 | third_pickled = f.getvalue() |
| 1383 | |
| 1384 | self.assertNotEqual(first_pickled, second_pickled) |
| 1385 | self.assertEqual(first_pickled, third_pickled) |
| 1386 | |
| 1387 | def test_priming_pickler_memo(self): |
| 1388 | # Verify that we can set the Pickler's memo attribute. |
| 1389 | data = ["abcdefg", "abcdefg", 44] |
| 1390 | f = cStringIO.StringIO() |
| 1391 | pickler = self.pickler_class(f) |
| 1392 | |
| 1393 | pickler.dump(data) |
| 1394 | first_pickled = f.getvalue() |
| 1395 | |
| 1396 | f = cStringIO.StringIO() |
| 1397 | primed = self.pickler_class(f) |
| 1398 | primed.memo = pickler.memo |
| 1399 | |
| 1400 | primed.dump(data) |
| 1401 | primed_pickled = f.getvalue() |
| 1402 | |
| 1403 | self.assertNotEqual(first_pickled, primed_pickled) |
| 1404 | |
| 1405 | def test_priming_unpickler_memo(self): |
| 1406 | # Verify that we can set the Unpickler's memo attribute. |
| 1407 | data = ["abcdefg", "abcdefg", 44] |
| 1408 | f = cStringIO.StringIO() |
| 1409 | pickler = self.pickler_class(f) |
| 1410 | |
| 1411 | pickler.dump(data) |
| 1412 | first_pickled = f.getvalue() |
| 1413 | |
| 1414 | f = cStringIO.StringIO() |
| 1415 | primed = self.pickler_class(f) |
| 1416 | primed.memo = pickler.memo |
| 1417 | |
| 1418 | primed.dump(data) |
| 1419 | primed_pickled = f.getvalue() |
| 1420 | |
| 1421 | unpickler = self.unpickler_class(cStringIO.StringIO(first_pickled)) |
| 1422 | unpickled_data1 = unpickler.load() |
| 1423 | |
| 1424 | self.assertEqual(unpickled_data1, data) |
| 1425 | |
| 1426 | primed = self.unpickler_class(cStringIO.StringIO(primed_pickled)) |
| 1427 | primed.memo = unpickler.memo |
| 1428 | unpickled_data2 = primed.load() |
| 1429 | |
| 1430 | primed.memo.clear() |
| 1431 | |
| 1432 | self.assertEqual(unpickled_data2, data) |
| 1433 | self.assertTrue(unpickled_data2 is unpickled_data1) |
| 1434 | |
| 1435 | def test_reusing_unpickler_objects(self): |
| 1436 | data1 = ["abcdefg", "abcdefg", 44] |
| 1437 | f = cStringIO.StringIO() |
| 1438 | pickler = self.pickler_class(f) |
| 1439 | pickler.dump(data1) |
| 1440 | pickled1 = f.getvalue() |
| 1441 | |
| 1442 | data2 = ["abcdefg", 44, 44] |
| 1443 | f = cStringIO.StringIO() |
| 1444 | pickler = self.pickler_class(f) |
| 1445 | pickler.dump(data2) |
| 1446 | pickled2 = f.getvalue() |
| 1447 | |
| 1448 | f = cStringIO.StringIO() |
| 1449 | f.write(pickled1) |
| 1450 | f.seek(0) |
| 1451 | unpickler = self.unpickler_class(f) |
| 1452 | self.assertEqual(unpickler.load(), data1) |
| 1453 | |
| 1454 | f.seek(0) |
| 1455 | f.truncate() |
| 1456 | f.write(pickled2) |
| 1457 | f.seek(0) |
| 1458 | self.assertEqual(unpickler.load(), data2) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1459 | |
| 1460 | class BigmemPickleTests(unittest.TestCase): |
| 1461 | |
| 1462 | # Memory requirements: 1 byte per character for input strings, 1 byte |
| 1463 | # for pickled data, 1 byte for unpickled strings, 1 byte for internal |
| 1464 | # buffer and 1 byte of free space for resizing of internal buffer. |
| 1465 | |
| 1466 | @precisionbigmemtest(size=_2G + 100*_1M, memuse=5) |
| 1467 | def test_huge_strlist(self, size): |
| 1468 | chunksize = 2**20 |
| 1469 | data = [] |
| 1470 | while size > chunksize: |
| 1471 | data.append('x' * chunksize) |
| 1472 | size -= chunksize |
| 1473 | chunksize += 1 |
| 1474 | data.append('y' * size) |
| 1475 | |
| 1476 | try: |
| 1477 | for proto in protocols: |
| 1478 | try: |
| 1479 | pickled = self.dumps(data, proto) |
| 1480 | res = self.loads(pickled) |
| 1481 | self.assertEqual(res, data) |
| 1482 | finally: |
| 1483 | res = None |
| 1484 | pickled = None |
| 1485 | finally: |
| 1486 | data = None |