1 | n/a | import collections |
---|
2 | n/a | import copyreg |
---|
3 | n/a | import dbm |
---|
4 | n/a | import io |
---|
5 | n/a | import functools |
---|
6 | n/a | import pickle |
---|
7 | n/a | import pickletools |
---|
8 | n/a | import struct |
---|
9 | n/a | import sys |
---|
10 | n/a | import unittest |
---|
11 | n/a | import weakref |
---|
12 | n/a | from http.cookies import SimpleCookie |
---|
13 | n/a | |
---|
14 | n/a | from test import support |
---|
15 | n/a | from test.support import ( |
---|
16 | n/a | TestFailed, TESTFN, run_with_locale, no_tracing, |
---|
17 | n/a | _2G, _4G, bigmemtest, |
---|
18 | n/a | ) |
---|
19 | n/a | |
---|
20 | n/a | from pickle import bytes_types |
---|
21 | n/a | |
---|
22 | n/a | requires_32b = unittest.skipUnless(sys.maxsize < 2**32, |
---|
23 | n/a | "test is only meaningful on 32-bit builds") |
---|
24 | n/a | |
---|
25 | n/a | # Tests that try a number of pickle protocols should have a |
---|
26 | n/a | # for proto in protocols: |
---|
27 | n/a | # kind of outer loop. |
---|
28 | n/a | protocols = range(pickle.HIGHEST_PROTOCOL + 1) |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | # Return True if opcode code appears in the pickle, else False. |
---|
32 | n/a | def opcode_in_pickle(code, pickle): |
---|
33 | n/a | for op, dummy, dummy in pickletools.genops(pickle): |
---|
34 | n/a | if op.code == code.decode("latin-1"): |
---|
35 | n/a | return True |
---|
36 | n/a | return False |
---|
37 | n/a | |
---|
38 | n/a | # Return the number of times opcode code appears in pickle. |
---|
39 | n/a | def count_opcode(code, pickle): |
---|
40 | n/a | n = 0 |
---|
41 | n/a | for op, dummy, dummy in pickletools.genops(pickle): |
---|
42 | n/a | if op.code == code.decode("latin-1"): |
---|
43 | n/a | n += 1 |
---|
44 | n/a | return n |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | class UnseekableIO(io.BytesIO): |
---|
48 | n/a | def peek(self, *args): |
---|
49 | n/a | raise NotImplementedError |
---|
50 | n/a | |
---|
51 | n/a | def seekable(self): |
---|
52 | n/a | return False |
---|
53 | n/a | |
---|
54 | n/a | def seek(self, *args): |
---|
55 | n/a | raise io.UnsupportedOperation |
---|
56 | n/a | |
---|
57 | n/a | def tell(self): |
---|
58 | n/a | raise io.UnsupportedOperation |
---|
59 | n/a | |
---|
60 | n/a | |
---|
61 | n/a | # We can't very well test the extension registry without putting known stuff |
---|
62 | n/a | # in it, but we have to be careful to restore its original state. Code |
---|
63 | n/a | # should do this: |
---|
64 | n/a | # |
---|
65 | n/a | # e = ExtensionSaver(extension_code) |
---|
66 | n/a | # try: |
---|
67 | n/a | # fiddle w/ the extension registry's stuff for extension_code |
---|
68 | n/a | # finally: |
---|
69 | n/a | # e.restore() |
---|
70 | n/a | |
---|
71 | n/a | class ExtensionSaver: |
---|
72 | n/a | # Remember current registration for code (if any), and remove it (if |
---|
73 | n/a | # there is one). |
---|
74 | n/a | def __init__(self, code): |
---|
75 | n/a | self.code = code |
---|
76 | n/a | if code in copyreg._inverted_registry: |
---|
77 | n/a | self.pair = copyreg._inverted_registry[code] |
---|
78 | n/a | copyreg.remove_extension(self.pair[0], self.pair[1], code) |
---|
79 | n/a | else: |
---|
80 | n/a | self.pair = None |
---|
81 | n/a | |
---|
82 | n/a | # Restore previous registration for code. |
---|
83 | n/a | def restore(self): |
---|
84 | n/a | code = self.code |
---|
85 | n/a | curpair = copyreg._inverted_registry.get(code) |
---|
86 | n/a | if curpair is not None: |
---|
87 | n/a | copyreg.remove_extension(curpair[0], curpair[1], code) |
---|
88 | n/a | pair = self.pair |
---|
89 | n/a | if pair is not None: |
---|
90 | n/a | copyreg.add_extension(pair[0], pair[1], code) |
---|
91 | n/a | |
---|
92 | n/a | class C: |
---|
93 | n/a | def __eq__(self, other): |
---|
94 | n/a | return self.__dict__ == other.__dict__ |
---|
95 | n/a | |
---|
96 | n/a | class D(C): |
---|
97 | n/a | def __init__(self, arg): |
---|
98 | n/a | pass |
---|
99 | n/a | |
---|
100 | n/a | class E(C): |
---|
101 | n/a | def __getinitargs__(self): |
---|
102 | n/a | return () |
---|
103 | n/a | |
---|
104 | n/a | class H(object): |
---|
105 | n/a | pass |
---|
106 | n/a | |
---|
107 | n/a | # Hashable mutable key |
---|
108 | n/a | class K(object): |
---|
109 | n/a | def __init__(self, value): |
---|
110 | n/a | self.value = value |
---|
111 | n/a | |
---|
112 | n/a | def __reduce__(self): |
---|
113 | n/a | # Shouldn't support the recursion itself |
---|
114 | n/a | return K, (self.value,) |
---|
115 | n/a | |
---|
116 | n/a | import __main__ |
---|
117 | n/a | __main__.C = C |
---|
118 | n/a | C.__module__ = "__main__" |
---|
119 | n/a | __main__.D = D |
---|
120 | n/a | D.__module__ = "__main__" |
---|
121 | n/a | __main__.E = E |
---|
122 | n/a | E.__module__ = "__main__" |
---|
123 | n/a | __main__.H = H |
---|
124 | n/a | H.__module__ = "__main__" |
---|
125 | n/a | __main__.K = K |
---|
126 | n/a | K.__module__ = "__main__" |
---|
127 | n/a | |
---|
128 | n/a | class myint(int): |
---|
129 | n/a | def __init__(self, x): |
---|
130 | n/a | self.str = str(x) |
---|
131 | n/a | |
---|
132 | n/a | class initarg(C): |
---|
133 | n/a | |
---|
134 | n/a | def __init__(self, a, b): |
---|
135 | n/a | self.a = a |
---|
136 | n/a | self.b = b |
---|
137 | n/a | |
---|
138 | n/a | def __getinitargs__(self): |
---|
139 | n/a | return self.a, self.b |
---|
140 | n/a | |
---|
141 | n/a | class metaclass(type): |
---|
142 | n/a | pass |
---|
143 | n/a | |
---|
144 | n/a | class use_metaclass(object, metaclass=metaclass): |
---|
145 | n/a | pass |
---|
146 | n/a | |
---|
147 | n/a | class pickling_metaclass(type): |
---|
148 | n/a | def __eq__(self, other): |
---|
149 | n/a | return (type(self) == type(other) and |
---|
150 | n/a | self.reduce_args == other.reduce_args) |
---|
151 | n/a | |
---|
152 | n/a | def __reduce__(self): |
---|
153 | n/a | return (create_dynamic_class, self.reduce_args) |
---|
154 | n/a | |
---|
155 | n/a | def create_dynamic_class(name, bases): |
---|
156 | n/a | result = pickling_metaclass(name, bases, dict()) |
---|
157 | n/a | result.reduce_args = (name, bases) |
---|
158 | n/a | return result |
---|
159 | n/a | |
---|
160 | n/a | # DATA0 .. DATA4 are the pickles we expect under the various protocols, for |
---|
161 | n/a | # the object returned by create_data(). |
---|
162 | n/a | |
---|
163 | n/a | DATA0 = ( |
---|
164 | n/a | b'(lp0\nL0L\naL1L\naF2.0\n' |
---|
165 | n/a | b'ac__builtin__\ncomple' |
---|
166 | n/a | b'x\np1\n(F3.0\nF0.0\ntp2\n' |
---|
167 | n/a | b'Rp3\naL1L\naL-1L\naL255' |
---|
168 | n/a | b'L\naL-255L\naL-256L\naL' |
---|
169 | n/a | b'65535L\naL-65535L\naL-' |
---|
170 | n/a | b'65536L\naL2147483647L' |
---|
171 | n/a | b'\naL-2147483647L\naL-2' |
---|
172 | n/a | b'147483648L\na(Vabc\np4' |
---|
173 | n/a | b'\ng4\nccopy_reg\n_recon' |
---|
174 | n/a | b'structor\np5\n(c__main' |
---|
175 | n/a | b'__\nC\np6\nc__builtin__' |
---|
176 | n/a | b'\nobject\np7\nNtp8\nRp9\n' |
---|
177 | n/a | b'(dp10\nVfoo\np11\nL1L\ns' |
---|
178 | n/a | b'Vbar\np12\nL2L\nsbg9\ntp' |
---|
179 | n/a | b'13\nag13\naL5L\na.' |
---|
180 | n/a | ) |
---|
181 | n/a | |
---|
182 | n/a | # Disassembly of DATA0 |
---|
183 | n/a | DATA0_DIS = """\ |
---|
184 | n/a | 0: ( MARK |
---|
185 | n/a | 1: l LIST (MARK at 0) |
---|
186 | n/a | 2: p PUT 0 |
---|
187 | n/a | 5: L LONG 0 |
---|
188 | n/a | 9: a APPEND |
---|
189 | n/a | 10: L LONG 1 |
---|
190 | n/a | 14: a APPEND |
---|
191 | n/a | 15: F FLOAT 2.0 |
---|
192 | n/a | 20: a APPEND |
---|
193 | n/a | 21: c GLOBAL '__builtin__ complex' |
---|
194 | n/a | 42: p PUT 1 |
---|
195 | n/a | 45: ( MARK |
---|
196 | n/a | 46: F FLOAT 3.0 |
---|
197 | n/a | 51: F FLOAT 0.0 |
---|
198 | n/a | 56: t TUPLE (MARK at 45) |
---|
199 | n/a | 57: p PUT 2 |
---|
200 | n/a | 60: R REDUCE |
---|
201 | n/a | 61: p PUT 3 |
---|
202 | n/a | 64: a APPEND |
---|
203 | n/a | 65: L LONG 1 |
---|
204 | n/a | 69: a APPEND |
---|
205 | n/a | 70: L LONG -1 |
---|
206 | n/a | 75: a APPEND |
---|
207 | n/a | 76: L LONG 255 |
---|
208 | n/a | 82: a APPEND |
---|
209 | n/a | 83: L LONG -255 |
---|
210 | n/a | 90: a APPEND |
---|
211 | n/a | 91: L LONG -256 |
---|
212 | n/a | 98: a APPEND |
---|
213 | n/a | 99: L LONG 65535 |
---|
214 | n/a | 107: a APPEND |
---|
215 | n/a | 108: L LONG -65535 |
---|
216 | n/a | 117: a APPEND |
---|
217 | n/a | 118: L LONG -65536 |
---|
218 | n/a | 127: a APPEND |
---|
219 | n/a | 128: L LONG 2147483647 |
---|
220 | n/a | 141: a APPEND |
---|
221 | n/a | 142: L LONG -2147483647 |
---|
222 | n/a | 156: a APPEND |
---|
223 | n/a | 157: L LONG -2147483648 |
---|
224 | n/a | 171: a APPEND |
---|
225 | n/a | 172: ( MARK |
---|
226 | n/a | 173: V UNICODE 'abc' |
---|
227 | n/a | 178: p PUT 4 |
---|
228 | n/a | 181: g GET 4 |
---|
229 | n/a | 184: c GLOBAL 'copy_reg _reconstructor' |
---|
230 | n/a | 209: p PUT 5 |
---|
231 | n/a | 212: ( MARK |
---|
232 | n/a | 213: c GLOBAL '__main__ C' |
---|
233 | n/a | 225: p PUT 6 |
---|
234 | n/a | 228: c GLOBAL '__builtin__ object' |
---|
235 | n/a | 248: p PUT 7 |
---|
236 | n/a | 251: N NONE |
---|
237 | n/a | 252: t TUPLE (MARK at 212) |
---|
238 | n/a | 253: p PUT 8 |
---|
239 | n/a | 256: R REDUCE |
---|
240 | n/a | 257: p PUT 9 |
---|
241 | n/a | 260: ( MARK |
---|
242 | n/a | 261: d DICT (MARK at 260) |
---|
243 | n/a | 262: p PUT 10 |
---|
244 | n/a | 266: V UNICODE 'foo' |
---|
245 | n/a | 271: p PUT 11 |
---|
246 | n/a | 275: L LONG 1 |
---|
247 | n/a | 279: s SETITEM |
---|
248 | n/a | 280: V UNICODE 'bar' |
---|
249 | n/a | 285: p PUT 12 |
---|
250 | n/a | 289: L LONG 2 |
---|
251 | n/a | 293: s SETITEM |
---|
252 | n/a | 294: b BUILD |
---|
253 | n/a | 295: g GET 9 |
---|
254 | n/a | 298: t TUPLE (MARK at 172) |
---|
255 | n/a | 299: p PUT 13 |
---|
256 | n/a | 303: a APPEND |
---|
257 | n/a | 304: g GET 13 |
---|
258 | n/a | 308: a APPEND |
---|
259 | n/a | 309: L LONG 5 |
---|
260 | n/a | 313: a APPEND |
---|
261 | n/a | 314: . STOP |
---|
262 | n/a | highest protocol among opcodes = 0 |
---|
263 | n/a | """ |
---|
264 | n/a | |
---|
265 | n/a | DATA1 = ( |
---|
266 | n/a | b']q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c__' |
---|
267 | n/a | b'builtin__\ncomplex\nq\x01' |
---|
268 | n/a | b'(G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00t' |
---|
269 | n/a | b'q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xffJ' |
---|
270 | n/a | b'\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff' |
---|
271 | n/a | b'\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00ab' |
---|
272 | n/a | b'cq\x04h\x04ccopy_reg\n_reco' |
---|
273 | n/a | b'nstructor\nq\x05(c__main' |
---|
274 | n/a | b'__\nC\nq\x06c__builtin__\n' |
---|
275 | n/a | b'object\nq\x07Ntq\x08Rq\t}q\n(' |
---|
276 | n/a | b'X\x03\x00\x00\x00fooq\x0bK\x01X\x03\x00\x00\x00bar' |
---|
277 | n/a | b'q\x0cK\x02ubh\ttq\rh\rK\x05e.' |
---|
278 | n/a | ) |
---|
279 | n/a | |
---|
280 | n/a | # Disassembly of DATA1 |
---|
281 | n/a | DATA1_DIS = """\ |
---|
282 | n/a | 0: ] EMPTY_LIST |
---|
283 | n/a | 1: q BINPUT 0 |
---|
284 | n/a | 3: ( MARK |
---|
285 | n/a | 4: K BININT1 0 |
---|
286 | n/a | 6: K BININT1 1 |
---|
287 | n/a | 8: G BINFLOAT 2.0 |
---|
288 | n/a | 17: c GLOBAL '__builtin__ complex' |
---|
289 | n/a | 38: q BINPUT 1 |
---|
290 | n/a | 40: ( MARK |
---|
291 | n/a | 41: G BINFLOAT 3.0 |
---|
292 | n/a | 50: G BINFLOAT 0.0 |
---|
293 | n/a | 59: t TUPLE (MARK at 40) |
---|
294 | n/a | 60: q BINPUT 2 |
---|
295 | n/a | 62: R REDUCE |
---|
296 | n/a | 63: q BINPUT 3 |
---|
297 | n/a | 65: K BININT1 1 |
---|
298 | n/a | 67: J BININT -1 |
---|
299 | n/a | 72: K BININT1 255 |
---|
300 | n/a | 74: J BININT -255 |
---|
301 | n/a | 79: J BININT -256 |
---|
302 | n/a | 84: M BININT2 65535 |
---|
303 | n/a | 87: J BININT -65535 |
---|
304 | n/a | 92: J BININT -65536 |
---|
305 | n/a | 97: J BININT 2147483647 |
---|
306 | n/a | 102: J BININT -2147483647 |
---|
307 | n/a | 107: J BININT -2147483648 |
---|
308 | n/a | 112: ( MARK |
---|
309 | n/a | 113: X BINUNICODE 'abc' |
---|
310 | n/a | 121: q BINPUT 4 |
---|
311 | n/a | 123: h BINGET 4 |
---|
312 | n/a | 125: c GLOBAL 'copy_reg _reconstructor' |
---|
313 | n/a | 150: q BINPUT 5 |
---|
314 | n/a | 152: ( MARK |
---|
315 | n/a | 153: c GLOBAL '__main__ C' |
---|
316 | n/a | 165: q BINPUT 6 |
---|
317 | n/a | 167: c GLOBAL '__builtin__ object' |
---|
318 | n/a | 187: q BINPUT 7 |
---|
319 | n/a | 189: N NONE |
---|
320 | n/a | 190: t TUPLE (MARK at 152) |
---|
321 | n/a | 191: q BINPUT 8 |
---|
322 | n/a | 193: R REDUCE |
---|
323 | n/a | 194: q BINPUT 9 |
---|
324 | n/a | 196: } EMPTY_DICT |
---|
325 | n/a | 197: q BINPUT 10 |
---|
326 | n/a | 199: ( MARK |
---|
327 | n/a | 200: X BINUNICODE 'foo' |
---|
328 | n/a | 208: q BINPUT 11 |
---|
329 | n/a | 210: K BININT1 1 |
---|
330 | n/a | 212: X BINUNICODE 'bar' |
---|
331 | n/a | 220: q BINPUT 12 |
---|
332 | n/a | 222: K BININT1 2 |
---|
333 | n/a | 224: u SETITEMS (MARK at 199) |
---|
334 | n/a | 225: b BUILD |
---|
335 | n/a | 226: h BINGET 9 |
---|
336 | n/a | 228: t TUPLE (MARK at 112) |
---|
337 | n/a | 229: q BINPUT 13 |
---|
338 | n/a | 231: h BINGET 13 |
---|
339 | n/a | 233: K BININT1 5 |
---|
340 | n/a | 235: e APPENDS (MARK at 3) |
---|
341 | n/a | 236: . STOP |
---|
342 | n/a | highest protocol among opcodes = 1 |
---|
343 | n/a | """ |
---|
344 | n/a | |
---|
345 | n/a | DATA2 = ( |
---|
346 | n/a | b'\x80\x02]q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c' |
---|
347 | n/a | b'__builtin__\ncomplex\n' |
---|
348 | n/a | b'q\x01G@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00' |
---|
349 | n/a | b'\x86q\x02Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xff' |
---|
350 | n/a | b'J\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff' |
---|
351 | n/a | b'\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00a' |
---|
352 | n/a | b'bcq\x04h\x04c__main__\nC\nq\x05' |
---|
353 | n/a | b')\x81q\x06}q\x07(X\x03\x00\x00\x00fooq\x08K\x01' |
---|
354 | n/a | b'X\x03\x00\x00\x00barq\tK\x02ubh\x06tq\nh' |
---|
355 | n/a | b'\nK\x05e.' |
---|
356 | n/a | ) |
---|
357 | n/a | |
---|
358 | n/a | # Disassembly of DATA2 |
---|
359 | n/a | DATA2_DIS = """\ |
---|
360 | n/a | 0: \x80 PROTO 2 |
---|
361 | n/a | 2: ] EMPTY_LIST |
---|
362 | n/a | 3: q BINPUT 0 |
---|
363 | n/a | 5: ( MARK |
---|
364 | n/a | 6: K BININT1 0 |
---|
365 | n/a | 8: K BININT1 1 |
---|
366 | n/a | 10: G BINFLOAT 2.0 |
---|
367 | n/a | 19: c GLOBAL '__builtin__ complex' |
---|
368 | n/a | 40: q BINPUT 1 |
---|
369 | n/a | 42: G BINFLOAT 3.0 |
---|
370 | n/a | 51: G BINFLOAT 0.0 |
---|
371 | n/a | 60: \x86 TUPLE2 |
---|
372 | n/a | 61: q BINPUT 2 |
---|
373 | n/a | 63: R REDUCE |
---|
374 | n/a | 64: q BINPUT 3 |
---|
375 | n/a | 66: K BININT1 1 |
---|
376 | n/a | 68: J BININT -1 |
---|
377 | n/a | 73: K BININT1 255 |
---|
378 | n/a | 75: J BININT -255 |
---|
379 | n/a | 80: J BININT -256 |
---|
380 | n/a | 85: M BININT2 65535 |
---|
381 | n/a | 88: J BININT -65535 |
---|
382 | n/a | 93: J BININT -65536 |
---|
383 | n/a | 98: J BININT 2147483647 |
---|
384 | n/a | 103: J BININT -2147483647 |
---|
385 | n/a | 108: J BININT -2147483648 |
---|
386 | n/a | 113: ( MARK |
---|
387 | n/a | 114: X BINUNICODE 'abc' |
---|
388 | n/a | 122: q BINPUT 4 |
---|
389 | n/a | 124: h BINGET 4 |
---|
390 | n/a | 126: c GLOBAL '__main__ C' |
---|
391 | n/a | 138: q BINPUT 5 |
---|
392 | n/a | 140: ) EMPTY_TUPLE |
---|
393 | n/a | 141: \x81 NEWOBJ |
---|
394 | n/a | 142: q BINPUT 6 |
---|
395 | n/a | 144: } EMPTY_DICT |
---|
396 | n/a | 145: q BINPUT 7 |
---|
397 | n/a | 147: ( MARK |
---|
398 | n/a | 148: X BINUNICODE 'foo' |
---|
399 | n/a | 156: q BINPUT 8 |
---|
400 | n/a | 158: K BININT1 1 |
---|
401 | n/a | 160: X BINUNICODE 'bar' |
---|
402 | n/a | 168: q BINPUT 9 |
---|
403 | n/a | 170: K BININT1 2 |
---|
404 | n/a | 172: u SETITEMS (MARK at 147) |
---|
405 | n/a | 173: b BUILD |
---|
406 | n/a | 174: h BINGET 6 |
---|
407 | n/a | 176: t TUPLE (MARK at 113) |
---|
408 | n/a | 177: q BINPUT 10 |
---|
409 | n/a | 179: h BINGET 10 |
---|
410 | n/a | 181: K BININT1 5 |
---|
411 | n/a | 183: e APPENDS (MARK at 5) |
---|
412 | n/a | 184: . STOP |
---|
413 | n/a | highest protocol among opcodes = 2 |
---|
414 | n/a | """ |
---|
415 | n/a | |
---|
416 | n/a | DATA3 = ( |
---|
417 | n/a | b'\x80\x03]q\x00(K\x00K\x01G@\x00\x00\x00\x00\x00\x00\x00c' |
---|
418 | n/a | b'builtins\ncomplex\nq\x01G' |
---|
419 | n/a | b'@\x08\x00\x00\x00\x00\x00\x00G\x00\x00\x00\x00\x00\x00\x00\x00\x86q\x02' |
---|
420 | n/a | b'Rq\x03K\x01J\xff\xff\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff' |
---|
421 | n/a | b'\xff\xffM\xff\xffJ\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7f' |
---|
422 | n/a | b'J\x01\x00\x00\x80J\x00\x00\x00\x80(X\x03\x00\x00\x00abcq' |
---|
423 | n/a | b'\x04h\x04c__main__\nC\nq\x05)\x81q' |
---|
424 | n/a | b'\x06}q\x07(X\x03\x00\x00\x00barq\x08K\x02X\x03\x00' |
---|
425 | n/a | b'\x00\x00fooq\tK\x01ubh\x06tq\nh\nK\x05' |
---|
426 | n/a | b'e.' |
---|
427 | n/a | ) |
---|
428 | n/a | |
---|
429 | n/a | # Disassembly of DATA3 |
---|
430 | n/a | DATA3_DIS = """\ |
---|
431 | n/a | 0: \x80 PROTO 3 |
---|
432 | n/a | 2: ] EMPTY_LIST |
---|
433 | n/a | 3: q BINPUT 0 |
---|
434 | n/a | 5: ( MARK |
---|
435 | n/a | 6: K BININT1 0 |
---|
436 | n/a | 8: K BININT1 1 |
---|
437 | n/a | 10: G BINFLOAT 2.0 |
---|
438 | n/a | 19: c GLOBAL 'builtins complex' |
---|
439 | n/a | 37: q BINPUT 1 |
---|
440 | n/a | 39: G BINFLOAT 3.0 |
---|
441 | n/a | 48: G BINFLOAT 0.0 |
---|
442 | n/a | 57: \x86 TUPLE2 |
---|
443 | n/a | 58: q BINPUT 2 |
---|
444 | n/a | 60: R REDUCE |
---|
445 | n/a | 61: q BINPUT 3 |
---|
446 | n/a | 63: K BININT1 1 |
---|
447 | n/a | 65: J BININT -1 |
---|
448 | n/a | 70: K BININT1 255 |
---|
449 | n/a | 72: J BININT -255 |
---|
450 | n/a | 77: J BININT -256 |
---|
451 | n/a | 82: M BININT2 65535 |
---|
452 | n/a | 85: J BININT -65535 |
---|
453 | n/a | 90: J BININT -65536 |
---|
454 | n/a | 95: J BININT 2147483647 |
---|
455 | n/a | 100: J BININT -2147483647 |
---|
456 | n/a | 105: J BININT -2147483648 |
---|
457 | n/a | 110: ( MARK |
---|
458 | n/a | 111: X BINUNICODE 'abc' |
---|
459 | n/a | 119: q BINPUT 4 |
---|
460 | n/a | 121: h BINGET 4 |
---|
461 | n/a | 123: c GLOBAL '__main__ C' |
---|
462 | n/a | 135: q BINPUT 5 |
---|
463 | n/a | 137: ) EMPTY_TUPLE |
---|
464 | n/a | 138: \x81 NEWOBJ |
---|
465 | n/a | 139: q BINPUT 6 |
---|
466 | n/a | 141: } EMPTY_DICT |
---|
467 | n/a | 142: q BINPUT 7 |
---|
468 | n/a | 144: ( MARK |
---|
469 | n/a | 145: X BINUNICODE 'bar' |
---|
470 | n/a | 153: q BINPUT 8 |
---|
471 | n/a | 155: K BININT1 2 |
---|
472 | n/a | 157: X BINUNICODE 'foo' |
---|
473 | n/a | 165: q BINPUT 9 |
---|
474 | n/a | 167: K BININT1 1 |
---|
475 | n/a | 169: u SETITEMS (MARK at 144) |
---|
476 | n/a | 170: b BUILD |
---|
477 | n/a | 171: h BINGET 6 |
---|
478 | n/a | 173: t TUPLE (MARK at 110) |
---|
479 | n/a | 174: q BINPUT 10 |
---|
480 | n/a | 176: h BINGET 10 |
---|
481 | n/a | 178: K BININT1 5 |
---|
482 | n/a | 180: e APPENDS (MARK at 5) |
---|
483 | n/a | 181: . STOP |
---|
484 | n/a | highest protocol among opcodes = 2 |
---|
485 | n/a | """ |
---|
486 | n/a | |
---|
487 | n/a | DATA4 = ( |
---|
488 | n/a | b'\x80\x04\x95\xa8\x00\x00\x00\x00\x00\x00\x00]\x94(K\x00K\x01G@' |
---|
489 | n/a | b'\x00\x00\x00\x00\x00\x00\x00\x8c\x08builtins\x94\x8c\x07' |
---|
490 | n/a | b'complex\x94\x93\x94G@\x08\x00\x00\x00\x00\x00\x00G' |
---|
491 | n/a | b'\x00\x00\x00\x00\x00\x00\x00\x00\x86\x94R\x94K\x01J\xff\xff\xff\xffK' |
---|
492 | n/a | b'\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xffJ' |
---|
493 | n/a | b'\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00\x80(' |
---|
494 | n/a | b'\x8c\x03abc\x94h\x06\x8c\x08__main__\x94\x8c' |
---|
495 | n/a | b'\x01C\x94\x93\x94)\x81\x94}\x94(\x8c\x03bar\x94K\x02\x8c' |
---|
496 | n/a | b'\x03foo\x94K\x01ubh\nt\x94h\x0eK\x05e.' |
---|
497 | n/a | ) |
---|
498 | n/a | |
---|
499 | n/a | # Disassembly of DATA4 |
---|
500 | n/a | DATA4_DIS = """\ |
---|
501 | n/a | 0: \x80 PROTO 4 |
---|
502 | n/a | 2: \x95 FRAME 168 |
---|
503 | n/a | 11: ] EMPTY_LIST |
---|
504 | n/a | 12: \x94 MEMOIZE |
---|
505 | n/a | 13: ( MARK |
---|
506 | n/a | 14: K BININT1 0 |
---|
507 | n/a | 16: K BININT1 1 |
---|
508 | n/a | 18: G BINFLOAT 2.0 |
---|
509 | n/a | 27: \x8c SHORT_BINUNICODE 'builtins' |
---|
510 | n/a | 37: \x94 MEMOIZE |
---|
511 | n/a | 38: \x8c SHORT_BINUNICODE 'complex' |
---|
512 | n/a | 47: \x94 MEMOIZE |
---|
513 | n/a | 48: \x93 STACK_GLOBAL |
---|
514 | n/a | 49: \x94 MEMOIZE |
---|
515 | n/a | 50: G BINFLOAT 3.0 |
---|
516 | n/a | 59: G BINFLOAT 0.0 |
---|
517 | n/a | 68: \x86 TUPLE2 |
---|
518 | n/a | 69: \x94 MEMOIZE |
---|
519 | n/a | 70: R REDUCE |
---|
520 | n/a | 71: \x94 MEMOIZE |
---|
521 | n/a | 72: K BININT1 1 |
---|
522 | n/a | 74: J BININT -1 |
---|
523 | n/a | 79: K BININT1 255 |
---|
524 | n/a | 81: J BININT -255 |
---|
525 | n/a | 86: J BININT -256 |
---|
526 | n/a | 91: M BININT2 65535 |
---|
527 | n/a | 94: J BININT -65535 |
---|
528 | n/a | 99: J BININT -65536 |
---|
529 | n/a | 104: J BININT 2147483647 |
---|
530 | n/a | 109: J BININT -2147483647 |
---|
531 | n/a | 114: J BININT -2147483648 |
---|
532 | n/a | 119: ( MARK |
---|
533 | n/a | 120: \x8c SHORT_BINUNICODE 'abc' |
---|
534 | n/a | 125: \x94 MEMOIZE |
---|
535 | n/a | 126: h BINGET 6 |
---|
536 | n/a | 128: \x8c SHORT_BINUNICODE '__main__' |
---|
537 | n/a | 138: \x94 MEMOIZE |
---|
538 | n/a | 139: \x8c SHORT_BINUNICODE 'C' |
---|
539 | n/a | 142: \x94 MEMOIZE |
---|
540 | n/a | 143: \x93 STACK_GLOBAL |
---|
541 | n/a | 144: \x94 MEMOIZE |
---|
542 | n/a | 145: ) EMPTY_TUPLE |
---|
543 | n/a | 146: \x81 NEWOBJ |
---|
544 | n/a | 147: \x94 MEMOIZE |
---|
545 | n/a | 148: } EMPTY_DICT |
---|
546 | n/a | 149: \x94 MEMOIZE |
---|
547 | n/a | 150: ( MARK |
---|
548 | n/a | 151: \x8c SHORT_BINUNICODE 'bar' |
---|
549 | n/a | 156: \x94 MEMOIZE |
---|
550 | n/a | 157: K BININT1 2 |
---|
551 | n/a | 159: \x8c SHORT_BINUNICODE 'foo' |
---|
552 | n/a | 164: \x94 MEMOIZE |
---|
553 | n/a | 165: K BININT1 1 |
---|
554 | n/a | 167: u SETITEMS (MARK at 150) |
---|
555 | n/a | 168: b BUILD |
---|
556 | n/a | 169: h BINGET 10 |
---|
557 | n/a | 171: t TUPLE (MARK at 119) |
---|
558 | n/a | 172: \x94 MEMOIZE |
---|
559 | n/a | 173: h BINGET 14 |
---|
560 | n/a | 175: K BININT1 5 |
---|
561 | n/a | 177: e APPENDS (MARK at 13) |
---|
562 | n/a | 178: . STOP |
---|
563 | n/a | highest protocol among opcodes = 4 |
---|
564 | n/a | """ |
---|
565 | n/a | |
---|
566 | n/a | # set([1,2]) pickled from 2.x with protocol 2 |
---|
567 | n/a | DATA_SET = b'\x80\x02c__builtin__\nset\nq\x00]q\x01(K\x01K\x02e\x85q\x02Rq\x03.' |
---|
568 | n/a | |
---|
569 | n/a | # xrange(5) pickled from 2.x with protocol 2 |
---|
570 | n/a | DATA_XRANGE = b'\x80\x02c__builtin__\nxrange\nq\x00K\x00K\x05K\x01\x87q\x01Rq\x02.' |
---|
571 | n/a | |
---|
572 | n/a | # a SimpleCookie() object pickled from 2.x with protocol 2 |
---|
573 | n/a | DATA_COOKIE = (b'\x80\x02cCookie\nSimpleCookie\nq\x00)\x81q\x01U\x03key' |
---|
574 | n/a | b'q\x02cCookie\nMorsel\nq\x03)\x81q\x04(U\x07commentq\x05U' |
---|
575 | n/a | b'\x00q\x06U\x06domainq\x07h\x06U\x06secureq\x08h\x06U\x07' |
---|
576 | n/a | b'expiresq\th\x06U\x07max-ageq\nh\x06U\x07versionq\x0bh\x06U' |
---|
577 | n/a | b'\x04pathq\x0ch\x06U\x08httponlyq\rh\x06u}q\x0e(U\x0b' |
---|
578 | n/a | b'coded_valueq\x0fU\x05valueq\x10h\x10h\x10h\x02h\x02ubs}q\x11b.') |
---|
579 | n/a | |
---|
580 | n/a | # set([3]) pickled from 2.x with protocol 2 |
---|
581 | n/a | DATA_SET2 = b'\x80\x02c__builtin__\nset\nq\x00]q\x01K\x03a\x85q\x02Rq\x03.' |
---|
582 | n/a | |
---|
583 | n/a | python2_exceptions_without_args = ( |
---|
584 | n/a | ArithmeticError, |
---|
585 | n/a | AssertionError, |
---|
586 | n/a | AttributeError, |
---|
587 | n/a | BaseException, |
---|
588 | n/a | BufferError, |
---|
589 | n/a | BytesWarning, |
---|
590 | n/a | DeprecationWarning, |
---|
591 | n/a | EOFError, |
---|
592 | n/a | EnvironmentError, |
---|
593 | n/a | Exception, |
---|
594 | n/a | FloatingPointError, |
---|
595 | n/a | FutureWarning, |
---|
596 | n/a | GeneratorExit, |
---|
597 | n/a | IOError, |
---|
598 | n/a | ImportError, |
---|
599 | n/a | ImportWarning, |
---|
600 | n/a | IndentationError, |
---|
601 | n/a | IndexError, |
---|
602 | n/a | KeyError, |
---|
603 | n/a | KeyboardInterrupt, |
---|
604 | n/a | LookupError, |
---|
605 | n/a | MemoryError, |
---|
606 | n/a | NameError, |
---|
607 | n/a | NotImplementedError, |
---|
608 | n/a | OSError, |
---|
609 | n/a | OverflowError, |
---|
610 | n/a | PendingDeprecationWarning, |
---|
611 | n/a | ReferenceError, |
---|
612 | n/a | RuntimeError, |
---|
613 | n/a | RuntimeWarning, |
---|
614 | n/a | # StandardError is gone in Python 3, we map it to Exception |
---|
615 | n/a | StopIteration, |
---|
616 | n/a | SyntaxError, |
---|
617 | n/a | SyntaxWarning, |
---|
618 | n/a | SystemError, |
---|
619 | n/a | SystemExit, |
---|
620 | n/a | TabError, |
---|
621 | n/a | TypeError, |
---|
622 | n/a | UnboundLocalError, |
---|
623 | n/a | UnicodeError, |
---|
624 | n/a | UnicodeWarning, |
---|
625 | n/a | UserWarning, |
---|
626 | n/a | ValueError, |
---|
627 | n/a | Warning, |
---|
628 | n/a | ZeroDivisionError, |
---|
629 | n/a | ) |
---|
630 | n/a | |
---|
631 | n/a | exception_pickle = b'\x80\x02cexceptions\n?\nq\x00)Rq\x01.' |
---|
632 | n/a | |
---|
633 | n/a | # UnicodeEncodeError object pickled from 2.x with protocol 2 |
---|
634 | n/a | DATA_UEERR = (b'\x80\x02cexceptions\nUnicodeEncodeError\n' |
---|
635 | n/a | b'q\x00(U\x05asciiq\x01X\x03\x00\x00\x00fooq\x02K\x00K\x01' |
---|
636 | n/a | b'U\x03badq\x03tq\x04Rq\x05.') |
---|
637 | n/a | |
---|
638 | n/a | |
---|
639 | n/a | def create_data(): |
---|
640 | n/a | c = C() |
---|
641 | n/a | c.foo = 1 |
---|
642 | n/a | c.bar = 2 |
---|
643 | n/a | x = [0, 1, 2.0, 3.0+0j] |
---|
644 | n/a | # Append some integer test cases at cPickle.c's internal size |
---|
645 | n/a | # cutoffs. |
---|
646 | n/a | uint1max = 0xff |
---|
647 | n/a | uint2max = 0xffff |
---|
648 | n/a | int4max = 0x7fffffff |
---|
649 | n/a | x.extend([1, -1, |
---|
650 | n/a | uint1max, -uint1max, -uint1max-1, |
---|
651 | n/a | uint2max, -uint2max, -uint2max-1, |
---|
652 | n/a | int4max, -int4max, -int4max-1]) |
---|
653 | n/a | y = ('abc', 'abc', c, c) |
---|
654 | n/a | x.append(y) |
---|
655 | n/a | x.append(y) |
---|
656 | n/a | x.append(5) |
---|
657 | n/a | return x |
---|
658 | n/a | |
---|
659 | n/a | |
---|
660 | n/a | class AbstractUnpickleTests(unittest.TestCase): |
---|
661 | n/a | # Subclass must define self.loads. |
---|
662 | n/a | |
---|
663 | n/a | _testdata = create_data() |
---|
664 | n/a | |
---|
665 | n/a | def assert_is_copy(self, obj, objcopy, msg=None): |
---|
666 | n/a | """Utility method to verify if two objects are copies of each others. |
---|
667 | n/a | """ |
---|
668 | n/a | if msg is None: |
---|
669 | n/a | msg = "{!r} is not a copy of {!r}".format(obj, objcopy) |
---|
670 | n/a | self.assertEqual(obj, objcopy, msg=msg) |
---|
671 | n/a | self.assertIs(type(obj), type(objcopy), msg=msg) |
---|
672 | n/a | if hasattr(obj, '__dict__'): |
---|
673 | n/a | self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg) |
---|
674 | n/a | self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg) |
---|
675 | n/a | if hasattr(obj, '__slots__'): |
---|
676 | n/a | self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg) |
---|
677 | n/a | for slot in obj.__slots__: |
---|
678 | n/a | self.assertEqual( |
---|
679 | n/a | hasattr(obj, slot), hasattr(objcopy, slot), msg=msg) |
---|
680 | n/a | self.assertEqual(getattr(obj, slot, None), |
---|
681 | n/a | getattr(objcopy, slot, None), msg=msg) |
---|
682 | n/a | |
---|
683 | n/a | def check_unpickling_error(self, errors, data): |
---|
684 | n/a | with self.subTest(data=data), \ |
---|
685 | n/a | self.assertRaises(errors): |
---|
686 | n/a | try: |
---|
687 | n/a | self.loads(data) |
---|
688 | n/a | except BaseException as exc: |
---|
689 | n/a | if support.verbose > 1: |
---|
690 | n/a | print('%-32r - %s: %s' % |
---|
691 | n/a | (data, exc.__class__.__name__, exc)) |
---|
692 | n/a | raise |
---|
693 | n/a | |
---|
694 | n/a | def test_load_from_data0(self): |
---|
695 | n/a | self.assert_is_copy(self._testdata, self.loads(DATA0)) |
---|
696 | n/a | |
---|
697 | n/a | def test_load_from_data1(self): |
---|
698 | n/a | self.assert_is_copy(self._testdata, self.loads(DATA1)) |
---|
699 | n/a | |
---|
700 | n/a | def test_load_from_data2(self): |
---|
701 | n/a | self.assert_is_copy(self._testdata, self.loads(DATA2)) |
---|
702 | n/a | |
---|
703 | n/a | def test_load_from_data3(self): |
---|
704 | n/a | self.assert_is_copy(self._testdata, self.loads(DATA3)) |
---|
705 | n/a | |
---|
706 | n/a | def test_load_from_data4(self): |
---|
707 | n/a | self.assert_is_copy(self._testdata, self.loads(DATA4)) |
---|
708 | n/a | |
---|
709 | n/a | def test_load_classic_instance(self): |
---|
710 | n/a | # See issue5180. Test loading 2.x pickles that |
---|
711 | n/a | # contain an instance of old style class. |
---|
712 | n/a | for X, args in [(C, ()), (D, ('x',)), (E, ())]: |
---|
713 | n/a | xname = X.__name__.encode('ascii') |
---|
714 | n/a | # Protocol 0 (text mode pickle): |
---|
715 | n/a | """ |
---|
716 | n/a | 0: ( MARK |
---|
717 | n/a | 1: i INST '__main__ X' (MARK at 0) |
---|
718 | n/a | 13: p PUT 0 |
---|
719 | n/a | 16: ( MARK |
---|
720 | n/a | 17: d DICT (MARK at 16) |
---|
721 | n/a | 18: p PUT 1 |
---|
722 | n/a | 21: b BUILD |
---|
723 | n/a | 22: . STOP |
---|
724 | n/a | """ |
---|
725 | n/a | pickle0 = (b"(i__main__\n" |
---|
726 | n/a | b"X\n" |
---|
727 | n/a | b"p0\n" |
---|
728 | n/a | b"(dp1\nb.").replace(b'X', xname) |
---|
729 | n/a | self.assert_is_copy(X(*args), self.loads(pickle0)) |
---|
730 | n/a | |
---|
731 | n/a | # Protocol 1 (binary mode pickle) |
---|
732 | n/a | """ |
---|
733 | n/a | 0: ( MARK |
---|
734 | n/a | 1: c GLOBAL '__main__ X' |
---|
735 | n/a | 13: q BINPUT 0 |
---|
736 | n/a | 15: o OBJ (MARK at 0) |
---|
737 | n/a | 16: q BINPUT 1 |
---|
738 | n/a | 18: } EMPTY_DICT |
---|
739 | n/a | 19: q BINPUT 2 |
---|
740 | n/a | 21: b BUILD |
---|
741 | n/a | 22: . STOP |
---|
742 | n/a | """ |
---|
743 | n/a | pickle1 = (b'(c__main__\n' |
---|
744 | n/a | b'X\n' |
---|
745 | n/a | b'q\x00oq\x01}q\x02b.').replace(b'X', xname) |
---|
746 | n/a | self.assert_is_copy(X(*args), self.loads(pickle1)) |
---|
747 | n/a | |
---|
748 | n/a | # Protocol 2 (pickle2 = b'\x80\x02' + pickle1) |
---|
749 | n/a | """ |
---|
750 | n/a | 0: \x80 PROTO 2 |
---|
751 | n/a | 2: ( MARK |
---|
752 | n/a | 3: c GLOBAL '__main__ X' |
---|
753 | n/a | 15: q BINPUT 0 |
---|
754 | n/a | 17: o OBJ (MARK at 2) |
---|
755 | n/a | 18: q BINPUT 1 |
---|
756 | n/a | 20: } EMPTY_DICT |
---|
757 | n/a | 21: q BINPUT 2 |
---|
758 | n/a | 23: b BUILD |
---|
759 | n/a | 24: . STOP |
---|
760 | n/a | """ |
---|
761 | n/a | pickle2 = (b'\x80\x02(c__main__\n' |
---|
762 | n/a | b'X\n' |
---|
763 | n/a | b'q\x00oq\x01}q\x02b.').replace(b'X', xname) |
---|
764 | n/a | self.assert_is_copy(X(*args), self.loads(pickle2)) |
---|
765 | n/a | |
---|
766 | n/a | def test_maxint64(self): |
---|
767 | n/a | maxint64 = (1 << 63) - 1 |
---|
768 | n/a | data = b'I' + str(maxint64).encode("ascii") + b'\n.' |
---|
769 | n/a | got = self.loads(data) |
---|
770 | n/a | self.assert_is_copy(maxint64, got) |
---|
771 | n/a | |
---|
772 | n/a | # Try too with a bogus literal. |
---|
773 | n/a | data = b'I' + str(maxint64).encode("ascii") + b'JUNK\n.' |
---|
774 | n/a | self.check_unpickling_error(ValueError, data) |
---|
775 | n/a | |
---|
776 | n/a | def test_unpickle_from_2x(self): |
---|
777 | n/a | # Unpickle non-trivial data from Python 2.x. |
---|
778 | n/a | loaded = self.loads(DATA_SET) |
---|
779 | n/a | self.assertEqual(loaded, set([1, 2])) |
---|
780 | n/a | loaded = self.loads(DATA_XRANGE) |
---|
781 | n/a | self.assertEqual(type(loaded), type(range(0))) |
---|
782 | n/a | self.assertEqual(list(loaded), list(range(5))) |
---|
783 | n/a | loaded = self.loads(DATA_COOKIE) |
---|
784 | n/a | self.assertEqual(type(loaded), SimpleCookie) |
---|
785 | n/a | self.assertEqual(list(loaded.keys()), ["key"]) |
---|
786 | n/a | self.assertEqual(loaded["key"].value, "value") |
---|
787 | n/a | |
---|
788 | n/a | # Exception objects without arguments pickled from 2.x with protocol 2 |
---|
789 | n/a | for exc in python2_exceptions_without_args: |
---|
790 | n/a | data = exception_pickle.replace(b'?', exc.__name__.encode("ascii")) |
---|
791 | n/a | loaded = self.loads(data) |
---|
792 | n/a | self.assertIs(type(loaded), exc) |
---|
793 | n/a | |
---|
794 | n/a | # StandardError is mapped to Exception, test that separately |
---|
795 | n/a | loaded = self.loads(exception_pickle.replace(b'?', b'StandardError')) |
---|
796 | n/a | self.assertIs(type(loaded), Exception) |
---|
797 | n/a | |
---|
798 | n/a | loaded = self.loads(DATA_UEERR) |
---|
799 | n/a | self.assertIs(type(loaded), UnicodeEncodeError) |
---|
800 | n/a | self.assertEqual(loaded.object, "foo") |
---|
801 | n/a | self.assertEqual(loaded.encoding, "ascii") |
---|
802 | n/a | self.assertEqual(loaded.start, 0) |
---|
803 | n/a | self.assertEqual(loaded.end, 1) |
---|
804 | n/a | self.assertEqual(loaded.reason, "bad") |
---|
805 | n/a | |
---|
806 | n/a | def test_load_python2_str_as_bytes(self): |
---|
807 | n/a | # From Python 2: pickle.dumps('a\x00\xa0', protocol=0) |
---|
808 | n/a | self.assertEqual(self.loads(b"S'a\\x00\\xa0'\n.", |
---|
809 | n/a | encoding="bytes"), b'a\x00\xa0') |
---|
810 | n/a | # From Python 2: pickle.dumps('a\x00\xa0', protocol=1) |
---|
811 | n/a | self.assertEqual(self.loads(b'U\x03a\x00\xa0.', |
---|
812 | n/a | encoding="bytes"), b'a\x00\xa0') |
---|
813 | n/a | # From Python 2: pickle.dumps('a\x00\xa0', protocol=2) |
---|
814 | n/a | self.assertEqual(self.loads(b'\x80\x02U\x03a\x00\xa0.', |
---|
815 | n/a | encoding="bytes"), b'a\x00\xa0') |
---|
816 | n/a | |
---|
817 | n/a | def test_load_python2_unicode_as_str(self): |
---|
818 | n/a | # From Python 2: pickle.dumps(u'ร', protocol=0) |
---|
819 | n/a | self.assertEqual(self.loads(b'V\\u03c0\n.', |
---|
820 | n/a | encoding='bytes'), 'ร') |
---|
821 | n/a | # From Python 2: pickle.dumps(u'ร', protocol=1) |
---|
822 | n/a | self.assertEqual(self.loads(b'X\x02\x00\x00\x00\xcf\x80.', |
---|
823 | n/a | encoding="bytes"), 'ร') |
---|
824 | n/a | # From Python 2: pickle.dumps(u'ร', protocol=2) |
---|
825 | n/a | self.assertEqual(self.loads(b'\x80\x02X\x02\x00\x00\x00\xcf\x80.', |
---|
826 | n/a | encoding="bytes"), 'ร') |
---|
827 | n/a | |
---|
828 | n/a | def test_load_long_python2_str_as_bytes(self): |
---|
829 | n/a | # From Python 2: pickle.dumps('x' * 300, protocol=1) |
---|
830 | n/a | self.assertEqual(self.loads(pickle.BINSTRING + |
---|
831 | n/a | struct.pack("<I", 300) + |
---|
832 | n/a | b'x' * 300 + pickle.STOP, |
---|
833 | n/a | encoding='bytes'), b'x' * 300) |
---|
834 | n/a | |
---|
835 | n/a | def test_constants(self): |
---|
836 | n/a | self.assertIsNone(self.loads(b'N.')) |
---|
837 | n/a | self.assertIs(self.loads(b'\x88.'), True) |
---|
838 | n/a | self.assertIs(self.loads(b'\x89.'), False) |
---|
839 | n/a | self.assertIs(self.loads(b'I01\n.'), True) |
---|
840 | n/a | self.assertIs(self.loads(b'I00\n.'), False) |
---|
841 | n/a | |
---|
842 | n/a | def test_empty_bytestring(self): |
---|
843 | n/a | # issue 11286 |
---|
844 | n/a | empty = self.loads(b'\x80\x03U\x00q\x00.', encoding='koi8-r') |
---|
845 | n/a | self.assertEqual(empty, '') |
---|
846 | n/a | |
---|
847 | n/a | def test_short_binbytes(self): |
---|
848 | n/a | dumped = b'\x80\x03C\x04\xe2\x82\xac\x00.' |
---|
849 | n/a | self.assertEqual(self.loads(dumped), b'\xe2\x82\xac\x00') |
---|
850 | n/a | |
---|
851 | n/a | def test_binbytes(self): |
---|
852 | n/a | dumped = b'\x80\x03B\x04\x00\x00\x00\xe2\x82\xac\x00.' |
---|
853 | n/a | self.assertEqual(self.loads(dumped), b'\xe2\x82\xac\x00') |
---|
854 | n/a | |
---|
855 | n/a | @requires_32b |
---|
856 | n/a | def test_negative_32b_binbytes(self): |
---|
857 | n/a | # On 32-bit builds, a BINBYTES of 2**31 or more is refused |
---|
858 | n/a | dumped = b'\x80\x03B\xff\xff\xff\xffxyzq\x00.' |
---|
859 | n/a | self.check_unpickling_error((pickle.UnpicklingError, OverflowError), |
---|
860 | n/a | dumped) |
---|
861 | n/a | |
---|
862 | n/a | @requires_32b |
---|
863 | n/a | def test_negative_32b_binunicode(self): |
---|
864 | n/a | # On 32-bit builds, a BINUNICODE of 2**31 or more is refused |
---|
865 | n/a | dumped = b'\x80\x03X\xff\xff\xff\xffxyzq\x00.' |
---|
866 | n/a | self.check_unpickling_error((pickle.UnpicklingError, OverflowError), |
---|
867 | n/a | dumped) |
---|
868 | n/a | |
---|
869 | n/a | def test_short_binunicode(self): |
---|
870 | n/a | dumped = b'\x80\x04\x8c\x04\xe2\x82\xac\x00.' |
---|
871 | n/a | self.assertEqual(self.loads(dumped), '\u20ac\x00') |
---|
872 | n/a | |
---|
873 | n/a | def test_misc_get(self): |
---|
874 | n/a | self.check_unpickling_error(KeyError, b'g0\np0') |
---|
875 | n/a | self.assert_is_copy([(100,), (100,)], |
---|
876 | n/a | self.loads(b'((Kdtp0\nh\x00l.))')) |
---|
877 | n/a | |
---|
878 | n/a | def test_binbytes8(self): |
---|
879 | n/a | dumped = b'\x80\x04\x8e\4\0\0\0\0\0\0\0\xe2\x82\xac\x00.' |
---|
880 | n/a | self.assertEqual(self.loads(dumped), b'\xe2\x82\xac\x00') |
---|
881 | n/a | |
---|
882 | n/a | def test_binunicode8(self): |
---|
883 | n/a | dumped = b'\x80\x04\x8d\4\0\0\0\0\0\0\0\xe2\x82\xac\x00.' |
---|
884 | n/a | self.assertEqual(self.loads(dumped), '\u20ac\x00') |
---|
885 | n/a | |
---|
886 | n/a | @requires_32b |
---|
887 | n/a | def test_large_32b_binbytes8(self): |
---|
888 | n/a | dumped = b'\x80\x04\x8e\4\0\0\0\1\0\0\0\xe2\x82\xac\x00.' |
---|
889 | n/a | self.check_unpickling_error((pickle.UnpicklingError, OverflowError), |
---|
890 | n/a | dumped) |
---|
891 | n/a | |
---|
892 | n/a | @requires_32b |
---|
893 | n/a | def test_large_32b_binunicode8(self): |
---|
894 | n/a | dumped = b'\x80\x04\x8d\4\0\0\0\1\0\0\0\xe2\x82\xac\x00.' |
---|
895 | n/a | self.check_unpickling_error((pickle.UnpicklingError, OverflowError), |
---|
896 | n/a | dumped) |
---|
897 | n/a | |
---|
898 | n/a | def test_get(self): |
---|
899 | n/a | pickled = b'((lp100000\ng100000\nt.' |
---|
900 | n/a | unpickled = self.loads(pickled) |
---|
901 | n/a | self.assertEqual(unpickled, ([],)*2) |
---|
902 | n/a | self.assertIs(unpickled[0], unpickled[1]) |
---|
903 | n/a | |
---|
904 | n/a | def test_binget(self): |
---|
905 | n/a | pickled = b'(]q\xffh\xfft.' |
---|
906 | n/a | unpickled = self.loads(pickled) |
---|
907 | n/a | self.assertEqual(unpickled, ([],)*2) |
---|
908 | n/a | self.assertIs(unpickled[0], unpickled[1]) |
---|
909 | n/a | |
---|
910 | n/a | def test_long_binget(self): |
---|
911 | n/a | pickled = b'(]r\x00\x00\x01\x00j\x00\x00\x01\x00t.' |
---|
912 | n/a | unpickled = self.loads(pickled) |
---|
913 | n/a | self.assertEqual(unpickled, ([],)*2) |
---|
914 | n/a | self.assertIs(unpickled[0], unpickled[1]) |
---|
915 | n/a | |
---|
916 | n/a | def test_dup(self): |
---|
917 | n/a | pickled = b'((l2t.' |
---|
918 | n/a | unpickled = self.loads(pickled) |
---|
919 | n/a | self.assertEqual(unpickled, ([],)*2) |
---|
920 | n/a | self.assertIs(unpickled[0], unpickled[1]) |
---|
921 | n/a | |
---|
922 | n/a | def test_negative_put(self): |
---|
923 | n/a | # Issue #12847 |
---|
924 | n/a | dumped = b'Va\np-1\n.' |
---|
925 | n/a | self.check_unpickling_error(ValueError, dumped) |
---|
926 | n/a | |
---|
927 | n/a | @requires_32b |
---|
928 | n/a | def test_negative_32b_binput(self): |
---|
929 | n/a | # Issue #12847 |
---|
930 | n/a | dumped = b'\x80\x03X\x01\x00\x00\x00ar\xff\xff\xff\xff.' |
---|
931 | n/a | self.check_unpickling_error(ValueError, dumped) |
---|
932 | n/a | |
---|
933 | n/a | def test_badly_escaped_string(self): |
---|
934 | n/a | self.check_unpickling_error(ValueError, b"S'\\'\n.") |
---|
935 | n/a | |
---|
936 | n/a | def test_badly_quoted_string(self): |
---|
937 | n/a | # Issue #17710 |
---|
938 | n/a | badpickles = [b"S'\n.", |
---|
939 | n/a | b'S"\n.', |
---|
940 | n/a | b'S\' \n.', |
---|
941 | n/a | b'S" \n.', |
---|
942 | n/a | b'S\'"\n.', |
---|
943 | n/a | b'S"\'\n.', |
---|
944 | n/a | b"S' ' \n.", |
---|
945 | n/a | b'S" " \n.', |
---|
946 | n/a | b"S ''\n.", |
---|
947 | n/a | b'S ""\n.', |
---|
948 | n/a | b'S \n.', |
---|
949 | n/a | b'S\n.', |
---|
950 | n/a | b'S.'] |
---|
951 | n/a | for p in badpickles: |
---|
952 | n/a | self.check_unpickling_error(pickle.UnpicklingError, p) |
---|
953 | n/a | |
---|
954 | n/a | def test_correctly_quoted_string(self): |
---|
955 | n/a | goodpickles = [(b"S''\n.", ''), |
---|
956 | n/a | (b'S""\n.', ''), |
---|
957 | n/a | (b'S"\\n"\n.', '\n'), |
---|
958 | n/a | (b"S'\\n'\n.", '\n')] |
---|
959 | n/a | for p, expected in goodpickles: |
---|
960 | n/a | self.assertEqual(self.loads(p), expected) |
---|
961 | n/a | |
---|
962 | n/a | def test_frame_readline(self): |
---|
963 | n/a | pickled = b'\x80\x04\x95\x05\x00\x00\x00\x00\x00\x00\x00I42\n.' |
---|
964 | n/a | # 0: \x80 PROTO 4 |
---|
965 | n/a | # 2: \x95 FRAME 5 |
---|
966 | n/a | # 11: I INT 42 |
---|
967 | n/a | # 15: . STOP |
---|
968 | n/a | self.assertEqual(self.loads(pickled), 42) |
---|
969 | n/a | |
---|
970 | n/a | def test_compat_unpickle(self): |
---|
971 | n/a | # xrange(1, 7) |
---|
972 | n/a | pickled = b'\x80\x02c__builtin__\nxrange\nK\x01K\x07K\x01\x87R.' |
---|
973 | n/a | unpickled = self.loads(pickled) |
---|
974 | n/a | self.assertIs(type(unpickled), range) |
---|
975 | n/a | self.assertEqual(unpickled, range(1, 7)) |
---|
976 | n/a | self.assertEqual(list(unpickled), [1, 2, 3, 4, 5, 6]) |
---|
977 | n/a | # reduce |
---|
978 | n/a | pickled = b'\x80\x02c__builtin__\nreduce\n.' |
---|
979 | n/a | self.assertIs(self.loads(pickled), functools.reduce) |
---|
980 | n/a | # whichdb.whichdb |
---|
981 | n/a | pickled = b'\x80\x02cwhichdb\nwhichdb\n.' |
---|
982 | n/a | self.assertIs(self.loads(pickled), dbm.whichdb) |
---|
983 | n/a | # Exception(), StandardError() |
---|
984 | n/a | for name in (b'Exception', b'StandardError'): |
---|
985 | n/a | pickled = (b'\x80\x02cexceptions\n' + name + b'\nU\x03ugh\x85R.') |
---|
986 | n/a | unpickled = self.loads(pickled) |
---|
987 | n/a | self.assertIs(type(unpickled), Exception) |
---|
988 | n/a | self.assertEqual(str(unpickled), 'ugh') |
---|
989 | n/a | # UserDict.UserDict({1: 2}), UserDict.IterableUserDict({1: 2}) |
---|
990 | n/a | for name in (b'UserDict', b'IterableUserDict'): |
---|
991 | n/a | pickled = (b'\x80\x02(cUserDict\n' + name + |
---|
992 | n/a | b'\no}U\x04data}K\x01K\x02ssb.') |
---|
993 | n/a | unpickled = self.loads(pickled) |
---|
994 | n/a | self.assertIs(type(unpickled), collections.UserDict) |
---|
995 | n/a | self.assertEqual(unpickled, collections.UserDict({1: 2})) |
---|
996 | n/a | |
---|
997 | n/a | def test_bad_stack(self): |
---|
998 | n/a | badpickles = [ |
---|
999 | n/a | b'.', # STOP |
---|
1000 | n/a | b'0', # POP |
---|
1001 | n/a | b'1', # POP_MARK |
---|
1002 | n/a | b'2', # DUP |
---|
1003 | n/a | b'(2', |
---|
1004 | n/a | b'R', # REDUCE |
---|
1005 | n/a | b')R', |
---|
1006 | n/a | b'a', # APPEND |
---|
1007 | n/a | b'Na', |
---|
1008 | n/a | b'b', # BUILD |
---|
1009 | n/a | b'Nb', |
---|
1010 | n/a | b'd', # DICT |
---|
1011 | n/a | b'e', # APPENDS |
---|
1012 | n/a | b'(e', |
---|
1013 | n/a | b'ibuiltins\nlist\n', # INST |
---|
1014 | n/a | b'l', # LIST |
---|
1015 | n/a | b'o', # OBJ |
---|
1016 | n/a | b'(o', |
---|
1017 | n/a | b'p1\n', # PUT |
---|
1018 | n/a | b'q\x00', # BINPUT |
---|
1019 | n/a | b'r\x00\x00\x00\x00', # LONG_BINPUT |
---|
1020 | n/a | b's', # SETITEM |
---|
1021 | n/a | b'Ns', |
---|
1022 | n/a | b'NNs', |
---|
1023 | n/a | b't', # TUPLE |
---|
1024 | n/a | b'u', # SETITEMS |
---|
1025 | n/a | b'(u', |
---|
1026 | n/a | b'}(Nu', |
---|
1027 | n/a | b'\x81', # NEWOBJ |
---|
1028 | n/a | b')\x81', |
---|
1029 | n/a | b'\x85', # TUPLE1 |
---|
1030 | n/a | b'\x86', # TUPLE2 |
---|
1031 | n/a | b'N\x86', |
---|
1032 | n/a | b'\x87', # TUPLE3 |
---|
1033 | n/a | b'N\x87', |
---|
1034 | n/a | b'NN\x87', |
---|
1035 | n/a | b'\x90', # ADDITEMS |
---|
1036 | n/a | b'(\x90', |
---|
1037 | n/a | b'\x91', # FROZENSET |
---|
1038 | n/a | b'\x92', # NEWOBJ_EX |
---|
1039 | n/a | b')}\x92', |
---|
1040 | n/a | b'\x93', # STACK_GLOBAL |
---|
1041 | n/a | b'Vlist\n\x93', |
---|
1042 | n/a | b'\x94', # MEMOIZE |
---|
1043 | n/a | ] |
---|
1044 | n/a | for p in badpickles: |
---|
1045 | n/a | self.check_unpickling_error(self.bad_stack_errors, p) |
---|
1046 | n/a | |
---|
1047 | n/a | def test_bad_mark(self): |
---|
1048 | n/a | badpickles = [ |
---|
1049 | n/a | b'N(.', # STOP |
---|
1050 | n/a | b'N(2', # DUP |
---|
1051 | n/a | b'cbuiltins\nlist\n)(R', # REDUCE |
---|
1052 | n/a | b'cbuiltins\nlist\n()R', |
---|
1053 | n/a | b']N(a', # APPEND |
---|
1054 | n/a | # BUILD |
---|
1055 | n/a | b'cbuiltins\nValueError\n)R}(b', |
---|
1056 | n/a | b'cbuiltins\nValueError\n)R(}b', |
---|
1057 | n/a | b'(Nd', # DICT |
---|
1058 | n/a | b'N(p1\n', # PUT |
---|
1059 | n/a | b'N(q\x00', # BINPUT |
---|
1060 | n/a | b'N(r\x00\x00\x00\x00', # LONG_BINPUT |
---|
1061 | n/a | b'}NN(s', # SETITEM |
---|
1062 | n/a | b'}N(Ns', |
---|
1063 | n/a | b'}(NNs', |
---|
1064 | n/a | b'}((u', # SETITEMS |
---|
1065 | n/a | b'cbuiltins\nlist\n)(\x81', # NEWOBJ |
---|
1066 | n/a | b'cbuiltins\nlist\n()\x81', |
---|
1067 | n/a | b'N(\x85', # TUPLE1 |
---|
1068 | n/a | b'NN(\x86', # TUPLE2 |
---|
1069 | n/a | b'N(N\x86', |
---|
1070 | n/a | b'NNN(\x87', # TUPLE3 |
---|
1071 | n/a | b'NN(N\x87', |
---|
1072 | n/a | b'N(NN\x87', |
---|
1073 | n/a | b']((\x90', # ADDITEMS |
---|
1074 | n/a | # NEWOBJ_EX |
---|
1075 | n/a | b'cbuiltins\nlist\n)}(\x92', |
---|
1076 | n/a | b'cbuiltins\nlist\n)(}\x92', |
---|
1077 | n/a | b'cbuiltins\nlist\n()}\x92', |
---|
1078 | n/a | # STACK_GLOBAL |
---|
1079 | n/a | b'Vbuiltins\n(Vlist\n\x93', |
---|
1080 | n/a | b'Vbuiltins\nVlist\n(\x93', |
---|
1081 | n/a | b'N(\x94', # MEMOIZE |
---|
1082 | n/a | ] |
---|
1083 | n/a | for p in badpickles: |
---|
1084 | n/a | self.check_unpickling_error(self.bad_stack_errors, p) |
---|
1085 | n/a | |
---|
1086 | n/a | def test_truncated_data(self): |
---|
1087 | n/a | self.check_unpickling_error(EOFError, b'') |
---|
1088 | n/a | self.check_unpickling_error(EOFError, b'N') |
---|
1089 | n/a | badpickles = [ |
---|
1090 | n/a | b'B', # BINBYTES |
---|
1091 | n/a | b'B\x03\x00\x00', |
---|
1092 | n/a | b'B\x03\x00\x00\x00', |
---|
1093 | n/a | b'B\x03\x00\x00\x00ab', |
---|
1094 | n/a | b'C', # SHORT_BINBYTES |
---|
1095 | n/a | b'C\x03', |
---|
1096 | n/a | b'C\x03ab', |
---|
1097 | n/a | b'F', # FLOAT |
---|
1098 | n/a | b'F0.0', |
---|
1099 | n/a | b'F0.00', |
---|
1100 | n/a | b'G', # BINFLOAT |
---|
1101 | n/a | b'G\x00\x00\x00\x00\x00\x00\x00', |
---|
1102 | n/a | b'I', # INT |
---|
1103 | n/a | b'I0', |
---|
1104 | n/a | b'J', # BININT |
---|
1105 | n/a | b'J\x00\x00\x00', |
---|
1106 | n/a | b'K', # BININT1 |
---|
1107 | n/a | b'L', # LONG |
---|
1108 | n/a | b'L0', |
---|
1109 | n/a | b'L10', |
---|
1110 | n/a | b'L0L', |
---|
1111 | n/a | b'L10L', |
---|
1112 | n/a | b'M', # BININT2 |
---|
1113 | n/a | b'M\x00', |
---|
1114 | n/a | # b'P', # PERSID |
---|
1115 | n/a | # b'Pabc', |
---|
1116 | n/a | b'S', # STRING |
---|
1117 | n/a | b"S'abc'", |
---|
1118 | n/a | b'T', # BINSTRING |
---|
1119 | n/a | b'T\x03\x00\x00', |
---|
1120 | n/a | b'T\x03\x00\x00\x00', |
---|
1121 | n/a | b'T\x03\x00\x00\x00ab', |
---|
1122 | n/a | b'U', # SHORT_BINSTRING |
---|
1123 | n/a | b'U\x03', |
---|
1124 | n/a | b'U\x03ab', |
---|
1125 | n/a | b'V', # UNICODE |
---|
1126 | n/a | b'Vabc', |
---|
1127 | n/a | b'X', # BINUNICODE |
---|
1128 | n/a | b'X\x03\x00\x00', |
---|
1129 | n/a | b'X\x03\x00\x00\x00', |
---|
1130 | n/a | b'X\x03\x00\x00\x00ab', |
---|
1131 | n/a | b'(c', # GLOBAL |
---|
1132 | n/a | b'(cbuiltins', |
---|
1133 | n/a | b'(cbuiltins\n', |
---|
1134 | n/a | b'(cbuiltins\nlist', |
---|
1135 | n/a | b'Ng', # GET |
---|
1136 | n/a | b'Ng0', |
---|
1137 | n/a | b'(i', # INST |
---|
1138 | n/a | b'(ibuiltins', |
---|
1139 | n/a | b'(ibuiltins\n', |
---|
1140 | n/a | b'(ibuiltins\nlist', |
---|
1141 | n/a | b'Nh', # BINGET |
---|
1142 | n/a | b'Nj', # LONG_BINGET |
---|
1143 | n/a | b'Nj\x00\x00\x00', |
---|
1144 | n/a | b'Np', # PUT |
---|
1145 | n/a | b'Np0', |
---|
1146 | n/a | b'Nq', # BINPUT |
---|
1147 | n/a | b'Nr', # LONG_BINPUT |
---|
1148 | n/a | b'Nr\x00\x00\x00', |
---|
1149 | n/a | b'\x80', # PROTO |
---|
1150 | n/a | b'\x82', # EXT1 |
---|
1151 | n/a | b'\x83', # EXT2 |
---|
1152 | n/a | b'\x84\x01', |
---|
1153 | n/a | b'\x84', # EXT4 |
---|
1154 | n/a | b'\x84\x01\x00\x00', |
---|
1155 | n/a | b'\x8a', # LONG1 |
---|
1156 | n/a | b'\x8b', # LONG4 |
---|
1157 | n/a | b'\x8b\x00\x00\x00', |
---|
1158 | n/a | b'\x8c', # SHORT_BINUNICODE |
---|
1159 | n/a | b'\x8c\x03', |
---|
1160 | n/a | b'\x8c\x03ab', |
---|
1161 | n/a | b'\x8d', # BINUNICODE8 |
---|
1162 | n/a | b'\x8d\x03\x00\x00\x00\x00\x00\x00', |
---|
1163 | n/a | b'\x8d\x03\x00\x00\x00\x00\x00\x00\x00', |
---|
1164 | n/a | b'\x8d\x03\x00\x00\x00\x00\x00\x00\x00ab', |
---|
1165 | n/a | b'\x8e', # BINBYTES8 |
---|
1166 | n/a | b'\x8e\x03\x00\x00\x00\x00\x00\x00', |
---|
1167 | n/a | b'\x8e\x03\x00\x00\x00\x00\x00\x00\x00', |
---|
1168 | n/a | b'\x8e\x03\x00\x00\x00\x00\x00\x00\x00ab', |
---|
1169 | n/a | b'\x95', # FRAME |
---|
1170 | n/a | b'\x95\x02\x00\x00\x00\x00\x00\x00', |
---|
1171 | n/a | b'\x95\x02\x00\x00\x00\x00\x00\x00\x00', |
---|
1172 | n/a | b'\x95\x02\x00\x00\x00\x00\x00\x00\x00N', |
---|
1173 | n/a | ] |
---|
1174 | n/a | for p in badpickles: |
---|
1175 | n/a | self.check_unpickling_error(self.truncated_errors, p) |
---|
1176 | n/a | |
---|
1177 | n/a | |
---|
1178 | n/a | class AbstractPickleTests(unittest.TestCase): |
---|
1179 | n/a | # Subclass must define self.dumps, self.loads. |
---|
1180 | n/a | |
---|
1181 | n/a | optimized = False |
---|
1182 | n/a | |
---|
1183 | n/a | _testdata = AbstractUnpickleTests._testdata |
---|
1184 | n/a | |
---|
1185 | n/a | def setUp(self): |
---|
1186 | n/a | pass |
---|
1187 | n/a | |
---|
1188 | n/a | assert_is_copy = AbstractUnpickleTests.assert_is_copy |
---|
1189 | n/a | |
---|
1190 | n/a | def test_misc(self): |
---|
1191 | n/a | # test various datatypes not tested by testdata |
---|
1192 | n/a | for proto in protocols: |
---|
1193 | n/a | x = myint(4) |
---|
1194 | n/a | s = self.dumps(x, proto) |
---|
1195 | n/a | y = self.loads(s) |
---|
1196 | n/a | self.assert_is_copy(x, y) |
---|
1197 | n/a | |
---|
1198 | n/a | x = (1, ()) |
---|
1199 | n/a | s = self.dumps(x, proto) |
---|
1200 | n/a | y = self.loads(s) |
---|
1201 | n/a | self.assert_is_copy(x, y) |
---|
1202 | n/a | |
---|
1203 | n/a | x = initarg(1, x) |
---|
1204 | n/a | s = self.dumps(x, proto) |
---|
1205 | n/a | y = self.loads(s) |
---|
1206 | n/a | self.assert_is_copy(x, y) |
---|
1207 | n/a | |
---|
1208 | n/a | # XXX test __reduce__ protocol? |
---|
1209 | n/a | |
---|
1210 | n/a | def test_roundtrip_equality(self): |
---|
1211 | n/a | expected = self._testdata |
---|
1212 | n/a | for proto in protocols: |
---|
1213 | n/a | s = self.dumps(expected, proto) |
---|
1214 | n/a | got = self.loads(s) |
---|
1215 | n/a | self.assert_is_copy(expected, got) |
---|
1216 | n/a | |
---|
1217 | n/a | # There are gratuitous differences between pickles produced by |
---|
1218 | n/a | # pickle and cPickle, largely because cPickle starts PUT indices at |
---|
1219 | n/a | # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- |
---|
1220 | n/a | # there's a comment with an exclamation point there whose meaning |
---|
1221 | n/a | # is a mystery. cPickle also suppresses PUT for objects with a refcount |
---|
1222 | n/a | # of 1. |
---|
1223 | n/a | def dont_test_disassembly(self): |
---|
1224 | n/a | from io import StringIO |
---|
1225 | n/a | from pickletools import dis |
---|
1226 | n/a | |
---|
1227 | n/a | for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): |
---|
1228 | n/a | s = self.dumps(self._testdata, proto) |
---|
1229 | n/a | filelike = StringIO() |
---|
1230 | n/a | dis(s, out=filelike) |
---|
1231 | n/a | got = filelike.getvalue() |
---|
1232 | n/a | self.assertEqual(expected, got) |
---|
1233 | n/a | |
---|
1234 | n/a | def test_recursive_list(self): |
---|
1235 | n/a | l = [] |
---|
1236 | n/a | l.append(l) |
---|
1237 | n/a | for proto in protocols: |
---|
1238 | n/a | s = self.dumps(l, proto) |
---|
1239 | n/a | x = self.loads(s) |
---|
1240 | n/a | self.assertIsInstance(x, list) |
---|
1241 | n/a | self.assertEqual(len(x), 1) |
---|
1242 | n/a | self.assertIs(x[0], x) |
---|
1243 | n/a | |
---|
1244 | n/a | def test_recursive_tuple_and_list(self): |
---|
1245 | n/a | t = ([],) |
---|
1246 | n/a | t[0].append(t) |
---|
1247 | n/a | for proto in protocols: |
---|
1248 | n/a | s = self.dumps(t, proto) |
---|
1249 | n/a | x = self.loads(s) |
---|
1250 | n/a | self.assertIsInstance(x, tuple) |
---|
1251 | n/a | self.assertEqual(len(x), 1) |
---|
1252 | n/a | self.assertIsInstance(x[0], list) |
---|
1253 | n/a | self.assertEqual(len(x[0]), 1) |
---|
1254 | n/a | self.assertIs(x[0][0], x) |
---|
1255 | n/a | |
---|
1256 | n/a | def test_recursive_dict(self): |
---|
1257 | n/a | d = {} |
---|
1258 | n/a | d[1] = d |
---|
1259 | n/a | for proto in protocols: |
---|
1260 | n/a | s = self.dumps(d, proto) |
---|
1261 | n/a | x = self.loads(s) |
---|
1262 | n/a | self.assertIsInstance(x, dict) |
---|
1263 | n/a | self.assertEqual(list(x.keys()), [1]) |
---|
1264 | n/a | self.assertIs(x[1], x) |
---|
1265 | n/a | |
---|
1266 | n/a | def test_recursive_dict_key(self): |
---|
1267 | n/a | d = {} |
---|
1268 | n/a | k = K(d) |
---|
1269 | n/a | d[k] = 1 |
---|
1270 | n/a | for proto in protocols: |
---|
1271 | n/a | s = self.dumps(d, proto) |
---|
1272 | n/a | x = self.loads(s) |
---|
1273 | n/a | self.assertIsInstance(x, dict) |
---|
1274 | n/a | self.assertEqual(len(x.keys()), 1) |
---|
1275 | n/a | self.assertIsInstance(list(x.keys())[0], K) |
---|
1276 | n/a | self.assertIs(list(x.keys())[0].value, x) |
---|
1277 | n/a | |
---|
1278 | n/a | def test_recursive_set(self): |
---|
1279 | n/a | y = set() |
---|
1280 | n/a | k = K(y) |
---|
1281 | n/a | y.add(k) |
---|
1282 | n/a | for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
---|
1283 | n/a | s = self.dumps(y, proto) |
---|
1284 | n/a | x = self.loads(s) |
---|
1285 | n/a | self.assertIsInstance(x, set) |
---|
1286 | n/a | self.assertEqual(len(x), 1) |
---|
1287 | n/a | self.assertIsInstance(list(x)[0], K) |
---|
1288 | n/a | self.assertIs(list(x)[0].value, x) |
---|
1289 | n/a | |
---|
1290 | n/a | def test_recursive_list_subclass(self): |
---|
1291 | n/a | y = MyList() |
---|
1292 | n/a | y.append(y) |
---|
1293 | n/a | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
---|
1294 | n/a | s = self.dumps(y, proto) |
---|
1295 | n/a | x = self.loads(s) |
---|
1296 | n/a | self.assertIsInstance(x, MyList) |
---|
1297 | n/a | self.assertEqual(len(x), 1) |
---|
1298 | n/a | self.assertIs(x[0], x) |
---|
1299 | n/a | |
---|
1300 | n/a | def test_recursive_dict_subclass(self): |
---|
1301 | n/a | d = MyDict() |
---|
1302 | n/a | d[1] = d |
---|
1303 | n/a | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
---|
1304 | n/a | s = self.dumps(d, proto) |
---|
1305 | n/a | x = self.loads(s) |
---|
1306 | n/a | self.assertIsInstance(x, MyDict) |
---|
1307 | n/a | self.assertEqual(list(x.keys()), [1]) |
---|
1308 | n/a | self.assertIs(x[1], x) |
---|
1309 | n/a | |
---|
1310 | n/a | def test_recursive_dict_subclass_key(self): |
---|
1311 | n/a | d = MyDict() |
---|
1312 | n/a | k = K(d) |
---|
1313 | n/a | d[k] = 1 |
---|
1314 | n/a | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
---|
1315 | n/a | s = self.dumps(d, proto) |
---|
1316 | n/a | x = self.loads(s) |
---|
1317 | n/a | self.assertIsInstance(x, MyDict) |
---|
1318 | n/a | self.assertEqual(len(list(x.keys())), 1) |
---|
1319 | n/a | self.assertIsInstance(list(x.keys())[0], K) |
---|
1320 | n/a | self.assertIs(list(x.keys())[0].value, x) |
---|
1321 | n/a | |
---|
1322 | n/a | def test_recursive_inst(self): |
---|
1323 | n/a | i = C() |
---|
1324 | n/a | i.attr = i |
---|
1325 | n/a | for proto in protocols: |
---|
1326 | n/a | s = self.dumps(i, proto) |
---|
1327 | n/a | x = self.loads(s) |
---|
1328 | n/a | self.assertIsInstance(x, C) |
---|
1329 | n/a | self.assertEqual(dir(x), dir(i)) |
---|
1330 | n/a | self.assertIs(x.attr, x) |
---|
1331 | n/a | |
---|
1332 | n/a | def test_recursive_multi(self): |
---|
1333 | n/a | l = [] |
---|
1334 | n/a | d = {1:l} |
---|
1335 | n/a | i = C() |
---|
1336 | n/a | i.attr = d |
---|
1337 | n/a | l.append(i) |
---|
1338 | n/a | for proto in protocols: |
---|
1339 | n/a | s = self.dumps(l, proto) |
---|
1340 | n/a | x = self.loads(s) |
---|
1341 | n/a | self.assertIsInstance(x, list) |
---|
1342 | n/a | self.assertEqual(len(x), 1) |
---|
1343 | n/a | self.assertEqual(dir(x[0]), dir(i)) |
---|
1344 | n/a | self.assertEqual(list(x[0].attr.keys()), [1]) |
---|
1345 | n/a | self.assertTrue(x[0].attr[1] is x) |
---|
1346 | n/a | |
---|
1347 | n/a | def check_recursive_collection_and_inst(self, factory): |
---|
1348 | n/a | h = H() |
---|
1349 | n/a | y = factory([h]) |
---|
1350 | n/a | h.attr = y |
---|
1351 | n/a | for proto in protocols: |
---|
1352 | n/a | s = self.dumps(y, proto) |
---|
1353 | n/a | x = self.loads(s) |
---|
1354 | n/a | self.assertIsInstance(x, type(y)) |
---|
1355 | n/a | self.assertEqual(len(x), 1) |
---|
1356 | n/a | self.assertIsInstance(list(x)[0], H) |
---|
1357 | n/a | self.assertIs(list(x)[0].attr, x) |
---|
1358 | n/a | |
---|
1359 | n/a | def test_recursive_list_and_inst(self): |
---|
1360 | n/a | self.check_recursive_collection_and_inst(list) |
---|
1361 | n/a | |
---|
1362 | n/a | def test_recursive_tuple_and_inst(self): |
---|
1363 | n/a | self.check_recursive_collection_and_inst(tuple) |
---|
1364 | n/a | |
---|
1365 | n/a | def test_recursive_dict_and_inst(self): |
---|
1366 | n/a | self.check_recursive_collection_and_inst(dict.fromkeys) |
---|
1367 | n/a | |
---|
1368 | n/a | def test_recursive_set_and_inst(self): |
---|
1369 | n/a | self.check_recursive_collection_and_inst(set) |
---|
1370 | n/a | |
---|
1371 | n/a | def test_recursive_frozenset_and_inst(self): |
---|
1372 | n/a | self.check_recursive_collection_and_inst(frozenset) |
---|
1373 | n/a | |
---|
1374 | n/a | def test_recursive_list_subclass_and_inst(self): |
---|
1375 | n/a | self.check_recursive_collection_and_inst(MyList) |
---|
1376 | n/a | |
---|
1377 | n/a | def test_recursive_tuple_subclass_and_inst(self): |
---|
1378 | n/a | self.check_recursive_collection_and_inst(MyTuple) |
---|
1379 | n/a | |
---|
1380 | n/a | def test_recursive_dict_subclass_and_inst(self): |
---|
1381 | n/a | self.check_recursive_collection_and_inst(MyDict.fromkeys) |
---|
1382 | n/a | |
---|
1383 | n/a | def test_recursive_set_subclass_and_inst(self): |
---|
1384 | n/a | self.check_recursive_collection_and_inst(MySet) |
---|
1385 | n/a | |
---|
1386 | n/a | def test_recursive_frozenset_subclass_and_inst(self): |
---|
1387 | n/a | self.check_recursive_collection_and_inst(MyFrozenSet) |
---|
1388 | n/a | |
---|
1389 | n/a | def test_unicode(self): |
---|
1390 | n/a | endcases = ['', '<\\u>', '<\\\u1234>', '<\n>', |
---|
1391 | n/a | '<\\>', '<\\\U00012345>', |
---|
1392 | n/a | # surrogates |
---|
1393 | n/a | '<\udc80>'] |
---|
1394 | n/a | for proto in protocols: |
---|
1395 | n/a | for u in endcases: |
---|
1396 | n/a | p = self.dumps(u, proto) |
---|
1397 | n/a | u2 = self.loads(p) |
---|
1398 | n/a | self.assert_is_copy(u, u2) |
---|
1399 | n/a | |
---|
1400 | n/a | def test_unicode_high_plane(self): |
---|
1401 | n/a | t = '\U00012345' |
---|
1402 | n/a | for proto in protocols: |
---|
1403 | n/a | p = self.dumps(t, proto) |
---|
1404 | n/a | t2 = self.loads(p) |
---|
1405 | n/a | self.assert_is_copy(t, t2) |
---|
1406 | n/a | |
---|
1407 | n/a | def test_bytes(self): |
---|
1408 | n/a | for proto in protocols: |
---|
1409 | n/a | for s in b'', b'xyz', b'xyz'*100: |
---|
1410 | n/a | p = self.dumps(s, proto) |
---|
1411 | n/a | self.assert_is_copy(s, self.loads(p)) |
---|
1412 | n/a | for s in [bytes([i]) for i in range(256)]: |
---|
1413 | n/a | p = self.dumps(s, proto) |
---|
1414 | n/a | self.assert_is_copy(s, self.loads(p)) |
---|
1415 | n/a | for s in [bytes([i, i]) for i in range(256)]: |
---|
1416 | n/a | p = self.dumps(s, proto) |
---|
1417 | n/a | self.assert_is_copy(s, self.loads(p)) |
---|
1418 | n/a | |
---|
1419 | n/a | def test_ints(self): |
---|
1420 | n/a | for proto in protocols: |
---|
1421 | n/a | n = sys.maxsize |
---|
1422 | n/a | while n: |
---|
1423 | n/a | for expected in (-n, n): |
---|
1424 | n/a | s = self.dumps(expected, proto) |
---|
1425 | n/a | n2 = self.loads(s) |
---|
1426 | n/a | self.assert_is_copy(expected, n2) |
---|
1427 | n/a | n = n >> 1 |
---|
1428 | n/a | |
---|
1429 | n/a | def test_long(self): |
---|
1430 | n/a | for proto in protocols: |
---|
1431 | n/a | # 256 bytes is where LONG4 begins. |
---|
1432 | n/a | for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
---|
1433 | n/a | nbase = 1 << nbits |
---|
1434 | n/a | for npos in nbase-1, nbase, nbase+1: |
---|
1435 | n/a | for n in npos, -npos: |
---|
1436 | n/a | pickle = self.dumps(n, proto) |
---|
1437 | n/a | got = self.loads(pickle) |
---|
1438 | n/a | self.assert_is_copy(n, got) |
---|
1439 | n/a | # Try a monster. This is quadratic-time in protos 0 & 1, so don't |
---|
1440 | n/a | # bother with those. |
---|
1441 | n/a | nbase = int("deadbeeffeedface", 16) |
---|
1442 | n/a | nbase += nbase << 1000000 |
---|
1443 | n/a | for n in nbase, -nbase: |
---|
1444 | n/a | p = self.dumps(n, 2) |
---|
1445 | n/a | got = self.loads(p) |
---|
1446 | n/a | # assert_is_copy is very expensive here as it precomputes |
---|
1447 | n/a | # a failure message by computing the repr() of n and got, |
---|
1448 | n/a | # we just do the check ourselves. |
---|
1449 | n/a | self.assertIs(type(got), int) |
---|
1450 | n/a | self.assertEqual(n, got) |
---|
1451 | n/a | |
---|
1452 | n/a | def test_float(self): |
---|
1453 | n/a | test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5, |
---|
1454 | n/a | 3.14, 263.44582062374053, 6.022e23, 1e30] |
---|
1455 | n/a | test_values = test_values + [-x for x in test_values] |
---|
1456 | n/a | for proto in protocols: |
---|
1457 | n/a | for value in test_values: |
---|
1458 | n/a | pickle = self.dumps(value, proto) |
---|
1459 | n/a | got = self.loads(pickle) |
---|
1460 | n/a | self.assert_is_copy(value, got) |
---|
1461 | n/a | |
---|
1462 | n/a | @run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
---|
1463 | n/a | def test_float_format(self): |
---|
1464 | n/a | # make sure that floats are formatted locale independent with proto 0 |
---|
1465 | n/a | self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.') |
---|
1466 | n/a | |
---|
1467 | n/a | def test_reduce(self): |
---|
1468 | n/a | for proto in protocols: |
---|
1469 | n/a | inst = AAA() |
---|
1470 | n/a | dumped = self.dumps(inst, proto) |
---|
1471 | n/a | loaded = self.loads(dumped) |
---|
1472 | n/a | self.assertEqual(loaded, REDUCE_A) |
---|
1473 | n/a | |
---|
1474 | n/a | def test_getinitargs(self): |
---|
1475 | n/a | for proto in protocols: |
---|
1476 | n/a | inst = initarg(1, 2) |
---|
1477 | n/a | dumped = self.dumps(inst, proto) |
---|
1478 | n/a | loaded = self.loads(dumped) |
---|
1479 | n/a | self.assert_is_copy(inst, loaded) |
---|
1480 | n/a | |
---|
1481 | n/a | def test_metaclass(self): |
---|
1482 | n/a | a = use_metaclass() |
---|
1483 | n/a | for proto in protocols: |
---|
1484 | n/a | s = self.dumps(a, proto) |
---|
1485 | n/a | b = self.loads(s) |
---|
1486 | n/a | self.assertEqual(a.__class__, b.__class__) |
---|
1487 | n/a | |
---|
1488 | n/a | def test_dynamic_class(self): |
---|
1489 | n/a | a = create_dynamic_class("my_dynamic_class", (object,)) |
---|
1490 | n/a | copyreg.pickle(pickling_metaclass, pickling_metaclass.__reduce__) |
---|
1491 | n/a | for proto in protocols: |
---|
1492 | n/a | s = self.dumps(a, proto) |
---|
1493 | n/a | b = self.loads(s) |
---|
1494 | n/a | self.assertEqual(a, b) |
---|
1495 | n/a | self.assertIs(type(a), type(b)) |
---|
1496 | n/a | |
---|
1497 | n/a | def test_structseq(self): |
---|
1498 | n/a | import time |
---|
1499 | n/a | import os |
---|
1500 | n/a | |
---|
1501 | n/a | t = time.localtime() |
---|
1502 | n/a | for proto in protocols: |
---|
1503 | n/a | s = self.dumps(t, proto) |
---|
1504 | n/a | u = self.loads(s) |
---|
1505 | n/a | self.assert_is_copy(t, u) |
---|
1506 | n/a | if hasattr(os, "stat"): |
---|
1507 | n/a | t = os.stat(os.curdir) |
---|
1508 | n/a | s = self.dumps(t, proto) |
---|
1509 | n/a | u = self.loads(s) |
---|
1510 | n/a | self.assert_is_copy(t, u) |
---|
1511 | n/a | if hasattr(os, "statvfs"): |
---|
1512 | n/a | t = os.statvfs(os.curdir) |
---|
1513 | n/a | s = self.dumps(t, proto) |
---|
1514 | n/a | u = self.loads(s) |
---|
1515 | n/a | self.assert_is_copy(t, u) |
---|
1516 | n/a | |
---|
1517 | n/a | def test_ellipsis(self): |
---|
1518 | n/a | for proto in protocols: |
---|
1519 | n/a | s = self.dumps(..., proto) |
---|
1520 | n/a | u = self.loads(s) |
---|
1521 | n/a | self.assertIs(..., u) |
---|
1522 | n/a | |
---|
1523 | n/a | def test_notimplemented(self): |
---|
1524 | n/a | for proto in protocols: |
---|
1525 | n/a | s = self.dumps(NotImplemented, proto) |
---|
1526 | n/a | u = self.loads(s) |
---|
1527 | n/a | self.assertIs(NotImplemented, u) |
---|
1528 | n/a | |
---|
1529 | n/a | def test_singleton_types(self): |
---|
1530 | n/a | # Issue #6477: Test that types of built-in singletons can be pickled. |
---|
1531 | n/a | singletons = [None, ..., NotImplemented] |
---|
1532 | n/a | for singleton in singletons: |
---|
1533 | n/a | for proto in protocols: |
---|
1534 | n/a | s = self.dumps(type(singleton), proto) |
---|
1535 | n/a | u = self.loads(s) |
---|
1536 | n/a | self.assertIs(type(singleton), u) |
---|
1537 | n/a | |
---|
1538 | n/a | # Tests for protocol 2 |
---|
1539 | n/a | |
---|
1540 | n/a | def test_proto(self): |
---|
1541 | n/a | for proto in protocols: |
---|
1542 | n/a | pickled = self.dumps(None, proto) |
---|
1543 | n/a | if proto >= 2: |
---|
1544 | n/a | proto_header = pickle.PROTO + bytes([proto]) |
---|
1545 | n/a | self.assertTrue(pickled.startswith(proto_header)) |
---|
1546 | n/a | else: |
---|
1547 | n/a | self.assertEqual(count_opcode(pickle.PROTO, pickled), 0) |
---|
1548 | n/a | |
---|
1549 | n/a | oob = protocols[-1] + 1 # a future protocol |
---|
1550 | n/a | build_none = pickle.NONE + pickle.STOP |
---|
1551 | n/a | badpickle = pickle.PROTO + bytes([oob]) + build_none |
---|
1552 | n/a | try: |
---|
1553 | n/a | self.loads(badpickle) |
---|
1554 | n/a | except ValueError as err: |
---|
1555 | n/a | self.assertIn("unsupported pickle protocol", str(err)) |
---|
1556 | n/a | else: |
---|
1557 | n/a | self.fail("expected bad protocol number to raise ValueError") |
---|
1558 | n/a | |
---|
1559 | n/a | def test_long1(self): |
---|
1560 | n/a | x = 12345678910111213141516178920 |
---|
1561 | n/a | for proto in protocols: |
---|
1562 | n/a | s = self.dumps(x, proto) |
---|
1563 | n/a | y = self.loads(s) |
---|
1564 | n/a | self.assert_is_copy(x, y) |
---|
1565 | n/a | self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2) |
---|
1566 | n/a | |
---|
1567 | n/a | def test_long4(self): |
---|
1568 | n/a | x = 12345678910111213141516178920 << (256*8) |
---|
1569 | n/a | for proto in protocols: |
---|
1570 | n/a | s = self.dumps(x, proto) |
---|
1571 | n/a | y = self.loads(s) |
---|
1572 | n/a | self.assert_is_copy(x, y) |
---|
1573 | n/a | self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2) |
---|
1574 | n/a | |
---|
1575 | n/a | def test_short_tuples(self): |
---|
1576 | n/a | # Map (proto, len(tuple)) to expected opcode. |
---|
1577 | n/a | expected_opcode = {(0, 0): pickle.TUPLE, |
---|
1578 | n/a | (0, 1): pickle.TUPLE, |
---|
1579 | n/a | (0, 2): pickle.TUPLE, |
---|
1580 | n/a | (0, 3): pickle.TUPLE, |
---|
1581 | n/a | (0, 4): pickle.TUPLE, |
---|
1582 | n/a | |
---|
1583 | n/a | (1, 0): pickle.EMPTY_TUPLE, |
---|
1584 | n/a | (1, 1): pickle.TUPLE, |
---|
1585 | n/a | (1, 2): pickle.TUPLE, |
---|
1586 | n/a | (1, 3): pickle.TUPLE, |
---|
1587 | n/a | (1, 4): pickle.TUPLE, |
---|
1588 | n/a | |
---|
1589 | n/a | (2, 0): pickle.EMPTY_TUPLE, |
---|
1590 | n/a | (2, 1): pickle.TUPLE1, |
---|
1591 | n/a | (2, 2): pickle.TUPLE2, |
---|
1592 | n/a | (2, 3): pickle.TUPLE3, |
---|
1593 | n/a | (2, 4): pickle.TUPLE, |
---|
1594 | n/a | |
---|
1595 | n/a | (3, 0): pickle.EMPTY_TUPLE, |
---|
1596 | n/a | (3, 1): pickle.TUPLE1, |
---|
1597 | n/a | (3, 2): pickle.TUPLE2, |
---|
1598 | n/a | (3, 3): pickle.TUPLE3, |
---|
1599 | n/a | (3, 4): pickle.TUPLE, |
---|
1600 | n/a | } |
---|
1601 | n/a | a = () |
---|
1602 | n/a | b = (1,) |
---|
1603 | n/a | c = (1, 2) |
---|
1604 | n/a | d = (1, 2, 3) |
---|
1605 | n/a | e = (1, 2, 3, 4) |
---|
1606 | n/a | for proto in protocols: |
---|
1607 | n/a | for x in a, b, c, d, e: |
---|
1608 | n/a | s = self.dumps(x, proto) |
---|
1609 | n/a | y = self.loads(s) |
---|
1610 | n/a | self.assert_is_copy(x, y) |
---|
1611 | n/a | expected = expected_opcode[min(proto, 3), len(x)] |
---|
1612 | n/a | self.assertTrue(opcode_in_pickle(expected, s)) |
---|
1613 | n/a | |
---|
1614 | n/a | def test_singletons(self): |
---|
1615 | n/a | # Map (proto, singleton) to expected opcode. |
---|
1616 | n/a | expected_opcode = {(0, None): pickle.NONE, |
---|
1617 | n/a | (1, None): pickle.NONE, |
---|
1618 | n/a | (2, None): pickle.NONE, |
---|
1619 | n/a | (3, None): pickle.NONE, |
---|
1620 | n/a | |
---|
1621 | n/a | (0, True): pickle.INT, |
---|
1622 | n/a | (1, True): pickle.INT, |
---|
1623 | n/a | (2, True): pickle.NEWTRUE, |
---|
1624 | n/a | (3, True): pickle.NEWTRUE, |
---|
1625 | n/a | |
---|
1626 | n/a | (0, False): pickle.INT, |
---|
1627 | n/a | (1, False): pickle.INT, |
---|
1628 | n/a | (2, False): pickle.NEWFALSE, |
---|
1629 | n/a | (3, False): pickle.NEWFALSE, |
---|
1630 | n/a | } |
---|
1631 | n/a | for proto in protocols: |
---|
1632 | n/a | for x in None, False, True: |
---|
1633 | n/a | s = self.dumps(x, proto) |
---|
1634 | n/a | y = self.loads(s) |
---|
1635 | n/a | self.assertTrue(x is y, (proto, x, s, y)) |
---|
1636 | n/a | expected = expected_opcode[min(proto, 3), x] |
---|
1637 | n/a | self.assertTrue(opcode_in_pickle(expected, s)) |
---|
1638 | n/a | |
---|
1639 | n/a | def test_newobj_tuple(self): |
---|
1640 | n/a | x = MyTuple([1, 2, 3]) |
---|
1641 | n/a | x.foo = 42 |
---|
1642 | n/a | x.bar = "hello" |
---|
1643 | n/a | for proto in protocols: |
---|
1644 | n/a | s = self.dumps(x, proto) |
---|
1645 | n/a | y = self.loads(s) |
---|
1646 | n/a | self.assert_is_copy(x, y) |
---|
1647 | n/a | |
---|
1648 | n/a | def test_newobj_list(self): |
---|
1649 | n/a | x = MyList([1, 2, 3]) |
---|
1650 | n/a | x.foo = 42 |
---|
1651 | n/a | x.bar = "hello" |
---|
1652 | n/a | for proto in protocols: |
---|
1653 | n/a | s = self.dumps(x, proto) |
---|
1654 | n/a | y = self.loads(s) |
---|
1655 | n/a | self.assert_is_copy(x, y) |
---|
1656 | n/a | |
---|
1657 | n/a | def test_newobj_generic(self): |
---|
1658 | n/a | for proto in protocols: |
---|
1659 | n/a | for C in myclasses: |
---|
1660 | n/a | B = C.__base__ |
---|
1661 | n/a | x = C(C.sample) |
---|
1662 | n/a | x.foo = 42 |
---|
1663 | n/a | s = self.dumps(x, proto) |
---|
1664 | n/a | y = self.loads(s) |
---|
1665 | n/a | detail = (proto, C, B, x, y, type(y)) |
---|
1666 | n/a | self.assert_is_copy(x, y) # XXX revisit |
---|
1667 | n/a | self.assertEqual(B(x), B(y), detail) |
---|
1668 | n/a | self.assertEqual(x.__dict__, y.__dict__, detail) |
---|
1669 | n/a | |
---|
1670 | n/a | def test_newobj_proxies(self): |
---|
1671 | n/a | # NEWOBJ should use the __class__ rather than the raw type |
---|
1672 | n/a | classes = myclasses[:] |
---|
1673 | n/a | # Cannot create weakproxies to these classes |
---|
1674 | n/a | for c in (MyInt, MyTuple): |
---|
1675 | n/a | classes.remove(c) |
---|
1676 | n/a | for proto in protocols: |
---|
1677 | n/a | for C in classes: |
---|
1678 | n/a | B = C.__base__ |
---|
1679 | n/a | x = C(C.sample) |
---|
1680 | n/a | x.foo = 42 |
---|
1681 | n/a | p = weakref.proxy(x) |
---|
1682 | n/a | s = self.dumps(p, proto) |
---|
1683 | n/a | y = self.loads(s) |
---|
1684 | n/a | self.assertEqual(type(y), type(x)) # rather than type(p) |
---|
1685 | n/a | detail = (proto, C, B, x, y, type(y)) |
---|
1686 | n/a | self.assertEqual(B(x), B(y), detail) |
---|
1687 | n/a | self.assertEqual(x.__dict__, y.__dict__, detail) |
---|
1688 | n/a | |
---|
1689 | n/a | def test_newobj_not_class(self): |
---|
1690 | n/a | # Issue 24552 |
---|
1691 | n/a | global SimpleNewObj |
---|
1692 | n/a | save = SimpleNewObj |
---|
1693 | n/a | o = SimpleNewObj.__new__(SimpleNewObj) |
---|
1694 | n/a | b = self.dumps(o, 4) |
---|
1695 | n/a | try: |
---|
1696 | n/a | SimpleNewObj = 42 |
---|
1697 | n/a | self.assertRaises((TypeError, pickle.UnpicklingError), self.loads, b) |
---|
1698 | n/a | finally: |
---|
1699 | n/a | SimpleNewObj = save |
---|
1700 | n/a | |
---|
1701 | n/a | # Register a type with copyreg, with extension code extcode. Pickle |
---|
1702 | n/a | # an object of that type. Check that the resulting pickle uses opcode |
---|
1703 | n/a | # (EXT[124]) under proto 2, and not in proto 1. |
---|
1704 | n/a | |
---|
1705 | n/a | def produce_global_ext(self, extcode, opcode): |
---|
1706 | n/a | e = ExtensionSaver(extcode) |
---|
1707 | n/a | try: |
---|
1708 | n/a | copyreg.add_extension(__name__, "MyList", extcode) |
---|
1709 | n/a | x = MyList([1, 2, 3]) |
---|
1710 | n/a | x.foo = 42 |
---|
1711 | n/a | x.bar = "hello" |
---|
1712 | n/a | |
---|
1713 | n/a | # Dump using protocol 1 for comparison. |
---|
1714 | n/a | s1 = self.dumps(x, 1) |
---|
1715 | n/a | self.assertIn(__name__.encode("utf-8"), s1) |
---|
1716 | n/a | self.assertIn(b"MyList", s1) |
---|
1717 | n/a | self.assertFalse(opcode_in_pickle(opcode, s1)) |
---|
1718 | n/a | |
---|
1719 | n/a | y = self.loads(s1) |
---|
1720 | n/a | self.assert_is_copy(x, y) |
---|
1721 | n/a | |
---|
1722 | n/a | # Dump using protocol 2 for test. |
---|
1723 | n/a | s2 = self.dumps(x, 2) |
---|
1724 | n/a | self.assertNotIn(__name__.encode("utf-8"), s2) |
---|
1725 | n/a | self.assertNotIn(b"MyList", s2) |
---|
1726 | n/a | self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2)) |
---|
1727 | n/a | |
---|
1728 | n/a | y = self.loads(s2) |
---|
1729 | n/a | self.assert_is_copy(x, y) |
---|
1730 | n/a | finally: |
---|
1731 | n/a | e.restore() |
---|
1732 | n/a | |
---|
1733 | n/a | def test_global_ext1(self): |
---|
1734 | n/a | self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code |
---|
1735 | n/a | self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code |
---|
1736 | n/a | |
---|
1737 | n/a | def test_global_ext2(self): |
---|
1738 | n/a | self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code |
---|
1739 | n/a | self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code |
---|
1740 | n/a | self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness |
---|
1741 | n/a | |
---|
1742 | n/a | def test_global_ext4(self): |
---|
1743 | n/a | self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code |
---|
1744 | n/a | self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code |
---|
1745 | n/a | self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness |
---|
1746 | n/a | |
---|
1747 | n/a | def test_list_chunking(self): |
---|
1748 | n/a | n = 10 # too small to chunk |
---|
1749 | n/a | x = list(range(n)) |
---|
1750 | n/a | for proto in protocols: |
---|
1751 | n/a | s = self.dumps(x, proto) |
---|
1752 | n/a | y = self.loads(s) |
---|
1753 | n/a | self.assert_is_copy(x, y) |
---|
1754 | n/a | num_appends = count_opcode(pickle.APPENDS, s) |
---|
1755 | n/a | self.assertEqual(num_appends, proto > 0) |
---|
1756 | n/a | |
---|
1757 | n/a | n = 2500 # expect at least two chunks when proto > 0 |
---|
1758 | n/a | x = list(range(n)) |
---|
1759 | n/a | for proto in protocols: |
---|
1760 | n/a | s = self.dumps(x, proto) |
---|
1761 | n/a | y = self.loads(s) |
---|
1762 | n/a | self.assert_is_copy(x, y) |
---|
1763 | n/a | num_appends = count_opcode(pickle.APPENDS, s) |
---|
1764 | n/a | if proto == 0: |
---|
1765 | n/a | self.assertEqual(num_appends, 0) |
---|
1766 | n/a | else: |
---|
1767 | n/a | self.assertTrue(num_appends >= 2) |
---|
1768 | n/a | |
---|
1769 | n/a | def test_dict_chunking(self): |
---|
1770 | n/a | n = 10 # too small to chunk |
---|
1771 | n/a | x = dict.fromkeys(range(n)) |
---|
1772 | n/a | for proto in protocols: |
---|
1773 | n/a | s = self.dumps(x, proto) |
---|
1774 | n/a | self.assertIsInstance(s, bytes_types) |
---|
1775 | n/a | y = self.loads(s) |
---|
1776 | n/a | self.assert_is_copy(x, y) |
---|
1777 | n/a | num_setitems = count_opcode(pickle.SETITEMS, s) |
---|
1778 | n/a | self.assertEqual(num_setitems, proto > 0) |
---|
1779 | n/a | |
---|
1780 | n/a | n = 2500 # expect at least two chunks when proto > 0 |
---|
1781 | n/a | x = dict.fromkeys(range(n)) |
---|
1782 | n/a | for proto in protocols: |
---|
1783 | n/a | s = self.dumps(x, proto) |
---|
1784 | n/a | y = self.loads(s) |
---|
1785 | n/a | self.assert_is_copy(x, y) |
---|
1786 | n/a | num_setitems = count_opcode(pickle.SETITEMS, s) |
---|
1787 | n/a | if proto == 0: |
---|
1788 | n/a | self.assertEqual(num_setitems, 0) |
---|
1789 | n/a | else: |
---|
1790 | n/a | self.assertTrue(num_setitems >= 2) |
---|
1791 | n/a | |
---|
1792 | n/a | def test_set_chunking(self): |
---|
1793 | n/a | n = 10 # too small to chunk |
---|
1794 | n/a | x = set(range(n)) |
---|
1795 | n/a | for proto in protocols: |
---|
1796 | n/a | s = self.dumps(x, proto) |
---|
1797 | n/a | y = self.loads(s) |
---|
1798 | n/a | self.assert_is_copy(x, y) |
---|
1799 | n/a | num_additems = count_opcode(pickle.ADDITEMS, s) |
---|
1800 | n/a | if proto < 4: |
---|
1801 | n/a | self.assertEqual(num_additems, 0) |
---|
1802 | n/a | else: |
---|
1803 | n/a | self.assertEqual(num_additems, 1) |
---|
1804 | n/a | |
---|
1805 | n/a | n = 2500 # expect at least two chunks when proto >= 4 |
---|
1806 | n/a | x = set(range(n)) |
---|
1807 | n/a | for proto in protocols: |
---|
1808 | n/a | s = self.dumps(x, proto) |
---|
1809 | n/a | y = self.loads(s) |
---|
1810 | n/a | self.assert_is_copy(x, y) |
---|
1811 | n/a | num_additems = count_opcode(pickle.ADDITEMS, s) |
---|
1812 | n/a | if proto < 4: |
---|
1813 | n/a | self.assertEqual(num_additems, 0) |
---|
1814 | n/a | else: |
---|
1815 | n/a | self.assertGreaterEqual(num_additems, 2) |
---|
1816 | n/a | |
---|
1817 | n/a | def test_simple_newobj(self): |
---|
1818 | n/a | x = SimpleNewObj.__new__(SimpleNewObj, 0xface) # avoid __init__ |
---|
1819 | n/a | x.abc = 666 |
---|
1820 | n/a | for proto in protocols: |
---|
1821 | n/a | with self.subTest(proto=proto): |
---|
1822 | n/a | s = self.dumps(x, proto) |
---|
1823 | n/a | if proto < 1: |
---|
1824 | n/a | self.assertIn(b'\nL64206', s) # LONG |
---|
1825 | n/a | else: |
---|
1826 | n/a | self.assertIn(b'M\xce\xfa', s) # BININT2 |
---|
1827 | n/a | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), |
---|
1828 | n/a | 2 <= proto) |
---|
1829 | n/a | self.assertFalse(opcode_in_pickle(pickle.NEWOBJ_EX, s)) |
---|
1830 | n/a | y = self.loads(s) # will raise TypeError if __init__ called |
---|
1831 | n/a | self.assert_is_copy(x, y) |
---|
1832 | n/a | |
---|
1833 | n/a | def test_complex_newobj(self): |
---|
1834 | n/a | x = ComplexNewObj.__new__(ComplexNewObj, 0xface) # avoid __init__ |
---|
1835 | n/a | x.abc = 666 |
---|
1836 | n/a | for proto in protocols: |
---|
1837 | n/a | with self.subTest(proto=proto): |
---|
1838 | n/a | s = self.dumps(x, proto) |
---|
1839 | n/a | if proto < 1: |
---|
1840 | n/a | self.assertIn(b'\nL64206', s) # LONG |
---|
1841 | n/a | elif proto < 2: |
---|
1842 | n/a | self.assertIn(b'M\xce\xfa', s) # BININT2 |
---|
1843 | n/a | elif proto < 4: |
---|
1844 | n/a | self.assertIn(b'X\x04\x00\x00\x00FACE', s) # BINUNICODE |
---|
1845 | n/a | else: |
---|
1846 | n/a | self.assertIn(b'\x8c\x04FACE', s) # SHORT_BINUNICODE |
---|
1847 | n/a | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), |
---|
1848 | n/a | 2 <= proto) |
---|
1849 | n/a | self.assertFalse(opcode_in_pickle(pickle.NEWOBJ_EX, s)) |
---|
1850 | n/a | y = self.loads(s) # will raise TypeError if __init__ called |
---|
1851 | n/a | self.assert_is_copy(x, y) |
---|
1852 | n/a | |
---|
1853 | n/a | def test_complex_newobj_ex(self): |
---|
1854 | n/a | x = ComplexNewObjEx.__new__(ComplexNewObjEx, 0xface) # avoid __init__ |
---|
1855 | n/a | x.abc = 666 |
---|
1856 | n/a | for proto in protocols: |
---|
1857 | n/a | with self.subTest(proto=proto): |
---|
1858 | n/a | s = self.dumps(x, proto) |
---|
1859 | n/a | if proto < 1: |
---|
1860 | n/a | self.assertIn(b'\nL64206', s) # LONG |
---|
1861 | n/a | elif proto < 2: |
---|
1862 | n/a | self.assertIn(b'M\xce\xfa', s) # BININT2 |
---|
1863 | n/a | elif proto < 4: |
---|
1864 | n/a | self.assertIn(b'X\x04\x00\x00\x00FACE', s) # BINUNICODE |
---|
1865 | n/a | else: |
---|
1866 | n/a | self.assertIn(b'\x8c\x04FACE', s) # SHORT_BINUNICODE |
---|
1867 | n/a | self.assertFalse(opcode_in_pickle(pickle.NEWOBJ, s)) |
---|
1868 | n/a | self.assertEqual(opcode_in_pickle(pickle.NEWOBJ_EX, s), |
---|
1869 | n/a | 4 <= proto) |
---|
1870 | n/a | y = self.loads(s) # will raise TypeError if __init__ called |
---|
1871 | n/a | self.assert_is_copy(x, y) |
---|
1872 | n/a | |
---|
1873 | n/a | def test_newobj_list_slots(self): |
---|
1874 | n/a | x = SlotList([1, 2, 3]) |
---|
1875 | n/a | x.foo = 42 |
---|
1876 | n/a | x.bar = "hello" |
---|
1877 | n/a | s = self.dumps(x, 2) |
---|
1878 | n/a | y = self.loads(s) |
---|
1879 | n/a | self.assert_is_copy(x, y) |
---|
1880 | n/a | |
---|
1881 | n/a | def test_reduce_overrides_default_reduce_ex(self): |
---|
1882 | n/a | for proto in protocols: |
---|
1883 | n/a | x = REX_one() |
---|
1884 | n/a | self.assertEqual(x._reduce_called, 0) |
---|
1885 | n/a | s = self.dumps(x, proto) |
---|
1886 | n/a | self.assertEqual(x._reduce_called, 1) |
---|
1887 | n/a | y = self.loads(s) |
---|
1888 | n/a | self.assertEqual(y._reduce_called, 0) |
---|
1889 | n/a | |
---|
1890 | n/a | def test_reduce_ex_called(self): |
---|
1891 | n/a | for proto in protocols: |
---|
1892 | n/a | x = REX_two() |
---|
1893 | n/a | self.assertEqual(x._proto, None) |
---|
1894 | n/a | s = self.dumps(x, proto) |
---|
1895 | n/a | self.assertEqual(x._proto, proto) |
---|
1896 | n/a | y = self.loads(s) |
---|
1897 | n/a | self.assertEqual(y._proto, None) |
---|
1898 | n/a | |
---|
1899 | n/a | def test_reduce_ex_overrides_reduce(self): |
---|
1900 | n/a | for proto in protocols: |
---|
1901 | n/a | x = REX_three() |
---|
1902 | n/a | self.assertEqual(x._proto, None) |
---|
1903 | n/a | s = self.dumps(x, proto) |
---|
1904 | n/a | self.assertEqual(x._proto, proto) |
---|
1905 | n/a | y = self.loads(s) |
---|
1906 | n/a | self.assertEqual(y._proto, None) |
---|
1907 | n/a | |
---|
1908 | n/a | def test_reduce_ex_calls_base(self): |
---|
1909 | n/a | for proto in protocols: |
---|
1910 | n/a | x = REX_four() |
---|
1911 | n/a | self.assertEqual(x._proto, None) |
---|
1912 | n/a | s = self.dumps(x, proto) |
---|
1913 | n/a | self.assertEqual(x._proto, proto) |
---|
1914 | n/a | y = self.loads(s) |
---|
1915 | n/a | self.assertEqual(y._proto, proto) |
---|
1916 | n/a | |
---|
1917 | n/a | def test_reduce_calls_base(self): |
---|
1918 | n/a | for proto in protocols: |
---|
1919 | n/a | x = REX_five() |
---|
1920 | n/a | self.assertEqual(x._reduce_called, 0) |
---|
1921 | n/a | s = self.dumps(x, proto) |
---|
1922 | n/a | self.assertEqual(x._reduce_called, 1) |
---|
1923 | n/a | y = self.loads(s) |
---|
1924 | n/a | self.assertEqual(y._reduce_called, 1) |
---|
1925 | n/a | |
---|
1926 | n/a | @no_tracing |
---|
1927 | n/a | def test_bad_getattr(self): |
---|
1928 | n/a | # Issue #3514: crash when there is an infinite loop in __getattr__ |
---|
1929 | n/a | x = BadGetattr() |
---|
1930 | n/a | for proto in protocols: |
---|
1931 | n/a | self.assertRaises(RuntimeError, self.dumps, x, proto) |
---|
1932 | n/a | |
---|
1933 | n/a | def test_reduce_bad_iterator(self): |
---|
1934 | n/a | # Issue4176: crash when 4th and 5th items of __reduce__() |
---|
1935 | n/a | # are not iterators |
---|
1936 | n/a | class C(object): |
---|
1937 | n/a | def __reduce__(self): |
---|
1938 | n/a | # 4th item is not an iterator |
---|
1939 | n/a | return list, (), None, [], None |
---|
1940 | n/a | class D(object): |
---|
1941 | n/a | def __reduce__(self): |
---|
1942 | n/a | # 5th item is not an iterator |
---|
1943 | n/a | return dict, (), None, None, [] |
---|
1944 | n/a | |
---|
1945 | n/a | # Python implementation is less strict and also accepts iterables. |
---|
1946 | n/a | for proto in protocols: |
---|
1947 | n/a | try: |
---|
1948 | n/a | self.dumps(C(), proto) |
---|
1949 | n/a | except pickle.PicklingError: |
---|
1950 | n/a | pass |
---|
1951 | n/a | try: |
---|
1952 | n/a | self.dumps(D(), proto) |
---|
1953 | n/a | except pickle.PicklingError: |
---|
1954 | n/a | pass |
---|
1955 | n/a | |
---|
1956 | n/a | def test_many_puts_and_gets(self): |
---|
1957 | n/a | # Test that internal data structures correctly deal with lots of |
---|
1958 | n/a | # puts/gets. |
---|
1959 | n/a | keys = ("aaa" + str(i) for i in range(100)) |
---|
1960 | n/a | large_dict = dict((k, [4, 5, 6]) for k in keys) |
---|
1961 | n/a | obj = [dict(large_dict), dict(large_dict), dict(large_dict)] |
---|
1962 | n/a | |
---|
1963 | n/a | for proto in protocols: |
---|
1964 | n/a | with self.subTest(proto=proto): |
---|
1965 | n/a | dumped = self.dumps(obj, proto) |
---|
1966 | n/a | loaded = self.loads(dumped) |
---|
1967 | n/a | self.assert_is_copy(obj, loaded) |
---|
1968 | n/a | |
---|
1969 | n/a | def test_attribute_name_interning(self): |
---|
1970 | n/a | # Test that attribute names of pickled objects are interned when |
---|
1971 | n/a | # unpickling. |
---|
1972 | n/a | for proto in protocols: |
---|
1973 | n/a | x = C() |
---|
1974 | n/a | x.foo = 42 |
---|
1975 | n/a | x.bar = "hello" |
---|
1976 | n/a | s = self.dumps(x, proto) |
---|
1977 | n/a | y = self.loads(s) |
---|
1978 | n/a | x_keys = sorted(x.__dict__) |
---|
1979 | n/a | y_keys = sorted(y.__dict__) |
---|
1980 | n/a | for x_key, y_key in zip(x_keys, y_keys): |
---|
1981 | n/a | self.assertIs(x_key, y_key) |
---|
1982 | n/a | |
---|
1983 | n/a | def test_pickle_to_2x(self): |
---|
1984 | n/a | # Pickle non-trivial data with protocol 2, expecting that it yields |
---|
1985 | n/a | # the same result as Python 2.x did. |
---|
1986 | n/a | # NOTE: this test is a bit too strong since we can produce different |
---|
1987 | n/a | # bytecode that 2.x will still understand. |
---|
1988 | n/a | dumped = self.dumps(range(5), 2) |
---|
1989 | n/a | self.assertEqual(dumped, DATA_XRANGE) |
---|
1990 | n/a | dumped = self.dumps(set([3]), 2) |
---|
1991 | n/a | self.assertEqual(dumped, DATA_SET2) |
---|
1992 | n/a | |
---|
1993 | n/a | def test_large_pickles(self): |
---|
1994 | n/a | # Test the correctness of internal buffering routines when handling |
---|
1995 | n/a | # large data. |
---|
1996 | n/a | for proto in protocols: |
---|
1997 | n/a | data = (1, min, b'xy' * (30 * 1024), len) |
---|
1998 | n/a | dumped = self.dumps(data, proto) |
---|
1999 | n/a | loaded = self.loads(dumped) |
---|
2000 | n/a | self.assertEqual(len(loaded), len(data)) |
---|
2001 | n/a | self.assertEqual(loaded, data) |
---|
2002 | n/a | |
---|
2003 | n/a | def test_int_pickling_efficiency(self): |
---|
2004 | n/a | # Test compacity of int representation (see issue #12744) |
---|
2005 | n/a | for proto in protocols: |
---|
2006 | n/a | with self.subTest(proto=proto): |
---|
2007 | n/a | pickles = [self.dumps(2**n, proto) for n in range(70)] |
---|
2008 | n/a | sizes = list(map(len, pickles)) |
---|
2009 | n/a | # the size function is monotonic |
---|
2010 | n/a | self.assertEqual(sorted(sizes), sizes) |
---|
2011 | n/a | if proto >= 2: |
---|
2012 | n/a | for p in pickles: |
---|
2013 | n/a | self.assertFalse(opcode_in_pickle(pickle.LONG, p)) |
---|
2014 | n/a | |
---|
2015 | n/a | def _check_pickling_with_opcode(self, obj, opcode, proto): |
---|
2016 | n/a | pickled = self.dumps(obj, proto) |
---|
2017 | n/a | self.assertTrue(opcode_in_pickle(opcode, pickled)) |
---|
2018 | n/a | unpickled = self.loads(pickled) |
---|
2019 | n/a | self.assertEqual(obj, unpickled) |
---|
2020 | n/a | |
---|
2021 | n/a | def test_appends_on_non_lists(self): |
---|
2022 | n/a | # Issue #17720 |
---|
2023 | n/a | obj = REX_six([1, 2, 3]) |
---|
2024 | n/a | for proto in protocols: |
---|
2025 | n/a | if proto == 0: |
---|
2026 | n/a | self._check_pickling_with_opcode(obj, pickle.APPEND, proto) |
---|
2027 | n/a | else: |
---|
2028 | n/a | self._check_pickling_with_opcode(obj, pickle.APPENDS, proto) |
---|
2029 | n/a | |
---|
2030 | n/a | def test_setitems_on_non_dicts(self): |
---|
2031 | n/a | obj = REX_seven({1: -1, 2: -2, 3: -3}) |
---|
2032 | n/a | for proto in protocols: |
---|
2033 | n/a | if proto == 0: |
---|
2034 | n/a | self._check_pickling_with_opcode(obj, pickle.SETITEM, proto) |
---|
2035 | n/a | else: |
---|
2036 | n/a | self._check_pickling_with_opcode(obj, pickle.SETITEMS, proto) |
---|
2037 | n/a | |
---|
2038 | n/a | # Exercise framing (proto >= 4) for significant workloads |
---|
2039 | n/a | |
---|
2040 | n/a | FRAME_SIZE_TARGET = 64 * 1024 |
---|
2041 | n/a | |
---|
2042 | n/a | def check_frame_opcodes(self, pickled): |
---|
2043 | n/a | """ |
---|
2044 | n/a | Check the arguments of FRAME opcodes in a protocol 4+ pickle. |
---|
2045 | n/a | """ |
---|
2046 | n/a | frame_opcode_size = 9 |
---|
2047 | n/a | last_arg = last_pos = None |
---|
2048 | n/a | for op, arg, pos in pickletools.genops(pickled): |
---|
2049 | n/a | if op.name != 'FRAME': |
---|
2050 | n/a | continue |
---|
2051 | n/a | if last_pos is not None: |
---|
2052 | n/a | # The previous frame's size should be equal to the number |
---|
2053 | n/a | # of bytes up to the current frame. |
---|
2054 | n/a | frame_size = pos - last_pos - frame_opcode_size |
---|
2055 | n/a | self.assertEqual(frame_size, last_arg) |
---|
2056 | n/a | last_arg, last_pos = arg, pos |
---|
2057 | n/a | # The last frame's size should be equal to the number of bytes up |
---|
2058 | n/a | # to the pickle's end. |
---|
2059 | n/a | frame_size = len(pickled) - last_pos - frame_opcode_size |
---|
2060 | n/a | self.assertEqual(frame_size, last_arg) |
---|
2061 | n/a | |
---|
2062 | n/a | def test_framing_many_objects(self): |
---|
2063 | n/a | obj = list(range(10**5)) |
---|
2064 | n/a | for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
---|
2065 | n/a | with self.subTest(proto=proto): |
---|
2066 | n/a | pickled = self.dumps(obj, proto) |
---|
2067 | n/a | unpickled = self.loads(pickled) |
---|
2068 | n/a | self.assertEqual(obj, unpickled) |
---|
2069 | n/a | bytes_per_frame = (len(pickled) / |
---|
2070 | n/a | count_opcode(pickle.FRAME, pickled)) |
---|
2071 | n/a | self.assertGreater(bytes_per_frame, |
---|
2072 | n/a | self.FRAME_SIZE_TARGET / 2) |
---|
2073 | n/a | self.assertLessEqual(bytes_per_frame, |
---|
2074 | n/a | self.FRAME_SIZE_TARGET * 1) |
---|
2075 | n/a | self.check_frame_opcodes(pickled) |
---|
2076 | n/a | |
---|
2077 | n/a | def test_framing_large_objects(self): |
---|
2078 | n/a | N = 1024 * 1024 |
---|
2079 | n/a | obj = [b'x' * N, b'y' * N, b'z' * N] |
---|
2080 | n/a | for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
---|
2081 | n/a | with self.subTest(proto=proto): |
---|
2082 | n/a | pickled = self.dumps(obj, proto) |
---|
2083 | n/a | unpickled = self.loads(pickled) |
---|
2084 | n/a | self.assertEqual(obj, unpickled) |
---|
2085 | n/a | n_frames = count_opcode(pickle.FRAME, pickled) |
---|
2086 | n/a | self.assertGreaterEqual(n_frames, len(obj)) |
---|
2087 | n/a | self.check_frame_opcodes(pickled) |
---|
2088 | n/a | |
---|
2089 | n/a | def test_optional_frames(self): |
---|
2090 | n/a | if pickle.HIGHEST_PROTOCOL < 4: |
---|
2091 | n/a | return |
---|
2092 | n/a | |
---|
2093 | n/a | def remove_frames(pickled, keep_frame=None): |
---|
2094 | n/a | """Remove frame opcodes from the given pickle.""" |
---|
2095 | n/a | frame_starts = [] |
---|
2096 | n/a | # 1 byte for the opcode and 8 for the argument |
---|
2097 | n/a | frame_opcode_size = 9 |
---|
2098 | n/a | for opcode, _, pos in pickletools.genops(pickled): |
---|
2099 | n/a | if opcode.name == 'FRAME': |
---|
2100 | n/a | frame_starts.append(pos) |
---|
2101 | n/a | |
---|
2102 | n/a | newpickle = bytearray() |
---|
2103 | n/a | last_frame_end = 0 |
---|
2104 | n/a | for i, pos in enumerate(frame_starts): |
---|
2105 | n/a | if keep_frame and keep_frame(i): |
---|
2106 | n/a | continue |
---|
2107 | n/a | newpickle += pickled[last_frame_end:pos] |
---|
2108 | n/a | last_frame_end = pos + frame_opcode_size |
---|
2109 | n/a | newpickle += pickled[last_frame_end:] |
---|
2110 | n/a | return newpickle |
---|
2111 | n/a | |
---|
2112 | n/a | frame_size = self.FRAME_SIZE_TARGET |
---|
2113 | n/a | num_frames = 20 |
---|
2114 | n/a | obj = [bytes([i]) * frame_size for i in range(num_frames)] |
---|
2115 | n/a | |
---|
2116 | n/a | for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
---|
2117 | n/a | pickled = self.dumps(obj, proto) |
---|
2118 | n/a | |
---|
2119 | n/a | frameless_pickle = remove_frames(pickled) |
---|
2120 | n/a | self.assertEqual(count_opcode(pickle.FRAME, frameless_pickle), 0) |
---|
2121 | n/a | self.assertEqual(obj, self.loads(frameless_pickle)) |
---|
2122 | n/a | |
---|
2123 | n/a | some_frames_pickle = remove_frames(pickled, lambda i: i % 2) |
---|
2124 | n/a | self.assertLess(count_opcode(pickle.FRAME, some_frames_pickle), |
---|
2125 | n/a | count_opcode(pickle.FRAME, pickled)) |
---|
2126 | n/a | self.assertEqual(obj, self.loads(some_frames_pickle)) |
---|
2127 | n/a | |
---|
2128 | n/a | def test_nested_names(self): |
---|
2129 | n/a | global Nested |
---|
2130 | n/a | class Nested: |
---|
2131 | n/a | class A: |
---|
2132 | n/a | class B: |
---|
2133 | n/a | class C: |
---|
2134 | n/a | pass |
---|
2135 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2136 | n/a | for obj in [Nested.A, Nested.A.B, Nested.A.B.C]: |
---|
2137 | n/a | with self.subTest(proto=proto, obj=obj): |
---|
2138 | n/a | unpickled = self.loads(self.dumps(obj, proto)) |
---|
2139 | n/a | self.assertIs(obj, unpickled) |
---|
2140 | n/a | |
---|
2141 | n/a | def test_recursive_nested_names(self): |
---|
2142 | n/a | global Recursive |
---|
2143 | n/a | class Recursive: |
---|
2144 | n/a | pass |
---|
2145 | n/a | Recursive.mod = sys.modules[Recursive.__module__] |
---|
2146 | n/a | Recursive.__qualname__ = 'Recursive.mod.Recursive' |
---|
2147 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2148 | n/a | with self.subTest(proto=proto): |
---|
2149 | n/a | unpickled = self.loads(self.dumps(Recursive, proto)) |
---|
2150 | n/a | self.assertIs(unpickled, Recursive) |
---|
2151 | n/a | del Recursive.mod # break reference loop |
---|
2152 | n/a | |
---|
2153 | n/a | def test_py_methods(self): |
---|
2154 | n/a | global PyMethodsTest |
---|
2155 | n/a | class PyMethodsTest: |
---|
2156 | n/a | @staticmethod |
---|
2157 | n/a | def cheese(): |
---|
2158 | n/a | return "cheese" |
---|
2159 | n/a | @classmethod |
---|
2160 | n/a | def wine(cls): |
---|
2161 | n/a | assert cls is PyMethodsTest |
---|
2162 | n/a | return "wine" |
---|
2163 | n/a | def biscuits(self): |
---|
2164 | n/a | assert isinstance(self, PyMethodsTest) |
---|
2165 | n/a | return "biscuits" |
---|
2166 | n/a | class Nested: |
---|
2167 | n/a | "Nested class" |
---|
2168 | n/a | @staticmethod |
---|
2169 | n/a | def ketchup(): |
---|
2170 | n/a | return "ketchup" |
---|
2171 | n/a | @classmethod |
---|
2172 | n/a | def maple(cls): |
---|
2173 | n/a | assert cls is PyMethodsTest.Nested |
---|
2174 | n/a | return "maple" |
---|
2175 | n/a | def pie(self): |
---|
2176 | n/a | assert isinstance(self, PyMethodsTest.Nested) |
---|
2177 | n/a | return "pie" |
---|
2178 | n/a | |
---|
2179 | n/a | py_methods = ( |
---|
2180 | n/a | PyMethodsTest.cheese, |
---|
2181 | n/a | PyMethodsTest.wine, |
---|
2182 | n/a | PyMethodsTest().biscuits, |
---|
2183 | n/a | PyMethodsTest.Nested.ketchup, |
---|
2184 | n/a | PyMethodsTest.Nested.maple, |
---|
2185 | n/a | PyMethodsTest.Nested().pie |
---|
2186 | n/a | ) |
---|
2187 | n/a | py_unbound_methods = ( |
---|
2188 | n/a | (PyMethodsTest.biscuits, PyMethodsTest), |
---|
2189 | n/a | (PyMethodsTest.Nested.pie, PyMethodsTest.Nested) |
---|
2190 | n/a | ) |
---|
2191 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2192 | n/a | for method in py_methods: |
---|
2193 | n/a | with self.subTest(proto=proto, method=method): |
---|
2194 | n/a | unpickled = self.loads(self.dumps(method, proto)) |
---|
2195 | n/a | self.assertEqual(method(), unpickled()) |
---|
2196 | n/a | for method, cls in py_unbound_methods: |
---|
2197 | n/a | obj = cls() |
---|
2198 | n/a | with self.subTest(proto=proto, method=method): |
---|
2199 | n/a | unpickled = self.loads(self.dumps(method, proto)) |
---|
2200 | n/a | self.assertEqual(method(obj), unpickled(obj)) |
---|
2201 | n/a | |
---|
2202 | n/a | def test_c_methods(self): |
---|
2203 | n/a | global Subclass |
---|
2204 | n/a | class Subclass(tuple): |
---|
2205 | n/a | class Nested(str): |
---|
2206 | n/a | pass |
---|
2207 | n/a | |
---|
2208 | n/a | c_methods = ( |
---|
2209 | n/a | # bound built-in method |
---|
2210 | n/a | ("abcd".index, ("c",)), |
---|
2211 | n/a | # unbound built-in method |
---|
2212 | n/a | (str.index, ("abcd", "c")), |
---|
2213 | n/a | # bound "slot" method |
---|
2214 | n/a | ([1, 2, 3].__len__, ()), |
---|
2215 | n/a | # unbound "slot" method |
---|
2216 | n/a | (list.__len__, ([1, 2, 3],)), |
---|
2217 | n/a | # bound "coexist" method |
---|
2218 | n/a | ({1, 2}.__contains__, (2,)), |
---|
2219 | n/a | # unbound "coexist" method |
---|
2220 | n/a | (set.__contains__, ({1, 2}, 2)), |
---|
2221 | n/a | # built-in class method |
---|
2222 | n/a | (dict.fromkeys, (("a", 1), ("b", 2))), |
---|
2223 | n/a | # built-in static method |
---|
2224 | n/a | (bytearray.maketrans, (b"abc", b"xyz")), |
---|
2225 | n/a | # subclass methods |
---|
2226 | n/a | (Subclass([1,2,2]).count, (2,)), |
---|
2227 | n/a | (Subclass.count, (Subclass([1,2,2]), 2)), |
---|
2228 | n/a | (Subclass.Nested("sweet").count, ("e",)), |
---|
2229 | n/a | (Subclass.Nested.count, (Subclass.Nested("sweet"), "e")), |
---|
2230 | n/a | ) |
---|
2231 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
2232 | n/a | for method, args in c_methods: |
---|
2233 | n/a | with self.subTest(proto=proto, method=method): |
---|
2234 | n/a | unpickled = self.loads(self.dumps(method, proto)) |
---|
2235 | n/a | self.assertEqual(method(*args), unpickled(*args)) |
---|
2236 | n/a | |
---|
2237 | n/a | def test_compat_pickle(self): |
---|
2238 | n/a | tests = [ |
---|
2239 | n/a | (range(1, 7), '__builtin__', 'xrange'), |
---|
2240 | n/a | (map(int, '123'), 'itertools', 'imap'), |
---|
2241 | n/a | (functools.reduce, '__builtin__', 'reduce'), |
---|
2242 | n/a | (dbm.whichdb, 'whichdb', 'whichdb'), |
---|
2243 | n/a | (Exception(), 'exceptions', 'Exception'), |
---|
2244 | n/a | (collections.UserDict(), 'UserDict', 'IterableUserDict'), |
---|
2245 | n/a | (collections.UserList(), 'UserList', 'UserList'), |
---|
2246 | n/a | (collections.defaultdict(), 'collections', 'defaultdict'), |
---|
2247 | n/a | ] |
---|
2248 | n/a | for val, mod, name in tests: |
---|
2249 | n/a | for proto in range(3): |
---|
2250 | n/a | with self.subTest(type=type(val), proto=proto): |
---|
2251 | n/a | pickled = self.dumps(val, proto) |
---|
2252 | n/a | self.assertIn(('c%s\n%s' % (mod, name)).encode(), pickled) |
---|
2253 | n/a | self.assertIs(type(self.loads(pickled)), type(val)) |
---|
2254 | n/a | |
---|
2255 | n/a | def test_local_lookup_error(self): |
---|
2256 | n/a | # Test that whichmodule() errors out cleanly when looking up |
---|
2257 | n/a | # an assumed globally-reachable object fails. |
---|
2258 | n/a | def f(): |
---|
2259 | n/a | pass |
---|
2260 | n/a | # Since the function is local, lookup will fail |
---|
2261 | n/a | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
---|
2262 | n/a | with self.assertRaises((AttributeError, pickle.PicklingError)): |
---|
2263 | n/a | pickletools.dis(self.dumps(f, proto)) |
---|
2264 | n/a | # Same without a __module__ attribute (exercises a different path |
---|
2265 | n/a | # in _pickle.c). |
---|
2266 | n/a | del f.__module__ |
---|
2267 | n/a | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
---|
2268 | n/a | with self.assertRaises((AttributeError, pickle.PicklingError)): |
---|
2269 | n/a | pickletools.dis(self.dumps(f, proto)) |
---|
2270 | n/a | # Yet a different path. |
---|
2271 | n/a | f.__name__ = f.__qualname__ |
---|
2272 | n/a | for proto in range(0, pickle.HIGHEST_PROTOCOL + 1): |
---|
2273 | n/a | with self.assertRaises((AttributeError, pickle.PicklingError)): |
---|
2274 | n/a | pickletools.dis(self.dumps(f, proto)) |
---|
2275 | n/a | |
---|
2276 | n/a | |
---|
2277 | n/a | class BigmemPickleTests(unittest.TestCase): |
---|
2278 | n/a | |
---|
2279 | n/a | # Binary protocols can serialize longs of up to 2GB-1 |
---|
2280 | n/a | |
---|
2281 | n/a | @bigmemtest(size=_2G, memuse=3.6, dry_run=False) |
---|
2282 | n/a | def test_huge_long_32b(self, size): |
---|
2283 | n/a | data = 1 << (8 * size) |
---|
2284 | n/a | try: |
---|
2285 | n/a | for proto in protocols: |
---|
2286 | n/a | if proto < 2: |
---|
2287 | n/a | continue |
---|
2288 | n/a | with self.subTest(proto=proto): |
---|
2289 | n/a | with self.assertRaises((ValueError, OverflowError)): |
---|
2290 | n/a | self.dumps(data, protocol=proto) |
---|
2291 | n/a | finally: |
---|
2292 | n/a | data = None |
---|
2293 | n/a | |
---|
2294 | n/a | # Protocol 3 can serialize up to 4GB-1 as a bytes object |
---|
2295 | n/a | # (older protocols don't have a dedicated opcode for bytes and are |
---|
2296 | n/a | # too inefficient) |
---|
2297 | n/a | |
---|
2298 | n/a | @bigmemtest(size=_2G, memuse=2.5, dry_run=False) |
---|
2299 | n/a | def test_huge_bytes_32b(self, size): |
---|
2300 | n/a | data = b"abcd" * (size // 4) |
---|
2301 | n/a | try: |
---|
2302 | n/a | for proto in protocols: |
---|
2303 | n/a | if proto < 3: |
---|
2304 | n/a | continue |
---|
2305 | n/a | with self.subTest(proto=proto): |
---|
2306 | n/a | try: |
---|
2307 | n/a | pickled = self.dumps(data, protocol=proto) |
---|
2308 | n/a | header = (pickle.BINBYTES + |
---|
2309 | n/a | struct.pack("<I", len(data))) |
---|
2310 | n/a | data_start = pickled.index(data) |
---|
2311 | n/a | self.assertEqual( |
---|
2312 | n/a | header, |
---|
2313 | n/a | pickled[data_start-len(header):data_start]) |
---|
2314 | n/a | finally: |
---|
2315 | n/a | pickled = None |
---|
2316 | n/a | finally: |
---|
2317 | n/a | data = None |
---|
2318 | n/a | |
---|
2319 | n/a | @bigmemtest(size=_4G, memuse=2.5, dry_run=False) |
---|
2320 | n/a | def test_huge_bytes_64b(self, size): |
---|
2321 | n/a | data = b"acbd" * (size // 4) |
---|
2322 | n/a | try: |
---|
2323 | n/a | for proto in protocols: |
---|
2324 | n/a | if proto < 3: |
---|
2325 | n/a | continue |
---|
2326 | n/a | with self.subTest(proto=proto): |
---|
2327 | n/a | if proto == 3: |
---|
2328 | n/a | # Protocol 3 does not support large bytes objects. |
---|
2329 | n/a | # Verify that we do not crash when processing one. |
---|
2330 | n/a | with self.assertRaises((ValueError, OverflowError)): |
---|
2331 | n/a | self.dumps(data, protocol=proto) |
---|
2332 | n/a | continue |
---|
2333 | n/a | try: |
---|
2334 | n/a | pickled = self.dumps(data, protocol=proto) |
---|
2335 | n/a | header = (pickle.BINBYTES8 + |
---|
2336 | n/a | struct.pack("<Q", len(data))) |
---|
2337 | n/a | data_start = pickled.index(data) |
---|
2338 | n/a | self.assertEqual( |
---|
2339 | n/a | header, |
---|
2340 | n/a | pickled[data_start-len(header):data_start]) |
---|
2341 | n/a | finally: |
---|
2342 | n/a | pickled = None |
---|
2343 | n/a | finally: |
---|
2344 | n/a | data = None |
---|
2345 | n/a | |
---|
2346 | n/a | # All protocols use 1-byte per printable ASCII character; we add another |
---|
2347 | n/a | # byte because the encoded form has to be copied into the internal buffer. |
---|
2348 | n/a | |
---|
2349 | n/a | @bigmemtest(size=_2G, memuse=8, dry_run=False) |
---|
2350 | n/a | def test_huge_str_32b(self, size): |
---|
2351 | n/a | data = "abcd" * (size // 4) |
---|
2352 | n/a | try: |
---|
2353 | n/a | for proto in protocols: |
---|
2354 | n/a | if proto == 0: |
---|
2355 | n/a | continue |
---|
2356 | n/a | with self.subTest(proto=proto): |
---|
2357 | n/a | try: |
---|
2358 | n/a | pickled = self.dumps(data, protocol=proto) |
---|
2359 | n/a | header = (pickle.BINUNICODE + |
---|
2360 | n/a | struct.pack("<I", len(data))) |
---|
2361 | n/a | data_start = pickled.index(b'abcd') |
---|
2362 | n/a | self.assertEqual( |
---|
2363 | n/a | header, |
---|
2364 | n/a | pickled[data_start-len(header):data_start]) |
---|
2365 | n/a | self.assertEqual((pickled.rindex(b"abcd") + len(b"abcd") - |
---|
2366 | n/a | pickled.index(b"abcd")), len(data)) |
---|
2367 | n/a | finally: |
---|
2368 | n/a | pickled = None |
---|
2369 | n/a | finally: |
---|
2370 | n/a | data = None |
---|
2371 | n/a | |
---|
2372 | n/a | # BINUNICODE (protocols 1, 2 and 3) cannot carry more than 2**32 - 1 bytes |
---|
2373 | n/a | # of utf-8 encoded unicode. BINUNICODE8 (protocol 4) supports these huge |
---|
2374 | n/a | # unicode strings however. |
---|
2375 | n/a | |
---|
2376 | n/a | @bigmemtest(size=_4G, memuse=8, dry_run=False) |
---|
2377 | n/a | def test_huge_str_64b(self, size): |
---|
2378 | n/a | data = "abcd" * (size // 4) |
---|
2379 | n/a | try: |
---|
2380 | n/a | for proto in protocols: |
---|
2381 | n/a | if proto == 0: |
---|
2382 | n/a | continue |
---|
2383 | n/a | with self.subTest(proto=proto): |
---|
2384 | n/a | if proto < 4: |
---|
2385 | n/a | with self.assertRaises((ValueError, OverflowError)): |
---|
2386 | n/a | self.dumps(data, protocol=proto) |
---|
2387 | n/a | continue |
---|
2388 | n/a | try: |
---|
2389 | n/a | pickled = self.dumps(data, protocol=proto) |
---|
2390 | n/a | header = (pickle.BINUNICODE8 + |
---|
2391 | n/a | struct.pack("<Q", len(data))) |
---|
2392 | n/a | data_start = pickled.index(b'abcd') |
---|
2393 | n/a | self.assertEqual( |
---|
2394 | n/a | header, |
---|
2395 | n/a | pickled[data_start-len(header):data_start]) |
---|
2396 | n/a | self.assertEqual((pickled.rindex(b"abcd") + len(b"abcd") - |
---|
2397 | n/a | pickled.index(b"abcd")), len(data)) |
---|
2398 | n/a | finally: |
---|
2399 | n/a | pickled = None |
---|
2400 | n/a | finally: |
---|
2401 | n/a | data = None |
---|
2402 | n/a | |
---|
2403 | n/a | |
---|
2404 | n/a | # Test classes for reduce_ex |
---|
2405 | n/a | |
---|
2406 | n/a | class REX_one(object): |
---|
2407 | n/a | """No __reduce_ex__ here, but inheriting it from object""" |
---|
2408 | n/a | _reduce_called = 0 |
---|
2409 | n/a | def __reduce__(self): |
---|
2410 | n/a | self._reduce_called = 1 |
---|
2411 | n/a | return REX_one, () |
---|
2412 | n/a | |
---|
2413 | n/a | class REX_two(object): |
---|
2414 | n/a | """No __reduce__ here, but inheriting it from object""" |
---|
2415 | n/a | _proto = None |
---|
2416 | n/a | def __reduce_ex__(self, proto): |
---|
2417 | n/a | self._proto = proto |
---|
2418 | n/a | return REX_two, () |
---|
2419 | n/a | |
---|
2420 | n/a | class REX_three(object): |
---|
2421 | n/a | _proto = None |
---|
2422 | n/a | def __reduce_ex__(self, proto): |
---|
2423 | n/a | self._proto = proto |
---|
2424 | n/a | return REX_two, () |
---|
2425 | n/a | def __reduce__(self): |
---|
2426 | n/a | raise TestFailed("This __reduce__ shouldn't be called") |
---|
2427 | n/a | |
---|
2428 | n/a | class REX_four(object): |
---|
2429 | n/a | """Calling base class method should succeed""" |
---|
2430 | n/a | _proto = None |
---|
2431 | n/a | def __reduce_ex__(self, proto): |
---|
2432 | n/a | self._proto = proto |
---|
2433 | n/a | return object.__reduce_ex__(self, proto) |
---|
2434 | n/a | |
---|
2435 | n/a | class REX_five(object): |
---|
2436 | n/a | """This one used to fail with infinite recursion""" |
---|
2437 | n/a | _reduce_called = 0 |
---|
2438 | n/a | def __reduce__(self): |
---|
2439 | n/a | self._reduce_called = 1 |
---|
2440 | n/a | return object.__reduce__(self) |
---|
2441 | n/a | |
---|
2442 | n/a | class REX_six(object): |
---|
2443 | n/a | """This class is used to check the 4th argument (list iterator) of |
---|
2444 | n/a | the reduce protocol. |
---|
2445 | n/a | """ |
---|
2446 | n/a | def __init__(self, items=None): |
---|
2447 | n/a | self.items = items if items is not None else [] |
---|
2448 | n/a | def __eq__(self, other): |
---|
2449 | n/a | return type(self) is type(other) and self.items == other.items |
---|
2450 | n/a | def append(self, item): |
---|
2451 | n/a | self.items.append(item) |
---|
2452 | n/a | def __reduce__(self): |
---|
2453 | n/a | return type(self), (), None, iter(self.items), None |
---|
2454 | n/a | |
---|
2455 | n/a | class REX_seven(object): |
---|
2456 | n/a | """This class is used to check the 5th argument (dict iterator) of |
---|
2457 | n/a | the reduce protocol. |
---|
2458 | n/a | """ |
---|
2459 | n/a | def __init__(self, table=None): |
---|
2460 | n/a | self.table = table if table is not None else {} |
---|
2461 | n/a | def __eq__(self, other): |
---|
2462 | n/a | return type(self) is type(other) and self.table == other.table |
---|
2463 | n/a | def __setitem__(self, key, value): |
---|
2464 | n/a | self.table[key] = value |
---|
2465 | n/a | def __reduce__(self): |
---|
2466 | n/a | return type(self), (), None, None, iter(self.table.items()) |
---|
2467 | n/a | |
---|
2468 | n/a | |
---|
2469 | n/a | # Test classes for newobj |
---|
2470 | n/a | |
---|
2471 | n/a | class MyInt(int): |
---|
2472 | n/a | sample = 1 |
---|
2473 | n/a | |
---|
2474 | n/a | class MyFloat(float): |
---|
2475 | n/a | sample = 1.0 |
---|
2476 | n/a | |
---|
2477 | n/a | class MyComplex(complex): |
---|
2478 | n/a | sample = 1.0 + 0.0j |
---|
2479 | n/a | |
---|
2480 | n/a | class MyStr(str): |
---|
2481 | n/a | sample = "hello" |
---|
2482 | n/a | |
---|
2483 | n/a | class MyUnicode(str): |
---|
2484 | n/a | sample = "hello \u1234" |
---|
2485 | n/a | |
---|
2486 | n/a | class MyTuple(tuple): |
---|
2487 | n/a | sample = (1, 2, 3) |
---|
2488 | n/a | |
---|
2489 | n/a | class MyList(list): |
---|
2490 | n/a | sample = [1, 2, 3] |
---|
2491 | n/a | |
---|
2492 | n/a | class MyDict(dict): |
---|
2493 | n/a | sample = {"a": 1, "b": 2} |
---|
2494 | n/a | |
---|
2495 | n/a | class MySet(set): |
---|
2496 | n/a | sample = {"a", "b"} |
---|
2497 | n/a | |
---|
2498 | n/a | class MyFrozenSet(frozenset): |
---|
2499 | n/a | sample = frozenset({"a", "b"}) |
---|
2500 | n/a | |
---|
2501 | n/a | myclasses = [MyInt, MyFloat, |
---|
2502 | n/a | MyComplex, |
---|
2503 | n/a | MyStr, MyUnicode, |
---|
2504 | n/a | MyTuple, MyList, MyDict, MySet, MyFrozenSet] |
---|
2505 | n/a | |
---|
2506 | n/a | |
---|
2507 | n/a | class SlotList(MyList): |
---|
2508 | n/a | __slots__ = ["foo"] |
---|
2509 | n/a | |
---|
2510 | n/a | class SimpleNewObj(int): |
---|
2511 | n/a | def __init__(self, *args, **kwargs): |
---|
2512 | n/a | # raise an error, to make sure this isn't called |
---|
2513 | n/a | raise TypeError("SimpleNewObj.__init__() didn't expect to get called") |
---|
2514 | n/a | def __eq__(self, other): |
---|
2515 | n/a | return int(self) == int(other) and self.__dict__ == other.__dict__ |
---|
2516 | n/a | |
---|
2517 | n/a | class ComplexNewObj(SimpleNewObj): |
---|
2518 | n/a | def __getnewargs__(self): |
---|
2519 | n/a | return ('%X' % self, 16) |
---|
2520 | n/a | |
---|
2521 | n/a | class ComplexNewObjEx(SimpleNewObj): |
---|
2522 | n/a | def __getnewargs_ex__(self): |
---|
2523 | n/a | return ('%X' % self,), {'base': 16} |
---|
2524 | n/a | |
---|
2525 | n/a | class BadGetattr: |
---|
2526 | n/a | def __getattr__(self, key): |
---|
2527 | n/a | self.foo |
---|
2528 | n/a | |
---|
2529 | n/a | |
---|
2530 | n/a | class AbstractPickleModuleTests(unittest.TestCase): |
---|
2531 | n/a | |
---|
2532 | n/a | def test_dump_closed_file(self): |
---|
2533 | n/a | import os |
---|
2534 | n/a | f = open(TESTFN, "wb") |
---|
2535 | n/a | try: |
---|
2536 | n/a | f.close() |
---|
2537 | n/a | self.assertRaises(ValueError, pickle.dump, 123, f) |
---|
2538 | n/a | finally: |
---|
2539 | n/a | os.remove(TESTFN) |
---|
2540 | n/a | |
---|
2541 | n/a | def test_load_closed_file(self): |
---|
2542 | n/a | import os |
---|
2543 | n/a | f = open(TESTFN, "wb") |
---|
2544 | n/a | try: |
---|
2545 | n/a | f.close() |
---|
2546 | n/a | self.assertRaises(ValueError, pickle.dump, 123, f) |
---|
2547 | n/a | finally: |
---|
2548 | n/a | os.remove(TESTFN) |
---|
2549 | n/a | |
---|
2550 | n/a | def test_load_from_and_dump_to_file(self): |
---|
2551 | n/a | stream = io.BytesIO() |
---|
2552 | n/a | data = [123, {}, 124] |
---|
2553 | n/a | pickle.dump(data, stream) |
---|
2554 | n/a | stream.seek(0) |
---|
2555 | n/a | unpickled = pickle.load(stream) |
---|
2556 | n/a | self.assertEqual(unpickled, data) |
---|
2557 | n/a | |
---|
2558 | n/a | def test_highest_protocol(self): |
---|
2559 | n/a | # Of course this needs to be changed when HIGHEST_PROTOCOL changes. |
---|
2560 | n/a | self.assertEqual(pickle.HIGHEST_PROTOCOL, 4) |
---|
2561 | n/a | |
---|
2562 | n/a | def test_callapi(self): |
---|
2563 | n/a | f = io.BytesIO() |
---|
2564 | n/a | # With and without keyword arguments |
---|
2565 | n/a | pickle.dump(123, f, -1) |
---|
2566 | n/a | pickle.dump(123, file=f, protocol=-1) |
---|
2567 | n/a | pickle.dumps(123, -1) |
---|
2568 | n/a | pickle.dumps(123, protocol=-1) |
---|
2569 | n/a | pickle.Pickler(f, -1) |
---|
2570 | n/a | pickle.Pickler(f, protocol=-1) |
---|
2571 | n/a | |
---|
2572 | n/a | def test_bad_init(self): |
---|
2573 | n/a | # Test issue3664 (pickle can segfault from a badly initialized Pickler). |
---|
2574 | n/a | # Override initialization without calling __init__() of the superclass. |
---|
2575 | n/a | class BadPickler(pickle.Pickler): |
---|
2576 | n/a | def __init__(self): pass |
---|
2577 | n/a | |
---|
2578 | n/a | class BadUnpickler(pickle.Unpickler): |
---|
2579 | n/a | def __init__(self): pass |
---|
2580 | n/a | |
---|
2581 | n/a | self.assertRaises(pickle.PicklingError, BadPickler().dump, 0) |
---|
2582 | n/a | self.assertRaises(pickle.UnpicklingError, BadUnpickler().load) |
---|
2583 | n/a | |
---|
2584 | n/a | |
---|
2585 | n/a | class AbstractPersistentPicklerTests(unittest.TestCase): |
---|
2586 | n/a | |
---|
2587 | n/a | # This class defines persistent_id() and persistent_load() |
---|
2588 | n/a | # functions that should be used by the pickler. All even integers |
---|
2589 | n/a | # are pickled using persistent ids. |
---|
2590 | n/a | |
---|
2591 | n/a | def persistent_id(self, object): |
---|
2592 | n/a | if isinstance(object, int) and object % 2 == 0: |
---|
2593 | n/a | self.id_count += 1 |
---|
2594 | n/a | return str(object) |
---|
2595 | n/a | elif object == "test_false_value": |
---|
2596 | n/a | self.false_count += 1 |
---|
2597 | n/a | return "" |
---|
2598 | n/a | else: |
---|
2599 | n/a | return None |
---|
2600 | n/a | |
---|
2601 | n/a | def persistent_load(self, oid): |
---|
2602 | n/a | if not oid: |
---|
2603 | n/a | self.load_false_count += 1 |
---|
2604 | n/a | return "test_false_value" |
---|
2605 | n/a | else: |
---|
2606 | n/a | self.load_count += 1 |
---|
2607 | n/a | object = int(oid) |
---|
2608 | n/a | assert object % 2 == 0 |
---|
2609 | n/a | return object |
---|
2610 | n/a | |
---|
2611 | n/a | def test_persistence(self): |
---|
2612 | n/a | L = list(range(10)) + ["test_false_value"] |
---|
2613 | n/a | for proto in protocols: |
---|
2614 | n/a | self.id_count = 0 |
---|
2615 | n/a | self.false_count = 0 |
---|
2616 | n/a | self.load_false_count = 0 |
---|
2617 | n/a | self.load_count = 0 |
---|
2618 | n/a | self.assertEqual(self.loads(self.dumps(L, proto)), L) |
---|
2619 | n/a | self.assertEqual(self.id_count, 5) |
---|
2620 | n/a | self.assertEqual(self.false_count, 1) |
---|
2621 | n/a | self.assertEqual(self.load_count, 5) |
---|
2622 | n/a | self.assertEqual(self.load_false_count, 1) |
---|
2623 | n/a | |
---|
2624 | n/a | |
---|
2625 | n/a | class AbstractIdentityPersistentPicklerTests(unittest.TestCase): |
---|
2626 | n/a | |
---|
2627 | n/a | def persistent_id(self, obj): |
---|
2628 | n/a | return obj |
---|
2629 | n/a | |
---|
2630 | n/a | def persistent_load(self, pid): |
---|
2631 | n/a | return pid |
---|
2632 | n/a | |
---|
2633 | n/a | def _check_return_correct_type(self, obj, proto): |
---|
2634 | n/a | unpickled = self.loads(self.dumps(obj, proto)) |
---|
2635 | n/a | self.assertIsInstance(unpickled, type(obj)) |
---|
2636 | n/a | self.assertEqual(unpickled, obj) |
---|
2637 | n/a | |
---|
2638 | n/a | def test_return_correct_type(self): |
---|
2639 | n/a | for proto in protocols: |
---|
2640 | n/a | # Protocol 0 supports only ASCII strings. |
---|
2641 | n/a | if proto == 0: |
---|
2642 | n/a | self._check_return_correct_type("abc", 0) |
---|
2643 | n/a | else: |
---|
2644 | n/a | for obj in [b"abc\n", "abc\n", -1, -1.1 * 0.1, str]: |
---|
2645 | n/a | self._check_return_correct_type(obj, proto) |
---|
2646 | n/a | |
---|
2647 | n/a | def test_protocol0_is_ascii_only(self): |
---|
2648 | n/a | non_ascii_str = "\N{EMPTY SET}" |
---|
2649 | n/a | self.assertRaises(pickle.PicklingError, self.dumps, non_ascii_str, 0) |
---|
2650 | n/a | pickled = pickle.PERSID + non_ascii_str.encode('utf-8') + b'\n.' |
---|
2651 | n/a | self.assertRaises(pickle.UnpicklingError, self.loads, pickled) |
---|
2652 | n/a | |
---|
2653 | n/a | |
---|
2654 | n/a | class AbstractPicklerUnpicklerObjectTests(unittest.TestCase): |
---|
2655 | n/a | |
---|
2656 | n/a | pickler_class = None |
---|
2657 | n/a | unpickler_class = None |
---|
2658 | n/a | |
---|
2659 | n/a | def setUp(self): |
---|
2660 | n/a | assert self.pickler_class |
---|
2661 | n/a | assert self.unpickler_class |
---|
2662 | n/a | |
---|
2663 | n/a | def test_clear_pickler_memo(self): |
---|
2664 | n/a | # To test whether clear_memo() has any effect, we pickle an object, |
---|
2665 | n/a | # then pickle it again without clearing the memo; the two serialized |
---|
2666 | n/a | # forms should be different. If we clear_memo() and then pickle the |
---|
2667 | n/a | # object again, the third serialized form should be identical to the |
---|
2668 | n/a | # first one we obtained. |
---|
2669 | n/a | data = ["abcdefg", "abcdefg", 44] |
---|
2670 | n/a | f = io.BytesIO() |
---|
2671 | n/a | pickler = self.pickler_class(f) |
---|
2672 | n/a | |
---|
2673 | n/a | pickler.dump(data) |
---|
2674 | n/a | first_pickled = f.getvalue() |
---|
2675 | n/a | |
---|
2676 | n/a | # Reset BytesIO object. |
---|
2677 | n/a | f.seek(0) |
---|
2678 | n/a | f.truncate() |
---|
2679 | n/a | |
---|
2680 | n/a | pickler.dump(data) |
---|
2681 | n/a | second_pickled = f.getvalue() |
---|
2682 | n/a | |
---|
2683 | n/a | # Reset the Pickler and BytesIO objects. |
---|
2684 | n/a | pickler.clear_memo() |
---|
2685 | n/a | f.seek(0) |
---|
2686 | n/a | f.truncate() |
---|
2687 | n/a | |
---|
2688 | n/a | pickler.dump(data) |
---|
2689 | n/a | third_pickled = f.getvalue() |
---|
2690 | n/a | |
---|
2691 | n/a | self.assertNotEqual(first_pickled, second_pickled) |
---|
2692 | n/a | self.assertEqual(first_pickled, third_pickled) |
---|
2693 | n/a | |
---|
2694 | n/a | def test_priming_pickler_memo(self): |
---|
2695 | n/a | # Verify that we can set the Pickler's memo attribute. |
---|
2696 | n/a | data = ["abcdefg", "abcdefg", 44] |
---|
2697 | n/a | f = io.BytesIO() |
---|
2698 | n/a | pickler = self.pickler_class(f) |
---|
2699 | n/a | |
---|
2700 | n/a | pickler.dump(data) |
---|
2701 | n/a | first_pickled = f.getvalue() |
---|
2702 | n/a | |
---|
2703 | n/a | f = io.BytesIO() |
---|
2704 | n/a | primed = self.pickler_class(f) |
---|
2705 | n/a | primed.memo = pickler.memo |
---|
2706 | n/a | |
---|
2707 | n/a | primed.dump(data) |
---|
2708 | n/a | primed_pickled = f.getvalue() |
---|
2709 | n/a | |
---|
2710 | n/a | self.assertNotEqual(first_pickled, primed_pickled) |
---|
2711 | n/a | |
---|
2712 | n/a | def test_priming_unpickler_memo(self): |
---|
2713 | n/a | # Verify that we can set the Unpickler's memo attribute. |
---|
2714 | n/a | data = ["abcdefg", "abcdefg", 44] |
---|
2715 | n/a | f = io.BytesIO() |
---|
2716 | n/a | pickler = self.pickler_class(f) |
---|
2717 | n/a | |
---|
2718 | n/a | pickler.dump(data) |
---|
2719 | n/a | first_pickled = f.getvalue() |
---|
2720 | n/a | |
---|
2721 | n/a | f = io.BytesIO() |
---|
2722 | n/a | primed = self.pickler_class(f) |
---|
2723 | n/a | primed.memo = pickler.memo |
---|
2724 | n/a | |
---|
2725 | n/a | primed.dump(data) |
---|
2726 | n/a | primed_pickled = f.getvalue() |
---|
2727 | n/a | |
---|
2728 | n/a | unpickler = self.unpickler_class(io.BytesIO(first_pickled)) |
---|
2729 | n/a | unpickled_data1 = unpickler.load() |
---|
2730 | n/a | |
---|
2731 | n/a | self.assertEqual(unpickled_data1, data) |
---|
2732 | n/a | |
---|
2733 | n/a | primed = self.unpickler_class(io.BytesIO(primed_pickled)) |
---|
2734 | n/a | primed.memo = unpickler.memo |
---|
2735 | n/a | unpickled_data2 = primed.load() |
---|
2736 | n/a | |
---|
2737 | n/a | primed.memo.clear() |
---|
2738 | n/a | |
---|
2739 | n/a | self.assertEqual(unpickled_data2, data) |
---|
2740 | n/a | self.assertTrue(unpickled_data2 is unpickled_data1) |
---|
2741 | n/a | |
---|
2742 | n/a | def test_reusing_unpickler_objects(self): |
---|
2743 | n/a | data1 = ["abcdefg", "abcdefg", 44] |
---|
2744 | n/a | f = io.BytesIO() |
---|
2745 | n/a | pickler = self.pickler_class(f) |
---|
2746 | n/a | pickler.dump(data1) |
---|
2747 | n/a | pickled1 = f.getvalue() |
---|
2748 | n/a | |
---|
2749 | n/a | data2 = ["abcdefg", 44, 44] |
---|
2750 | n/a | f = io.BytesIO() |
---|
2751 | n/a | pickler = self.pickler_class(f) |
---|
2752 | n/a | pickler.dump(data2) |
---|
2753 | n/a | pickled2 = f.getvalue() |
---|
2754 | n/a | |
---|
2755 | n/a | f = io.BytesIO() |
---|
2756 | n/a | f.write(pickled1) |
---|
2757 | n/a | f.seek(0) |
---|
2758 | n/a | unpickler = self.unpickler_class(f) |
---|
2759 | n/a | self.assertEqual(unpickler.load(), data1) |
---|
2760 | n/a | |
---|
2761 | n/a | f.seek(0) |
---|
2762 | n/a | f.truncate() |
---|
2763 | n/a | f.write(pickled2) |
---|
2764 | n/a | f.seek(0) |
---|
2765 | n/a | self.assertEqual(unpickler.load(), data2) |
---|
2766 | n/a | |
---|
2767 | n/a | def _check_multiple_unpicklings(self, ioclass): |
---|
2768 | n/a | for proto in protocols: |
---|
2769 | n/a | with self.subTest(proto=proto): |
---|
2770 | n/a | data1 = [(x, str(x)) for x in range(2000)] + [b"abcde", len] |
---|
2771 | n/a | f = ioclass() |
---|
2772 | n/a | pickler = self.pickler_class(f, protocol=proto) |
---|
2773 | n/a | pickler.dump(data1) |
---|
2774 | n/a | pickled = f.getvalue() |
---|
2775 | n/a | |
---|
2776 | n/a | N = 5 |
---|
2777 | n/a | f = ioclass(pickled * N) |
---|
2778 | n/a | unpickler = self.unpickler_class(f) |
---|
2779 | n/a | for i in range(N): |
---|
2780 | n/a | if f.seekable(): |
---|
2781 | n/a | pos = f.tell() |
---|
2782 | n/a | self.assertEqual(unpickler.load(), data1) |
---|
2783 | n/a | if f.seekable(): |
---|
2784 | n/a | self.assertEqual(f.tell(), pos + len(pickled)) |
---|
2785 | n/a | self.assertRaises(EOFError, unpickler.load) |
---|
2786 | n/a | |
---|
2787 | n/a | def test_multiple_unpicklings_seekable(self): |
---|
2788 | n/a | self._check_multiple_unpicklings(io.BytesIO) |
---|
2789 | n/a | |
---|
2790 | n/a | def test_multiple_unpicklings_unseekable(self): |
---|
2791 | n/a | self._check_multiple_unpicklings(UnseekableIO) |
---|
2792 | n/a | |
---|
2793 | n/a | def test_unpickling_buffering_readline(self): |
---|
2794 | n/a | # Issue #12687: the unpickler's buffering logic could fail with |
---|
2795 | n/a | # text mode opcodes. |
---|
2796 | n/a | data = list(range(10)) |
---|
2797 | n/a | for proto in protocols: |
---|
2798 | n/a | for buf_size in range(1, 11): |
---|
2799 | n/a | f = io.BufferedRandom(io.BytesIO(), buffer_size=buf_size) |
---|
2800 | n/a | pickler = self.pickler_class(f, protocol=proto) |
---|
2801 | n/a | pickler.dump(data) |
---|
2802 | n/a | f.seek(0) |
---|
2803 | n/a | unpickler = self.unpickler_class(f) |
---|
2804 | n/a | self.assertEqual(unpickler.load(), data) |
---|
2805 | n/a | |
---|
2806 | n/a | |
---|
2807 | n/a | # Tests for dispatch_table attribute |
---|
2808 | n/a | |
---|
2809 | n/a | REDUCE_A = 'reduce_A' |
---|
2810 | n/a | |
---|
2811 | n/a | class AAA(object): |
---|
2812 | n/a | def __reduce__(self): |
---|
2813 | n/a | return str, (REDUCE_A,) |
---|
2814 | n/a | |
---|
2815 | n/a | class BBB(object): |
---|
2816 | n/a | pass |
---|
2817 | n/a | |
---|
2818 | n/a | class AbstractDispatchTableTests(unittest.TestCase): |
---|
2819 | n/a | |
---|
2820 | n/a | def test_default_dispatch_table(self): |
---|
2821 | n/a | # No dispatch_table attribute by default |
---|
2822 | n/a | f = io.BytesIO() |
---|
2823 | n/a | p = self.pickler_class(f, 0) |
---|
2824 | n/a | with self.assertRaises(AttributeError): |
---|
2825 | n/a | p.dispatch_table |
---|
2826 | n/a | self.assertFalse(hasattr(p, 'dispatch_table')) |
---|
2827 | n/a | |
---|
2828 | n/a | def test_class_dispatch_table(self): |
---|
2829 | n/a | # A dispatch_table attribute can be specified class-wide |
---|
2830 | n/a | dt = self.get_dispatch_table() |
---|
2831 | n/a | |
---|
2832 | n/a | class MyPickler(self.pickler_class): |
---|
2833 | n/a | dispatch_table = dt |
---|
2834 | n/a | |
---|
2835 | n/a | def dumps(obj, protocol=None): |
---|
2836 | n/a | f = io.BytesIO() |
---|
2837 | n/a | p = MyPickler(f, protocol) |
---|
2838 | n/a | self.assertEqual(p.dispatch_table, dt) |
---|
2839 | n/a | p.dump(obj) |
---|
2840 | n/a | return f.getvalue() |
---|
2841 | n/a | |
---|
2842 | n/a | self._test_dispatch_table(dumps, dt) |
---|
2843 | n/a | |
---|
2844 | n/a | def test_instance_dispatch_table(self): |
---|
2845 | n/a | # A dispatch_table attribute can also be specified instance-wide |
---|
2846 | n/a | dt = self.get_dispatch_table() |
---|
2847 | n/a | |
---|
2848 | n/a | def dumps(obj, protocol=None): |
---|
2849 | n/a | f = io.BytesIO() |
---|
2850 | n/a | p = self.pickler_class(f, protocol) |
---|
2851 | n/a | p.dispatch_table = dt |
---|
2852 | n/a | self.assertEqual(p.dispatch_table, dt) |
---|
2853 | n/a | p.dump(obj) |
---|
2854 | n/a | return f.getvalue() |
---|
2855 | n/a | |
---|
2856 | n/a | self._test_dispatch_table(dumps, dt) |
---|
2857 | n/a | |
---|
2858 | n/a | def _test_dispatch_table(self, dumps, dispatch_table): |
---|
2859 | n/a | def custom_load_dump(obj): |
---|
2860 | n/a | return pickle.loads(dumps(obj, 0)) |
---|
2861 | n/a | |
---|
2862 | n/a | def default_load_dump(obj): |
---|
2863 | n/a | return pickle.loads(pickle.dumps(obj, 0)) |
---|
2864 | n/a | |
---|
2865 | n/a | # pickling complex numbers using protocol 0 relies on copyreg |
---|
2866 | n/a | # so check pickling a complex number still works |
---|
2867 | n/a | z = 1 + 2j |
---|
2868 | n/a | self.assertEqual(custom_load_dump(z), z) |
---|
2869 | n/a | self.assertEqual(default_load_dump(z), z) |
---|
2870 | n/a | |
---|
2871 | n/a | # modify pickling of complex |
---|
2872 | n/a | REDUCE_1 = 'reduce_1' |
---|
2873 | n/a | def reduce_1(obj): |
---|
2874 | n/a | return str, (REDUCE_1,) |
---|
2875 | n/a | dispatch_table[complex] = reduce_1 |
---|
2876 | n/a | self.assertEqual(custom_load_dump(z), REDUCE_1) |
---|
2877 | n/a | self.assertEqual(default_load_dump(z), z) |
---|
2878 | n/a | |
---|
2879 | n/a | # check picklability of AAA and BBB |
---|
2880 | n/a | a = AAA() |
---|
2881 | n/a | b = BBB() |
---|
2882 | n/a | self.assertEqual(custom_load_dump(a), REDUCE_A) |
---|
2883 | n/a | self.assertIsInstance(custom_load_dump(b), BBB) |
---|
2884 | n/a | self.assertEqual(default_load_dump(a), REDUCE_A) |
---|
2885 | n/a | self.assertIsInstance(default_load_dump(b), BBB) |
---|
2886 | n/a | |
---|
2887 | n/a | # modify pickling of BBB |
---|
2888 | n/a | dispatch_table[BBB] = reduce_1 |
---|
2889 | n/a | self.assertEqual(custom_load_dump(a), REDUCE_A) |
---|
2890 | n/a | self.assertEqual(custom_load_dump(b), REDUCE_1) |
---|
2891 | n/a | self.assertEqual(default_load_dump(a), REDUCE_A) |
---|
2892 | n/a | self.assertIsInstance(default_load_dump(b), BBB) |
---|
2893 | n/a | |
---|
2894 | n/a | # revert pickling of BBB and modify pickling of AAA |
---|
2895 | n/a | REDUCE_2 = 'reduce_2' |
---|
2896 | n/a | def reduce_2(obj): |
---|
2897 | n/a | return str, (REDUCE_2,) |
---|
2898 | n/a | dispatch_table[AAA] = reduce_2 |
---|
2899 | n/a | del dispatch_table[BBB] |
---|
2900 | n/a | self.assertEqual(custom_load_dump(a), REDUCE_2) |
---|
2901 | n/a | self.assertIsInstance(custom_load_dump(b), BBB) |
---|
2902 | n/a | self.assertEqual(default_load_dump(a), REDUCE_A) |
---|
2903 | n/a | self.assertIsInstance(default_load_dump(b), BBB) |
---|
2904 | n/a | |
---|
2905 | n/a | |
---|
2906 | n/a | if __name__ == "__main__": |
---|
2907 | n/a | # Print some stuff that can be used to rewrite DATA{0,1,2} |
---|
2908 | n/a | from pickletools import dis |
---|
2909 | n/a | x = create_data() |
---|
2910 | n/a | for i in range(pickle.HIGHEST_PROTOCOL+1): |
---|
2911 | n/a | p = pickle.dumps(x, i) |
---|
2912 | n/a | print("DATA{0} = (".format(i)) |
---|
2913 | n/a | for j in range(0, len(p), 20): |
---|
2914 | n/a | b = bytes(p[j:j+20]) |
---|
2915 | n/a | print(" {0!r}".format(b)) |
---|
2916 | n/a | print(")") |
---|
2917 | n/a | print() |
---|
2918 | n/a | print("# Disassembly of DATA{0}".format(i)) |
---|
2919 | n/a | print("DATA{0}_DIS = \"\"\"\\".format(i)) |
---|
2920 | n/a | dis(p) |
---|
2921 | n/a | print("\"\"\"") |
---|
2922 | n/a | print() |
---|