Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Serhiy Storchaka | 43566ae | 2017-05-02 18:26:25 +0300 | [diff] [blame^] | 2 | from __future__ import absolute_import |
| 3 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 4 | import unittest |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 5 | import pickle |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 6 | import cPickle |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 7 | import StringIO |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 8 | import cStringIO |
Tim Peters | 31f119e | 2003-02-03 16:20:13 +0000 | [diff] [blame] | 9 | import pickletools |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 10 | import copy_reg |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 11 | import sys |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 12 | |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 13 | from test import test_support as support |
Zachary Ware | 916c7c7 | 2015-11-27 01:21:51 -0600 | [diff] [blame] | 14 | from test.test_support import TestFailed, verbose, have_unicode, TESTFN |
Benjamin Peterson | d627e12 | 2013-03-30 10:36:31 -0400 | [diff] [blame] | 15 | try: |
| 16 | from test.test_support import _2G, _1M, precisionbigmemtest |
| 17 | except ImportError: |
| 18 | # 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] | 19 | _2G = _1M = 0 |
Benjamin Peterson | d627e12 | 2013-03-30 10:36:31 -0400 | [diff] [blame] | 20 | def precisionbigmemtest(*args, **kwargs): |
| 21 | return lambda self: None |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 22 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 23 | # Tests that try a number of pickle protocols should have a |
| 24 | # for proto in protocols: |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 25 | # kind of outer loop. |
| 26 | assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2 |
| 27 | protocols = range(pickle.HIGHEST_PROTOCOL + 1) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 28 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 29 | # Copy of test.test_support.run_with_locale. This is needed to support Python |
| 30 | # 2.4, which didn't include it. This is all to support test_xpickle, which |
| 31 | # bounces pickled objects through older Python versions to test backwards |
| 32 | # compatibility. |
| 33 | def run_with_locale(catstr, *locales): |
| 34 | def decorator(func): |
| 35 | def inner(*args, **kwds): |
| 36 | try: |
| 37 | import locale |
| 38 | category = getattr(locale, catstr) |
| 39 | orig_locale = locale.setlocale(category) |
| 40 | except AttributeError: |
| 41 | # if the test author gives us an invalid category string |
| 42 | raise |
| 43 | except: |
| 44 | # cannot retrieve original locale, so do nothing |
| 45 | locale = orig_locale = None |
| 46 | else: |
| 47 | for loc in locales: |
| 48 | try: |
| 49 | locale.setlocale(category, loc) |
| 50 | break |
| 51 | except: |
| 52 | pass |
| 53 | |
| 54 | # now run the function, resetting the locale on exceptions |
| 55 | try: |
| 56 | return func(*args, **kwds) |
| 57 | finally: |
| 58 | if locale and orig_locale: |
| 59 | locale.setlocale(category, orig_locale) |
| 60 | inner.func_name = func.func_name |
| 61 | inner.__doc__ = func.__doc__ |
| 62 | return inner |
| 63 | return decorator |
| 64 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 65 | def no_tracing(func): |
| 66 | """Decorator to temporarily turn off tracing for the duration of a test.""" |
| 67 | if not hasattr(sys, 'gettrace'): |
| 68 | return func |
| 69 | else: |
| 70 | def wrapper(*args, **kwargs): |
| 71 | original_trace = sys.gettrace() |
| 72 | try: |
| 73 | sys.settrace(None) |
| 74 | return func(*args, **kwargs) |
| 75 | finally: |
| 76 | sys.settrace(original_trace) |
| 77 | wrapper.__name__ = func.__name__ |
| 78 | return wrapper |
| 79 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 80 | |
| 81 | # Return True if opcode code appears in the pickle, else False. |
| 82 | def opcode_in_pickle(code, pickle): |
| 83 | for op, dummy, dummy in pickletools.genops(pickle): |
| 84 | if op.code == code: |
| 85 | return True |
| 86 | return False |
| 87 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 88 | # Return the number of times opcode code appears in pickle. |
| 89 | def count_opcode(code, pickle): |
| 90 | n = 0 |
| 91 | for op, dummy, dummy in pickletools.genops(pickle): |
| 92 | if op.code == code: |
| 93 | n += 1 |
| 94 | return n |
| 95 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 96 | class UnseekableIO(StringIO.StringIO): |
| 97 | def peek(self, *args): |
| 98 | raise NotImplementedError |
| 99 | |
| 100 | def seek(self, *args): |
| 101 | raise NotImplementedError |
| 102 | |
| 103 | def tell(self): |
| 104 | raise NotImplementedError |
| 105 | |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 106 | # We can't very well test the extension registry without putting known stuff |
| 107 | # in it, but we have to be careful to restore its original state. Code |
| 108 | # should do this: |
| 109 | # |
| 110 | # e = ExtensionSaver(extension_code) |
| 111 | # try: |
| 112 | # fiddle w/ the extension registry's stuff for extension_code |
| 113 | # finally: |
| 114 | # e.restore() |
| 115 | |
| 116 | class ExtensionSaver: |
| 117 | # Remember current registration for code (if any), and remove it (if |
| 118 | # there is one). |
| 119 | def __init__(self, code): |
| 120 | self.code = code |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 121 | if code in copy_reg._inverted_registry: |
| 122 | self.pair = copy_reg._inverted_registry[code] |
| 123 | copy_reg.remove_extension(self.pair[0], self.pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 124 | else: |
| 125 | self.pair = None |
| 126 | |
| 127 | # Restore previous registration for code. |
| 128 | def restore(self): |
| 129 | code = self.code |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 130 | curpair = copy_reg._inverted_registry.get(code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 131 | if curpair is not None: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 132 | copy_reg.remove_extension(curpair[0], curpair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 133 | pair = self.pair |
| 134 | if pair is not None: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 135 | copy_reg.add_extension(pair[0], pair[1], code) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 136 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 137 | class C: |
| 138 | def __cmp__(self, other): |
| 139 | return cmp(self.__dict__, other.__dict__) |
| 140 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 141 | class D(C): |
| 142 | def __init__(self, arg): |
| 143 | pass |
| 144 | |
| 145 | class E(C): |
| 146 | def __getinitargs__(self): |
| 147 | return () |
| 148 | |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 149 | class H(object): |
| 150 | pass |
| 151 | |
Serhiy Storchaka | 6560e22 | 2016-12-15 12:51:34 +0200 | [diff] [blame] | 152 | class MyErr(Exception): |
| 153 | def __init__(self): |
| 154 | pass |
| 155 | |
| 156 | class I: |
| 157 | def __init__(self, *args, **kwargs): |
| 158 | raise MyErr() |
| 159 | |
| 160 | def __getinitargs__(self): |
| 161 | return () |
| 162 | |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 163 | # Hashable mutable key |
| 164 | class K(object): |
| 165 | def __init__(self, value): |
| 166 | self.value = value |
| 167 | |
| 168 | def __reduce__(self): |
| 169 | # Shouldn't support the recursion itself |
| 170 | return K, (self.value,) |
| 171 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 172 | import __main__ |
| 173 | __main__.C = C |
| 174 | C.__module__ = "__main__" |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 175 | __main__.D = D |
| 176 | D.__module__ = "__main__" |
| 177 | __main__.E = E |
| 178 | E.__module__ = "__main__" |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 179 | __main__.H = H |
| 180 | H.__module__ = "__main__" |
Serhiy Storchaka | 6560e22 | 2016-12-15 12:51:34 +0200 | [diff] [blame] | 181 | __main__.I = I |
| 182 | I.__module__ = "__main__" |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 183 | __main__.K = K |
| 184 | K.__module__ = "__main__" |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 185 | |
| 186 | class myint(int): |
| 187 | def __init__(self, x): |
| 188 | self.str = str(x) |
| 189 | |
| 190 | class initarg(C): |
Guido van Rossum | 1444f67 | 2001-12-19 16:38:29 +0000 | [diff] [blame] | 191 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 192 | def __init__(self, a, b): |
| 193 | self.a = a |
| 194 | self.b = b |
| 195 | |
| 196 | def __getinitargs__(self): |
| 197 | return self.a, self.b |
| 198 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 199 | class metaclass(type): |
| 200 | pass |
| 201 | |
| 202 | class use_metaclass(object): |
| 203 | __metaclass__ = metaclass |
| 204 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 205 | class pickling_metaclass(type): |
| 206 | def __eq__(self, other): |
| 207 | return (type(self) == type(other) and |
| 208 | self.reduce_args == other.reduce_args) |
| 209 | |
| 210 | def __reduce__(self): |
| 211 | return (create_dynamic_class, self.reduce_args) |
| 212 | |
Ezio Melotti | 030aa35 | 2011-11-06 18:50:32 +0200 | [diff] [blame] | 213 | __hash__ = None |
| 214 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 215 | def create_dynamic_class(name, bases): |
| 216 | result = pickling_metaclass(name, bases, dict()) |
| 217 | result.reduce_args = (name, bases) |
| 218 | return result |
| 219 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 220 | # DATA0 .. DATA2 are the pickles we expect under the various protocols, for |
| 221 | # the object returned by create_data(). |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 222 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 223 | # break into multiple strings to avoid confusing font-lock-mode |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 224 | DATA0 = """(lp1 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 225 | I0 |
| 226 | aL1L |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 227 | aF2 |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 228 | ac__builtin__ |
| 229 | complex |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 230 | p2 |
| 231 | """ + \ |
| 232 | """(F3 |
| 233 | F0 |
| 234 | tRp3 |
| 235 | aI1 |
| 236 | aI-1 |
| 237 | aI255 |
| 238 | aI-255 |
| 239 | aI-256 |
| 240 | aI65535 |
| 241 | aI-65535 |
| 242 | aI-65536 |
| 243 | aI2147483647 |
| 244 | aI-2147483647 |
| 245 | aI-2147483648 |
| 246 | a""" + \ |
| 247 | """(S'abc' |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 248 | p4 |
| 249 | g4 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 250 | """ + \ |
Guido van Rossum | 42f92da | 2001-04-16 00:28:21 +0000 | [diff] [blame] | 251 | """(i__main__ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 252 | C |
| 253 | p5 |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 254 | """ + \ |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 255 | """(dp6 |
| 256 | S'foo' |
| 257 | p7 |
| 258 | I1 |
| 259 | sS'bar' |
| 260 | p8 |
| 261 | I2 |
| 262 | sbg5 |
| 263 | tp9 |
| 264 | ag9 |
| 265 | aI5 |
| 266 | a. |
| 267 | """ |
| 268 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 269 | # Disassembly of DATA0. |
| 270 | DATA0_DIS = """\ |
| 271 | 0: ( MARK |
| 272 | 1: l LIST (MARK at 0) |
| 273 | 2: p PUT 1 |
| 274 | 5: I INT 0 |
| 275 | 8: a APPEND |
| 276 | 9: L LONG 1L |
| 277 | 13: a APPEND |
| 278 | 14: F FLOAT 2.0 |
| 279 | 17: a APPEND |
| 280 | 18: c GLOBAL '__builtin__ complex' |
| 281 | 39: p PUT 2 |
| 282 | 42: ( MARK |
| 283 | 43: F FLOAT 3.0 |
| 284 | 46: F FLOAT 0.0 |
| 285 | 49: t TUPLE (MARK at 42) |
| 286 | 50: R REDUCE |
| 287 | 51: p PUT 3 |
| 288 | 54: a APPEND |
| 289 | 55: I INT 1 |
| 290 | 58: a APPEND |
| 291 | 59: I INT -1 |
| 292 | 63: a APPEND |
| 293 | 64: I INT 255 |
| 294 | 69: a APPEND |
| 295 | 70: I INT -255 |
| 296 | 76: a APPEND |
| 297 | 77: I INT -256 |
| 298 | 83: a APPEND |
| 299 | 84: I INT 65535 |
| 300 | 91: a APPEND |
| 301 | 92: I INT -65535 |
| 302 | 100: a APPEND |
| 303 | 101: I INT -65536 |
| 304 | 109: a APPEND |
| 305 | 110: I INT 2147483647 |
| 306 | 122: a APPEND |
| 307 | 123: I INT -2147483647 |
| 308 | 136: a APPEND |
| 309 | 137: I INT -2147483648 |
| 310 | 150: a APPEND |
| 311 | 151: ( MARK |
| 312 | 152: S STRING 'abc' |
| 313 | 159: p PUT 4 |
| 314 | 162: g GET 4 |
| 315 | 165: ( MARK |
| 316 | 166: i INST '__main__ C' (MARK at 165) |
| 317 | 178: p PUT 5 |
| 318 | 181: ( MARK |
| 319 | 182: d DICT (MARK at 181) |
| 320 | 183: p PUT 6 |
| 321 | 186: S STRING 'foo' |
| 322 | 193: p PUT 7 |
| 323 | 196: I INT 1 |
| 324 | 199: s SETITEM |
| 325 | 200: S STRING 'bar' |
| 326 | 207: p PUT 8 |
| 327 | 210: I INT 2 |
| 328 | 213: s SETITEM |
| 329 | 214: b BUILD |
| 330 | 215: g GET 5 |
| 331 | 218: t TUPLE (MARK at 151) |
| 332 | 219: p PUT 9 |
| 333 | 222: a APPEND |
| 334 | 223: g GET 9 |
| 335 | 226: a APPEND |
| 336 | 227: I INT 5 |
| 337 | 230: a APPEND |
| 338 | 231: . STOP |
| 339 | highest protocol among opcodes = 0 |
| 340 | """ |
| 341 | |
| 342 | DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00' |
| 343 | 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00' |
| 344 | '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff' |
| 345 | '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff' |
| 346 | 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00' |
| 347 | '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n' |
| 348 | 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh' |
| 349 | '\x06tq\nh\nK\x05e.' |
| 350 | ) |
| 351 | |
| 352 | # Disassembly of DATA1. |
| 353 | DATA1_DIS = """\ |
| 354 | 0: ] EMPTY_LIST |
| 355 | 1: q BINPUT 1 |
| 356 | 3: ( MARK |
| 357 | 4: K BININT1 0 |
| 358 | 6: L LONG 1L |
| 359 | 10: G BINFLOAT 2.0 |
| 360 | 19: c GLOBAL '__builtin__ complex' |
| 361 | 40: q BINPUT 2 |
| 362 | 42: ( MARK |
| 363 | 43: G BINFLOAT 3.0 |
| 364 | 52: G BINFLOAT 0.0 |
| 365 | 61: t TUPLE (MARK at 42) |
| 366 | 62: R REDUCE |
| 367 | 63: q BINPUT 3 |
| 368 | 65: K BININT1 1 |
| 369 | 67: J BININT -1 |
| 370 | 72: K BININT1 255 |
| 371 | 74: J BININT -255 |
| 372 | 79: J BININT -256 |
| 373 | 84: M BININT2 65535 |
| 374 | 87: J BININT -65535 |
| 375 | 92: J BININT -65536 |
| 376 | 97: J BININT 2147483647 |
| 377 | 102: J BININT -2147483647 |
| 378 | 107: J BININT -2147483648 |
| 379 | 112: ( MARK |
| 380 | 113: U SHORT_BINSTRING 'abc' |
| 381 | 118: q BINPUT 4 |
| 382 | 120: h BINGET 4 |
| 383 | 122: ( MARK |
| 384 | 123: c GLOBAL '__main__ C' |
| 385 | 135: q BINPUT 5 |
| 386 | 137: o OBJ (MARK at 122) |
| 387 | 138: q BINPUT 6 |
| 388 | 140: } EMPTY_DICT |
| 389 | 141: q BINPUT 7 |
| 390 | 143: ( MARK |
| 391 | 144: U SHORT_BINSTRING 'foo' |
| 392 | 149: q BINPUT 8 |
| 393 | 151: K BININT1 1 |
| 394 | 153: U SHORT_BINSTRING 'bar' |
| 395 | 158: q BINPUT 9 |
| 396 | 160: K BININT1 2 |
| 397 | 162: u SETITEMS (MARK at 143) |
| 398 | 163: b BUILD |
| 399 | 164: h BINGET 6 |
| 400 | 166: t TUPLE (MARK at 112) |
| 401 | 167: q BINPUT 10 |
| 402 | 169: h BINGET 10 |
| 403 | 171: K BININT1 5 |
| 404 | 173: e APPENDS (MARK at 3) |
| 405 | 174: . STOP |
| 406 | highest protocol among opcodes = 1 |
| 407 | """ |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 408 | |
Tim Peters | fc27375 | 2003-03-02 04:54:24 +0000 | [diff] [blame] | 409 | DATA2 = ('\x80\x02]q\x01(K\x00\x8a\x01\x01G@\x00\x00\x00\x00\x00\x00\x00' |
| 410 | 'c__builtin__\ncomplex\nq\x02G@\x08\x00\x00\x00\x00\x00\x00G\x00' |
| 411 | '\x00\x00\x00\x00\x00\x00\x00\x86Rq\x03K\x01J\xff\xff\xff\xffK' |
| 412 | '\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xff' |
| 413 | 'J\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00' |
| 414 | '\x80(U\x03abcq\x04h\x04(c__main__\nC\nq\x05oq\x06}q\x07(U\x03foo' |
| 415 | 'q\x08K\x01U\x03barq\tK\x02ubh\x06tq\nh\nK\x05e.') |
| 416 | |
| 417 | # Disassembly of DATA2. |
| 418 | DATA2_DIS = """\ |
| 419 | 0: \x80 PROTO 2 |
| 420 | 2: ] EMPTY_LIST |
| 421 | 3: q BINPUT 1 |
| 422 | 5: ( MARK |
| 423 | 6: K BININT1 0 |
| 424 | 8: \x8a LONG1 1L |
| 425 | 11: G BINFLOAT 2.0 |
| 426 | 20: c GLOBAL '__builtin__ complex' |
| 427 | 41: q BINPUT 2 |
| 428 | 43: G BINFLOAT 3.0 |
| 429 | 52: G BINFLOAT 0.0 |
| 430 | 61: \x86 TUPLE2 |
| 431 | 62: R REDUCE |
| 432 | 63: q BINPUT 3 |
| 433 | 65: K BININT1 1 |
| 434 | 67: J BININT -1 |
| 435 | 72: K BININT1 255 |
| 436 | 74: J BININT -255 |
| 437 | 79: J BININT -256 |
| 438 | 84: M BININT2 65535 |
| 439 | 87: J BININT -65535 |
| 440 | 92: J BININT -65536 |
| 441 | 97: J BININT 2147483647 |
| 442 | 102: J BININT -2147483647 |
| 443 | 107: J BININT -2147483648 |
| 444 | 112: ( MARK |
| 445 | 113: U SHORT_BINSTRING 'abc' |
| 446 | 118: q BINPUT 4 |
| 447 | 120: h BINGET 4 |
| 448 | 122: ( MARK |
| 449 | 123: c GLOBAL '__main__ C' |
| 450 | 135: q BINPUT 5 |
| 451 | 137: o OBJ (MARK at 122) |
| 452 | 138: q BINPUT 6 |
| 453 | 140: } EMPTY_DICT |
| 454 | 141: q BINPUT 7 |
| 455 | 143: ( MARK |
| 456 | 144: U SHORT_BINSTRING 'foo' |
| 457 | 149: q BINPUT 8 |
| 458 | 151: K BININT1 1 |
| 459 | 153: U SHORT_BINSTRING 'bar' |
| 460 | 158: q BINPUT 9 |
| 461 | 160: K BININT1 2 |
| 462 | 162: u SETITEMS (MARK at 143) |
| 463 | 163: b BUILD |
| 464 | 164: h BINGET 6 |
| 465 | 166: t TUPLE (MARK at 112) |
| 466 | 167: q BINPUT 10 |
| 467 | 169: h BINGET 10 |
| 468 | 171: K BININT1 5 |
| 469 | 173: e APPENDS (MARK at 5) |
| 470 | 174: . STOP |
| 471 | highest protocol among opcodes = 2 |
| 472 | """ |
| 473 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 474 | def create_data(): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 475 | c = C() |
| 476 | c.foo = 1 |
| 477 | c.bar = 2 |
| 478 | x = [0, 1L, 2.0, 3.0+0j] |
Tim Peters | 461922a | 2001-04-09 20:07:05 +0000 | [diff] [blame] | 479 | # Append some integer test cases at cPickle.c's internal size |
| 480 | # cutoffs. |
| 481 | uint1max = 0xff |
| 482 | uint2max = 0xffff |
| 483 | int4max = 0x7fffffff |
| 484 | x.extend([1, -1, |
| 485 | uint1max, -uint1max, -uint1max-1, |
| 486 | uint2max, -uint2max, -uint2max-1, |
| 487 | int4max, -int4max, -int4max-1]) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 488 | y = ('abc', 'abc', c, c) |
| 489 | x.append(y) |
| 490 | x.append(y) |
| 491 | x.append(5) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 492 | return x |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 493 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 494 | |
| 495 | class AbstractUnpickleTests(unittest.TestCase): |
| 496 | # Subclass must define self.loads, self.error. |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 497 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 498 | _testdata = create_data() |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 499 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 500 | def assert_is_copy(self, obj, objcopy, msg=None): |
| 501 | """Utility method to verify if two objects are copies of each others. |
| 502 | """ |
| 503 | if msg is None: |
| 504 | msg = "{!r} is not a copy of {!r}".format(obj, objcopy) |
| 505 | self.assertEqual(obj, objcopy, msg=msg) |
| 506 | self.assertIs(type(obj), type(objcopy), msg=msg) |
| 507 | if hasattr(obj, '__dict__'): |
| 508 | self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg) |
| 509 | self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg) |
| 510 | if hasattr(obj, '__slots__'): |
| 511 | self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg) |
| 512 | for slot in obj.__slots__: |
| 513 | self.assertEqual( |
| 514 | hasattr(obj, slot), hasattr(objcopy, slot), msg=msg) |
| 515 | self.assertEqual(getattr(obj, slot, None), |
| 516 | getattr(objcopy, slot, None), msg=msg) |
| 517 | |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 518 | def check_unpickling_error(self, errors, data): |
Serhiy Storchaka | d26b663 | 2015-11-29 16:13:51 +0200 | [diff] [blame] | 519 | try: |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 520 | try: |
| 521 | self.loads(data) |
| 522 | except: |
| 523 | if support.verbose > 1: |
Serhiy Storchaka | d26b663 | 2015-11-29 16:13:51 +0200 | [diff] [blame] | 524 | exc_type, exc, tb = sys.exc_info() |
| 525 | print '%-32r - %s: %s' % (data, exc_type.__name__, exc) |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 526 | raise |
Serhiy Storchaka | d26b663 | 2015-11-29 16:13:51 +0200 | [diff] [blame] | 527 | except errors: |
| 528 | pass |
| 529 | else: |
| 530 | try: |
| 531 | exc_name = errors.__name__ |
| 532 | except AttributeError: |
| 533 | exc_name = str(errors) |
| 534 | raise self.failureException('%s not raised' % exc_name) |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 535 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 536 | def test_load_from_canned_string(self): |
| 537 | expected = self._testdata |
| 538 | for canned in DATA0, DATA1, DATA2: |
| 539 | got = self.loads(canned) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 540 | self.assert_is_copy(expected, got) |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 541 | |
| 542 | def test_garyp(self): |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 543 | self.check_unpickling_error(self.error, 'garyp') |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 544 | |
| 545 | def test_maxint64(self): |
| 546 | maxint64 = (1L << 63) - 1 |
| 547 | data = 'I' + str(maxint64) + '\n.' |
| 548 | got = self.loads(data) |
| 549 | self.assertEqual(got, maxint64) |
| 550 | |
| 551 | # Try too with a bogus literal. |
| 552 | data = 'I' + str(maxint64) + 'JUNK\n.' |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 553 | self.check_unpickling_error(ValueError, data) |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 554 | |
| 555 | def test_insecure_strings(self): |
| 556 | insecure = ["abc", "2 + 2", # not quoted |
| 557 | #"'abc' + 'def'", # not a single quoted string |
| 558 | "'abc", # quote is not closed |
| 559 | "'abc\"", # open quote and close quote don't match |
| 560 | "'abc' ?", # junk after close quote |
| 561 | "'\\'", # trailing backslash |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 562 | # issue #17710 |
| 563 | "'", '"', |
| 564 | "' ", '" ', |
| 565 | '\'"', '"\'', |
| 566 | " ''", ' ""', |
| 567 | ' ', |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 568 | # some tests of the quoting rules |
| 569 | #"'abc\"\''", |
| 570 | #"'\\\\a\'\'\'\\\'\\\\\''", |
| 571 | ] |
| 572 | for s in insecure: |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 573 | buf = "S" + s + "\n." |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 574 | self.check_unpickling_error(ValueError, buf) |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 575 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 576 | def test_correctly_quoted_string(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 577 | goodpickles = [("S''\n.", ''), |
| 578 | ('S""\n.', ''), |
| 579 | ('S"\\n"\n.', '\n'), |
| 580 | ("S'\\n'\n.", '\n')] |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 581 | for p, expected in goodpickles: |
| 582 | self.assertEqual(self.loads(p), expected) |
| 583 | |
| 584 | def test_load_classic_instance(self): |
| 585 | # See issue5180. Test loading 2.x pickles that |
| 586 | # contain an instance of old style class. |
| 587 | for X, args in [(C, ()), (D, ('x',)), (E, ())]: |
| 588 | xname = X.__name__.encode('ascii') |
| 589 | # Protocol 0 (text mode pickle): |
| 590 | """ |
| 591 | 0: ( MARK |
| 592 | 1: i INST '__main__ X' (MARK at 0) |
| 593 | 13: p PUT 0 |
| 594 | 16: ( MARK |
| 595 | 17: d DICT (MARK at 16) |
| 596 | 18: p PUT 1 |
| 597 | 21: b BUILD |
| 598 | 22: . STOP |
| 599 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 600 | pickle0 = ("(i__main__\n" |
| 601 | "X\n" |
| 602 | "p0\n" |
| 603 | "(dp1\nb.").replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 604 | self.assert_is_copy(X(*args), self.loads(pickle0)) |
| 605 | |
| 606 | # Protocol 1 (binary mode pickle) |
| 607 | """ |
| 608 | 0: ( MARK |
| 609 | 1: c GLOBAL '__main__ X' |
| 610 | 13: q BINPUT 0 |
| 611 | 15: o OBJ (MARK at 0) |
| 612 | 16: q BINPUT 1 |
| 613 | 18: } EMPTY_DICT |
| 614 | 19: q BINPUT 2 |
| 615 | 21: b BUILD |
| 616 | 22: . STOP |
| 617 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 618 | pickle1 = ('(c__main__\n' |
| 619 | 'X\n' |
| 620 | 'q\x00oq\x01}q\x02b.').replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 621 | self.assert_is_copy(X(*args), self.loads(pickle1)) |
| 622 | |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 623 | # Protocol 2 (pickle2 = '\x80\x02' + pickle1) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 624 | """ |
| 625 | 0: \x80 PROTO 2 |
| 626 | 2: ( MARK |
| 627 | 3: c GLOBAL '__main__ X' |
| 628 | 15: q BINPUT 0 |
| 629 | 17: o OBJ (MARK at 2) |
| 630 | 18: q BINPUT 1 |
| 631 | 20: } EMPTY_DICT |
| 632 | 21: q BINPUT 2 |
| 633 | 23: b BUILD |
| 634 | 24: . STOP |
| 635 | """ |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 636 | pickle2 = ('\x80\x02(c__main__\n' |
| 637 | 'X\n' |
| 638 | 'q\x00oq\x01}q\x02b.').replace('X', xname) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 639 | self.assert_is_copy(X(*args), self.loads(pickle2)) |
| 640 | |
Serhiy Storchaka | 6560e22 | 2016-12-15 12:51:34 +0200 | [diff] [blame] | 641 | def test_load_classic_instance_error(self): |
| 642 | # Issue #28925. |
| 643 | # Protocol 0 (text mode pickle): |
| 644 | """ |
| 645 | 0: ( MARK |
| 646 | 1: i INST '__main__ I' (MARK at 0) |
| 647 | 13: ( MARK |
| 648 | 14: d DICT (MARK at 13) |
| 649 | 15: b BUILD |
| 650 | 16: . STOP |
| 651 | """ |
| 652 | pickle0 = ('(i__main__\n' |
| 653 | 'I\n' |
| 654 | '(db.') |
| 655 | self.assertRaises(MyErr, self.loads, pickle0) |
| 656 | |
| 657 | # Protocol 1 (binary mode pickle) |
| 658 | """ |
| 659 | 0: ( MARK |
| 660 | 1: c GLOBAL '__main__ I' |
| 661 | 13: o OBJ (MARK at 0) |
| 662 | 14: } EMPTY_DICT |
| 663 | 15: b BUILD |
| 664 | 16: . STOP |
| 665 | """ |
| 666 | pickle1 = ('(c__main__\n' |
| 667 | 'I\n' |
| 668 | 'o}b.') |
| 669 | self.assertRaises(MyErr, self.loads, pickle1) |
| 670 | |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 671 | def test_load_str(self): |
| 672 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=0) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 673 | self.assertEqual(self.loads("S'a\\x00\\xa0'\n."), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 674 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=1) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 675 | self.assertEqual(self.loads('U\x03a\x00\xa0.'), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 676 | # From Python 2: pickle.dumps('a\x00\xa0', protocol=2) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 677 | self.assertEqual(self.loads('\x80\x02U\x03a\x00\xa0.'), 'a\x00\xa0') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 678 | |
| 679 | def test_load_unicode(self): |
| 680 | # From Python 2: pickle.dumps(u'Ï€', protocol=0) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 681 | self.assertEqual(self.loads('V\\u03c0\n.'), u'Ï€') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 682 | # From Python 2: pickle.dumps(u'Ï€', protocol=1) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 683 | self.assertEqual(self.loads('X\x02\x00\x00\x00\xcf\x80.'), u'Ï€') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 684 | # From Python 2: pickle.dumps(u'Ï€', protocol=2) |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 685 | 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] | 686 | |
| 687 | def test_constants(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 688 | self.assertIsNone(self.loads('N.')) |
| 689 | self.assertIs(self.loads('\x88.'), True) |
| 690 | self.assertIs(self.loads('\x89.'), False) |
| 691 | self.assertIs(self.loads('I01\n.'), True) |
| 692 | self.assertIs(self.loads('I00\n.'), False) |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 693 | |
| 694 | def test_misc_get(self): |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 695 | self.check_unpickling_error(self.error, 'g0\np0\n') |
| 696 | self.check_unpickling_error(self.error, 'h\x00q\x00') |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 697 | |
| 698 | def test_get(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 699 | pickled = '((lp100000\ng100000\nt.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 700 | unpickled = self.loads(pickled) |
| 701 | self.assertEqual(unpickled, ([],)*2) |
| 702 | self.assertIs(unpickled[0], unpickled[1]) |
| 703 | |
| 704 | def test_binget(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 705 | pickled = '(]q\xffh\xfft.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 706 | unpickled = self.loads(pickled) |
| 707 | self.assertEqual(unpickled, ([],)*2) |
| 708 | self.assertIs(unpickled[0], unpickled[1]) |
| 709 | |
| 710 | def test_long_binget(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 711 | pickled = '(]r\x00\x00\x01\x00j\x00\x00\x01\x00t.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 712 | unpickled = self.loads(pickled) |
| 713 | self.assertEqual(unpickled, ([],)*2) |
| 714 | self.assertIs(unpickled[0], unpickled[1]) |
| 715 | |
| 716 | def test_dup(self): |
Serhiy Storchaka | f6eced5 | 2015-10-02 20:23:46 +0300 | [diff] [blame] | 717 | pickled = '((l2t.' |
Serhiy Storchaka | 22afc50 | 2015-09-29 15:51:40 +0300 | [diff] [blame] | 718 | unpickled = self.loads(pickled) |
| 719 | self.assertEqual(unpickled, ([],)*2) |
| 720 | self.assertIs(unpickled[0], unpickled[1]) |
| 721 | |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 722 | def test_bad_stack(self): |
| 723 | badpickles = [ |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 724 | '.', # STOP |
| 725 | '0', # POP |
| 726 | '1', # POP_MARK |
| 727 | '2', # DUP |
| 728 | # '(2', # PyUnpickler doesn't raise |
| 729 | 'R', # REDUCE |
| 730 | ')R', |
| 731 | 'a', # APPEND |
| 732 | 'Na', |
| 733 | 'b', # BUILD |
| 734 | 'Nb', |
| 735 | 'd', # DICT |
| 736 | 'e', # APPENDS |
| 737 | # '(e', # PyUnpickler raises AttributeError |
| 738 | 'i__builtin__\nlist\n', # INST |
| 739 | 'l', # LIST |
| 740 | 'o', # OBJ |
| 741 | '(o', |
| 742 | 'p1\n', # PUT |
| 743 | 'q\x00', # BINPUT |
| 744 | 'r\x00\x00\x00\x00', # LONG_BINPUT |
| 745 | 's', # SETITEM |
| 746 | 'Ns', |
| 747 | 'NNs', |
| 748 | 't', # TUPLE |
| 749 | 'u', # SETITEMS |
| 750 | # '(u', # PyUnpickler doesn't raise |
| 751 | '}(Nu', |
| 752 | '\x81', # NEWOBJ |
| 753 | ')\x81', |
| 754 | '\x85', # TUPLE1 |
| 755 | '\x86', # TUPLE2 |
| 756 | 'N\x86', |
| 757 | '\x87', # TUPLE3 |
| 758 | 'N\x87', |
| 759 | 'NN\x87', |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 760 | ] |
| 761 | for p in badpickles: |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 762 | self.check_unpickling_error(self.bad_stack_errors, p) |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 763 | |
| 764 | def test_bad_mark(self): |
| 765 | badpickles = [ |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 766 | # 'N(.', # STOP |
| 767 | 'N(2', # DUP |
| 768 | 'c__builtin__\nlist\n)(R', # REDUCE |
| 769 | 'c__builtin__\nlist\n()R', |
| 770 | ']N(a', # APPEND |
| 771 | # BUILD |
| 772 | 'c__builtin__\nValueError\n)R}(b', |
| 773 | 'c__builtin__\nValueError\n)R(}b', |
| 774 | '(Nd', # DICT |
| 775 | 'N(p1\n', # PUT |
| 776 | 'N(q\x00', # BINPUT |
| 777 | 'N(r\x00\x00\x00\x00', # LONG_BINPUT |
| 778 | '}NN(s', # SETITEM |
| 779 | '}N(Ns', |
| 780 | '}(NNs', |
| 781 | '}((u', # SETITEMS |
| 782 | # NEWOBJ |
| 783 | 'c__builtin__\nlist\n)(\x81', |
| 784 | 'c__builtin__\nlist\n()\x81', |
| 785 | 'N(\x85', # TUPLE1 |
| 786 | 'NN(\x86', # TUPLE2 |
| 787 | 'N(N\x86', |
| 788 | 'NNN(\x87', # TUPLE3 |
| 789 | 'NN(N\x87', |
| 790 | 'N(NN\x87', |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 791 | ] |
| 792 | for p in badpickles: |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 793 | self.check_unpickling_error(self.bad_mark_errors, p) |
| 794 | |
| 795 | def test_truncated_data(self): |
Serhiy Storchaka | 81772f1 | 2015-11-29 19:20:11 +0200 | [diff] [blame] | 796 | self.check_unpickling_error(EOFError, '') |
| 797 | self.check_unpickling_error(EOFError, 'N') |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 798 | badpickles = [ |
| 799 | 'F', # FLOAT |
| 800 | 'F0.0', |
| 801 | 'F0.00', |
| 802 | 'G', # BINFLOAT |
| 803 | 'G\x00\x00\x00\x00\x00\x00\x00', |
| 804 | 'I', # INT |
| 805 | 'I0', |
| 806 | 'J', # BININT |
| 807 | 'J\x00\x00\x00', |
| 808 | 'K', # BININT1 |
| 809 | 'L', # LONG |
| 810 | 'L0', |
| 811 | 'L10', |
| 812 | 'L0L', |
| 813 | 'L10L', |
| 814 | 'M', # BININT2 |
| 815 | 'M\x00', |
| 816 | # 'P', # PERSID |
| 817 | # 'Pabc', |
| 818 | 'S', # STRING |
Serhiy Storchaka | 03f3c2f | 2015-11-29 20:18:27 +0200 | [diff] [blame] | 819 | "S'abc'", |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 820 | 'T', # BINSTRING |
| 821 | 'T\x03\x00\x00', |
| 822 | 'T\x03\x00\x00\x00', |
| 823 | 'T\x03\x00\x00\x00ab', |
| 824 | 'U', # SHORT_BINSTRING |
| 825 | 'U\x03', |
| 826 | 'U\x03ab', |
| 827 | 'V', # UNICODE |
| 828 | 'Vabc', |
| 829 | 'X', # BINUNICODE |
| 830 | 'X\x03\x00\x00', |
| 831 | 'X\x03\x00\x00\x00', |
| 832 | 'X\x03\x00\x00\x00ab', |
| 833 | '(c', # GLOBAL |
| 834 | '(c__builtin__', |
| 835 | '(c__builtin__\n', |
| 836 | '(c__builtin__\nlist', |
| 837 | 'Ng', # GET |
| 838 | 'Ng0', |
| 839 | '(i', # INST |
| 840 | '(i__builtin__', |
| 841 | '(i__builtin__\n', |
| 842 | '(i__builtin__\nlist', |
| 843 | 'Nh', # BINGET |
| 844 | 'Nj', # LONG_BINGET |
| 845 | 'Nj\x00\x00\x00', |
| 846 | 'Np', # PUT |
| 847 | 'Np0', |
| 848 | 'Nq', # BINPUT |
| 849 | 'Nr', # LONG_BINPUT |
| 850 | 'Nr\x00\x00\x00', |
| 851 | '\x80', # PROTO |
| 852 | '\x82', # EXT1 |
| 853 | '\x83', # EXT2 |
| 854 | '\x84\x01', |
| 855 | '\x84', # EXT4 |
| 856 | '\x84\x01\x00\x00', |
| 857 | '\x8a', # LONG1 |
| 858 | '\x8b', # LONG4 |
| 859 | '\x8b\x00\x00\x00', |
| 860 | ] |
| 861 | for p in badpickles: |
| 862 | self.check_unpickling_error(self.truncated_errors, p) |
Serhiy Storchaka | 5c13766 | 2015-11-23 15:20:43 +0200 | [diff] [blame] | 863 | |
Serhiy Storchaka | 4d2cf55 | 2015-09-29 15:36:28 +0300 | [diff] [blame] | 864 | |
| 865 | class AbstractPickleTests(unittest.TestCase): |
| 866 | # Subclass must define self.dumps, self.loads. |
| 867 | |
| 868 | _testdata = AbstractUnpickleTests._testdata |
| 869 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 870 | def setUp(self): |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 871 | pass |
Tim Peters | c58440f | 2001-04-09 17:16:31 +0000 | [diff] [blame] | 872 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 873 | def test_misc(self): |
| 874 | # test various datatypes not tested by testdata |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 875 | for proto in protocols: |
| 876 | x = myint(4) |
| 877 | s = self.dumps(x, proto) |
| 878 | y = self.loads(s) |
| 879 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 880 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 881 | x = (1, ()) |
| 882 | s = self.dumps(x, proto) |
| 883 | y = self.loads(s) |
| 884 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 885 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 886 | x = initarg(1, x) |
| 887 | s = self.dumps(x, proto) |
| 888 | y = self.loads(s) |
| 889 | self.assertEqual(x, y) |
Tim Peters | e935816 | 2001-01-22 22:05:20 +0000 | [diff] [blame] | 890 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 891 | # XXX test __reduce__ protocol? |
| 892 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 893 | def test_roundtrip_equality(self): |
| 894 | expected = self._testdata |
| 895 | for proto in protocols: |
| 896 | s = self.dumps(expected, proto) |
| 897 | got = self.loads(s) |
| 898 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 899 | |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 900 | # There are gratuitous differences between pickles produced by |
| 901 | # pickle and cPickle, largely because cPickle starts PUT indices at |
| 902 | # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- |
| 903 | # there's a comment with an exclamation point there whose meaning |
| 904 | # is a mystery. cPickle also suppresses PUT for objects with a refcount |
| 905 | # of 1. |
| 906 | def dont_test_disassembly(self): |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 907 | from pickletools import dis |
| 908 | |
| 909 | for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): |
| 910 | s = self.dumps(self._testdata, proto) |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 911 | filelike = cStringIO.StringIO() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 912 | dis(s, out=filelike) |
| 913 | got = filelike.getvalue() |
| 914 | self.assertEqual(expected, got) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 915 | |
| 916 | def test_recursive_list(self): |
| 917 | l = [] |
| 918 | l.append(l) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 919 | for proto in protocols: |
| 920 | s = self.dumps(l, proto) |
| 921 | x = self.loads(s) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 922 | self.assertIsInstance(x, list) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 923 | self.assertEqual(len(x), 1) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 924 | self.assertIs(x[0], x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 925 | |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 926 | def test_recursive_tuple_and_list(self): |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 927 | t = ([],) |
| 928 | t[0].append(t) |
| 929 | for proto in protocols: |
| 930 | s = self.dumps(t, proto) |
| 931 | x = self.loads(s) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 932 | self.assertIsInstance(x, tuple) |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 933 | self.assertEqual(len(x), 1) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 934 | self.assertIsInstance(x[0], list) |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 935 | self.assertEqual(len(x[0]), 1) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 936 | self.assertIs(x[0][0], x) |
Collin Winter | 57bef68 | 2009-05-26 04:12:39 +0000 | [diff] [blame] | 937 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 938 | def test_recursive_dict(self): |
| 939 | d = {} |
| 940 | d[1] = d |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 941 | for proto in protocols: |
| 942 | s = self.dumps(d, proto) |
| 943 | x = self.loads(s) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 944 | self.assertIsInstance(x, dict) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 945 | self.assertEqual(x.keys(), [1]) |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 946 | self.assertIs(x[1], x) |
| 947 | |
| 948 | def test_recursive_dict_key(self): |
| 949 | d = {} |
| 950 | k = K(d) |
| 951 | d[k] = 1 |
| 952 | for proto in protocols: |
| 953 | s = self.dumps(d, proto) |
| 954 | x = self.loads(s) |
| 955 | self.assertIsInstance(x, dict) |
| 956 | self.assertEqual(len(x.keys()), 1) |
| 957 | self.assertIsInstance(x.keys()[0], K) |
| 958 | self.assertIs(x.keys()[0].value, x) |
| 959 | |
| 960 | def test_recursive_list_subclass(self): |
| 961 | y = MyList() |
| 962 | y.append(y) |
| 963 | s = self.dumps(y, 2) |
| 964 | x = self.loads(s) |
| 965 | self.assertIsInstance(x, MyList) |
| 966 | self.assertEqual(len(x), 1) |
| 967 | self.assertIs(x[0], x) |
| 968 | |
| 969 | def test_recursive_dict_subclass(self): |
| 970 | d = MyDict() |
| 971 | d[1] = d |
| 972 | s = self.dumps(d, 2) |
| 973 | x = self.loads(s) |
| 974 | self.assertIsInstance(x, MyDict) |
| 975 | self.assertEqual(x.keys(), [1]) |
| 976 | self.assertIs(x[1], x) |
| 977 | |
| 978 | def test_recursive_dict_subclass_key(self): |
| 979 | d = MyDict() |
| 980 | k = K(d) |
| 981 | d[k] = 1 |
| 982 | s = self.dumps(d, 2) |
| 983 | x = self.loads(s) |
| 984 | self.assertIsInstance(x, MyDict) |
| 985 | self.assertEqual(len(x.keys()), 1) |
| 986 | self.assertIsInstance(x.keys()[0], K) |
| 987 | self.assertIs(x.keys()[0].value, x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 988 | |
| 989 | def test_recursive_inst(self): |
| 990 | i = C() |
| 991 | i.attr = i |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 992 | for proto in protocols: |
Ezio Melotti | a84ecc6 | 2013-03-04 15:23:12 +0200 | [diff] [blame] | 993 | s = self.dumps(i, proto) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 994 | x = self.loads(s) |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 995 | self.assertIsInstance(x, C) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 996 | self.assertEqual(dir(x), dir(i)) |
Ezio Melotti | a84ecc6 | 2013-03-04 15:23:12 +0200 | [diff] [blame] | 997 | self.assertIs(x.attr, x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 998 | |
| 999 | def test_recursive_multi(self): |
| 1000 | l = [] |
| 1001 | d = {1:l} |
| 1002 | i = C() |
| 1003 | i.attr = d |
| 1004 | l.append(i) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 1005 | for proto in protocols: |
| 1006 | s = self.dumps(l, proto) |
| 1007 | x = self.loads(s) |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1008 | self.assertIsInstance(x, list) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 1009 | self.assertEqual(len(x), 1) |
| 1010 | self.assertEqual(dir(x[0]), dir(i)) |
| 1011 | self.assertEqual(x[0].attr.keys(), [1]) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1012 | self.assertTrue(x[0].attr[1] is x) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1013 | |
Serhiy Storchaka | da87e45 | 2015-11-07 11:15:32 +0200 | [diff] [blame] | 1014 | def check_recursive_collection_and_inst(self, factory): |
| 1015 | h = H() |
| 1016 | y = factory([h]) |
| 1017 | h.attr = y |
| 1018 | for proto in protocols: |
| 1019 | s = self.dumps(y, proto) |
| 1020 | x = self.loads(s) |
| 1021 | self.assertIsInstance(x, type(y)) |
| 1022 | self.assertEqual(len(x), 1) |
| 1023 | self.assertIsInstance(list(x)[0], H) |
| 1024 | self.assertIs(list(x)[0].attr, x) |
| 1025 | |
| 1026 | def test_recursive_list_and_inst(self): |
| 1027 | self.check_recursive_collection_and_inst(list) |
| 1028 | |
| 1029 | def test_recursive_tuple_and_inst(self): |
| 1030 | self.check_recursive_collection_and_inst(tuple) |
| 1031 | |
| 1032 | def test_recursive_dict_and_inst(self): |
| 1033 | self.check_recursive_collection_and_inst(dict.fromkeys) |
| 1034 | |
| 1035 | def test_recursive_set_and_inst(self): |
| 1036 | self.check_recursive_collection_and_inst(set) |
| 1037 | |
| 1038 | def test_recursive_frozenset_and_inst(self): |
| 1039 | self.check_recursive_collection_and_inst(frozenset) |
| 1040 | |
| 1041 | def test_recursive_list_subclass_and_inst(self): |
| 1042 | self.check_recursive_collection_and_inst(MyList) |
| 1043 | |
| 1044 | def test_recursive_tuple_subclass_and_inst(self): |
| 1045 | self.check_recursive_collection_and_inst(MyTuple) |
| 1046 | |
| 1047 | def test_recursive_dict_subclass_and_inst(self): |
| 1048 | self.check_recursive_collection_and_inst(MyDict.fromkeys) |
| 1049 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1050 | if have_unicode: |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1051 | def test_unicode(self): |
Alexandre Vassalotti | e57e999 | 2008-12-27 10:02:59 +0000 | [diff] [blame] | 1052 | endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>', |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1053 | u'<\\>', u'<\\\U00012345>', |
| 1054 | # surrogates |
| 1055 | u'<\udc80>'] |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1056 | for proto in protocols: |
| 1057 | for u in endcases: |
| 1058 | p = self.dumps(u, proto) |
| 1059 | u2 = self.loads(p) |
| 1060 | self.assertEqual(u2, u) |
Tim Peters | e089c68 | 2001-04-10 03:41:41 +0000 | [diff] [blame] | 1061 | |
Alexandre Vassalotti | f852bf9 | 2008-12-27 07:08:47 +0000 | [diff] [blame] | 1062 | def test_unicode_high_plane(self): |
| 1063 | t = u'\U00012345' |
| 1064 | for proto in protocols: |
| 1065 | p = self.dumps(t, proto) |
| 1066 | t2 = self.loads(p) |
| 1067 | self.assertEqual(t2, t) |
| 1068 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1069 | def test_ints(self): |
| 1070 | import sys |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1071 | for proto in protocols: |
| 1072 | n = sys.maxint |
| 1073 | while n: |
| 1074 | for expected in (-n, n): |
| 1075 | s = self.dumps(expected, proto) |
| 1076 | n2 = self.loads(s) |
| 1077 | self.assertEqual(expected, n2) |
| 1078 | n = n >> 1 |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 1079 | |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1080 | def test_long(self): |
| 1081 | for proto in protocols: |
Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1082 | # 256 bytes is where LONG4 begins. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1083 | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
| 1084 | nbase = 1L << nbits |
| 1085 | for npos in nbase-1, nbase, nbase+1: |
| 1086 | for n in npos, -npos: |
| 1087 | pickle = self.dumps(n, proto) |
| 1088 | got = self.loads(pickle) |
| 1089 | self.assertEqual(n, got) |
| 1090 | # Try a monster. This is quadratic-time in protos 0 & 1, so don't |
| 1091 | # bother with those. |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1092 | nbase = long("deadbeeffeedface", 16) |
| 1093 | nbase += nbase << 1000000 |
| 1094 | for n in nbase, -nbase: |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1095 | p = self.dumps(n, 2) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1096 | got = self.loads(p) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 1097 | self.assertEqual(n, got) |
| 1098 | |
Mark Dickinson | a3ecd2c | 2009-01-24 16:40:29 +0000 | [diff] [blame] | 1099 | def test_float(self): |
| 1100 | test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5, |
| 1101 | 3.14, 263.44582062374053, 6.022e23, 1e30] |
| 1102 | test_values = test_values + [-x for x in test_values] |
| 1103 | for proto in protocols: |
| 1104 | for value in test_values: |
| 1105 | pickle = self.dumps(value, proto) |
| 1106 | got = self.loads(pickle) |
| 1107 | self.assertEqual(value, got) |
| 1108 | |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 1109 | @run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
| 1110 | def test_float_format(self): |
| 1111 | # make sure that floats are formatted locale independent |
| 1112 | self.assertEqual(self.dumps(1.2)[0:3], 'F1.') |
| 1113 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1114 | def test_reduce(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 1115 | pass |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1116 | |
| 1117 | def test_getinitargs(self): |
| 1118 | pass |
| 1119 | |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 1120 | def test_metaclass(self): |
| 1121 | a = use_metaclass() |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 1122 | for proto in protocols: |
| 1123 | s = self.dumps(a, proto) |
| 1124 | b = self.loads(s) |
| 1125 | self.assertEqual(a.__class__, b.__class__) |
Guido van Rossum | 04a8661 | 2001-12-19 16:58:54 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 1127 | def test_dynamic_class(self): |
| 1128 | a = create_dynamic_class("my_dynamic_class", (object,)) |
| 1129 | copy_reg.pickle(pickling_metaclass, pickling_metaclass.__reduce__) |
| 1130 | for proto in protocols: |
| 1131 | s = self.dumps(a, proto) |
| 1132 | b = self.loads(s) |
| 1133 | self.assertEqual(a, b) |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1134 | self.assertIs(a.__class__, b.__class__) |
Antoine Pitrou | 561a821 | 2011-10-04 09:34:48 +0200 | [diff] [blame] | 1135 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 1136 | def test_structseq(self): |
| 1137 | import time |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 1138 | import os |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 1139 | |
| 1140 | t = time.localtime() |
| 1141 | for proto in protocols: |
| 1142 | s = self.dumps(t, proto) |
Michael W. Hudson | 0e02530 | 2002-03-06 17:11:18 +0000 | [diff] [blame] | 1143 | u = self.loads(s) |
| 1144 | self.assertEqual(t, u) |
Tim Peters | 70b02d7 | 2003-02-02 17:26:40 +0000 | [diff] [blame] | 1145 | if hasattr(os, "stat"): |
| 1146 | t = os.stat(os.curdir) |
| 1147 | s = self.dumps(t, proto) |
| 1148 | u = self.loads(s) |
| 1149 | self.assertEqual(t, u) |
| 1150 | if hasattr(os, "statvfs"): |
| 1151 | t = os.statvfs(os.curdir) |
| 1152 | s = self.dumps(t, proto) |
| 1153 | u = self.loads(s) |
| 1154 | self.assertEqual(t, u) |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 1155 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1156 | # Tests for protocol 2 |
| 1157 | |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 1158 | def test_proto(self): |
| 1159 | build_none = pickle.NONE + pickle.STOP |
| 1160 | for proto in protocols: |
| 1161 | expected = build_none |
| 1162 | if proto >= 2: |
| 1163 | expected = pickle.PROTO + chr(proto) + expected |
| 1164 | p = self.dumps(None, proto) |
| 1165 | self.assertEqual(p, expected) |
| 1166 | |
| 1167 | oob = protocols[-1] + 1 # a future protocol |
| 1168 | badpickle = pickle.PROTO + chr(oob) + build_none |
| 1169 | try: |
| 1170 | self.loads(badpickle) |
| 1171 | except ValueError, detail: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1172 | self.assertTrue(str(detail).startswith( |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 1173 | "unsupported pickle protocol")) |
| 1174 | else: |
| 1175 | self.fail("expected bad protocol number to raise ValueError") |
| 1176 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1177 | def test_long1(self): |
| 1178 | x = 12345678910111213141516178920L |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 1179 | for proto in protocols: |
| 1180 | s = self.dumps(x, proto) |
| 1181 | y = self.loads(s) |
| 1182 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1183 | self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1184 | |
| 1185 | def test_long4(self): |
| 1186 | x = 12345678910111213141516178920L << (256*8) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 1187 | for proto in protocols: |
| 1188 | s = self.dumps(x, proto) |
| 1189 | y = self.loads(s) |
| 1190 | self.assertEqual(x, y) |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1191 | self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1192 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1193 | def test_short_tuples(self): |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1194 | # Map (proto, len(tuple)) to expected opcode. |
| 1195 | expected_opcode = {(0, 0): pickle.TUPLE, |
| 1196 | (0, 1): pickle.TUPLE, |
| 1197 | (0, 2): pickle.TUPLE, |
| 1198 | (0, 3): pickle.TUPLE, |
| 1199 | (0, 4): pickle.TUPLE, |
| 1200 | |
| 1201 | (1, 0): pickle.EMPTY_TUPLE, |
| 1202 | (1, 1): pickle.TUPLE, |
| 1203 | (1, 2): pickle.TUPLE, |
| 1204 | (1, 3): pickle.TUPLE, |
| 1205 | (1, 4): pickle.TUPLE, |
| 1206 | |
| 1207 | (2, 0): pickle.EMPTY_TUPLE, |
| 1208 | (2, 1): pickle.TUPLE1, |
| 1209 | (2, 2): pickle.TUPLE2, |
| 1210 | (2, 3): pickle.TUPLE3, |
| 1211 | (2, 4): pickle.TUPLE, |
| 1212 | } |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1213 | a = () |
Guido van Rossum | 025bc2f | 2003-01-28 04:20:02 +0000 | [diff] [blame] | 1214 | b = (1,) |
| 1215 | c = (1, 2) |
| 1216 | d = (1, 2, 3) |
| 1217 | e = (1, 2, 3, 4) |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 1218 | for proto in protocols: |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1219 | for x in a, b, c, d, e: |
| 1220 | s = self.dumps(x, proto) |
| 1221 | y = self.loads(s) |
| 1222 | self.assertEqual(x, y, (proto, x, s, y)) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1223 | expected = expected_opcode[proto, len(x)] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1224 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 1225 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 1226 | def test_singletons(self): |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 1227 | # Map (proto, singleton) to expected opcode. |
| 1228 | expected_opcode = {(0, None): pickle.NONE, |
| 1229 | (1, None): pickle.NONE, |
| 1230 | (2, None): pickle.NONE, |
| 1231 | |
| 1232 | (0, True): pickle.INT, |
| 1233 | (1, True): pickle.INT, |
| 1234 | (2, True): pickle.NEWTRUE, |
| 1235 | |
| 1236 | (0, False): pickle.INT, |
| 1237 | (1, False): pickle.INT, |
| 1238 | (2, False): pickle.NEWFALSE, |
| 1239 | } |
Tim Peters | 4190fb8 | 2003-02-02 16:09:05 +0000 | [diff] [blame] | 1240 | for proto in protocols: |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 1241 | for x in None, False, True: |
| 1242 | s = self.dumps(x, proto) |
| 1243 | y = self.loads(s) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1244 | self.assertTrue(x is y, (proto, x, s, y)) |
Tim Peters | 61bf257 | 2003-02-03 21:31:22 +0000 | [diff] [blame] | 1245 | expected = expected_opcode[proto, x] |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1246 | self.assertEqual(opcode_in_pickle(expected, s), True) |
Tim Peters | 3c67d79 | 2003-02-02 17:59:11 +0000 | [diff] [blame] | 1247 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1248 | def test_newobj_tuple(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 1249 | x = MyTuple([1, 2, 3]) |
| 1250 | x.foo = 42 |
| 1251 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 1252 | for proto in protocols: |
| 1253 | s = self.dumps(x, proto) |
| 1254 | y = self.loads(s) |
| 1255 | self.assertEqual(tuple(x), tuple(y)) |
| 1256 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1257 | |
| 1258 | def test_newobj_list(self): |
Guido van Rossum | 3d8c01b | 2003-01-28 19:48:18 +0000 | [diff] [blame] | 1259 | x = MyList([1, 2, 3]) |
| 1260 | x.foo = 42 |
| 1261 | x.bar = "hello" |
Tim Peters | 894453a | 2003-02-03 22:32:18 +0000 | [diff] [blame] | 1262 | for proto in protocols: |
| 1263 | s = self.dumps(x, proto) |
| 1264 | y = self.loads(s) |
| 1265 | self.assertEqual(list(x), list(y)) |
| 1266 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1267 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1268 | def test_newobj_generic(self): |
Tim Peters | 5013bd9 | 2003-02-03 22:28:41 +0000 | [diff] [blame] | 1269 | for proto in protocols: |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1270 | for C in myclasses: |
| 1271 | B = C.__base__ |
| 1272 | x = C(C.sample) |
| 1273 | x.foo = 42 |
| 1274 | s = self.dumps(x, proto) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1275 | y = self.loads(s) |
| 1276 | detail = (proto, C, B, x, y, type(y)) |
| 1277 | self.assertEqual(B(x), B(y), detail) |
| 1278 | self.assertEqual(x.__dict__, y.__dict__, detail) |
| 1279 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1280 | def test_newobj_proxies(self): |
| 1281 | # NEWOBJ should use the __class__ rather than the raw type |
| 1282 | import weakref |
| 1283 | classes = myclasses[:] |
| 1284 | # Cannot create weakproxies to these classes |
| 1285 | for c in (MyInt, MyLong, MyStr, MyTuple): |
| 1286 | classes.remove(c) |
| 1287 | for proto in protocols: |
| 1288 | for C in classes: |
| 1289 | B = C.__base__ |
| 1290 | x = C(C.sample) |
| 1291 | x.foo = 42 |
| 1292 | p = weakref.proxy(x) |
| 1293 | s = self.dumps(p, proto) |
| 1294 | y = self.loads(s) |
| 1295 | self.assertEqual(type(y), type(x)) # rather than type(p) |
| 1296 | detail = (proto, C, B, x, y, type(y)) |
| 1297 | self.assertEqual(B(x), B(y), detail) |
| 1298 | self.assertEqual(x.__dict__, y.__dict__, detail) |
| 1299 | |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 1300 | # Register a type with copy_reg, with extension code extcode. Pickle |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1301 | # an object of that type. Check that the resulting pickle uses opcode |
| 1302 | # (EXT[124]) under proto 2, and not in proto 1. |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1303 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1304 | def produce_global_ext(self, extcode, opcode): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1305 | e = ExtensionSaver(extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1306 | try: |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 1307 | copy_reg.add_extension(__name__, "MyList", extcode) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1308 | x = MyList([1, 2, 3]) |
| 1309 | x.foo = 42 |
| 1310 | x.bar = "hello" |
| 1311 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1312 | # Dump using protocol 1 for comparison. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1313 | s1 = self.dumps(x, 1) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 1314 | self.assertIn(__name__, s1) |
| 1315 | self.assertIn("MyList", s1) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1316 | self.assertEqual(opcode_in_pickle(opcode, s1), False) |
| 1317 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1318 | y = self.loads(s1) |
| 1319 | self.assertEqual(list(x), list(y)) |
| 1320 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1321 | |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1322 | # Dump using protocol 2 for test. |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1323 | s2 = self.dumps(x, 2) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 1324 | self.assertNotIn(__name__, s2) |
| 1325 | self.assertNotIn("MyList", s2) |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1326 | self.assertEqual(opcode_in_pickle(opcode, s2), True) |
| 1327 | |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1328 | y = self.loads(s2) |
| 1329 | self.assertEqual(list(x), list(y)) |
| 1330 | self.assertEqual(x.__dict__, y.__dict__) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1331 | |
| 1332 | finally: |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1333 | e.restore() |
Tim Peters | 22e7171 | 2003-02-03 22:27:38 +0000 | [diff] [blame] | 1334 | |
| 1335 | def test_global_ext1(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1336 | self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code |
| 1337 | self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1338 | |
| 1339 | def test_global_ext2(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1340 | self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code |
| 1341 | self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code |
| 1342 | self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1343 | |
| 1344 | def test_global_ext4(self): |
Tim Peters | 3e667d5 | 2003-02-04 21:47:44 +0000 | [diff] [blame] | 1345 | self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code |
| 1346 | self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code |
| 1347 | self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness |
| 1348 | |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 1349 | def test_list_chunking(self): |
| 1350 | n = 10 # too small to chunk |
| 1351 | x = range(n) |
| 1352 | for proto in protocols: |
| 1353 | s = self.dumps(x, proto) |
| 1354 | y = self.loads(s) |
| 1355 | self.assertEqual(x, y) |
| 1356 | num_appends = count_opcode(pickle.APPENDS, s) |
| 1357 | self.assertEqual(num_appends, proto > 0) |
| 1358 | |
| 1359 | n = 2500 # expect at least two chunks when proto > 0 |
| 1360 | x = range(n) |
| 1361 | for proto in protocols: |
| 1362 | s = self.dumps(x, proto) |
| 1363 | y = self.loads(s) |
| 1364 | self.assertEqual(x, y) |
| 1365 | num_appends = count_opcode(pickle.APPENDS, s) |
| 1366 | if proto == 0: |
| 1367 | self.assertEqual(num_appends, 0) |
| 1368 | else: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1369 | self.assertTrue(num_appends >= 2) |
Tim Peters | 8d2613a | 2003-02-11 16:40:16 +0000 | [diff] [blame] | 1370 | |
| 1371 | def test_dict_chunking(self): |
| 1372 | n = 10 # too small to chunk |
| 1373 | x = dict.fromkeys(range(n)) |
| 1374 | for proto in protocols: |
| 1375 | s = self.dumps(x, proto) |
| 1376 | y = self.loads(s) |
| 1377 | self.assertEqual(x, y) |
| 1378 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 1379 | self.assertEqual(num_setitems, proto > 0) |
| 1380 | |
| 1381 | n = 2500 # expect at least two chunks when proto > 0 |
| 1382 | x = dict.fromkeys(range(n)) |
| 1383 | for proto in protocols: |
| 1384 | s = self.dumps(x, proto) |
| 1385 | y = self.loads(s) |
| 1386 | self.assertEqual(x, y) |
| 1387 | num_setitems = count_opcode(pickle.SETITEMS, s) |
| 1388 | if proto == 0: |
| 1389 | self.assertEqual(num_setitems, 0) |
| 1390 | else: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1391 | self.assertTrue(num_setitems >= 2) |
Guido van Rossum | 0322d0f | 2003-01-29 06:12:46 +0000 | [diff] [blame] | 1392 | |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1393 | def test_simple_newobj(self): |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1394 | x = SimpleNewObj.__new__(SimpleNewObj, 0xface) # avoid __init__ |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1395 | x.abc = 666 |
| 1396 | for proto in protocols: |
| 1397 | s = self.dumps(x, proto) |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1398 | if proto < 1: |
| 1399 | self.assertIn('\nI64206', s) # INT |
| 1400 | else: |
| 1401 | self.assertIn('M\xce\xfa', s) # BININT2 |
| 1402 | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2) |
| 1403 | y = self.loads(s) # will raise TypeError if __init__ called |
| 1404 | self.assertEqual(y.abc, 666) |
| 1405 | self.assertEqual(x.__dict__, y.__dict__) |
| 1406 | |
| 1407 | def test_complex_newobj(self): |
| 1408 | x = ComplexNewObj.__new__(ComplexNewObj, 0xface) # avoid __init__ |
| 1409 | x.abc = 666 |
| 1410 | for proto in protocols: |
| 1411 | s = self.dumps(x, proto) |
| 1412 | if proto < 1: |
| 1413 | self.assertIn('\nI64206', s) # INT |
| 1414 | elif proto < 2: |
| 1415 | self.assertIn('M\xce\xfa', s) # BININT2 |
| 1416 | else: |
| 1417 | self.assertIn('U\x04FACE', s) # SHORT_BINSTRING |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1418 | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2) |
| 1419 | y = self.loads(s) # will raise TypeError if __init__ called |
| 1420 | self.assertEqual(y.abc, 666) |
| 1421 | self.assertEqual(x.__dict__, y.__dict__) |
| 1422 | |
Tim Peters | 42f08ac | 2003-02-11 22:43:24 +0000 | [diff] [blame] | 1423 | def test_newobj_list_slots(self): |
| 1424 | x = SlotList([1, 2, 3]) |
| 1425 | x.foo = 42 |
| 1426 | x.bar = "hello" |
| 1427 | s = self.dumps(x, 2) |
| 1428 | y = self.loads(s) |
| 1429 | self.assertEqual(list(x), list(y)) |
| 1430 | self.assertEqual(x.__dict__, y.__dict__) |
| 1431 | self.assertEqual(x.foo, y.foo) |
| 1432 | self.assertEqual(x.bar, y.bar) |
| 1433 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1434 | def test_reduce_overrides_default_reduce_ex(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1435 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1436 | x = REX_one() |
| 1437 | self.assertEqual(x._reduce_called, 0) |
| 1438 | s = self.dumps(x, proto) |
| 1439 | self.assertEqual(x._reduce_called, 1) |
| 1440 | y = self.loads(s) |
| 1441 | self.assertEqual(y._reduce_called, 0) |
| 1442 | |
| 1443 | def test_reduce_ex_called(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1444 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1445 | x = REX_two() |
| 1446 | self.assertEqual(x._proto, None) |
| 1447 | s = self.dumps(x, proto) |
| 1448 | self.assertEqual(x._proto, proto) |
| 1449 | y = self.loads(s) |
| 1450 | self.assertEqual(y._proto, None) |
| 1451 | |
| 1452 | def test_reduce_ex_overrides_reduce(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1453 | for proto in protocols: |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1454 | x = REX_three() |
| 1455 | self.assertEqual(x._proto, None) |
| 1456 | s = self.dumps(x, proto) |
| 1457 | self.assertEqual(x._proto, proto) |
| 1458 | y = self.loads(s) |
| 1459 | self.assertEqual(y._proto, None) |
| 1460 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1461 | def test_reduce_ex_calls_base(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1462 | for proto in protocols: |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1463 | x = REX_four() |
| 1464 | self.assertEqual(x._proto, None) |
| 1465 | s = self.dumps(x, proto) |
| 1466 | self.assertEqual(x._proto, proto) |
| 1467 | y = self.loads(s) |
| 1468 | self.assertEqual(y._proto, proto) |
| 1469 | |
| 1470 | def test_reduce_calls_base(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1471 | for proto in protocols: |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1472 | x = REX_five() |
| 1473 | self.assertEqual(x._reduce_called, 0) |
| 1474 | s = self.dumps(x, proto) |
| 1475 | self.assertEqual(x._reduce_called, 1) |
| 1476 | y = self.loads(s) |
| 1477 | self.assertEqual(y._reduce_called, 1) |
| 1478 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1479 | @no_tracing |
| 1480 | def test_bad_getattr(self): |
| 1481 | # Issue #3514: crash when there is an infinite loop in __getattr__ |
| 1482 | x = BadGetattr() |
| 1483 | for proto in protocols: |
| 1484 | self.assertRaises(RuntimeError, self.dumps, x, proto) |
| 1485 | |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1486 | def test_reduce_bad_iterator(self): |
| 1487 | # Issue4176: crash when 4th and 5th items of __reduce__() |
| 1488 | # are not iterators |
| 1489 | class C(object): |
| 1490 | def __reduce__(self): |
| 1491 | # 4th item is not an iterator |
| 1492 | return list, (), None, [], None |
| 1493 | class D(object): |
| 1494 | def __reduce__(self): |
| 1495 | # 5th item is not an iterator |
| 1496 | return dict, (), None, None, [] |
| 1497 | |
Serhiy Storchaka | ff41d45 | 2015-12-30 20:59:32 +0200 | [diff] [blame] | 1498 | # Protocol 0 in Python implementation is less strict and also accepts |
| 1499 | # iterables. |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1500 | for proto in protocols: |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1501 | try: |
| 1502 | self.dumps(C(), proto) |
Serhiy Storchaka | ff41d45 | 2015-12-30 20:59:32 +0200 | [diff] [blame] | 1503 | except (AttributeError, pickle.PicklingError, cPickle.PicklingError): |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1504 | pass |
| 1505 | try: |
| 1506 | self.dumps(D(), proto) |
Serhiy Storchaka | ff41d45 | 2015-12-30 20:59:32 +0200 | [diff] [blame] | 1507 | except (AttributeError, pickle.PicklingError, cPickle.PicklingError): |
Amaury Forgeot d'Arc | 69a9c5b | 2008-10-30 21:18:34 +0000 | [diff] [blame] | 1508 | pass |
| 1509 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1510 | def test_many_puts_and_gets(self): |
| 1511 | # Test that internal data structures correctly deal with lots of |
| 1512 | # puts/gets. |
| 1513 | keys = ("aaa" + str(i) for i in xrange(100)) |
| 1514 | large_dict = dict((k, [4, 5, 6]) for k in keys) |
| 1515 | obj = [dict(large_dict), dict(large_dict), dict(large_dict)] |
| 1516 | |
| 1517 | for proto in protocols: |
| 1518 | dumped = self.dumps(obj, proto) |
| 1519 | loaded = self.loads(dumped) |
| 1520 | self.assertEqual(loaded, obj, |
| 1521 | "Failed protocol %d: %r != %r" |
| 1522 | % (proto, obj, loaded)) |
| 1523 | |
Antoine Pitrou | 7430989 | 2009-05-02 21:13:23 +0000 | [diff] [blame] | 1524 | def test_attribute_name_interning(self): |
| 1525 | # Test that attribute names of pickled objects are interned when |
| 1526 | # unpickling. |
| 1527 | for proto in protocols: |
| 1528 | x = C() |
| 1529 | x.foo = 42 |
| 1530 | x.bar = "hello" |
| 1531 | s = self.dumps(x, proto) |
| 1532 | y = self.loads(s) |
| 1533 | x_keys = sorted(x.__dict__) |
| 1534 | y_keys = sorted(y.__dict__) |
| 1535 | for x_key, y_key in zip(x_keys, y_keys): |
| 1536 | self.assertIs(x_key, y_key) |
| 1537 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1538 | def test_large_pickles(self): |
| 1539 | # Test the correctness of internal buffering routines when handling |
| 1540 | # large data. |
| 1541 | for proto in protocols: |
| 1542 | data = (1, min, 'xy' * (30 * 1024), len) |
| 1543 | dumped = self.dumps(data, proto) |
| 1544 | loaded = self.loads(dumped) |
| 1545 | self.assertEqual(len(loaded), len(data)) |
| 1546 | self.assertEqual(loaded, data) |
| 1547 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1548 | def _check_pickling_with_opcode(self, obj, opcode, proto): |
| 1549 | pickled = self.dumps(obj, proto) |
| 1550 | self.assertTrue(opcode_in_pickle(opcode, pickled)) |
| 1551 | unpickled = self.loads(pickled) |
| 1552 | self.assertEqual(obj, unpickled) |
| 1553 | |
| 1554 | def test_appends_on_non_lists(self): |
| 1555 | # Issue #17720 |
| 1556 | obj = REX_six([1, 2, 3]) |
| 1557 | for proto in protocols: |
| 1558 | if proto == 0: |
| 1559 | self._check_pickling_with_opcode(obj, pickle.APPEND, proto) |
| 1560 | else: |
| 1561 | self._check_pickling_with_opcode(obj, pickle.APPENDS, proto) |
| 1562 | |
| 1563 | def test_setitems_on_non_dicts(self): |
| 1564 | obj = REX_seven({1: -1, 2: -2, 3: -3}) |
| 1565 | for proto in protocols: |
| 1566 | if proto == 0: |
| 1567 | self._check_pickling_with_opcode(obj, pickle.SETITEM, proto) |
| 1568 | else: |
| 1569 | self._check_pickling_with_opcode(obj, pickle.SETITEMS, proto) |
| 1570 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1571 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1572 | # Test classes for reduce_ex |
| 1573 | |
| 1574 | class REX_one(object): |
| 1575 | _reduce_called = 0 |
| 1576 | def __reduce__(self): |
| 1577 | self._reduce_called = 1 |
| 1578 | return REX_one, () |
| 1579 | # No __reduce_ex__ here, but inheriting it from object |
| 1580 | |
| 1581 | class REX_two(object): |
| 1582 | _proto = None |
| 1583 | def __reduce_ex__(self, proto): |
| 1584 | self._proto = proto |
| 1585 | return REX_two, () |
| 1586 | # No __reduce__ here, but inheriting it from object |
| 1587 | |
| 1588 | class REX_three(object): |
| 1589 | _proto = None |
| 1590 | def __reduce_ex__(self, proto): |
| 1591 | self._proto = proto |
| 1592 | return REX_two, () |
| 1593 | def __reduce__(self): |
| 1594 | raise TestFailed, "This __reduce__ shouldn't be called" |
| 1595 | |
Žiga Seilnacht | 20f43d3 | 2007-03-15 11:44:55 +0000 | [diff] [blame] | 1596 | class REX_four(object): |
| 1597 | _proto = None |
| 1598 | def __reduce_ex__(self, proto): |
| 1599 | self._proto = proto |
| 1600 | return object.__reduce_ex__(self, proto) |
| 1601 | # Calling base class method should succeed |
| 1602 | |
| 1603 | class REX_five(object): |
| 1604 | _reduce_called = 0 |
| 1605 | def __reduce__(self): |
| 1606 | self._reduce_called = 1 |
| 1607 | return object.__reduce__(self) |
| 1608 | # This one used to fail with infinite recursion |
| 1609 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1610 | class REX_six(object): |
| 1611 | """This class is used to check the 4th argument (list iterator) of |
| 1612 | the reduce protocol. |
| 1613 | """ |
| 1614 | def __init__(self, items=None): |
| 1615 | if items is None: |
| 1616 | items = [] |
| 1617 | self.items = items |
| 1618 | def __eq__(self, other): |
| 1619 | return type(self) is type(other) and self.items == other.items |
Serhiy Storchaka | f8cc287 | 2016-10-25 09:51:38 +0300 | [diff] [blame] | 1620 | __hash__ = None |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1621 | def append(self, item): |
| 1622 | self.items.append(item) |
| 1623 | def extend(self, items): |
| 1624 | for item in items: |
| 1625 | self.append(item) |
| 1626 | def __reduce__(self): |
| 1627 | return type(self), (), None, iter(self.items), None |
| 1628 | |
| 1629 | class REX_seven(object): |
| 1630 | """This class is used to check the 5th argument (dict iterator) of |
| 1631 | the reduce protocol. |
| 1632 | """ |
| 1633 | def __init__(self, table=None): |
| 1634 | if table is None: |
| 1635 | table = {} |
| 1636 | self.table = table |
| 1637 | def __eq__(self, other): |
| 1638 | return type(self) is type(other) and self.table == other.table |
Serhiy Storchaka | f8cc287 | 2016-10-25 09:51:38 +0300 | [diff] [blame] | 1639 | __hash__ = None |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1640 | def __setitem__(self, key, value): |
| 1641 | self.table[key] = value |
| 1642 | def __reduce__(self): |
| 1643 | return type(self), (), None, None, iter(self.table.items()) |
| 1644 | |
Guido van Rossum | 2a30b21 | 2003-02-18 22:41:24 +0000 | [diff] [blame] | 1645 | # Test classes for newobj |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1646 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1647 | class MyInt(int): |
| 1648 | sample = 1 |
| 1649 | |
| 1650 | class MyLong(long): |
| 1651 | sample = 1L |
| 1652 | |
| 1653 | class MyFloat(float): |
| 1654 | sample = 1.0 |
| 1655 | |
| 1656 | class MyComplex(complex): |
| 1657 | sample = 1.0 + 0.0j |
| 1658 | |
| 1659 | class MyStr(str): |
| 1660 | sample = "hello" |
| 1661 | |
| 1662 | class MyUnicode(unicode): |
| 1663 | sample = u"hello \u1234" |
| 1664 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1665 | class MyTuple(tuple): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1666 | sample = (1, 2, 3) |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1667 | |
| 1668 | class MyList(list): |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1669 | sample = [1, 2, 3] |
| 1670 | |
| 1671 | class MyDict(dict): |
| 1672 | sample = {"a": 1, "b": 2} |
| 1673 | |
| 1674 | myclasses = [MyInt, MyLong, MyFloat, |
Guido van Rossum | 206b9a7 | 2003-03-02 13:53:18 +0000 | [diff] [blame] | 1675 | MyComplex, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1676 | MyStr, MyUnicode, |
| 1677 | MyTuple, MyList, MyDict] |
| 1678 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 1679 | |
Guido van Rossum | c8d6ef5 | 2003-01-28 22:02:31 +0000 | [diff] [blame] | 1680 | class SlotList(MyList): |
| 1681 | __slots__ = ["foo"] |
| 1682 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1683 | class SimpleNewObj(int): |
| 1684 | def __init__(self, *args, **kwargs): |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1685 | # raise an error, to make sure this isn't called |
| 1686 | raise TypeError("SimpleNewObj.__init__() didn't expect to get called") |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1687 | def __eq__(self, other): |
| 1688 | return int(self) == int(other) and self.__dict__ == other.__dict__ |
Serhiy Storchaka | f8cc287 | 2016-10-25 09:51:38 +0300 | [diff] [blame] | 1689 | __hash__ = None |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1690 | |
| 1691 | class ComplexNewObj(SimpleNewObj): |
| 1692 | def __getnewargs__(self): |
| 1693 | return ('%X' % self, 16) |
| 1694 | |
| 1695 | class BadGetattr: |
| 1696 | def __getattr__(self, key): |
| 1697 | self.foo |
Tim Peters | e9ef203 | 2003-02-13 18:42:00 +0000 | [diff] [blame] | 1698 | |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1699 | class AbstractPickleModuleTests(unittest.TestCase): |
| 1700 | |
| 1701 | def test_dump_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1702 | import os |
| 1703 | f = open(TESTFN, "w") |
| 1704 | try: |
| 1705 | f.close() |
| 1706 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 1707 | finally: |
| 1708 | os.remove(TESTFN) |
Jeremy Hylton | 6642653 | 2001-10-15 21:38:56 +0000 | [diff] [blame] | 1709 | |
| 1710 | def test_load_closed_file(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 1711 | import os |
| 1712 | f = open(TESTFN, "w") |
| 1713 | try: |
| 1714 | f.close() |
| 1715 | self.assertRaises(ValueError, self.module.dump, 123, f) |
| 1716 | finally: |
| 1717 | os.remove(TESTFN) |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1718 | |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1719 | def test_load_from_and_dump_to_file(self): |
| 1720 | stream = cStringIO.StringIO() |
| 1721 | data = [123, {}, 124] |
| 1722 | self.module.dump(data, stream) |
| 1723 | stream.seek(0) |
| 1724 | unpickled = self.module.load(stream) |
| 1725 | self.assertEqual(unpickled, data) |
| 1726 | |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1727 | def test_highest_protocol(self): |
| 1728 | # Of course this needs to be changed when HIGHEST_PROTOCOL changes. |
| 1729 | self.assertEqual(self.module.HIGHEST_PROTOCOL, 2) |
| 1730 | |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1731 | def test_callapi(self): |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1732 | f = cStringIO.StringIO() |
Martin v. Löwis | 544f119 | 2004-07-27 05:22:33 +0000 | [diff] [blame] | 1733 | # With and without keyword arguments |
| 1734 | self.module.dump(123, f, -1) |
| 1735 | self.module.dump(123, file=f, protocol=-1) |
| 1736 | self.module.dumps(123, -1) |
| 1737 | self.module.dumps(123, protocol=-1) |
| 1738 | self.module.Pickler(f, -1) |
| 1739 | self.module.Pickler(f, protocol=-1) |
Tim Peters | c0c9370 | 2003-02-13 19:30:57 +0000 | [diff] [blame] | 1740 | |
Amaury Forgeot d'Arc | 74b3016 | 2009-07-23 19:26:02 +0000 | [diff] [blame] | 1741 | def test_incomplete_input(self): |
| 1742 | s = StringIO.StringIO("X''.") |
| 1743 | self.assertRaises(EOFError, self.module.load, s) |
| 1744 | |
Alexandre Vassalotti | 8b2d713 | 2009-11-24 17:53:23 +0000 | [diff] [blame] | 1745 | def test_restricted(self): |
| 1746 | # issue7128: cPickle failed in restricted mode |
| 1747 | builtins = {self.module.__name__: self.module, |
| 1748 | '__import__': __import__} |
| 1749 | d = {} |
| 1750 | teststr = "def f(): {0}.dumps(0)".format(self.module.__name__) |
| 1751 | exec teststr in {'__builtins__': builtins}, d |
| 1752 | d['f']() |
| 1753 | |
Antoine Pitrou | 0d423b8 | 2010-01-07 17:46:49 +0000 | [diff] [blame] | 1754 | def test_bad_input(self): |
| 1755 | # Test issue4298 |
| 1756 | s = '\x58\0\0\0\x54' |
| 1757 | self.assertRaises(EOFError, self.module.loads, s) |
Serhiy Storchaka | bf19ce2 | 2015-11-29 13:12:40 +0200 | [diff] [blame] | 1758 | |
Alexandre Vassalotti | 8b2d713 | 2009-11-24 17:53:23 +0000 | [diff] [blame] | 1759 | |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1760 | class AbstractPersistentPicklerTests(unittest.TestCase): |
| 1761 | |
| 1762 | # This class defines persistent_id() and persistent_load() |
| 1763 | # functions that should be used by the pickler. All even integers |
| 1764 | # are pickled using persistent ids. |
| 1765 | |
| 1766 | def persistent_id(self, object): |
| 1767 | if isinstance(object, int) and object % 2 == 0: |
| 1768 | self.id_count += 1 |
| 1769 | return str(object) |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1770 | elif object == "test_false_value": |
| 1771 | self.false_count += 1 |
| 1772 | return "" |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1773 | else: |
| 1774 | return None |
| 1775 | |
| 1776 | def persistent_load(self, oid): |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1777 | if not oid: |
| 1778 | self.load_false_count += 1 |
| 1779 | return "test_false_value" |
| 1780 | else: |
| 1781 | self.load_count += 1 |
| 1782 | object = int(oid) |
| 1783 | assert object % 2 == 0 |
| 1784 | return object |
Jeremy Hylton | 4c8be85 | 2002-11-13 22:10:47 +0000 | [diff] [blame] | 1785 | |
| 1786 | def test_persistence(self): |
Alexandre Vassalotti | 1d3a173 | 2013-11-30 13:24:13 -0800 | [diff] [blame] | 1787 | L = range(10) + ["test_false_value"] |
| 1788 | for proto in protocols: |
| 1789 | self.id_count = 0 |
| 1790 | self.false_count = 0 |
| 1791 | self.load_false_count = 0 |
| 1792 | self.load_count = 0 |
| 1793 | self.assertEqual(self.loads(self.dumps(L, proto)), L) |
| 1794 | self.assertEqual(self.id_count, 5) |
| 1795 | self.assertEqual(self.false_count, 1) |
| 1796 | self.assertEqual(self.load_count, 5) |
| 1797 | self.assertEqual(self.load_false_count, 1) |
Collin Winter | f8089c7 | 2009-04-09 16:46:46 +0000 | [diff] [blame] | 1798 | |
| 1799 | class AbstractPicklerUnpicklerObjectTests(unittest.TestCase): |
| 1800 | |
| 1801 | pickler_class = None |
| 1802 | unpickler_class = None |
| 1803 | |
| 1804 | def setUp(self): |
| 1805 | assert self.pickler_class |
| 1806 | assert self.unpickler_class |
| 1807 | |
| 1808 | def test_clear_pickler_memo(self): |
| 1809 | # To test whether clear_memo() has any effect, we pickle an object, |
| 1810 | # then pickle it again without clearing the memo; the two serialized |
| 1811 | # forms should be different. If we clear_memo() and then pickle the |
| 1812 | # object again, the third serialized form should be identical to the |
| 1813 | # first one we obtained. |
| 1814 | data = ["abcdefg", "abcdefg", 44] |
| 1815 | f = cStringIO.StringIO() |
| 1816 | pickler = self.pickler_class(f) |
| 1817 | |
| 1818 | pickler.dump(data) |
| 1819 | first_pickled = f.getvalue() |
| 1820 | |
| 1821 | # Reset StringIO object. |
| 1822 | f.seek(0) |
| 1823 | f.truncate() |
| 1824 | |
| 1825 | pickler.dump(data) |
| 1826 | second_pickled = f.getvalue() |
| 1827 | |
| 1828 | # Reset the Pickler and StringIO objects. |
| 1829 | pickler.clear_memo() |
| 1830 | f.seek(0) |
| 1831 | f.truncate() |
| 1832 | |
| 1833 | pickler.dump(data) |
| 1834 | third_pickled = f.getvalue() |
| 1835 | |
| 1836 | self.assertNotEqual(first_pickled, second_pickled) |
| 1837 | self.assertEqual(first_pickled, third_pickled) |
| 1838 | |
| 1839 | def test_priming_pickler_memo(self): |
| 1840 | # Verify that we can set the Pickler's memo attribute. |
| 1841 | data = ["abcdefg", "abcdefg", 44] |
| 1842 | f = cStringIO.StringIO() |
| 1843 | pickler = self.pickler_class(f) |
| 1844 | |
| 1845 | pickler.dump(data) |
| 1846 | first_pickled = f.getvalue() |
| 1847 | |
| 1848 | f = cStringIO.StringIO() |
| 1849 | primed = self.pickler_class(f) |
| 1850 | primed.memo = pickler.memo |
| 1851 | |
| 1852 | primed.dump(data) |
| 1853 | primed_pickled = f.getvalue() |
| 1854 | |
| 1855 | self.assertNotEqual(first_pickled, primed_pickled) |
| 1856 | |
| 1857 | def test_priming_unpickler_memo(self): |
| 1858 | # Verify that we can set the Unpickler's memo attribute. |
| 1859 | data = ["abcdefg", "abcdefg", 44] |
| 1860 | f = cStringIO.StringIO() |
| 1861 | pickler = self.pickler_class(f) |
| 1862 | |
| 1863 | pickler.dump(data) |
| 1864 | first_pickled = f.getvalue() |
| 1865 | |
| 1866 | f = cStringIO.StringIO() |
| 1867 | primed = self.pickler_class(f) |
| 1868 | primed.memo = pickler.memo |
| 1869 | |
| 1870 | primed.dump(data) |
| 1871 | primed_pickled = f.getvalue() |
| 1872 | |
| 1873 | unpickler = self.unpickler_class(cStringIO.StringIO(first_pickled)) |
| 1874 | unpickled_data1 = unpickler.load() |
| 1875 | |
| 1876 | self.assertEqual(unpickled_data1, data) |
| 1877 | |
| 1878 | primed = self.unpickler_class(cStringIO.StringIO(primed_pickled)) |
| 1879 | primed.memo = unpickler.memo |
| 1880 | unpickled_data2 = primed.load() |
| 1881 | |
| 1882 | primed.memo.clear() |
| 1883 | |
| 1884 | self.assertEqual(unpickled_data2, data) |
| 1885 | self.assertTrue(unpickled_data2 is unpickled_data1) |
| 1886 | |
| 1887 | def test_reusing_unpickler_objects(self): |
| 1888 | data1 = ["abcdefg", "abcdefg", 44] |
| 1889 | f = cStringIO.StringIO() |
| 1890 | pickler = self.pickler_class(f) |
| 1891 | pickler.dump(data1) |
| 1892 | pickled1 = f.getvalue() |
| 1893 | |
| 1894 | data2 = ["abcdefg", 44, 44] |
| 1895 | f = cStringIO.StringIO() |
| 1896 | pickler = self.pickler_class(f) |
| 1897 | pickler.dump(data2) |
| 1898 | pickled2 = f.getvalue() |
| 1899 | |
| 1900 | f = cStringIO.StringIO() |
| 1901 | f.write(pickled1) |
| 1902 | f.seek(0) |
| 1903 | unpickler = self.unpickler_class(f) |
| 1904 | self.assertEqual(unpickler.load(), data1) |
| 1905 | |
| 1906 | f.seek(0) |
| 1907 | f.truncate() |
| 1908 | f.write(pickled2) |
| 1909 | f.seek(0) |
| 1910 | self.assertEqual(unpickler.load(), data2) |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1911 | |
Serhiy Storchaka | 7c033e6 | 2016-03-06 09:05:47 +0200 | [diff] [blame] | 1912 | def _check_multiple_unpicklings(self, ioclass, seekable): |
| 1913 | for proto in protocols: |
| 1914 | data1 = [(x, str(x)) for x in xrange(2000)] + ["abcde", len] |
| 1915 | f = ioclass() |
| 1916 | pickler = self.pickler_class(f, protocol=proto) |
| 1917 | pickler.dump(data1) |
| 1918 | pickled = f.getvalue() |
| 1919 | |
| 1920 | N = 5 |
| 1921 | f = ioclass(pickled * N) |
| 1922 | unpickler = self.unpickler_class(f) |
| 1923 | for i in xrange(N): |
| 1924 | if seekable: |
| 1925 | pos = f.tell() |
| 1926 | self.assertEqual(unpickler.load(), data1) |
| 1927 | if seekable: |
| 1928 | self.assertEqual(f.tell(), pos + len(pickled)) |
| 1929 | self.assertRaises(EOFError, unpickler.load) |
| 1930 | |
| 1931 | def test_multiple_unpicklings_seekable(self): |
| 1932 | self._check_multiple_unpicklings(StringIO.StringIO, True) |
| 1933 | |
| 1934 | def test_multiple_unpicklings_unseekable(self): |
| 1935 | self._check_multiple_unpicklings(UnseekableIO, False) |
| 1936 | |
| 1937 | def test_unpickling_buffering_readline(self): |
| 1938 | # Issue #12687: the unpickler's buffering logic could fail with |
| 1939 | # text mode opcodes. |
| 1940 | import io |
| 1941 | data = list(xrange(10)) |
| 1942 | for proto in protocols: |
| 1943 | for buf_size in xrange(1, 11): |
| 1944 | f = io.BufferedRandom(io.BytesIO(), buffer_size=buf_size) |
| 1945 | pickler = self.pickler_class(f, protocol=proto) |
| 1946 | pickler.dump(data) |
| 1947 | f.seek(0) |
| 1948 | unpickler = self.unpickler_class(f) |
| 1949 | self.assertEqual(unpickler.load(), data) |
| 1950 | |
| 1951 | |
Serhiy Storchaka | cdc7a91 | 2013-02-12 21:36:47 +0200 | [diff] [blame] | 1952 | class BigmemPickleTests(unittest.TestCase): |
| 1953 | |
| 1954 | # Memory requirements: 1 byte per character for input strings, 1 byte |
| 1955 | # for pickled data, 1 byte for unpickled strings, 1 byte for internal |
| 1956 | # buffer and 1 byte of free space for resizing of internal buffer. |
| 1957 | |
| 1958 | @precisionbigmemtest(size=_2G + 100*_1M, memuse=5) |
| 1959 | def test_huge_strlist(self, size): |
| 1960 | chunksize = 2**20 |
| 1961 | data = [] |
| 1962 | while size > chunksize: |
| 1963 | data.append('x' * chunksize) |
| 1964 | size -= chunksize |
| 1965 | chunksize += 1 |
| 1966 | data.append('y' * size) |
| 1967 | |
| 1968 | try: |
| 1969 | for proto in protocols: |
| 1970 | try: |
| 1971 | pickled = self.dumps(data, proto) |
| 1972 | res = self.loads(pickled) |
| 1973 | self.assertEqual(res, data) |
| 1974 | finally: |
| 1975 | res = None |
| 1976 | pickled = None |
| 1977 | finally: |
| 1978 | data = None |