blob: 4d59bde85116176e23103be486d74b83a4067d1e [file] [log] [blame]
Collin Winter771d8342009-04-16 03:18:06 +00001import io
Jeremy Hylton66426532001-10-15 21:38:56 +00002import unittest
Tim Peters4190fb82003-02-02 16:09:05 +00003import pickle
Tim Peters31f119e2003-02-03 16:20:13 +00004import pickletools
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005import sys
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00006import copyreg
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01007import weakref
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00008from http.cookies import SimpleCookie
Tim Peters4190fb82003-02-02 16:09:05 +00009
Antoine Pitrou82be19f2011-08-29 23:09:33 +020010from test.support import (
Antoine Pitrouee763e22011-08-29 23:14:53 +020011 TestFailed, TESTFN, run_with_locale, no_tracing,
Antoine Pitrou94190bb2011-10-04 10:22:36 +020012 _2G, _4G, bigmemtest,
Antoine Pitrou82be19f2011-08-29 23:09:33 +020013 )
Tim Peterse089c682001-04-10 03:41:41 +000014
Guido van Rossum98297ee2007-11-06 21:34:58 +000015from pickle import bytes_types
16
Tim Petersee1a53c2003-02-02 02:57:53 +000017# Tests that try a number of pickle protocols should have a
18# for proto in protocols:
Tim Peters8587b3c2003-02-13 15:44:41 +000019# kind of outer loop.
Tim Peters8587b3c2003-02-13 15:44:41 +000020protocols = range(pickle.HIGHEST_PROTOCOL + 1)
Tim Petersee1a53c2003-02-02 02:57:53 +000021
Tim Peters22e71712003-02-03 22:27:38 +000022
23# Return True if opcode code appears in the pickle, else False.
24def opcode_in_pickle(code, pickle):
25 for op, dummy, dummy in pickletools.genops(pickle):
Guido van Rossumcfe5f202007-05-08 21:26:54 +000026 if op.code == code.decode("latin-1"):
Tim Peters22e71712003-02-03 22:27:38 +000027 return True
28 return False
29
Tim Peters8d2613a2003-02-11 16:40:16 +000030# Return the number of times opcode code appears in pickle.
31def count_opcode(code, pickle):
32 n = 0
33 for op, dummy, dummy in pickletools.genops(pickle):
Guido van Rossumcfe5f202007-05-08 21:26:54 +000034 if op.code == code.decode("latin-1"):
Tim Peters8d2613a2003-02-11 16:40:16 +000035 n += 1
36 return n
37
Antoine Pitrou04248a82010-10-12 20:51:21 +000038
39class UnseekableIO(io.BytesIO):
40 def peek(self, *args):
41 raise NotImplementedError
42
43 def seekable(self):
44 return False
45
46 def seek(self, *args):
47 raise io.UnsupportedOperation
48
49 def tell(self):
50 raise io.UnsupportedOperation
51
52
Tim Peters3e667d52003-02-04 21:47:44 +000053# We can't very well test the extension registry without putting known stuff
54# in it, but we have to be careful to restore its original state. Code
55# should do this:
56#
57# e = ExtensionSaver(extension_code)
58# try:
59# fiddle w/ the extension registry's stuff for extension_code
60# finally:
61# e.restore()
62
63class ExtensionSaver:
64 # Remember current registration for code (if any), and remove it (if
65 # there is one).
66 def __init__(self, code):
67 self.code = code
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000068 if code in copyreg._inverted_registry:
69 self.pair = copyreg._inverted_registry[code]
70 copyreg.remove_extension(self.pair[0], self.pair[1], code)
Tim Peters3e667d52003-02-04 21:47:44 +000071 else:
72 self.pair = None
73
74 # Restore previous registration for code.
75 def restore(self):
76 code = self.code
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000077 curpair = copyreg._inverted_registry.get(code)
Tim Peters3e667d52003-02-04 21:47:44 +000078 if curpair is not None:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000079 copyreg.remove_extension(curpair[0], curpair[1], code)
Tim Peters3e667d52003-02-04 21:47:44 +000080 pair = self.pair
81 if pair is not None:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000082 copyreg.add_extension(pair[0], pair[1], code)
Tim Peters3e667d52003-02-04 21:47:44 +000083
Jeremy Hylton66426532001-10-15 21:38:56 +000084class C:
Guido van Rossum47b9ff62006-08-24 00:41:19 +000085 def __eq__(self, other):
86 return self.__dict__ == other.__dict__
Jeremy Hylton66426532001-10-15 21:38:56 +000087
Alexander Belopolskyd92f0402010-07-17 22:50:45 +000088class D(C):
89 def __init__(self, arg):
90 pass
91
92class E(C):
93 def __getinitargs__(self):
94 return ()
95
Jeremy Hylton66426532001-10-15 21:38:56 +000096import __main__
97__main__.C = C
98C.__module__ = "__main__"
Alexander Belopolskyd92f0402010-07-17 22:50:45 +000099__main__.D = D
100D.__module__ = "__main__"
101__main__.E = E
102E.__module__ = "__main__"
Jeremy Hylton66426532001-10-15 21:38:56 +0000103
104class myint(int):
105 def __init__(self, x):
106 self.str = str(x)
107
108class initarg(C):
Guido van Rossum1444f672001-12-19 16:38:29 +0000109
Jeremy Hylton66426532001-10-15 21:38:56 +0000110 def __init__(self, a, b):
111 self.a = a
112 self.b = b
113
114 def __getinitargs__(self):
115 return self.a, self.b
116
Guido van Rossum04a86612001-12-19 16:58:54 +0000117class metaclass(type):
118 pass
119
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000120class use_metaclass(object, metaclass=metaclass):
121 pass
Guido van Rossum04a86612001-12-19 16:58:54 +0000122
Antoine Pitrouffd41d92011-10-04 09:23:04 +0200123class pickling_metaclass(type):
124 def __eq__(self, other):
125 return (type(self) == type(other) and
126 self.reduce_args == other.reduce_args)
127
128 def __reduce__(self):
129 return (create_dynamic_class, self.reduce_args)
130
131def create_dynamic_class(name, bases):
132 result = pickling_metaclass(name, bases, dict())
133 result.reduce_args = (name, bases)
134 return result
135
Tim Peters70b02d72003-02-02 17:26:40 +0000136# DATA0 .. DATA2 are the pickles we expect under the various protocols, for
137# the object returned by create_data().
Tim Petersee1a53c2003-02-02 02:57:53 +0000138
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139DATA0 = (
Mark Dickinson8dd05142009-01-20 20:43:58 +0000140 b'(lp0\nL0L\naL1L\naF2.0\nac'
Georg Brandl1a3284e2007-12-02 09:40:06 +0000141 b'builtins\ncomplex\n'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000142 b'p1\n(F3.0\nF0.0\ntp2\nRp'
Mark Dickinson8dd05142009-01-20 20:43:58 +0000143 b'3\naL1L\naL-1L\naL255L\naL-'
144 b'255L\naL-256L\naL65535L\na'
145 b'L-65535L\naL-65536L\naL2'
146 b'147483647L\naL-2147483'
147 b'647L\naL-2147483648L\na('
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000148 b'Vabc\np4\ng4\nccopyreg'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000149 b'\n_reconstructor\np5\n('
Georg Brandl1a3284e2007-12-02 09:40:06 +0000150 b'c__main__\nC\np6\ncbu'
151 b'iltins\nobject\np7\nNt'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000152 b'p8\nRp9\n(dp10\nVfoo\np1'
Mark Dickinson8dd05142009-01-20 20:43:58 +0000153 b'1\nL1L\nsVbar\np12\nL2L\nsb'
154 b'g9\ntp13\nag13\naL5L\na.'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000155)
Tim Peterse9358162001-01-22 22:05:20 +0000156
Guido van Rossum98297ee2007-11-06 21:34:58 +0000157# Disassembly of DATA0
Tim Peters70b02d72003-02-02 17:26:40 +0000158DATA0_DIS = """\
159 0: ( MARK
160 1: l LIST (MARK at 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000161 2: p PUT 0
162 5: L LONG 0
Mark Dickinson8dd05142009-01-20 20:43:58 +0000163 9: a APPEND
164 10: L LONG 1
165 14: a APPEND
166 15: F FLOAT 2.0
167 20: a APPEND
168 21: c GLOBAL 'builtins complex'
169 39: p PUT 1
170 42: ( MARK
171 43: F FLOAT 3.0
172 48: F FLOAT 0.0
173 53: t TUPLE (MARK at 42)
174 54: p PUT 2
175 57: R REDUCE
176 58: p PUT 3
177 61: a APPEND
178 62: L LONG 1
179 66: a APPEND
180 67: L LONG -1
181 72: a APPEND
182 73: L LONG 255
183 79: a APPEND
184 80: L LONG -255
185 87: a APPEND
186 88: L LONG -256
187 95: a APPEND
188 96: L LONG 65535
189 104: a APPEND
190 105: L LONG -65535
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000191 114: a APPEND
Mark Dickinson8dd05142009-01-20 20:43:58 +0000192 115: L LONG -65536
193 124: a APPEND
194 125: L LONG 2147483647
195 138: a APPEND
196 139: L LONG -2147483647
197 153: a APPEND
198 154: L LONG -2147483648
199 168: a APPEND
200 169: ( MARK
201 170: V UNICODE 'abc'
202 175: p PUT 4
203 178: g GET 4
204 181: c GLOBAL 'copyreg _reconstructor'
205 205: p PUT 5
206 208: ( MARK
207 209: c GLOBAL '__main__ C'
208 221: p PUT 6
209 224: c GLOBAL 'builtins object'
210 241: p PUT 7
211 244: N NONE
212 245: t TUPLE (MARK at 208)
213 246: p PUT 8
214 249: R REDUCE
215 250: p PUT 9
216 253: ( MARK
217 254: d DICT (MARK at 253)
218 255: p PUT 10
219 259: V UNICODE 'foo'
220 264: p PUT 11
221 268: L LONG 1
222 272: s SETITEM
223 273: V UNICODE 'bar'
224 278: p PUT 12
225 282: L LONG 2
226 286: s SETITEM
227 287: b BUILD
228 288: g GET 9
229 291: t TUPLE (MARK at 169)
230 292: p PUT 13
231 296: a APPEND
232 297: g GET 13
233 301: a APPEND
234 302: L LONG 5
235 306: a APPEND
236 307: . STOP
Tim Peters70b02d72003-02-02 17:26:40 +0000237highest protocol among opcodes = 0
238"""
239
Guido van Rossum98297ee2007-11-06 21:34:58 +0000240DATA1 = (
Georg Brandl1a3284e2007-12-02 09:40:06 +0000241 b']q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c'
242 b'builtins\ncomplex\nq\x01'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000243 b'(G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00t'
244 b'q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xffJ'
245 b'\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff'
246 b'\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00ab'
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000247 b'cq\x04h\x04ccopyreg\n_reco'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000248 b'nstructor\nq\x05(c__main'
Georg Brandl1a3284e2007-12-02 09:40:06 +0000249 b'__\nC\nq\x06cbuiltins\n'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000250 b'object\nq\x07Ntq\x08Rq\t}q\n('
251 b'X\x03\x00\x00\x00fooq\x0bK\x01X\x03\x00\x00\x00bar'
252 b'q\x0cK\x02ubh\ttq\rh\rK\x05e.'
253)
Tim Peters70b02d72003-02-02 17:26:40 +0000254
Guido van Rossum98297ee2007-11-06 21:34:58 +0000255# Disassembly of DATA1
Tim Peters70b02d72003-02-02 17:26:40 +0000256DATA1_DIS = """\
257 0: ] EMPTY_LIST
Guido van Rossum98297ee2007-11-06 21:34:58 +0000258 1: q BINPUT 0
Tim Peters70b02d72003-02-02 17:26:40 +0000259 3: ( MARK
260 4: K BININT1 0
Guido van Rossum98297ee2007-11-06 21:34:58 +0000261 6: K BININT1 1
262 8: G BINFLOAT 2.0
Georg Brandl1a3284e2007-12-02 09:40:06 +0000263 17: c GLOBAL 'builtins complex'
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000264 35: q BINPUT 1
265 37: ( MARK
266 38: G BINFLOAT 3.0
267 47: G BINFLOAT 0.0
268 56: t TUPLE (MARK at 37)
269 57: q BINPUT 2
270 59: R REDUCE
271 60: q BINPUT 3
272 62: K BININT1 1
273 64: J BININT -1
274 69: K BININT1 255
275 71: J BININT -255
276 76: J BININT -256
277 81: M BININT2 65535
278 84: J BININT -65535
279 89: J BININT -65536
280 94: J BININT 2147483647
281 99: J BININT -2147483647
282 104: J BININT -2147483648
283 109: ( MARK
284 110: X BINUNICODE 'abc'
285 118: q BINPUT 4
286 120: h BINGET 4
287 122: c GLOBAL 'copyreg _reconstructor'
288 146: q BINPUT 5
289 148: ( MARK
290 149: c GLOBAL '__main__ C'
291 161: q BINPUT 6
292 163: c GLOBAL 'builtins object'
293 180: q BINPUT 7
294 182: N NONE
295 183: t TUPLE (MARK at 148)
296 184: q BINPUT 8
297 186: R REDUCE
298 187: q BINPUT 9
299 189: } EMPTY_DICT
300 190: q BINPUT 10
301 192: ( MARK
302 193: X BINUNICODE 'foo'
303 201: q BINPUT 11
304 203: K BININT1 1
305 205: X BINUNICODE 'bar'
306 213: q BINPUT 12
307 215: K BININT1 2
308 217: u SETITEMS (MARK at 192)
309 218: b BUILD
310 219: h BINGET 9
311 221: t TUPLE (MARK at 109)
312 222: q BINPUT 13
313 224: h BINGET 13
314 226: K BININT1 5
315 228: e APPENDS (MARK at 3)
316 229: . STOP
Tim Peters70b02d72003-02-02 17:26:40 +0000317highest protocol among opcodes = 1
318"""
Tim Peterse0c446b2001-10-18 21:57:37 +0000319
Guido van Rossum98297ee2007-11-06 21:34:58 +0000320DATA2 = (
321 b'\x80\x02]q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c'
Georg Brandl1a3284e2007-12-02 09:40:06 +0000322 b'builtins\ncomplex\n'
Guido van Rossum98297ee2007-11-06 21:34:58 +0000323 b'q\x01G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00'
324 b'\x86q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xff'
325 b'J\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff'
326 b'\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00a'
327 b'bcq\x04h\x04c__main__\nC\nq\x05'
328 b')\x81q\x06}q\x07(X\x03\x00\x00\x00fooq\x08K\x01'
329 b'X\x03\x00\x00\x00barq\tK\x02ubh\x06tq\nh'
330 b'\nK\x05e.'
331)
Tim Petersfc273752003-03-02 04:54:24 +0000332
Guido van Rossum98297ee2007-11-06 21:34:58 +0000333# Disassembly of DATA2
Tim Petersfc273752003-03-02 04:54:24 +0000334DATA2_DIS = """\
335 0: \x80 PROTO 2
336 2: ] EMPTY_LIST
Guido van Rossum98297ee2007-11-06 21:34:58 +0000337 3: q BINPUT 0
Tim Petersfc273752003-03-02 04:54:24 +0000338 5: ( MARK
339 6: K BININT1 0
Guido van Rossum98297ee2007-11-06 21:34:58 +0000340 8: K BININT1 1
341 10: G BINFLOAT 2.0
Georg Brandl1a3284e2007-12-02 09:40:06 +0000342 19: c GLOBAL 'builtins complex'
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000343 37: q BINPUT 1
344 39: G BINFLOAT 3.0
345 48: G BINFLOAT 0.0
346 57: \x86 TUPLE2
347 58: q BINPUT 2
348 60: R REDUCE
349 61: q BINPUT 3
350 63: K BININT1 1
351 65: J BININT -1
352 70: K BININT1 255
353 72: J BININT -255
354 77: J BININT -256
355 82: M BININT2 65535
356 85: J BININT -65535
357 90: J BININT -65536
358 95: J BININT 2147483647
359 100: J BININT -2147483647
360 105: J BININT -2147483648
361 110: ( MARK
362 111: X BINUNICODE 'abc'
363 119: q BINPUT 4
364 121: h BINGET 4
365 123: c GLOBAL '__main__ C'
366 135: q BINPUT 5
367 137: ) EMPTY_TUPLE
368 138: \x81 NEWOBJ
369 139: q BINPUT 6
370 141: } EMPTY_DICT
371 142: q BINPUT 7
372 144: ( MARK
373 145: X BINUNICODE 'foo'
374 153: q BINPUT 8
375 155: K BININT1 1
376 157: X BINUNICODE 'bar'
377 165: q BINPUT 9
378 167: K BININT1 2
379 169: u SETITEMS (MARK at 144)
380 170: b BUILD
381 171: h BINGET 6
382 173: t TUPLE (MARK at 110)
383 174: q BINPUT 10
384 176: h BINGET 10
385 178: K BININT1 5
386 180: e APPENDS (MARK at 5)
387 181: . STOP
Tim Petersfc273752003-03-02 04:54:24 +0000388highest protocol among opcodes = 2
389"""
390
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000391# set([1,2]) pickled from 2.x with protocol 2
392DATA3 = b'\x80\x02c__builtin__\nset\nq\x00]q\x01(K\x01K\x02e\x85q\x02Rq\x03.'
393
394# xrange(5) pickled from 2.x with protocol 2
395DATA4 = b'\x80\x02c__builtin__\nxrange\nq\x00K\x00K\x05K\x01\x87q\x01Rq\x02.'
396
397# a SimpleCookie() object pickled from 2.x with protocol 2
398DATA5 = (b'\x80\x02cCookie\nSimpleCookie\nq\x00)\x81q\x01U\x03key'
399 b'q\x02cCookie\nMorsel\nq\x03)\x81q\x04(U\x07commentq\x05U'
400 b'\x00q\x06U\x06domainq\x07h\x06U\x06secureq\x08h\x06U\x07'
401 b'expiresq\th\x06U\x07max-ageq\nh\x06U\x07versionq\x0bh\x06U'
402 b'\x04pathq\x0ch\x06U\x08httponlyq\rh\x06u}q\x0e(U\x0b'
403 b'coded_valueq\x0fU\x05valueq\x10h\x10h\x10h\x02h\x02ubs}q\x11b.')
404
405# set([3]) pickled from 2.x with protocol 2
406DATA6 = b'\x80\x02c__builtin__\nset\nq\x00]q\x01K\x03a\x85q\x02Rq\x03.'
407
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100408python2_exceptions_without_args = (
409 ArithmeticError,
410 AssertionError,
411 AttributeError,
412 BaseException,
413 BufferError,
414 BytesWarning,
415 DeprecationWarning,
416 EOFError,
417 EnvironmentError,
418 Exception,
419 FloatingPointError,
420 FutureWarning,
421 GeneratorExit,
422 IOError,
423 ImportError,
424 ImportWarning,
425 IndentationError,
426 IndexError,
427 KeyError,
428 KeyboardInterrupt,
429 LookupError,
430 MemoryError,
431 NameError,
432 NotImplementedError,
433 OSError,
434 OverflowError,
435 PendingDeprecationWarning,
436 ReferenceError,
437 RuntimeError,
438 RuntimeWarning,
439 # StandardError is gone in Python 3, we map it to Exception
440 StopIteration,
441 SyntaxError,
442 SyntaxWarning,
443 SystemError,
444 SystemExit,
445 TabError,
446 TypeError,
447 UnboundLocalError,
448 UnicodeError,
449 UnicodeWarning,
450 UserWarning,
451 ValueError,
452 Warning,
453 ZeroDivisionError,
454)
455
456exception_pickle = b'\x80\x02cexceptions\n?\nq\x00)Rq\x01.'
457
458# Exception objects without arguments pickled from 2.x with protocol 2
459DATA7 = {
460 exception :
461 exception_pickle.replace(b'?', exception.__name__.encode("ascii"))
462 for exception in python2_exceptions_without_args
463}
464
465# StandardError is mapped to Exception, test that separately
466DATA8 = exception_pickle.replace(b'?', b'StandardError')
467
468# UnicodeEncodeError object pickled from 2.x with protocol 2
469DATA9 = (b'\x80\x02cexceptions\nUnicodeEncodeError\n'
470 b'q\x00(U\x05asciiq\x01X\x03\x00\x00\x00fooq\x02K\x00K\x01'
471 b'U\x03badq\x03tq\x04Rq\x05.')
472
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000473
Jeremy Hylton66426532001-10-15 21:38:56 +0000474def create_data():
Tim Peterse9358162001-01-22 22:05:20 +0000475 c = C()
476 c.foo = 1
477 c.bar = 2
Guido van Rossume2a383d2007-01-15 16:59:06 +0000478 x = [0, 1, 2.0, 3.0+0j]
Tim Peters461922a2001-04-09 20:07:05 +0000479 # 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 Peterse9358162001-01-22 22:05:20 +0000488 y = ('abc', 'abc', c, c)
489 x.append(y)
490 x.append(y)
491 x.append(5)
Jeremy Hylton66426532001-10-15 21:38:56 +0000492 return x
Tim Petersc58440f2001-04-09 17:16:31 +0000493
Jeremy Hylton66426532001-10-15 21:38:56 +0000494class AbstractPickleTests(unittest.TestCase):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495 # Subclass must define self.dumps, self.loads.
Tim Petersc58440f2001-04-09 17:16:31 +0000496
Jeremy Hylton66426532001-10-15 21:38:56 +0000497 _testdata = create_data()
Tim Petersc58440f2001-04-09 17:16:31 +0000498
Jeremy Hylton66426532001-10-15 21:38:56 +0000499 def setUp(self):
Tim Peterse9358162001-01-22 22:05:20 +0000500 pass
Tim Petersc58440f2001-04-09 17:16:31 +0000501
Jeremy Hylton66426532001-10-15 21:38:56 +0000502 def test_misc(self):
503 # test various datatypes not tested by testdata
Tim Peters70b02d72003-02-02 17:26:40 +0000504 for proto in protocols:
505 x = myint(4)
506 s = self.dumps(x, proto)
507 y = self.loads(s)
508 self.assertEqual(x, y)
Tim Peterse9358162001-01-22 22:05:20 +0000509
Tim Peters70b02d72003-02-02 17:26:40 +0000510 x = (1, ())
511 s = self.dumps(x, proto)
512 y = self.loads(s)
513 self.assertEqual(x, y)
Tim Peterse9358162001-01-22 22:05:20 +0000514
Tim Peters70b02d72003-02-02 17:26:40 +0000515 x = initarg(1, x)
516 s = self.dumps(x, proto)
517 y = self.loads(s)
518 self.assertEqual(x, y)
Tim Peterse9358162001-01-22 22:05:20 +0000519
Jeremy Hylton66426532001-10-15 21:38:56 +0000520 # XXX test __reduce__ protocol?
521
Tim Peters70b02d72003-02-02 17:26:40 +0000522 def test_roundtrip_equality(self):
523 expected = self._testdata
524 for proto in protocols:
525 s = self.dumps(expected, proto)
526 got = self.loads(s)
527 self.assertEqual(expected, got)
Jeremy Hylton66426532001-10-15 21:38:56 +0000528
Guido van Rossum98297ee2007-11-06 21:34:58 +0000529 def test_load_from_data0(self):
530 self.assertEqual(self._testdata, self.loads(DATA0))
531
532 def test_load_from_data1(self):
533 self.assertEqual(self._testdata, self.loads(DATA1))
534
535 def test_load_from_data2(self):
536 self.assertEqual(self._testdata, self.loads(DATA2))
Jeremy Hylton66426532001-10-15 21:38:56 +0000537
Alexander Belopolskyd92f0402010-07-17 22:50:45 +0000538 def test_load_classic_instance(self):
539 # See issue5180. Test loading 2.x pickles that
540 # contain an instance of old style class.
541 for X, args in [(C, ()), (D, ('x',)), (E, ())]:
542 xname = X.__name__.encode('ascii')
543 # Protocol 0 (text mode pickle):
544 """
545 0: ( MARK
546 1: i INST '__main__ X' (MARK at 0)
547 15: p PUT 0
548 18: ( MARK
549 19: d DICT (MARK at 18)
550 20: p PUT 1
551 23: b BUILD
552 24: . STOP
553 """
554 pickle0 = (b"(i__main__\n"
555 b"X\n"
556 b"p0\n"
557 b"(dp1\nb.").replace(b'X', xname)
558 self.assertEqual(X(*args), self.loads(pickle0))
559
560 # Protocol 1 (binary mode pickle)
561 """
562 0: ( MARK
563 1: c GLOBAL '__main__ X'
564 15: q BINPUT 0
565 17: o OBJ (MARK at 0)
566 18: q BINPUT 1
567 20: } EMPTY_DICT
568 21: q BINPUT 2
569 23: b BUILD
570 24: . STOP
571 """
572 pickle1 = (b'(c__main__\n'
573 b'X\n'
574 b'q\x00oq\x01}q\x02b.').replace(b'X', xname)
575 self.assertEqual(X(*args), self.loads(pickle1))
576
577 # Protocol 2 (pickle2 = b'\x80\x02' + pickle1)
578 """
579 0: \x80 PROTO 2
580 2: ( MARK
581 3: c GLOBAL '__main__ X'
582 17: q BINPUT 0
583 19: o OBJ (MARK at 2)
584 20: q BINPUT 1
585 22: } EMPTY_DICT
586 23: q BINPUT 2
587 25: b BUILD
588 26: . STOP
589 """
590 pickle2 = (b'\x80\x02(c__main__\n'
591 b'X\n'
592 b'q\x00oq\x01}q\x02b.').replace(b'X', xname)
593 self.assertEqual(X(*args), self.loads(pickle2))
594
Tim Peters70b02d72003-02-02 17:26:40 +0000595 # There are gratuitous differences between pickles produced by
596 # pickle and cPickle, largely because cPickle starts PUT indices at
597 # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() --
598 # there's a comment with an exclamation point there whose meaning
599 # is a mystery. cPickle also suppresses PUT for objects with a refcount
600 # of 1.
601 def dont_test_disassembly(self):
Guido van Rossum34d19282007-08-09 01:03:29 +0000602 from io import StringIO
Tim Peters70b02d72003-02-02 17:26:40 +0000603 from pickletools import dis
604
605 for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS):
606 s = self.dumps(self._testdata, proto)
607 filelike = StringIO()
608 dis(s, out=filelike)
609 got = filelike.getvalue()
610 self.assertEqual(expected, got)
Jeremy Hylton66426532001-10-15 21:38:56 +0000611
612 def test_recursive_list(self):
613 l = []
614 l.append(l)
Tim Peters70b02d72003-02-02 17:26:40 +0000615 for proto in protocols:
616 s = self.dumps(l, proto)
617 x = self.loads(s)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000618 self.assertEqual(len(x), 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000619 self.assertTrue(x is x[0])
Jeremy Hylton66426532001-10-15 21:38:56 +0000620
Collin Winter8ca69de2009-05-26 16:53:41 +0000621 def test_recursive_tuple(self):
622 t = ([],)
623 t[0].append(t)
624 for proto in protocols:
625 s = self.dumps(t, proto)
626 x = self.loads(s)
627 self.assertEqual(len(x), 1)
628 self.assertEqual(len(x[0]), 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000629 self.assertTrue(x is x[0][0])
Collin Winter8ca69de2009-05-26 16:53:41 +0000630
Jeremy Hylton66426532001-10-15 21:38:56 +0000631 def test_recursive_dict(self):
632 d = {}
633 d[1] = d
Tim Peters70b02d72003-02-02 17:26:40 +0000634 for proto in protocols:
635 s = self.dumps(d, proto)
636 x = self.loads(s)
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000637 self.assertEqual(list(x.keys()), [1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000638 self.assertTrue(x[1] is x)
Jeremy Hylton66426532001-10-15 21:38:56 +0000639
640 def test_recursive_inst(self):
641 i = C()
642 i.attr = i
Tim Peters70b02d72003-02-02 17:26:40 +0000643 for proto in protocols:
Ezio Melottiaaef3442013-03-04 15:17:56 +0200644 s = self.dumps(i, proto)
Tim Peters70b02d72003-02-02 17:26:40 +0000645 x = self.loads(s)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000646 self.assertEqual(dir(x), dir(i))
Ezio Melottiaaef3442013-03-04 15:17:56 +0200647 self.assertIs(x.attr, x)
Jeremy Hylton66426532001-10-15 21:38:56 +0000648
649 def test_recursive_multi(self):
650 l = []
651 d = {1:l}
652 i = C()
653 i.attr = d
654 l.append(i)
Tim Peters70b02d72003-02-02 17:26:40 +0000655 for proto in protocols:
656 s = self.dumps(l, proto)
657 x = self.loads(s)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000658 self.assertEqual(len(x), 1)
659 self.assertEqual(dir(x[0]), dir(i))
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000660 self.assertEqual(list(x[0].attr.keys()), [1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000661 self.assertTrue(x[0].attr[1] is x)
Jeremy Hylton66426532001-10-15 21:38:56 +0000662
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000663 def test_get(self):
664 self.assertRaises(KeyError, self.loads, b'g0\np0')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000665 self.assertEqual(self.loads(b'((Kdtp0\nh\x00l.))'), [(100,), (100,)])
Jeremy Hylton66426532001-10-15 21:38:56 +0000666
667 def test_insecure_strings(self):
Guido van Rossum39478e82007-08-27 17:23:59 +0000668 # XXX Some of these tests are temporarily disabled
669 insecure = [b"abc", b"2 + 2", # not quoted
670 ## b"'abc' + 'def'", # not a single quoted string
671 b"'abc", # quote is not closed
672 b"'abc\"", # open quote and close quote don't match
673 b"'abc' ?", # junk after close quote
674 b"'\\'", # trailing backslash
Antoine Pitrou3034efd2013-04-15 21:51:09 +0200675 # Variations on issue #17710
676 b"'",
677 b'"',
678 b"' ",
679 b"' ",
680 b"' ",
681 b"' ",
682 b'" ',
Jeremy Hylton66426532001-10-15 21:38:56 +0000683 # some tests of the quoting rules
Guido van Rossum39478e82007-08-27 17:23:59 +0000684 ## b"'abc\"\''",
685 ## b"'\\\\a\'\'\'\\\'\\\\\''",
Jeremy Hylton66426532001-10-15 21:38:56 +0000686 ]
Guido van Rossum39478e82007-08-27 17:23:59 +0000687 for b in insecure:
688 buf = b"S" + b + b"\012p0\012."
Jeremy Hylton66426532001-10-15 21:38:56 +0000689 self.assertRaises(ValueError, self.loads, buf)
690
Walter Dörwald9b775532007-06-08 14:30:53 +0000691 def test_unicode(self):
Benjamin Petersond1486302008-12-27 16:58:50 +0000692 endcases = ['', '<\\u>', '<\\\u1234>', '<\n>',
Victor Stinner485fb562010-04-13 11:07:24 +0000693 '<\\>', '<\\\U00012345>',
694 # surrogates
695 '<\udc80>']
Walter Dörwald9b775532007-06-08 14:30:53 +0000696 for proto in protocols:
697 for u in endcases:
698 p = self.dumps(u, proto)
699 u2 = self.loads(p)
700 self.assertEqual(u2, u)
Tim Peterse089c682001-04-10 03:41:41 +0000701
Alexandre Vassalotti554d8782008-12-27 07:32:41 +0000702 def test_unicode_high_plane(self):
703 t = '\U00012345'
704 for proto in protocols:
705 p = self.dumps(t, proto)
706 t2 = self.loads(p)
707 self.assertEqual(t2, t)
708
Guido van Rossumf4169812008-03-17 22:56:06 +0000709 def test_bytes(self):
710 for proto in protocols:
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500711 for s in b'', b'xyz', b'xyz'*100:
Ezio Melottiaaef3442013-03-04 15:17:56 +0200712 p = self.dumps(s, proto)
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500713 self.assertEqual(self.loads(p), s)
714 for s in [bytes([i]) for i in range(256)]:
Ezio Melottiaaef3442013-03-04 15:17:56 +0200715 p = self.dumps(s, proto)
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500716 self.assertEqual(self.loads(p), s)
717 for s in [bytes([i, i]) for i in range(256)]:
Ezio Melottiaaef3442013-03-04 15:17:56 +0200718 p = self.dumps(s, proto)
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500719 self.assertEqual(self.loads(p), s)
Guido van Rossumf4169812008-03-17 22:56:06 +0000720
Jeremy Hylton66426532001-10-15 21:38:56 +0000721 def test_ints(self):
722 import sys
Tim Petersee1a53c2003-02-02 02:57:53 +0000723 for proto in protocols:
Christian Heimesa37d4c62007-12-04 23:02:19 +0000724 n = sys.maxsize
Tim Petersee1a53c2003-02-02 02:57:53 +0000725 while n:
726 for expected in (-n, n):
727 s = self.dumps(expected, proto)
728 n2 = self.loads(s)
729 self.assertEqual(expected, n2)
730 n = n >> 1
Tim Peters19ef62d2001-08-28 22:21:18 +0000731
Jeremy Hylton66426532001-10-15 21:38:56 +0000732 def test_maxint64(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000733 maxint64 = (1 << 63) - 1
Guido van Rossum39478e82007-08-27 17:23:59 +0000734 data = b'I' + str(maxint64).encode("ascii") + b'\n.'
Jeremy Hylton66426532001-10-15 21:38:56 +0000735 got = self.loads(data)
736 self.assertEqual(got, maxint64)
737
738 # Try too with a bogus literal.
Guido van Rossum39478e82007-08-27 17:23:59 +0000739 data = b'I' + str(maxint64).encode("ascii") + b'JUNK\n.'
Jeremy Hylton66426532001-10-15 21:38:56 +0000740 self.assertRaises(ValueError, self.loads, data)
741
Tim Petersee1a53c2003-02-02 02:57:53 +0000742 def test_long(self):
743 for proto in protocols:
Tim Petersbf2674b2003-02-02 07:51:32 +0000744 # 256 bytes is where LONG4 begins.
Tim Petersee1a53c2003-02-02 02:57:53 +0000745 for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000746 nbase = 1 << nbits
Tim Petersee1a53c2003-02-02 02:57:53 +0000747 for npos in nbase-1, nbase, nbase+1:
748 for n in npos, -npos:
749 pickle = self.dumps(n, proto)
750 got = self.loads(pickle)
751 self.assertEqual(n, got)
752 # Try a monster. This is quadratic-time in protos 0 & 1, so don't
753 # bother with those.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000754 nbase = int("deadbeeffeedface", 16)
Tim Petersee1a53c2003-02-02 02:57:53 +0000755 nbase += nbase << 1000000
756 for n in nbase, -nbase:
Tim Petersee1a53c2003-02-02 02:57:53 +0000757 p = self.dumps(n, 2)
Tim Petersee1a53c2003-02-02 02:57:53 +0000758 got = self.loads(p)
Tim Petersee1a53c2003-02-02 02:57:53 +0000759 self.assertEqual(n, got)
760
Mark Dickinsoncddcf442009-01-24 21:46:33 +0000761 def test_float(self):
762 test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5,
763 3.14, 263.44582062374053, 6.022e23, 1e30]
764 test_values = test_values + [-x for x in test_values]
765 for proto in protocols:
766 for value in test_values:
767 pickle = self.dumps(value, proto)
768 got = self.loads(pickle)
769 self.assertEqual(value, got)
770
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 @run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
772 def test_float_format(self):
Guido van Rossumf4169812008-03-17 22:56:06 +0000773 # make sure that floats are formatted locale independent with proto 0
774 self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775
Jeremy Hylton66426532001-10-15 21:38:56 +0000776 def test_reduce(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000777 pass
Jeremy Hylton66426532001-10-15 21:38:56 +0000778
779 def test_getinitargs(self):
780 pass
781
Antoine Pitrou79035bd2012-06-26 23:04:48 +0200782 def test_pop_empty_stack(self):
783 # Test issue7455
784 s = b'0'
785 self.assertRaises((pickle.UnpicklingError, IndexError), self.loads, s)
786
Guido van Rossum04a86612001-12-19 16:58:54 +0000787 def test_metaclass(self):
788 a = use_metaclass()
Tim Peters70b02d72003-02-02 17:26:40 +0000789 for proto in protocols:
790 s = self.dumps(a, proto)
791 b = self.loads(s)
792 self.assertEqual(a.__class__, b.__class__)
Guido van Rossum04a86612001-12-19 16:58:54 +0000793
Antoine Pitrouffd41d92011-10-04 09:23:04 +0200794 def test_dynamic_class(self):
795 a = create_dynamic_class("my_dynamic_class", (object,))
796 copyreg.pickle(pickling_metaclass, pickling_metaclass.__reduce__)
797 for proto in protocols:
798 s = self.dumps(a, proto)
799 b = self.loads(s)
800 self.assertEqual(a, b)
801
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000802 def test_structseq(self):
803 import time
Michael W. Hudson0e025302002-03-06 17:11:18 +0000804 import os
Tim Peters70b02d72003-02-02 17:26:40 +0000805
806 t = time.localtime()
807 for proto in protocols:
808 s = self.dumps(t, proto)
Michael W. Hudson0e025302002-03-06 17:11:18 +0000809 u = self.loads(s)
810 self.assertEqual(t, u)
Tim Peters70b02d72003-02-02 17:26:40 +0000811 if hasattr(os, "stat"):
812 t = os.stat(os.curdir)
813 s = self.dumps(t, proto)
814 u = self.loads(s)
815 self.assertEqual(t, u)
816 if hasattr(os, "statvfs"):
817 t = os.statvfs(os.curdir)
818 s = self.dumps(t, proto)
819 u = self.loads(s)
820 self.assertEqual(t, u)
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000821
Łukasz Langaf3078fb2012-03-12 19:46:12 +0100822 def test_ellipsis(self):
823 for proto in protocols:
824 s = self.dumps(..., proto)
825 u = self.loads(s)
826 self.assertEqual(..., u)
827
828 def test_notimplemented(self):
829 for proto in protocols:
830 s = self.dumps(NotImplemented, proto)
831 u = self.loads(s)
832 self.assertEqual(NotImplemented, u)
833
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -0800834 def test_singleton_types(self):
835 # Issue #6477: Test that types of built-in singletons can be pickled.
836 singletons = [None, ..., NotImplemented]
837 for singleton in singletons:
838 for proto in protocols:
839 s = self.dumps(type(singleton), proto)
840 u = self.loads(s)
841 self.assertIs(type(singleton), u)
842
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000843 # Tests for protocol 2
844
Tim Peters4190fb82003-02-02 16:09:05 +0000845 def test_proto(self):
846 build_none = pickle.NONE + pickle.STOP
847 for proto in protocols:
848 expected = build_none
849 if proto >= 2:
Guido van Rossumcfe5f202007-05-08 21:26:54 +0000850 expected = pickle.PROTO + bytes([proto]) + expected
Tim Peters4190fb82003-02-02 16:09:05 +0000851 p = self.dumps(None, proto)
852 self.assertEqual(p, expected)
853
854 oob = protocols[-1] + 1 # a future protocol
Guido van Rossumcfe5f202007-05-08 21:26:54 +0000855 badpickle = pickle.PROTO + bytes([oob]) + build_none
Tim Peters4190fb82003-02-02 16:09:05 +0000856 try:
857 self.loads(badpickle)
Guido van Rossumb940e112007-01-10 16:19:56 +0000858 except ValueError as detail:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000859 self.assertTrue(str(detail).startswith(
Tim Peters4190fb82003-02-02 16:09:05 +0000860 "unsupported pickle protocol"))
861 else:
862 self.fail("expected bad protocol number to raise ValueError")
863
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000864 def test_long1(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000865 x = 12345678910111213141516178920
Tim Peters61bf2572003-02-03 21:31:22 +0000866 for proto in protocols:
867 s = self.dumps(x, proto)
868 y = self.loads(s)
869 self.assertEqual(x, y)
Tim Peters22e71712003-02-03 22:27:38 +0000870 self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000871
872 def test_long4(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000873 x = 12345678910111213141516178920 << (256*8)
Tim Peters61bf2572003-02-03 21:31:22 +0000874 for proto in protocols:
875 s = self.dumps(x, proto)
876 y = self.loads(s)
877 self.assertEqual(x, y)
Tim Peters22e71712003-02-03 22:27:38 +0000878 self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000879
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000880 def test_short_tuples(self):
Tim Peters1d63c9f2003-02-02 20:29:39 +0000881 # Map (proto, len(tuple)) to expected opcode.
882 expected_opcode = {(0, 0): pickle.TUPLE,
883 (0, 1): pickle.TUPLE,
884 (0, 2): pickle.TUPLE,
885 (0, 3): pickle.TUPLE,
886 (0, 4): pickle.TUPLE,
887
888 (1, 0): pickle.EMPTY_TUPLE,
889 (1, 1): pickle.TUPLE,
890 (1, 2): pickle.TUPLE,
891 (1, 3): pickle.TUPLE,
892 (1, 4): pickle.TUPLE,
893
894 (2, 0): pickle.EMPTY_TUPLE,
895 (2, 1): pickle.TUPLE1,
896 (2, 2): pickle.TUPLE2,
897 (2, 3): pickle.TUPLE3,
898 (2, 4): pickle.TUPLE,
Guido van Rossumf4169812008-03-17 22:56:06 +0000899
900 (3, 0): pickle.EMPTY_TUPLE,
901 (3, 1): pickle.TUPLE1,
902 (3, 2): pickle.TUPLE2,
903 (3, 3): pickle.TUPLE3,
904 (3, 4): pickle.TUPLE,
Tim Peters1d63c9f2003-02-02 20:29:39 +0000905 }
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000906 a = ()
Guido van Rossum025bc2f2003-01-28 04:20:02 +0000907 b = (1,)
908 c = (1, 2)
909 d = (1, 2, 3)
910 e = (1, 2, 3, 4)
Tim Peters4190fb82003-02-02 16:09:05 +0000911 for proto in protocols:
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000912 for x in a, b, c, d, e:
913 s = self.dumps(x, proto)
914 y = self.loads(s)
915 self.assertEqual(x, y, (proto, x, s, y))
Tim Peters1d63c9f2003-02-02 20:29:39 +0000916 expected = expected_opcode[proto, len(x)]
Tim Peters22e71712003-02-03 22:27:38 +0000917 self.assertEqual(opcode_in_pickle(expected, s), True)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000918
Guido van Rossum7d97d312003-01-28 04:25:27 +0000919 def test_singletons(self):
Tim Peters61bf2572003-02-03 21:31:22 +0000920 # Map (proto, singleton) to expected opcode.
921 expected_opcode = {(0, None): pickle.NONE,
922 (1, None): pickle.NONE,
923 (2, None): pickle.NONE,
Guido van Rossumf4169812008-03-17 22:56:06 +0000924 (3, None): pickle.NONE,
Tim Peters61bf2572003-02-03 21:31:22 +0000925
926 (0, True): pickle.INT,
927 (1, True): pickle.INT,
928 (2, True): pickle.NEWTRUE,
Guido van Rossumf4169812008-03-17 22:56:06 +0000929 (3, True): pickle.NEWTRUE,
Tim Peters61bf2572003-02-03 21:31:22 +0000930
931 (0, False): pickle.INT,
932 (1, False): pickle.INT,
933 (2, False): pickle.NEWFALSE,
Guido van Rossumf4169812008-03-17 22:56:06 +0000934 (3, False): pickle.NEWFALSE,
Tim Peters61bf2572003-02-03 21:31:22 +0000935 }
Tim Peters4190fb82003-02-02 16:09:05 +0000936 for proto in protocols:
Guido van Rossum7d97d312003-01-28 04:25:27 +0000937 for x in None, False, True:
938 s = self.dumps(x, proto)
939 y = self.loads(s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000940 self.assertTrue(x is y, (proto, x, s, y))
Tim Peters61bf2572003-02-03 21:31:22 +0000941 expected = expected_opcode[proto, x]
Tim Peters22e71712003-02-03 22:27:38 +0000942 self.assertEqual(opcode_in_pickle(expected, s), True)
Tim Peters3c67d792003-02-02 17:59:11 +0000943
Guido van Rossum533dbcf2003-01-28 17:55:05 +0000944 def test_newobj_tuple(self):
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000945 x = MyTuple([1, 2, 3])
946 x.foo = 42
947 x.bar = "hello"
Tim Peters894453a2003-02-03 22:32:18 +0000948 for proto in protocols:
949 s = self.dumps(x, proto)
950 y = self.loads(s)
951 self.assertEqual(tuple(x), tuple(y))
952 self.assertEqual(x.__dict__, y.__dict__)
Guido van Rossum533dbcf2003-01-28 17:55:05 +0000953
954 def test_newobj_list(self):
Guido van Rossum3d8c01b2003-01-28 19:48:18 +0000955 x = MyList([1, 2, 3])
956 x.foo = 42
957 x.bar = "hello"
Tim Peters894453a2003-02-03 22:32:18 +0000958 for proto in protocols:
959 s = self.dumps(x, proto)
960 y = self.loads(s)
961 self.assertEqual(list(x), list(y))
962 self.assertEqual(x.__dict__, y.__dict__)
Guido van Rossum533dbcf2003-01-28 17:55:05 +0000963
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000964 def test_newobj_generic(self):
Tim Peters5013bd92003-02-03 22:28:41 +0000965 for proto in protocols:
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000966 for C in myclasses:
967 B = C.__base__
968 x = C(C.sample)
969 x.foo = 42
970 s = self.dumps(x, proto)
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000971 y = self.loads(s)
972 detail = (proto, C, B, x, y, type(y))
973 self.assertEqual(B(x), B(y), detail)
974 self.assertEqual(x.__dict__, y.__dict__, detail)
975
Antoine Pitrou16c4ce12011-03-11 21:30:43 +0100976 def test_newobj_proxies(self):
977 # NEWOBJ should use the __class__ rather than the raw type
978 classes = myclasses[:]
979 # Cannot create weakproxies to these classes
980 for c in (MyInt, MyTuple):
981 classes.remove(c)
982 for proto in protocols:
983 for C in classes:
984 B = C.__base__
985 x = C(C.sample)
986 x.foo = 42
987 p = weakref.proxy(x)
988 s = self.dumps(p, proto)
989 y = self.loads(s)
990 self.assertEqual(type(y), type(x)) # rather than type(p)
991 detail = (proto, C, B, x, y, type(y))
992 self.assertEqual(B(x), B(y), detail)
993 self.assertEqual(x.__dict__, y.__dict__, detail)
994
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000995 # Register a type with copyreg, with extension code extcode. Pickle
Tim Peters22e71712003-02-03 22:27:38 +0000996 # an object of that type. Check that the resulting pickle uses opcode
997 # (EXT[124]) under proto 2, and not in proto 1.
Tim Peters3e667d52003-02-04 21:47:44 +0000998
Tim Peters22e71712003-02-03 22:27:38 +0000999 def produce_global_ext(self, extcode, opcode):
Tim Peters3e667d52003-02-04 21:47:44 +00001000 e = ExtensionSaver(extcode)
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001001 try:
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001002 copyreg.add_extension(__name__, "MyList", extcode)
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001003 x = MyList([1, 2, 3])
1004 x.foo = 42
1005 x.bar = "hello"
1006
Tim Peters22e71712003-02-03 22:27:38 +00001007 # Dump using protocol 1 for comparison.
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001008 s1 = self.dumps(x, 1)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001009 self.assertIn(__name__.encode("utf-8"), s1)
1010 self.assertIn(b"MyList", s1)
Tim Peters3e667d52003-02-04 21:47:44 +00001011 self.assertEqual(opcode_in_pickle(opcode, s1), False)
1012
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001013 y = self.loads(s1)
1014 self.assertEqual(list(x), list(y))
1015 self.assertEqual(x.__dict__, y.__dict__)
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001016
Tim Peters22e71712003-02-03 22:27:38 +00001017 # Dump using protocol 2 for test.
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001018 s2 = self.dumps(x, 2)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001019 self.assertNotIn(__name__.encode("utf-8"), s2)
1020 self.assertNotIn(b"MyList", s2)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001021 self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2))
Tim Peters3e667d52003-02-04 21:47:44 +00001022
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001023 y = self.loads(s2)
1024 self.assertEqual(list(x), list(y))
1025 self.assertEqual(x.__dict__, y.__dict__)
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001026
1027 finally:
Tim Peters3e667d52003-02-04 21:47:44 +00001028 e.restore()
Tim Peters22e71712003-02-03 22:27:38 +00001029
1030 def test_global_ext1(self):
Tim Peters3e667d52003-02-04 21:47:44 +00001031 self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code
1032 self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001033
1034 def test_global_ext2(self):
Tim Peters3e667d52003-02-04 21:47:44 +00001035 self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code
1036 self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code
1037 self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001038
1039 def test_global_ext4(self):
Tim Peters3e667d52003-02-04 21:47:44 +00001040 self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code
1041 self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code
1042 self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness
1043
Tim Peters8d2613a2003-02-11 16:40:16 +00001044 def test_list_chunking(self):
1045 n = 10 # too small to chunk
Guido van Rossum805365e2007-05-07 22:24:25 +00001046 x = list(range(n))
Tim Peters8d2613a2003-02-11 16:40:16 +00001047 for proto in protocols:
1048 s = self.dumps(x, proto)
1049 y = self.loads(s)
1050 self.assertEqual(x, y)
1051 num_appends = count_opcode(pickle.APPENDS, s)
1052 self.assertEqual(num_appends, proto > 0)
1053
1054 n = 2500 # expect at least two chunks when proto > 0
Guido van Rossum805365e2007-05-07 22:24:25 +00001055 x = list(range(n))
Tim Peters8d2613a2003-02-11 16:40:16 +00001056 for proto in protocols:
1057 s = self.dumps(x, proto)
1058 y = self.loads(s)
1059 self.assertEqual(x, y)
1060 num_appends = count_opcode(pickle.APPENDS, s)
1061 if proto == 0:
1062 self.assertEqual(num_appends, 0)
1063 else:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001064 self.assertTrue(num_appends >= 2)
Tim Peters8d2613a2003-02-11 16:40:16 +00001065
1066 def test_dict_chunking(self):
1067 n = 10 # too small to chunk
1068 x = dict.fromkeys(range(n))
1069 for proto in protocols:
1070 s = self.dumps(x, proto)
Ezio Melottie9615932010-01-24 19:26:24 +00001071 self.assertIsInstance(s, bytes_types)
Tim Peters8d2613a2003-02-11 16:40:16 +00001072 y = self.loads(s)
1073 self.assertEqual(x, y)
1074 num_setitems = count_opcode(pickle.SETITEMS, s)
1075 self.assertEqual(num_setitems, proto > 0)
1076
1077 n = 2500 # expect at least two chunks when proto > 0
1078 x = dict.fromkeys(range(n))
1079 for proto in protocols:
1080 s = self.dumps(x, proto)
1081 y = self.loads(s)
1082 self.assertEqual(x, y)
1083 num_setitems = count_opcode(pickle.SETITEMS, s)
1084 if proto == 0:
1085 self.assertEqual(num_setitems, 0)
1086 else:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001087 self.assertTrue(num_setitems >= 2)
Guido van Rossum0322d0f2003-01-29 06:12:46 +00001088
Tim Peterse9ef2032003-02-13 18:42:00 +00001089 def test_simple_newobj(self):
1090 x = object.__new__(SimpleNewObj) # avoid __init__
1091 x.abc = 666
1092 for proto in protocols:
1093 s = self.dumps(x, proto)
1094 self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
1095 y = self.loads(s) # will raise TypeError if __init__ called
1096 self.assertEqual(y.abc, 666)
1097 self.assertEqual(x.__dict__, y.__dict__)
1098
Tim Peters42f08ac2003-02-11 22:43:24 +00001099 def test_newobj_list_slots(self):
1100 x = SlotList([1, 2, 3])
1101 x.foo = 42
1102 x.bar = "hello"
1103 s = self.dumps(x, 2)
1104 y = self.loads(s)
1105 self.assertEqual(list(x), list(y))
1106 self.assertEqual(x.__dict__, y.__dict__)
1107 self.assertEqual(x.foo, y.foo)
1108 self.assertEqual(x.bar, y.bar)
1109
Guido van Rossum2a30b212003-02-18 22:41:24 +00001110 def test_reduce_overrides_default_reduce_ex(self):
Collin Winter771d8342009-04-16 03:18:06 +00001111 for proto in protocols:
Guido van Rossum2a30b212003-02-18 22:41:24 +00001112 x = REX_one()
1113 self.assertEqual(x._reduce_called, 0)
1114 s = self.dumps(x, proto)
1115 self.assertEqual(x._reduce_called, 1)
1116 y = self.loads(s)
1117 self.assertEqual(y._reduce_called, 0)
1118
1119 def test_reduce_ex_called(self):
Collin Winter771d8342009-04-16 03:18:06 +00001120 for proto in protocols:
Guido van Rossum2a30b212003-02-18 22:41:24 +00001121 x = REX_two()
1122 self.assertEqual(x._proto, None)
1123 s = self.dumps(x, proto)
1124 self.assertEqual(x._proto, proto)
1125 y = self.loads(s)
1126 self.assertEqual(y._proto, None)
1127
1128 def test_reduce_ex_overrides_reduce(self):
Collin Winter771d8342009-04-16 03:18:06 +00001129 for proto in protocols:
Guido van Rossum2a30b212003-02-18 22:41:24 +00001130 x = REX_three()
1131 self.assertEqual(x._proto, None)
1132 s = self.dumps(x, proto)
1133 self.assertEqual(x._proto, proto)
1134 y = self.loads(s)
1135 self.assertEqual(y._proto, None)
1136
Guido van Rossumd8faa362007-04-27 19:54:29 +00001137 def test_reduce_ex_calls_base(self):
Collin Winter771d8342009-04-16 03:18:06 +00001138 for proto in protocols:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001139 x = REX_four()
1140 self.assertEqual(x._proto, None)
1141 s = self.dumps(x, proto)
1142 self.assertEqual(x._proto, proto)
1143 y = self.loads(s)
1144 self.assertEqual(y._proto, proto)
1145
1146 def test_reduce_calls_base(self):
Collin Winter771d8342009-04-16 03:18:06 +00001147 for proto in protocols:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001148 x = REX_five()
1149 self.assertEqual(x._reduce_called, 0)
1150 s = self.dumps(x, proto)
1151 self.assertEqual(x._reduce_called, 1)
1152 y = self.loads(s)
1153 self.assertEqual(y._reduce_called, 1)
1154
Brett Cannon31f59292011-02-21 19:29:56 +00001155 @no_tracing
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00001156 def test_bad_getattr(self):
1157 x = BadGetattr()
1158 for proto in 0, 1:
1159 self.assertRaises(RuntimeError, self.dumps, x, proto)
1160 # protocol 2 don't raise a RuntimeError.
1161 d = self.dumps(x, 2)
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00001162
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00001163 def test_reduce_bad_iterator(self):
1164 # Issue4176: crash when 4th and 5th items of __reduce__()
1165 # are not iterators
1166 class C(object):
1167 def __reduce__(self):
1168 # 4th item is not an iterator
1169 return list, (), None, [], None
1170 class D(object):
1171 def __reduce__(self):
1172 # 5th item is not an iterator
1173 return dict, (), None, None, []
1174
Amaury Forgeot d'Arc6285ffd2008-10-31 17:52:47 +00001175 # Protocol 0 is less strict and also accept iterables.
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00001176 for proto in protocols:
Amaury Forgeot d'Arc6285ffd2008-10-31 17:52:47 +00001177 try:
1178 self.dumps(C(), proto)
1179 except (pickle.PickleError):
1180 pass
1181 try:
1182 self.dumps(D(), proto)
1183 except (pickle.PickleError):
1184 pass
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00001185
Collin Winter771d8342009-04-16 03:18:06 +00001186 def test_many_puts_and_gets(self):
1187 # Test that internal data structures correctly deal with lots of
1188 # puts/gets.
1189 keys = ("aaa" + str(i) for i in range(100))
1190 large_dict = dict((k, [4, 5, 6]) for k in keys)
1191 obj = [dict(large_dict), dict(large_dict), dict(large_dict)]
1192
1193 for proto in protocols:
1194 dumped = self.dumps(obj, proto)
1195 loaded = self.loads(dumped)
1196 self.assertEqual(loaded, obj,
1197 "Failed protocol %d: %r != %r"
1198 % (proto, obj, loaded))
1199
Antoine Pitroua9f48a02009-05-02 21:41:14 +00001200 def test_attribute_name_interning(self):
1201 # Test that attribute names of pickled objects are interned when
1202 # unpickling.
1203 for proto in protocols:
1204 x = C()
1205 x.foo = 42
1206 x.bar = "hello"
1207 s = self.dumps(x, proto)
1208 y = self.loads(s)
1209 x_keys = sorted(x.__dict__)
1210 y_keys = sorted(y.__dict__)
1211 for x_key, y_key in zip(x_keys, y_keys):
1212 self.assertIs(x_key, y_key)
1213
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001214 def test_unpickle_from_2x(self):
1215 # Unpickle non-trivial data from Python 2.x.
1216 loaded = self.loads(DATA3)
1217 self.assertEqual(loaded, set([1, 2]))
1218 loaded = self.loads(DATA4)
1219 self.assertEqual(type(loaded), type(range(0)))
1220 self.assertEqual(list(loaded), list(range(5)))
1221 loaded = self.loads(DATA5)
1222 self.assertEqual(type(loaded), SimpleCookie)
1223 self.assertEqual(list(loaded.keys()), ["key"])
1224 self.assertEqual(loaded["key"].value, "Set-Cookie: key=value")
1225
Walter Doerwald9d1dbca2013-12-02 11:41:01 +01001226 for (exc, data) in DATA7.items():
1227 loaded = self.loads(data)
1228 self.assertIs(type(loaded), exc)
1229
1230 loaded = self.loads(DATA8)
1231 self.assertIs(type(loaded), Exception)
1232
1233 loaded = self.loads(DATA9)
1234 self.assertIs(type(loaded), UnicodeEncodeError)
1235 self.assertEqual(loaded.object, "foo")
1236 self.assertEqual(loaded.encoding, "ascii")
1237 self.assertEqual(loaded.start, 0)
1238 self.assertEqual(loaded.end, 1)
1239 self.assertEqual(loaded.reason, "bad")
1240
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001241 def test_pickle_to_2x(self):
1242 # Pickle non-trivial data with protocol 2, expecting that it yields
1243 # the same result as Python 2.x did.
1244 # NOTE: this test is a bit too strong since we can produce different
1245 # bytecode that 2.x will still understand.
1246 dumped = self.dumps(range(5), 2)
1247 self.assertEqual(dumped, DATA4)
1248 dumped = self.dumps(set([3]), 2)
1249 self.assertEqual(dumped, DATA6)
1250
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001251 def test_large_pickles(self):
1252 # Test the correctness of internal buffering routines when handling
1253 # large data.
1254 for proto in protocols:
Antoine Pitrou04248a82010-10-12 20:51:21 +00001255 data = (1, min, b'xy' * (30 * 1024), len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001256 dumped = self.dumps(data, proto)
1257 loaded = self.loads(dumped)
Antoine Pitrou04248a82010-10-12 20:51:21 +00001258 self.assertEqual(len(loaded), len(data))
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001259 self.assertEqual(loaded, data)
1260
Alexander Belopolsky1ce92dc2011-02-24 19:40:09 +00001261 def test_empty_bytestring(self):
1262 # issue 11286
1263 empty = self.loads(b'\x80\x03U\x00q\x00.', encoding='koi8-r')
1264 self.assertEqual(empty, '')
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001265
Antoine Pitrou3c7e9282011-08-13 20:15:19 +02001266 def test_int_pickling_efficiency(self):
1267 # Test compacity of int representation (see issue #12744)
1268 for proto in protocols:
1269 sizes = [len(self.dumps(2**n, proto)) for n in range(70)]
Antoine Pitrou85674932011-08-14 01:51:52 +02001270 # the size function is monotonic
Antoine Pitrou3c7e9282011-08-13 20:15:19 +02001271 self.assertEqual(sorted(sizes), sizes)
1272 if proto >= 2:
1273 self.assertLessEqual(sizes[-1], 14)
1274
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001275 def check_negative_32b_binXXX(self, dumped):
1276 if sys.maxsize > 2**32:
1277 self.skipTest("test is only meaningful on 32-bit builds")
1278 # XXX Pure Python pickle reads lengths as signed and passes
1279 # them directly to read() (hence the EOFError)
1280 with self.assertRaises((pickle.UnpicklingError, EOFError,
1281 ValueError, OverflowError)):
1282 self.loads(dumped)
1283
1284 def test_negative_32b_binbytes(self):
1285 # On 32-bit builds, a BINBYTES of 2**31 or more is refused
1286 self.check_negative_32b_binXXX(b'\x80\x03B\xff\xff\xff\xffxyzq\x00.')
1287
1288 def test_negative_32b_binunicode(self):
1289 # On 32-bit builds, a BINUNICODE of 2**31 or more is refused
1290 self.check_negative_32b_binXXX(b'\x80\x03X\xff\xff\xff\xffxyzq\x00.')
1291
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001292 def test_negative_put(self):
1293 # Issue #12847
1294 dumped = b'Va\np-1\n.'
1295 self.assertRaises(ValueError, self.loads, dumped)
1296
1297 def test_negative_32b_binput(self):
1298 # Issue #12847
1299 if sys.maxsize > 2**32:
1300 self.skipTest("test is only meaningful on 32-bit builds")
1301 dumped = b'\x80\x03X\x01\x00\x00\x00ar\xff\xff\xff\xff.'
1302 self.assertRaises(ValueError, self.loads, dumped)
1303
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001304 def _check_pickling_with_opcode(self, obj, opcode, proto):
1305 pickled = self.dumps(obj, proto)
1306 self.assertTrue(opcode_in_pickle(opcode, pickled))
1307 unpickled = self.loads(pickled)
1308 self.assertEqual(obj, unpickled)
1309
1310 def test_appends_on_non_lists(self):
1311 # Issue #17720
1312 obj = REX_six([1, 2, 3])
1313 for proto in protocols:
1314 if proto == 0:
1315 self._check_pickling_with_opcode(obj, pickle.APPEND, proto)
1316 else:
1317 self._check_pickling_with_opcode(obj, pickle.APPENDS, proto)
1318
1319 def test_setitems_on_non_dicts(self):
1320 obj = REX_seven({1: -1, 2: -2, 3: -3})
1321 for proto in protocols:
1322 if proto == 0:
1323 self._check_pickling_with_opcode(obj, pickle.SETITEM, proto)
1324 else:
1325 self._check_pickling_with_opcode(obj, pickle.SETITEMS, proto)
1326
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001327
1328class BigmemPickleTests(unittest.TestCase):
1329
1330 # Binary protocols can serialize longs of up to 2GB-1
1331
Serhiy Storchaka4847e4e2014-01-10 13:37:54 +02001332 @bigmemtest(size=_2G, memuse=3.6, dry_run=False)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001333 def test_huge_long_32b(self, size):
1334 data = 1 << (8 * size)
1335 try:
1336 for proto in protocols:
1337 if proto < 2:
1338 continue
1339 with self.assertRaises((ValueError, OverflowError)):
1340 self.dumps(data, protocol=proto)
1341 finally:
1342 data = None
1343
1344 # Protocol 3 can serialize up to 4GB-1 as a bytes object
1345 # (older protocols don't have a dedicated opcode for bytes and are
1346 # too inefficient)
1347
Serhiy Storchaka4847e4e2014-01-10 13:37:54 +02001348 @bigmemtest(size=_2G, memuse=2.5, dry_run=False)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001349 def test_huge_bytes_32b(self, size):
1350 data = b"abcd" * (size // 4)
1351 try:
1352 for proto in protocols:
1353 if proto < 3:
1354 continue
1355 try:
1356 pickled = self.dumps(data, protocol=proto)
1357 self.assertTrue(b"abcd" in pickled[:15])
1358 self.assertTrue(b"abcd" in pickled[-15:])
1359 finally:
1360 pickled = None
1361 finally:
1362 data = None
1363
Serhiy Storchaka4847e4e2014-01-10 13:37:54 +02001364 @bigmemtest(size=_4G, memuse=2.5, dry_run=False)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001365 def test_huge_bytes_64b(self, size):
1366 data = b"a" * size
1367 try:
1368 for proto in protocols:
1369 if proto < 3:
1370 continue
1371 with self.assertRaises((ValueError, OverflowError)):
1372 self.dumps(data, protocol=proto)
1373 finally:
1374 data = None
1375
1376 # All protocols use 1-byte per printable ASCII character; we add another
1377 # byte because the encoded form has to be copied into the internal buffer.
1378
Serhiy Storchaka4847e4e2014-01-10 13:37:54 +02001379 @bigmemtest(size=_2G, memuse=8, dry_run=False)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001380 def test_huge_str_32b(self, size):
1381 data = "abcd" * (size // 4)
1382 try:
1383 for proto in protocols:
1384 try:
1385 pickled = self.dumps(data, protocol=proto)
1386 self.assertTrue(b"abcd" in pickled[:15])
1387 self.assertTrue(b"abcd" in pickled[-15:])
1388 finally:
1389 pickled = None
1390 finally:
1391 data = None
1392
Antoine Pitroue897e952011-08-30 23:39:34 +02001393 # BINUNICODE (protocols 1, 2 and 3) cannot carry more than
1394 # 2**32 - 1 bytes of utf-8 encoded unicode.
1395
Serhiy Storchaka4847e4e2014-01-10 13:37:54 +02001396 @bigmemtest(size=_4G, memuse=8, dry_run=False)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001397 def test_huge_str_64b(self, size):
1398 data = "a" * size
1399 try:
1400 for proto in protocols:
Antoine Pitroue897e952011-08-30 23:39:34 +02001401 if proto == 0:
1402 continue
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001403 with self.assertRaises((ValueError, OverflowError)):
1404 self.dumps(data, protocol=proto)
1405 finally:
1406 data = None
1407
Antoine Pitrou3c7e9282011-08-13 20:15:19 +02001408
Guido van Rossum2a30b212003-02-18 22:41:24 +00001409# Test classes for reduce_ex
1410
1411class REX_one(object):
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001412 """No __reduce_ex__ here, but inheriting it from object"""
Guido van Rossum2a30b212003-02-18 22:41:24 +00001413 _reduce_called = 0
1414 def __reduce__(self):
1415 self._reduce_called = 1
1416 return REX_one, ()
Guido van Rossum2a30b212003-02-18 22:41:24 +00001417
1418class REX_two(object):
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001419 """No __reduce__ here, but inheriting it from object"""
Guido van Rossum2a30b212003-02-18 22:41:24 +00001420 _proto = None
1421 def __reduce_ex__(self, proto):
1422 self._proto = proto
1423 return REX_two, ()
Guido van Rossum2a30b212003-02-18 22:41:24 +00001424
1425class REX_three(object):
1426 _proto = None
1427 def __reduce_ex__(self, proto):
1428 self._proto = proto
1429 return REX_two, ()
1430 def __reduce__(self):
Collin Winter3add4d72007-08-29 23:37:32 +00001431 raise TestFailed("This __reduce__ shouldn't be called")
Guido van Rossum2a30b212003-02-18 22:41:24 +00001432
Guido van Rossumd8faa362007-04-27 19:54:29 +00001433class REX_four(object):
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001434 """Calling base class method should succeed"""
Guido van Rossumd8faa362007-04-27 19:54:29 +00001435 _proto = None
1436 def __reduce_ex__(self, proto):
1437 self._proto = proto
1438 return object.__reduce_ex__(self, proto)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001439
1440class REX_five(object):
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001441 """This one used to fail with infinite recursion"""
Guido van Rossumd8faa362007-04-27 19:54:29 +00001442 _reduce_called = 0
1443 def __reduce__(self):
1444 self._reduce_called = 1
1445 return object.__reduce__(self)
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001446
1447class REX_six(object):
1448 """This class is used to check the 4th argument (list iterator) of the reduce
1449 protocol.
1450 """
1451 def __init__(self, items=None):
1452 self.items = items if items is not None else []
1453 def __eq__(self, other):
1454 return type(self) is type(other) and self.items == self.items
1455 def append(self, item):
1456 self.items.append(item)
1457 def __reduce__(self):
1458 return type(self), (), None, iter(self.items), None
1459
1460class REX_seven(object):
1461 """This class is used to check the 5th argument (dict iterator) of the reduce
1462 protocol.
1463 """
1464 def __init__(self, table=None):
1465 self.table = table if table is not None else {}
1466 def __eq__(self, other):
1467 return type(self) is type(other) and self.table == self.table
1468 def __setitem__(self, key, value):
1469 self.table[key] = value
1470 def __reduce__(self):
1471 return type(self), (), None, None, iter(self.table.items())
1472
Guido van Rossumd8faa362007-04-27 19:54:29 +00001473
Guido van Rossum2a30b212003-02-18 22:41:24 +00001474# Test classes for newobj
Tim Peters080c88b2003-02-15 03:01:11 +00001475
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001476class MyInt(int):
1477 sample = 1
1478
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001479class MyFloat(float):
1480 sample = 1.0
1481
1482class MyComplex(complex):
1483 sample = 1.0 + 0.0j
1484
1485class MyStr(str):
1486 sample = "hello"
1487
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001488class MyUnicode(str):
1489 sample = "hello \u1234"
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001490
Guido van Rossum533dbcf2003-01-28 17:55:05 +00001491class MyTuple(tuple):
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001492 sample = (1, 2, 3)
Guido van Rossum533dbcf2003-01-28 17:55:05 +00001493
1494class MyList(list):
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001495 sample = [1, 2, 3]
1496
1497class MyDict(dict):
1498 sample = {"a": 1, "b": 2}
1499
Mark Dickinson5c2db372009-12-05 20:28:34 +00001500myclasses = [MyInt, MyFloat,
Guido van Rossum206b9a72003-03-02 13:53:18 +00001501 MyComplex,
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001502 MyStr, MyUnicode,
1503 MyTuple, MyList, MyDict]
1504
Guido van Rossum533dbcf2003-01-28 17:55:05 +00001505
Guido van Rossumc8d6ef52003-01-28 22:02:31 +00001506class SlotList(MyList):
1507 __slots__ = ["foo"]
1508
Tim Peterse9ef2032003-02-13 18:42:00 +00001509class SimpleNewObj(object):
1510 def __init__(self, a, b, c):
1511 # raise an error, to make sure this isn't called
1512 raise TypeError("SimpleNewObj.__init__() didn't expect to get called")
1513
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00001514class BadGetattr:
1515 def __getattr__(self, key):
1516 self.foo
1517
Collin Winter771d8342009-04-16 03:18:06 +00001518
Jeremy Hylton66426532001-10-15 21:38:56 +00001519class AbstractPickleModuleTests(unittest.TestCase):
1520
1521 def test_dump_closed_file(self):
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001522 import os
Walter Dörwald11b41f62007-06-20 12:46:31 +00001523 f = open(TESTFN, "wb")
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001524 try:
1525 f.close()
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001526 self.assertRaises(ValueError, pickle.dump, 123, f)
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001527 finally:
1528 os.remove(TESTFN)
Jeremy Hylton66426532001-10-15 21:38:56 +00001529
1530 def test_load_closed_file(self):
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001531 import os
Walter Dörwald11b41f62007-06-20 12:46:31 +00001532 f = open(TESTFN, "wb")
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001533 try:
1534 f.close()
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001535 self.assertRaises(ValueError, pickle.dump, 123, f)
Guido van Rossum3b0a3292002-08-09 16:38:32 +00001536 finally:
1537 os.remove(TESTFN)
Jeremy Hylton4c8be852002-11-13 22:10:47 +00001538
Collin Winter771d8342009-04-16 03:18:06 +00001539 def test_load_from_and_dump_to_file(self):
1540 stream = io.BytesIO()
1541 data = [123, {}, 124]
1542 pickle.dump(data, stream)
1543 stream.seek(0)
1544 unpickled = pickle.load(stream)
1545 self.assertEqual(unpickled, data)
1546
Tim Petersc0c93702003-02-13 19:30:57 +00001547 def test_highest_protocol(self):
1548 # Of course this needs to be changed when HIGHEST_PROTOCOL changes.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 self.assertEqual(pickle.HIGHEST_PROTOCOL, 3)
Tim Petersc0c93702003-02-13 19:30:57 +00001550
Martin v. Löwis544f1192004-07-27 05:22:33 +00001551 def test_callapi(self):
Collin Winter771d8342009-04-16 03:18:06 +00001552 f = io.BytesIO()
Martin v. Löwis544f1192004-07-27 05:22:33 +00001553 # With and without keyword arguments
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 pickle.dump(123, f, -1)
1555 pickle.dump(123, file=f, protocol=-1)
1556 pickle.dumps(123, -1)
1557 pickle.dumps(123, protocol=-1)
1558 pickle.Pickler(f, -1)
1559 pickle.Pickler(f, protocol=-1)
Tim Petersc0c93702003-02-13 19:30:57 +00001560
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00001561 def test_bad_init(self):
1562 # Test issue3664 (pickle can segfault from a badly initialized Pickler).
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00001563 # Override initialization without calling __init__() of the superclass.
1564 class BadPickler(pickle.Pickler):
1565 def __init__(self): pass
1566
1567 class BadUnpickler(pickle.Unpickler):
1568 def __init__(self): pass
1569
1570 self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
1571 self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
1572
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001573 def test_bad_input(self):
1574 # Test issue4298
1575 s = bytes([0x58, 0, 0, 0, 0x54])
1576 self.assertRaises(EOFError, pickle.loads, s)
1577
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00001578
Jeremy Hylton4c8be852002-11-13 22:10:47 +00001579class AbstractPersistentPicklerTests(unittest.TestCase):
1580
1581 # This class defines persistent_id() and persistent_load()
1582 # functions that should be used by the pickler. All even integers
1583 # are pickled using persistent ids.
1584
1585 def persistent_id(self, object):
1586 if isinstance(object, int) and object % 2 == 0:
1587 self.id_count += 1
1588 return str(object)
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08001589 elif object == "test_false_value":
1590 self.false_count += 1
1591 return ""
Jeremy Hylton4c8be852002-11-13 22:10:47 +00001592 else:
1593 return None
1594
1595 def persistent_load(self, oid):
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08001596 if not oid:
1597 self.load_false_count += 1
1598 return "test_false_value"
1599 else:
1600 self.load_count += 1
1601 object = int(oid)
1602 assert object % 2 == 0
1603 return object
Jeremy Hylton4c8be852002-11-13 22:10:47 +00001604
1605 def test_persistence(self):
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08001606 L = list(range(10)) + ["test_false_value"]
1607 for proto in protocols:
1608 self.id_count = 0
1609 self.false_count = 0
1610 self.load_false_count = 0
1611 self.load_count = 0
1612 self.assertEqual(self.loads(self.dumps(L, proto)), L)
1613 self.assertEqual(self.id_count, 5)
1614 self.assertEqual(self.false_count, 1)
1615 self.assertEqual(self.load_count, 5)
1616 self.assertEqual(self.load_false_count, 1)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001617
Collin Winter771d8342009-04-16 03:18:06 +00001618
1619class AbstractPicklerUnpicklerObjectTests(unittest.TestCase):
1620
1621 pickler_class = None
1622 unpickler_class = None
1623
1624 def setUp(self):
1625 assert self.pickler_class
1626 assert self.unpickler_class
1627
1628 def test_clear_pickler_memo(self):
1629 # To test whether clear_memo() has any effect, we pickle an object,
1630 # then pickle it again without clearing the memo; the two serialized
1631 # forms should be different. If we clear_memo() and then pickle the
1632 # object again, the third serialized form should be identical to the
1633 # first one we obtained.
1634 data = ["abcdefg", "abcdefg", 44]
1635 f = io.BytesIO()
1636 pickler = self.pickler_class(f)
1637
1638 pickler.dump(data)
1639 first_pickled = f.getvalue()
1640
Serhiy Storchaka50254c52013-08-29 11:35:43 +03001641 # Reset BytesIO object.
Collin Winter771d8342009-04-16 03:18:06 +00001642 f.seek(0)
1643 f.truncate()
1644
1645 pickler.dump(data)
1646 second_pickled = f.getvalue()
1647
Serhiy Storchaka50254c52013-08-29 11:35:43 +03001648 # Reset the Pickler and BytesIO objects.
Collin Winter771d8342009-04-16 03:18:06 +00001649 pickler.clear_memo()
1650 f.seek(0)
1651 f.truncate()
1652
1653 pickler.dump(data)
1654 third_pickled = f.getvalue()
1655
1656 self.assertNotEqual(first_pickled, second_pickled)
1657 self.assertEqual(first_pickled, third_pickled)
1658
1659 def test_priming_pickler_memo(self):
1660 # Verify that we can set the Pickler's memo attribute.
1661 data = ["abcdefg", "abcdefg", 44]
1662 f = io.BytesIO()
1663 pickler = self.pickler_class(f)
1664
1665 pickler.dump(data)
1666 first_pickled = f.getvalue()
1667
1668 f = io.BytesIO()
1669 primed = self.pickler_class(f)
1670 primed.memo = pickler.memo
1671
1672 primed.dump(data)
1673 primed_pickled = f.getvalue()
1674
1675 self.assertNotEqual(first_pickled, primed_pickled)
1676
1677 def test_priming_unpickler_memo(self):
1678 # Verify that we can set the Unpickler's memo attribute.
1679 data = ["abcdefg", "abcdefg", 44]
1680 f = io.BytesIO()
1681 pickler = self.pickler_class(f)
1682
1683 pickler.dump(data)
1684 first_pickled = f.getvalue()
1685
1686 f = io.BytesIO()
1687 primed = self.pickler_class(f)
1688 primed.memo = pickler.memo
1689
1690 primed.dump(data)
1691 primed_pickled = f.getvalue()
1692
1693 unpickler = self.unpickler_class(io.BytesIO(first_pickled))
1694 unpickled_data1 = unpickler.load()
1695
1696 self.assertEqual(unpickled_data1, data)
1697
1698 primed = self.unpickler_class(io.BytesIO(primed_pickled))
1699 primed.memo = unpickler.memo
1700 unpickled_data2 = primed.load()
1701
1702 primed.memo.clear()
1703
1704 self.assertEqual(unpickled_data2, data)
1705 self.assertTrue(unpickled_data2 is unpickled_data1)
1706
1707 def test_reusing_unpickler_objects(self):
1708 data1 = ["abcdefg", "abcdefg", 44]
1709 f = io.BytesIO()
1710 pickler = self.pickler_class(f)
1711 pickler.dump(data1)
1712 pickled1 = f.getvalue()
1713
1714 data2 = ["abcdefg", 44, 44]
1715 f = io.BytesIO()
1716 pickler = self.pickler_class(f)
1717 pickler.dump(data2)
1718 pickled2 = f.getvalue()
1719
1720 f = io.BytesIO()
1721 f.write(pickled1)
1722 f.seek(0)
1723 unpickler = self.unpickler_class(f)
1724 self.assertEqual(unpickler.load(), data1)
1725
1726 f.seek(0)
1727 f.truncate()
1728 f.write(pickled2)
1729 f.seek(0)
1730 self.assertEqual(unpickler.load(), data2)
1731
Antoine Pitrou04248a82010-10-12 20:51:21 +00001732 def _check_multiple_unpicklings(self, ioclass):
1733 for proto in protocols:
1734 data1 = [(x, str(x)) for x in range(2000)] + [b"abcde", len]
1735 f = ioclass()
1736 pickler = self.pickler_class(f, protocol=proto)
1737 pickler.dump(data1)
1738 pickled = f.getvalue()
1739
1740 N = 5
1741 f = ioclass(pickled * N)
1742 unpickler = self.unpickler_class(f)
1743 for i in range(N):
1744 if f.seekable():
1745 pos = f.tell()
1746 self.assertEqual(unpickler.load(), data1)
1747 if f.seekable():
1748 self.assertEqual(f.tell(), pos + len(pickled))
1749 self.assertRaises(EOFError, unpickler.load)
1750
1751 def test_multiple_unpicklings_seekable(self):
1752 self._check_multiple_unpicklings(io.BytesIO)
1753
1754 def test_multiple_unpicklings_unseekable(self):
1755 self._check_multiple_unpicklings(UnseekableIO)
1756
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001757 def test_unpickling_buffering_readline(self):
1758 # Issue #12687: the unpickler's buffering logic could fail with
1759 # text mode opcodes.
1760 data = list(range(10))
1761 for proto in protocols:
1762 for buf_size in range(1, 11):
1763 f = io.BufferedRandom(io.BytesIO(), buffer_size=buf_size)
1764 pickler = self.pickler_class(f, protocol=proto)
1765 pickler.dump(data)
1766 f.seek(0)
1767 unpickler = self.unpickler_class(f)
1768 self.assertEqual(unpickler.load(), data)
1769
Collin Winter771d8342009-04-16 03:18:06 +00001770
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001771# Tests for dispatch_table attribute
1772
1773REDUCE_A = 'reduce_A'
1774
1775class AAA(object):
1776 def __reduce__(self):
1777 return str, (REDUCE_A,)
1778
1779class BBB(object):
1780 pass
1781
1782class AbstractDispatchTableTests(unittest.TestCase):
1783
1784 def test_default_dispatch_table(self):
1785 # No dispatch_table attribute by default
1786 f = io.BytesIO()
1787 p = self.pickler_class(f, 0)
1788 with self.assertRaises(AttributeError):
1789 p.dispatch_table
1790 self.assertFalse(hasattr(p, 'dispatch_table'))
1791
1792 def test_class_dispatch_table(self):
1793 # A dispatch_table attribute can be specified class-wide
1794 dt = self.get_dispatch_table()
1795
1796 class MyPickler(self.pickler_class):
1797 dispatch_table = dt
1798
1799 def dumps(obj, protocol=None):
1800 f = io.BytesIO()
1801 p = MyPickler(f, protocol)
1802 self.assertEqual(p.dispatch_table, dt)
1803 p.dump(obj)
1804 return f.getvalue()
1805
1806 self._test_dispatch_table(dumps, dt)
1807
1808 def test_instance_dispatch_table(self):
1809 # A dispatch_table attribute can also be specified instance-wide
1810 dt = self.get_dispatch_table()
1811
1812 def dumps(obj, protocol=None):
1813 f = io.BytesIO()
1814 p = self.pickler_class(f, protocol)
1815 p.dispatch_table = dt
1816 self.assertEqual(p.dispatch_table, dt)
1817 p.dump(obj)
1818 return f.getvalue()
1819
1820 self._test_dispatch_table(dumps, dt)
1821
1822 def _test_dispatch_table(self, dumps, dispatch_table):
1823 def custom_load_dump(obj):
1824 return pickle.loads(dumps(obj, 0))
1825
1826 def default_load_dump(obj):
1827 return pickle.loads(pickle.dumps(obj, 0))
1828
1829 # pickling complex numbers using protocol 0 relies on copyreg
1830 # so check pickling a complex number still works
1831 z = 1 + 2j
1832 self.assertEqual(custom_load_dump(z), z)
1833 self.assertEqual(default_load_dump(z), z)
1834
1835 # modify pickling of complex
1836 REDUCE_1 = 'reduce_1'
1837 def reduce_1(obj):
1838 return str, (REDUCE_1,)
1839 dispatch_table[complex] = reduce_1
1840 self.assertEqual(custom_load_dump(z), REDUCE_1)
1841 self.assertEqual(default_load_dump(z), z)
1842
1843 # check picklability of AAA and BBB
1844 a = AAA()
1845 b = BBB()
1846 self.assertEqual(custom_load_dump(a), REDUCE_A)
1847 self.assertIsInstance(custom_load_dump(b), BBB)
1848 self.assertEqual(default_load_dump(a), REDUCE_A)
1849 self.assertIsInstance(default_load_dump(b), BBB)
1850
1851 # modify pickling of BBB
1852 dispatch_table[BBB] = reduce_1
1853 self.assertEqual(custom_load_dump(a), REDUCE_A)
1854 self.assertEqual(custom_load_dump(b), REDUCE_1)
1855 self.assertEqual(default_load_dump(a), REDUCE_A)
1856 self.assertIsInstance(default_load_dump(b), BBB)
1857
1858 # revert pickling of BBB and modify pickling of AAA
1859 REDUCE_2 = 'reduce_2'
1860 def reduce_2(obj):
1861 return str, (REDUCE_2,)
1862 dispatch_table[AAA] = reduce_2
1863 del dispatch_table[BBB]
1864 self.assertEqual(custom_load_dump(a), REDUCE_2)
1865 self.assertIsInstance(custom_load_dump(b), BBB)
1866 self.assertEqual(default_load_dump(a), REDUCE_A)
1867 self.assertIsInstance(default_load_dump(b), BBB)
1868
1869
Guido van Rossum98297ee2007-11-06 21:34:58 +00001870if __name__ == "__main__":
1871 # Print some stuff that can be used to rewrite DATA{0,1,2}
1872 from pickletools import dis
1873 x = create_data()
1874 for i in range(3):
1875 p = pickle.dumps(x, i)
1876 print("DATA{0} = (".format(i))
1877 for j in range(0, len(p), 20):
1878 b = bytes(p[j:j+20])
1879 print(" {0!r}".format(b))
1880 print(")")
1881 print()
1882 print("# Disassembly of DATA{0}".format(i))
1883 print("DATA{0}_DIS = \"\"\"\\".format(i))
1884 dis(p)
1885 print("\"\"\"")
1886 print()