1 | n/a | """Create portable serialized representations of Python objects. |
---|
2 | n/a | |
---|
3 | n/a | See module copyreg for a mechanism for registering custom picklers. |
---|
4 | n/a | See module pickletools source for extensive comments. |
---|
5 | n/a | |
---|
6 | n/a | Classes: |
---|
7 | n/a | |
---|
8 | n/a | Pickler |
---|
9 | n/a | Unpickler |
---|
10 | n/a | |
---|
11 | n/a | Functions: |
---|
12 | n/a | |
---|
13 | n/a | dump(object, file) |
---|
14 | n/a | dumps(object) -> string |
---|
15 | n/a | load(file) -> object |
---|
16 | n/a | loads(string) -> object |
---|
17 | n/a | |
---|
18 | n/a | Misc variables: |
---|
19 | n/a | |
---|
20 | n/a | __version__ |
---|
21 | n/a | format_version |
---|
22 | n/a | compatible_formats |
---|
23 | n/a | |
---|
24 | n/a | """ |
---|
25 | n/a | |
---|
26 | n/a | from types import FunctionType |
---|
27 | n/a | from copyreg import dispatch_table |
---|
28 | n/a | from copyreg import _extension_registry, _inverted_registry, _extension_cache |
---|
29 | n/a | from itertools import islice |
---|
30 | n/a | from functools import partial |
---|
31 | n/a | import sys |
---|
32 | n/a | from sys import maxsize |
---|
33 | n/a | from struct import pack, unpack |
---|
34 | n/a | import re |
---|
35 | n/a | import io |
---|
36 | n/a | import codecs |
---|
37 | n/a | import _compat_pickle |
---|
38 | n/a | |
---|
39 | n/a | __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", |
---|
40 | n/a | "Unpickler", "dump", "dumps", "load", "loads"] |
---|
41 | n/a | |
---|
42 | n/a | # Shortcut for use in isinstance testing |
---|
43 | n/a | bytes_types = (bytes, bytearray) |
---|
44 | n/a | |
---|
45 | n/a | # These are purely informational; no code uses these. |
---|
46 | n/a | format_version = "4.0" # File format version we write |
---|
47 | n/a | compatible_formats = ["1.0", # Original protocol 0 |
---|
48 | n/a | "1.1", # Protocol 0 with INST added |
---|
49 | n/a | "1.2", # Original protocol 1 |
---|
50 | n/a | "1.3", # Protocol 1 with BINFLOAT added |
---|
51 | n/a | "2.0", # Protocol 2 |
---|
52 | n/a | "3.0", # Protocol 3 |
---|
53 | n/a | "4.0", # Protocol 4 |
---|
54 | n/a | ] # Old format versions we can read |
---|
55 | n/a | |
---|
56 | n/a | # This is the highest protocol number we know how to read. |
---|
57 | n/a | HIGHEST_PROTOCOL = 4 |
---|
58 | n/a | |
---|
59 | n/a | # The protocol we write by default. May be less than HIGHEST_PROTOCOL. |
---|
60 | n/a | # We intentionally write a protocol that Python 2.x cannot read; |
---|
61 | n/a | # there are too many issues with that. |
---|
62 | n/a | DEFAULT_PROTOCOL = 3 |
---|
63 | n/a | |
---|
64 | n/a | class PickleError(Exception): |
---|
65 | n/a | """A common base class for the other pickling exceptions.""" |
---|
66 | n/a | pass |
---|
67 | n/a | |
---|
68 | n/a | class PicklingError(PickleError): |
---|
69 | n/a | """This exception is raised when an unpicklable object is passed to the |
---|
70 | n/a | dump() method. |
---|
71 | n/a | |
---|
72 | n/a | """ |
---|
73 | n/a | pass |
---|
74 | n/a | |
---|
75 | n/a | class UnpicklingError(PickleError): |
---|
76 | n/a | """This exception is raised when there is a problem unpickling an object, |
---|
77 | n/a | such as a security violation. |
---|
78 | n/a | |
---|
79 | n/a | Note that other exceptions may also be raised during unpickling, including |
---|
80 | n/a | (but not necessarily limited to) AttributeError, EOFError, ImportError, |
---|
81 | n/a | and IndexError. |
---|
82 | n/a | |
---|
83 | n/a | """ |
---|
84 | n/a | pass |
---|
85 | n/a | |
---|
86 | n/a | # An instance of _Stop is raised by Unpickler.load_stop() in response to |
---|
87 | n/a | # the STOP opcode, passing the object that is the result of unpickling. |
---|
88 | n/a | class _Stop(Exception): |
---|
89 | n/a | def __init__(self, value): |
---|
90 | n/a | self.value = value |
---|
91 | n/a | |
---|
92 | n/a | # Jython has PyStringMap; it's a dict subclass with string keys |
---|
93 | n/a | try: |
---|
94 | n/a | from org.python.core import PyStringMap |
---|
95 | n/a | except ImportError: |
---|
96 | n/a | PyStringMap = None |
---|
97 | n/a | |
---|
98 | n/a | # Pickle opcodes. See pickletools.py for extensive docs. The listing |
---|
99 | n/a | # here is in kind-of alphabetical order of 1-character pickle code. |
---|
100 | n/a | # pickletools groups them by purpose. |
---|
101 | n/a | |
---|
102 | n/a | MARK = b'(' # push special markobject on stack |
---|
103 | n/a | STOP = b'.' # every pickle ends with STOP |
---|
104 | n/a | POP = b'0' # discard topmost stack item |
---|
105 | n/a | POP_MARK = b'1' # discard stack top through topmost markobject |
---|
106 | n/a | DUP = b'2' # duplicate top stack item |
---|
107 | n/a | FLOAT = b'F' # push float object; decimal string argument |
---|
108 | n/a | INT = b'I' # push integer or bool; decimal string argument |
---|
109 | n/a | BININT = b'J' # push four-byte signed int |
---|
110 | n/a | BININT1 = b'K' # push 1-byte unsigned int |
---|
111 | n/a | LONG = b'L' # push long; decimal string argument |
---|
112 | n/a | BININT2 = b'M' # push 2-byte unsigned int |
---|
113 | n/a | NONE = b'N' # push None |
---|
114 | n/a | PERSID = b'P' # push persistent object; id is taken from string arg |
---|
115 | n/a | BINPERSID = b'Q' # " " " ; " " " " stack |
---|
116 | n/a | REDUCE = b'R' # apply callable to argtuple, both on stack |
---|
117 | n/a | STRING = b'S' # push string; NL-terminated string argument |
---|
118 | n/a | BINSTRING = b'T' # push string; counted binary string argument |
---|
119 | n/a | SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes |
---|
120 | n/a | UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument |
---|
121 | n/a | BINUNICODE = b'X' # " " " ; counted UTF-8 string argument |
---|
122 | n/a | APPEND = b'a' # append stack top to list below it |
---|
123 | n/a | BUILD = b'b' # call __setstate__ or __dict__.update() |
---|
124 | n/a | GLOBAL = b'c' # push self.find_class(modname, name); 2 string args |
---|
125 | n/a | DICT = b'd' # build a dict from stack items |
---|
126 | n/a | EMPTY_DICT = b'}' # push empty dict |
---|
127 | n/a | APPENDS = b'e' # extend list on stack by topmost stack slice |
---|
128 | n/a | GET = b'g' # push item from memo on stack; index is string arg |
---|
129 | n/a | BINGET = b'h' # " " " " " " ; " " 1-byte arg |
---|
130 | n/a | INST = b'i' # build & push class instance |
---|
131 | n/a | LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg |
---|
132 | n/a | LIST = b'l' # build list from topmost stack items |
---|
133 | n/a | EMPTY_LIST = b']' # push empty list |
---|
134 | n/a | OBJ = b'o' # build & push class instance |
---|
135 | n/a | PUT = b'p' # store stack top in memo; index is string arg |
---|
136 | n/a | BINPUT = b'q' # " " " " " ; " " 1-byte arg |
---|
137 | n/a | LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg |
---|
138 | n/a | SETITEM = b's' # add key+value pair to dict |
---|
139 | n/a | TUPLE = b't' # build tuple from topmost stack items |
---|
140 | n/a | EMPTY_TUPLE = b')' # push empty tuple |
---|
141 | n/a | SETITEMS = b'u' # modify dict by adding topmost key+value pairs |
---|
142 | n/a | BINFLOAT = b'G' # push float; arg is 8-byte float encoding |
---|
143 | n/a | |
---|
144 | n/a | TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py |
---|
145 | n/a | FALSE = b'I00\n' # not an opcode; see INT docs in pickletools.py |
---|
146 | n/a | |
---|
147 | n/a | # Protocol 2 |
---|
148 | n/a | |
---|
149 | n/a | PROTO = b'\x80' # identify pickle protocol |
---|
150 | n/a | NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple |
---|
151 | n/a | EXT1 = b'\x82' # push object from extension registry; 1-byte index |
---|
152 | n/a | EXT2 = b'\x83' # ditto, but 2-byte index |
---|
153 | n/a | EXT4 = b'\x84' # ditto, but 4-byte index |
---|
154 | n/a | TUPLE1 = b'\x85' # build 1-tuple from stack top |
---|
155 | n/a | TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items |
---|
156 | n/a | TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items |
---|
157 | n/a | NEWTRUE = b'\x88' # push True |
---|
158 | n/a | NEWFALSE = b'\x89' # push False |
---|
159 | n/a | LONG1 = b'\x8a' # push long from < 256 bytes |
---|
160 | n/a | LONG4 = b'\x8b' # push really big long |
---|
161 | n/a | |
---|
162 | n/a | _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
---|
163 | n/a | |
---|
164 | n/a | # Protocol 3 (Python 3.x) |
---|
165 | n/a | |
---|
166 | n/a | BINBYTES = b'B' # push bytes; counted binary string argument |
---|
167 | n/a | SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes |
---|
168 | n/a | |
---|
169 | n/a | # Protocol 4 |
---|
170 | n/a | SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes |
---|
171 | n/a | BINUNICODE8 = b'\x8d' # push very long string |
---|
172 | n/a | BINBYTES8 = b'\x8e' # push very long bytes string |
---|
173 | n/a | EMPTY_SET = b'\x8f' # push empty set on the stack |
---|
174 | n/a | ADDITEMS = b'\x90' # modify set by adding topmost stack items |
---|
175 | n/a | FROZENSET = b'\x91' # build frozenset from topmost stack items |
---|
176 | n/a | NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments |
---|
177 | n/a | STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks |
---|
178 | n/a | MEMOIZE = b'\x94' # store top of the stack in memo |
---|
179 | n/a | FRAME = b'\x95' # indicate the beginning of a new frame |
---|
180 | n/a | |
---|
181 | n/a | __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)]) |
---|
182 | n/a | |
---|
183 | n/a | |
---|
184 | n/a | class _Framer: |
---|
185 | n/a | |
---|
186 | n/a | _FRAME_SIZE_TARGET = 64 * 1024 |
---|
187 | n/a | |
---|
188 | n/a | def __init__(self, file_write): |
---|
189 | n/a | self.file_write = file_write |
---|
190 | n/a | self.current_frame = None |
---|
191 | n/a | |
---|
192 | n/a | def start_framing(self): |
---|
193 | n/a | self.current_frame = io.BytesIO() |
---|
194 | n/a | |
---|
195 | n/a | def end_framing(self): |
---|
196 | n/a | if self.current_frame and self.current_frame.tell() > 0: |
---|
197 | n/a | self.commit_frame(force=True) |
---|
198 | n/a | self.current_frame = None |
---|
199 | n/a | |
---|
200 | n/a | def commit_frame(self, force=False): |
---|
201 | n/a | if self.current_frame: |
---|
202 | n/a | f = self.current_frame |
---|
203 | n/a | if f.tell() >= self._FRAME_SIZE_TARGET or force: |
---|
204 | n/a | with f.getbuffer() as data: |
---|
205 | n/a | n = len(data) |
---|
206 | n/a | write = self.file_write |
---|
207 | n/a | write(FRAME) |
---|
208 | n/a | write(pack("<Q", n)) |
---|
209 | n/a | write(data) |
---|
210 | n/a | f.seek(0) |
---|
211 | n/a | f.truncate() |
---|
212 | n/a | |
---|
213 | n/a | def write(self, data): |
---|
214 | n/a | if self.current_frame: |
---|
215 | n/a | return self.current_frame.write(data) |
---|
216 | n/a | else: |
---|
217 | n/a | return self.file_write(data) |
---|
218 | n/a | |
---|
219 | n/a | |
---|
220 | n/a | class _Unframer: |
---|
221 | n/a | |
---|
222 | n/a | def __init__(self, file_read, file_readline, file_tell=None): |
---|
223 | n/a | self.file_read = file_read |
---|
224 | n/a | self.file_readline = file_readline |
---|
225 | n/a | self.current_frame = None |
---|
226 | n/a | |
---|
227 | n/a | def read(self, n): |
---|
228 | n/a | if self.current_frame: |
---|
229 | n/a | data = self.current_frame.read(n) |
---|
230 | n/a | if not data and n != 0: |
---|
231 | n/a | self.current_frame = None |
---|
232 | n/a | return self.file_read(n) |
---|
233 | n/a | if len(data) < n: |
---|
234 | n/a | raise UnpicklingError( |
---|
235 | n/a | "pickle exhausted before end of frame") |
---|
236 | n/a | return data |
---|
237 | n/a | else: |
---|
238 | n/a | return self.file_read(n) |
---|
239 | n/a | |
---|
240 | n/a | def readline(self): |
---|
241 | n/a | if self.current_frame: |
---|
242 | n/a | data = self.current_frame.readline() |
---|
243 | n/a | if not data: |
---|
244 | n/a | self.current_frame = None |
---|
245 | n/a | return self.file_readline() |
---|
246 | n/a | if data[-1] != b'\n'[0]: |
---|
247 | n/a | raise UnpicklingError( |
---|
248 | n/a | "pickle exhausted before end of frame") |
---|
249 | n/a | return data |
---|
250 | n/a | else: |
---|
251 | n/a | return self.file_readline() |
---|
252 | n/a | |
---|
253 | n/a | def load_frame(self, frame_size): |
---|
254 | n/a | if self.current_frame and self.current_frame.read() != b'': |
---|
255 | n/a | raise UnpicklingError( |
---|
256 | n/a | "beginning of a new frame before end of current frame") |
---|
257 | n/a | self.current_frame = io.BytesIO(self.file_read(frame_size)) |
---|
258 | n/a | |
---|
259 | n/a | |
---|
260 | n/a | # Tools used for pickling. |
---|
261 | n/a | |
---|
262 | n/a | def _getattribute(obj, name): |
---|
263 | n/a | for subpath in name.split('.'): |
---|
264 | n/a | if subpath == '<locals>': |
---|
265 | n/a | raise AttributeError("Can't get local attribute {!r} on {!r}" |
---|
266 | n/a | .format(name, obj)) |
---|
267 | n/a | try: |
---|
268 | n/a | parent = obj |
---|
269 | n/a | obj = getattr(obj, subpath) |
---|
270 | n/a | except AttributeError: |
---|
271 | n/a | raise AttributeError("Can't get attribute {!r} on {!r}" |
---|
272 | n/a | .format(name, obj)) |
---|
273 | n/a | return obj, parent |
---|
274 | n/a | |
---|
275 | n/a | def whichmodule(obj, name): |
---|
276 | n/a | """Find the module an object belong to.""" |
---|
277 | n/a | module_name = getattr(obj, '__module__', None) |
---|
278 | n/a | if module_name is not None: |
---|
279 | n/a | return module_name |
---|
280 | n/a | # Protect the iteration by using a list copy of sys.modules against dynamic |
---|
281 | n/a | # modules that trigger imports of other modules upon calls to getattr. |
---|
282 | n/a | for module_name, module in list(sys.modules.items()): |
---|
283 | n/a | if module_name == '__main__' or module is None: |
---|
284 | n/a | continue |
---|
285 | n/a | try: |
---|
286 | n/a | if _getattribute(module, name)[0] is obj: |
---|
287 | n/a | return module_name |
---|
288 | n/a | except AttributeError: |
---|
289 | n/a | pass |
---|
290 | n/a | return '__main__' |
---|
291 | n/a | |
---|
292 | n/a | def encode_long(x): |
---|
293 | n/a | r"""Encode a long to a two's complement little-endian binary string. |
---|
294 | n/a | Note that 0 is a special case, returning an empty string, to save a |
---|
295 | n/a | byte in the LONG1 pickling context. |
---|
296 | n/a | |
---|
297 | n/a | >>> encode_long(0) |
---|
298 | n/a | b'' |
---|
299 | n/a | >>> encode_long(255) |
---|
300 | n/a | b'\xff\x00' |
---|
301 | n/a | >>> encode_long(32767) |
---|
302 | n/a | b'\xff\x7f' |
---|
303 | n/a | >>> encode_long(-256) |
---|
304 | n/a | b'\x00\xff' |
---|
305 | n/a | >>> encode_long(-32768) |
---|
306 | n/a | b'\x00\x80' |
---|
307 | n/a | >>> encode_long(-128) |
---|
308 | n/a | b'\x80' |
---|
309 | n/a | >>> encode_long(127) |
---|
310 | n/a | b'\x7f' |
---|
311 | n/a | >>> |
---|
312 | n/a | """ |
---|
313 | n/a | if x == 0: |
---|
314 | n/a | return b'' |
---|
315 | n/a | nbytes = (x.bit_length() >> 3) + 1 |
---|
316 | n/a | result = x.to_bytes(nbytes, byteorder='little', signed=True) |
---|
317 | n/a | if x < 0 and nbytes > 1: |
---|
318 | n/a | if result[-1] == 0xff and (result[-2] & 0x80) != 0: |
---|
319 | n/a | result = result[:-1] |
---|
320 | n/a | return result |
---|
321 | n/a | |
---|
322 | n/a | def decode_long(data): |
---|
323 | n/a | r"""Decode a long from a two's complement little-endian binary string. |
---|
324 | n/a | |
---|
325 | n/a | >>> decode_long(b'') |
---|
326 | n/a | 0 |
---|
327 | n/a | >>> decode_long(b"\xff\x00") |
---|
328 | n/a | 255 |
---|
329 | n/a | >>> decode_long(b"\xff\x7f") |
---|
330 | n/a | 32767 |
---|
331 | n/a | >>> decode_long(b"\x00\xff") |
---|
332 | n/a | -256 |
---|
333 | n/a | >>> decode_long(b"\x00\x80") |
---|
334 | n/a | -32768 |
---|
335 | n/a | >>> decode_long(b"\x80") |
---|
336 | n/a | -128 |
---|
337 | n/a | >>> decode_long(b"\x7f") |
---|
338 | n/a | 127 |
---|
339 | n/a | """ |
---|
340 | n/a | return int.from_bytes(data, byteorder='little', signed=True) |
---|
341 | n/a | |
---|
342 | n/a | |
---|
343 | n/a | # Pickling machinery |
---|
344 | n/a | |
---|
345 | n/a | class _Pickler: |
---|
346 | n/a | |
---|
347 | n/a | def __init__(self, file, protocol=None, *, fix_imports=True): |
---|
348 | n/a | """This takes a binary file for writing a pickle data stream. |
---|
349 | n/a | |
---|
350 | n/a | The optional *protocol* argument tells the pickler to use the |
---|
351 | n/a | given protocol; supported protocols are 0, 1, 2, 3 and 4. The |
---|
352 | n/a | default protocol is 3; a backward-incompatible protocol designed |
---|
353 | n/a | for Python 3. |
---|
354 | n/a | |
---|
355 | n/a | Specifying a negative protocol version selects the highest |
---|
356 | n/a | protocol version supported. The higher the protocol used, the |
---|
357 | n/a | more recent the version of Python needed to read the pickle |
---|
358 | n/a | produced. |
---|
359 | n/a | |
---|
360 | n/a | The *file* argument must have a write() method that accepts a |
---|
361 | n/a | single bytes argument. It can thus be a file object opened for |
---|
362 | n/a | binary writing, an io.BytesIO instance, or any other custom |
---|
363 | n/a | object that meets this interface. |
---|
364 | n/a | |
---|
365 | n/a | If *fix_imports* is True and *protocol* is less than 3, pickle |
---|
366 | n/a | will try to map the new Python 3 names to the old module names |
---|
367 | n/a | used in Python 2, so that the pickle data stream is readable |
---|
368 | n/a | with Python 2. |
---|
369 | n/a | """ |
---|
370 | n/a | if protocol is None: |
---|
371 | n/a | protocol = DEFAULT_PROTOCOL |
---|
372 | n/a | if protocol < 0: |
---|
373 | n/a | protocol = HIGHEST_PROTOCOL |
---|
374 | n/a | elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
---|
375 | n/a | raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL) |
---|
376 | n/a | try: |
---|
377 | n/a | self._file_write = file.write |
---|
378 | n/a | except AttributeError: |
---|
379 | n/a | raise TypeError("file must have a 'write' attribute") |
---|
380 | n/a | self.framer = _Framer(self._file_write) |
---|
381 | n/a | self.write = self.framer.write |
---|
382 | n/a | self.memo = {} |
---|
383 | n/a | self.proto = int(protocol) |
---|
384 | n/a | self.bin = protocol >= 1 |
---|
385 | n/a | self.fast = 0 |
---|
386 | n/a | self.fix_imports = fix_imports and protocol < 3 |
---|
387 | n/a | |
---|
388 | n/a | def clear_memo(self): |
---|
389 | n/a | """Clears the pickler's "memo". |
---|
390 | n/a | |
---|
391 | n/a | The memo is the data structure that remembers which objects the |
---|
392 | n/a | pickler has already seen, so that shared or recursive objects |
---|
393 | n/a | are pickled by reference and not by value. This method is |
---|
394 | n/a | useful when re-using picklers. |
---|
395 | n/a | """ |
---|
396 | n/a | self.memo.clear() |
---|
397 | n/a | |
---|
398 | n/a | def dump(self, obj): |
---|
399 | n/a | """Write a pickled representation of obj to the open file.""" |
---|
400 | n/a | # Check whether Pickler was initialized correctly. This is |
---|
401 | n/a | # only needed to mimic the behavior of _pickle.Pickler.dump(). |
---|
402 | n/a | if not hasattr(self, "_file_write"): |
---|
403 | n/a | raise PicklingError("Pickler.__init__() was not called by " |
---|
404 | n/a | "%s.__init__()" % (self.__class__.__name__,)) |
---|
405 | n/a | if self.proto >= 2: |
---|
406 | n/a | self.write(PROTO + pack("<B", self.proto)) |
---|
407 | n/a | if self.proto >= 4: |
---|
408 | n/a | self.framer.start_framing() |
---|
409 | n/a | self.save(obj) |
---|
410 | n/a | self.write(STOP) |
---|
411 | n/a | self.framer.end_framing() |
---|
412 | n/a | |
---|
413 | n/a | def memoize(self, obj): |
---|
414 | n/a | """Store an object in the memo.""" |
---|
415 | n/a | |
---|
416 | n/a | # The Pickler memo is a dictionary mapping object ids to 2-tuples |
---|
417 | n/a | # that contain the Unpickler memo key and the object being memoized. |
---|
418 | n/a | # The memo key is written to the pickle and will become |
---|
419 | n/a | # the key in the Unpickler's memo. The object is stored in the |
---|
420 | n/a | # Pickler memo so that transient objects are kept alive during |
---|
421 | n/a | # pickling. |
---|
422 | n/a | |
---|
423 | n/a | # The use of the Unpickler memo length as the memo key is just a |
---|
424 | n/a | # convention. The only requirement is that the memo values be unique. |
---|
425 | n/a | # But there appears no advantage to any other scheme, and this |
---|
426 | n/a | # scheme allows the Unpickler memo to be implemented as a plain (but |
---|
427 | n/a | # growable) array, indexed by memo key. |
---|
428 | n/a | if self.fast: |
---|
429 | n/a | return |
---|
430 | n/a | assert id(obj) not in self.memo |
---|
431 | n/a | idx = len(self.memo) |
---|
432 | n/a | self.write(self.put(idx)) |
---|
433 | n/a | self.memo[id(obj)] = idx, obj |
---|
434 | n/a | |
---|
435 | n/a | # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. |
---|
436 | n/a | def put(self, idx): |
---|
437 | n/a | if self.proto >= 4: |
---|
438 | n/a | return MEMOIZE |
---|
439 | n/a | elif self.bin: |
---|
440 | n/a | if idx < 256: |
---|
441 | n/a | return BINPUT + pack("<B", idx) |
---|
442 | n/a | else: |
---|
443 | n/a | return LONG_BINPUT + pack("<I", idx) |
---|
444 | n/a | else: |
---|
445 | n/a | return PUT + repr(idx).encode("ascii") + b'\n' |
---|
446 | n/a | |
---|
447 | n/a | # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. |
---|
448 | n/a | def get(self, i): |
---|
449 | n/a | if self.bin: |
---|
450 | n/a | if i < 256: |
---|
451 | n/a | return BINGET + pack("<B", i) |
---|
452 | n/a | else: |
---|
453 | n/a | return LONG_BINGET + pack("<I", i) |
---|
454 | n/a | |
---|
455 | n/a | return GET + repr(i).encode("ascii") + b'\n' |
---|
456 | n/a | |
---|
457 | n/a | def save(self, obj, save_persistent_id=True): |
---|
458 | n/a | self.framer.commit_frame() |
---|
459 | n/a | |
---|
460 | n/a | # Check for persistent id (defined by a subclass) |
---|
461 | n/a | pid = self.persistent_id(obj) |
---|
462 | n/a | if pid is not None and save_persistent_id: |
---|
463 | n/a | self.save_pers(pid) |
---|
464 | n/a | return |
---|
465 | n/a | |
---|
466 | n/a | # Check the memo |
---|
467 | n/a | x = self.memo.get(id(obj)) |
---|
468 | n/a | if x is not None: |
---|
469 | n/a | self.write(self.get(x[0])) |
---|
470 | n/a | return |
---|
471 | n/a | |
---|
472 | n/a | # Check the type dispatch table |
---|
473 | n/a | t = type(obj) |
---|
474 | n/a | f = self.dispatch.get(t) |
---|
475 | n/a | if f is not None: |
---|
476 | n/a | f(self, obj) # Call unbound method with explicit self |
---|
477 | n/a | return |
---|
478 | n/a | |
---|
479 | n/a | # Check private dispatch table if any, or else copyreg.dispatch_table |
---|
480 | n/a | reduce = getattr(self, 'dispatch_table', dispatch_table).get(t) |
---|
481 | n/a | if reduce is not None: |
---|
482 | n/a | rv = reduce(obj) |
---|
483 | n/a | else: |
---|
484 | n/a | # Check for a class with a custom metaclass; treat as regular class |
---|
485 | n/a | try: |
---|
486 | n/a | issc = issubclass(t, type) |
---|
487 | n/a | except TypeError: # t is not a class (old Boost; see SF #502085) |
---|
488 | n/a | issc = False |
---|
489 | n/a | if issc: |
---|
490 | n/a | self.save_global(obj) |
---|
491 | n/a | return |
---|
492 | n/a | |
---|
493 | n/a | # Check for a __reduce_ex__ method, fall back to __reduce__ |
---|
494 | n/a | reduce = getattr(obj, "__reduce_ex__", None) |
---|
495 | n/a | if reduce is not None: |
---|
496 | n/a | rv = reduce(self.proto) |
---|
497 | n/a | else: |
---|
498 | n/a | reduce = getattr(obj, "__reduce__", None) |
---|
499 | n/a | if reduce is not None: |
---|
500 | n/a | rv = reduce() |
---|
501 | n/a | else: |
---|
502 | n/a | raise PicklingError("Can't pickle %r object: %r" % |
---|
503 | n/a | (t.__name__, obj)) |
---|
504 | n/a | |
---|
505 | n/a | # Check for string returned by reduce(), meaning "save as global" |
---|
506 | n/a | if isinstance(rv, str): |
---|
507 | n/a | self.save_global(obj, rv) |
---|
508 | n/a | return |
---|
509 | n/a | |
---|
510 | n/a | # Assert that reduce() returned a tuple |
---|
511 | n/a | if not isinstance(rv, tuple): |
---|
512 | n/a | raise PicklingError("%s must return string or tuple" % reduce) |
---|
513 | n/a | |
---|
514 | n/a | # Assert that it returned an appropriately sized tuple |
---|
515 | n/a | l = len(rv) |
---|
516 | n/a | if not (2 <= l <= 5): |
---|
517 | n/a | raise PicklingError("Tuple returned by %s must have " |
---|
518 | n/a | "two to five elements" % reduce) |
---|
519 | n/a | |
---|
520 | n/a | # Save the reduce() output and finally memoize the object |
---|
521 | n/a | self.save_reduce(obj=obj, *rv) |
---|
522 | n/a | |
---|
523 | n/a | def persistent_id(self, obj): |
---|
524 | n/a | # This exists so a subclass can override it |
---|
525 | n/a | return None |
---|
526 | n/a | |
---|
527 | n/a | def save_pers(self, pid): |
---|
528 | n/a | # Save a persistent id reference |
---|
529 | n/a | if self.bin: |
---|
530 | n/a | self.save(pid, save_persistent_id=False) |
---|
531 | n/a | self.write(BINPERSID) |
---|
532 | n/a | else: |
---|
533 | n/a | try: |
---|
534 | n/a | self.write(PERSID + str(pid).encode("ascii") + b'\n') |
---|
535 | n/a | except UnicodeEncodeError: |
---|
536 | n/a | raise PicklingError( |
---|
537 | n/a | "persistent IDs in protocol 0 must be ASCII strings") |
---|
538 | n/a | |
---|
539 | n/a | def save_reduce(self, func, args, state=None, listitems=None, |
---|
540 | n/a | dictitems=None, obj=None): |
---|
541 | n/a | # This API is called by some subclasses |
---|
542 | n/a | |
---|
543 | n/a | if not isinstance(args, tuple): |
---|
544 | n/a | raise PicklingError("args from save_reduce() must be a tuple") |
---|
545 | n/a | if not callable(func): |
---|
546 | n/a | raise PicklingError("func from save_reduce() must be callable") |
---|
547 | n/a | |
---|
548 | n/a | save = self.save |
---|
549 | n/a | write = self.write |
---|
550 | n/a | |
---|
551 | n/a | func_name = getattr(func, "__name__", "") |
---|
552 | n/a | if self.proto >= 2 and func_name == "__newobj_ex__": |
---|
553 | n/a | cls, args, kwargs = args |
---|
554 | n/a | if not hasattr(cls, "__new__"): |
---|
555 | n/a | raise PicklingError("args[0] from {} args has no __new__" |
---|
556 | n/a | .format(func_name)) |
---|
557 | n/a | if obj is not None and cls is not obj.__class__: |
---|
558 | n/a | raise PicklingError("args[0] from {} args has the wrong class" |
---|
559 | n/a | .format(func_name)) |
---|
560 | n/a | if self.proto >= 4: |
---|
561 | n/a | save(cls) |
---|
562 | n/a | save(args) |
---|
563 | n/a | save(kwargs) |
---|
564 | n/a | write(NEWOBJ_EX) |
---|
565 | n/a | else: |
---|
566 | n/a | func = partial(cls.__new__, cls, *args, **kwargs) |
---|
567 | n/a | save(func) |
---|
568 | n/a | save(()) |
---|
569 | n/a | write(REDUCE) |
---|
570 | n/a | elif self.proto >= 2 and func_name == "__newobj__": |
---|
571 | n/a | # A __reduce__ implementation can direct protocol 2 or newer to |
---|
572 | n/a | # use the more efficient NEWOBJ opcode, while still |
---|
573 | n/a | # allowing protocol 0 and 1 to work normally. For this to |
---|
574 | n/a | # work, the function returned by __reduce__ should be |
---|
575 | n/a | # called __newobj__, and its first argument should be a |
---|
576 | n/a | # class. The implementation for __newobj__ |
---|
577 | n/a | # should be as follows, although pickle has no way to |
---|
578 | n/a | # verify this: |
---|
579 | n/a | # |
---|
580 | n/a | # def __newobj__(cls, *args): |
---|
581 | n/a | # return cls.__new__(cls, *args) |
---|
582 | n/a | # |
---|
583 | n/a | # Protocols 0 and 1 will pickle a reference to __newobj__, |
---|
584 | n/a | # while protocol 2 (and above) will pickle a reference to |
---|
585 | n/a | # cls, the remaining args tuple, and the NEWOBJ code, |
---|
586 | n/a | # which calls cls.__new__(cls, *args) at unpickling time |
---|
587 | n/a | # (see load_newobj below). If __reduce__ returns a |
---|
588 | n/a | # three-tuple, the state from the third tuple item will be |
---|
589 | n/a | # pickled regardless of the protocol, calling __setstate__ |
---|
590 | n/a | # at unpickling time (see load_build below). |
---|
591 | n/a | # |
---|
592 | n/a | # Note that no standard __newobj__ implementation exists; |
---|
593 | n/a | # you have to provide your own. This is to enforce |
---|
594 | n/a | # compatibility with Python 2.2 (pickles written using |
---|
595 | n/a | # protocol 0 or 1 in Python 2.3 should be unpicklable by |
---|
596 | n/a | # Python 2.2). |
---|
597 | n/a | cls = args[0] |
---|
598 | n/a | if not hasattr(cls, "__new__"): |
---|
599 | n/a | raise PicklingError( |
---|
600 | n/a | "args[0] from __newobj__ args has no __new__") |
---|
601 | n/a | if obj is not None and cls is not obj.__class__: |
---|
602 | n/a | raise PicklingError( |
---|
603 | n/a | "args[0] from __newobj__ args has the wrong class") |
---|
604 | n/a | args = args[1:] |
---|
605 | n/a | save(cls) |
---|
606 | n/a | save(args) |
---|
607 | n/a | write(NEWOBJ) |
---|
608 | n/a | else: |
---|
609 | n/a | save(func) |
---|
610 | n/a | save(args) |
---|
611 | n/a | write(REDUCE) |
---|
612 | n/a | |
---|
613 | n/a | if obj is not None: |
---|
614 | n/a | # If the object is already in the memo, this means it is |
---|
615 | n/a | # recursive. In this case, throw away everything we put on the |
---|
616 | n/a | # stack, and fetch the object back from the memo. |
---|
617 | n/a | if id(obj) in self.memo: |
---|
618 | n/a | write(POP + self.get(self.memo[id(obj)][0])) |
---|
619 | n/a | else: |
---|
620 | n/a | self.memoize(obj) |
---|
621 | n/a | |
---|
622 | n/a | # More new special cases (that work with older protocols as |
---|
623 | n/a | # well): when __reduce__ returns a tuple with 4 or 5 items, |
---|
624 | n/a | # the 4th and 5th item should be iterators that provide list |
---|
625 | n/a | # items and dict items (as (key, value) tuples), or None. |
---|
626 | n/a | |
---|
627 | n/a | if listitems is not None: |
---|
628 | n/a | self._batch_appends(listitems) |
---|
629 | n/a | |
---|
630 | n/a | if dictitems is not None: |
---|
631 | n/a | self._batch_setitems(dictitems) |
---|
632 | n/a | |
---|
633 | n/a | if state is not None: |
---|
634 | n/a | save(state) |
---|
635 | n/a | write(BUILD) |
---|
636 | n/a | |
---|
637 | n/a | # Methods below this point are dispatched through the dispatch table |
---|
638 | n/a | |
---|
639 | n/a | dispatch = {} |
---|
640 | n/a | |
---|
641 | n/a | def save_none(self, obj): |
---|
642 | n/a | self.write(NONE) |
---|
643 | n/a | dispatch[type(None)] = save_none |
---|
644 | n/a | |
---|
645 | n/a | def save_bool(self, obj): |
---|
646 | n/a | if self.proto >= 2: |
---|
647 | n/a | self.write(NEWTRUE if obj else NEWFALSE) |
---|
648 | n/a | else: |
---|
649 | n/a | self.write(TRUE if obj else FALSE) |
---|
650 | n/a | dispatch[bool] = save_bool |
---|
651 | n/a | |
---|
652 | n/a | def save_long(self, obj): |
---|
653 | n/a | if self.bin: |
---|
654 | n/a | # If the int is small enough to fit in a signed 4-byte 2's-comp |
---|
655 | n/a | # format, we can store it more efficiently than the general |
---|
656 | n/a | # case. |
---|
657 | n/a | # First one- and two-byte unsigned ints: |
---|
658 | n/a | if obj >= 0: |
---|
659 | n/a | if obj <= 0xff: |
---|
660 | n/a | self.write(BININT1 + pack("<B", obj)) |
---|
661 | n/a | return |
---|
662 | n/a | if obj <= 0xffff: |
---|
663 | n/a | self.write(BININT2 + pack("<H", obj)) |
---|
664 | n/a | return |
---|
665 | n/a | # Next check for 4-byte signed ints: |
---|
666 | n/a | if -0x80000000 <= obj <= 0x7fffffff: |
---|
667 | n/a | self.write(BININT + pack("<i", obj)) |
---|
668 | n/a | return |
---|
669 | n/a | if self.proto >= 2: |
---|
670 | n/a | encoded = encode_long(obj) |
---|
671 | n/a | n = len(encoded) |
---|
672 | n/a | if n < 256: |
---|
673 | n/a | self.write(LONG1 + pack("<B", n) + encoded) |
---|
674 | n/a | else: |
---|
675 | n/a | self.write(LONG4 + pack("<i", n) + encoded) |
---|
676 | n/a | return |
---|
677 | n/a | self.write(LONG + repr(obj).encode("ascii") + b'L\n') |
---|
678 | n/a | dispatch[int] = save_long |
---|
679 | n/a | |
---|
680 | n/a | def save_float(self, obj): |
---|
681 | n/a | if self.bin: |
---|
682 | n/a | self.write(BINFLOAT + pack('>d', obj)) |
---|
683 | n/a | else: |
---|
684 | n/a | self.write(FLOAT + repr(obj).encode("ascii") + b'\n') |
---|
685 | n/a | dispatch[float] = save_float |
---|
686 | n/a | |
---|
687 | n/a | def save_bytes(self, obj): |
---|
688 | n/a | if self.proto < 3: |
---|
689 | n/a | if not obj: # bytes object is empty |
---|
690 | n/a | self.save_reduce(bytes, (), obj=obj) |
---|
691 | n/a | else: |
---|
692 | n/a | self.save_reduce(codecs.encode, |
---|
693 | n/a | (str(obj, 'latin1'), 'latin1'), obj=obj) |
---|
694 | n/a | return |
---|
695 | n/a | n = len(obj) |
---|
696 | n/a | if n <= 0xff: |
---|
697 | n/a | self.write(SHORT_BINBYTES + pack("<B", n) + obj) |
---|
698 | n/a | elif n > 0xffffffff and self.proto >= 4: |
---|
699 | n/a | self.write(BINBYTES8 + pack("<Q", n) + obj) |
---|
700 | n/a | else: |
---|
701 | n/a | self.write(BINBYTES + pack("<I", n) + obj) |
---|
702 | n/a | self.memoize(obj) |
---|
703 | n/a | dispatch[bytes] = save_bytes |
---|
704 | n/a | |
---|
705 | n/a | def save_str(self, obj): |
---|
706 | n/a | if self.bin: |
---|
707 | n/a | encoded = obj.encode('utf-8', 'surrogatepass') |
---|
708 | n/a | n = len(encoded) |
---|
709 | n/a | if n <= 0xff and self.proto >= 4: |
---|
710 | n/a | self.write(SHORT_BINUNICODE + pack("<B", n) + encoded) |
---|
711 | n/a | elif n > 0xffffffff and self.proto >= 4: |
---|
712 | n/a | self.write(BINUNICODE8 + pack("<Q", n) + encoded) |
---|
713 | n/a | else: |
---|
714 | n/a | self.write(BINUNICODE + pack("<I", n) + encoded) |
---|
715 | n/a | else: |
---|
716 | n/a | obj = obj.replace("\\", "\\u005c") |
---|
717 | n/a | obj = obj.replace("\n", "\\u000a") |
---|
718 | n/a | self.write(UNICODE + obj.encode('raw-unicode-escape') + |
---|
719 | n/a | b'\n') |
---|
720 | n/a | self.memoize(obj) |
---|
721 | n/a | dispatch[str] = save_str |
---|
722 | n/a | |
---|
723 | n/a | def save_tuple(self, obj): |
---|
724 | n/a | if not obj: # tuple is empty |
---|
725 | n/a | if self.bin: |
---|
726 | n/a | self.write(EMPTY_TUPLE) |
---|
727 | n/a | else: |
---|
728 | n/a | self.write(MARK + TUPLE) |
---|
729 | n/a | return |
---|
730 | n/a | |
---|
731 | n/a | n = len(obj) |
---|
732 | n/a | save = self.save |
---|
733 | n/a | memo = self.memo |
---|
734 | n/a | if n <= 3 and self.proto >= 2: |
---|
735 | n/a | for element in obj: |
---|
736 | n/a | save(element) |
---|
737 | n/a | # Subtle. Same as in the big comment below. |
---|
738 | n/a | if id(obj) in memo: |
---|
739 | n/a | get = self.get(memo[id(obj)][0]) |
---|
740 | n/a | self.write(POP * n + get) |
---|
741 | n/a | else: |
---|
742 | n/a | self.write(_tuplesize2code[n]) |
---|
743 | n/a | self.memoize(obj) |
---|
744 | n/a | return |
---|
745 | n/a | |
---|
746 | n/a | # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple |
---|
747 | n/a | # has more than 3 elements. |
---|
748 | n/a | write = self.write |
---|
749 | n/a | write(MARK) |
---|
750 | n/a | for element in obj: |
---|
751 | n/a | save(element) |
---|
752 | n/a | |
---|
753 | n/a | if id(obj) in memo: |
---|
754 | n/a | # Subtle. d was not in memo when we entered save_tuple(), so |
---|
755 | n/a | # the process of saving the tuple's elements must have saved |
---|
756 | n/a | # the tuple itself: the tuple is recursive. The proper action |
---|
757 | n/a | # now is to throw away everything we put on the stack, and |
---|
758 | n/a | # simply GET the tuple (it's already constructed). This check |
---|
759 | n/a | # could have been done in the "for element" loop instead, but |
---|
760 | n/a | # recursive tuples are a rare thing. |
---|
761 | n/a | get = self.get(memo[id(obj)][0]) |
---|
762 | n/a | if self.bin: |
---|
763 | n/a | write(POP_MARK + get) |
---|
764 | n/a | else: # proto 0 -- POP_MARK not available |
---|
765 | n/a | write(POP * (n+1) + get) |
---|
766 | n/a | return |
---|
767 | n/a | |
---|
768 | n/a | # No recursion. |
---|
769 | n/a | write(TUPLE) |
---|
770 | n/a | self.memoize(obj) |
---|
771 | n/a | |
---|
772 | n/a | dispatch[tuple] = save_tuple |
---|
773 | n/a | |
---|
774 | n/a | def save_list(self, obj): |
---|
775 | n/a | if self.bin: |
---|
776 | n/a | self.write(EMPTY_LIST) |
---|
777 | n/a | else: # proto 0 -- can't use EMPTY_LIST |
---|
778 | n/a | self.write(MARK + LIST) |
---|
779 | n/a | |
---|
780 | n/a | self.memoize(obj) |
---|
781 | n/a | self._batch_appends(obj) |
---|
782 | n/a | |
---|
783 | n/a | dispatch[list] = save_list |
---|
784 | n/a | |
---|
785 | n/a | _BATCHSIZE = 1000 |
---|
786 | n/a | |
---|
787 | n/a | def _batch_appends(self, items): |
---|
788 | n/a | # Helper to batch up APPENDS sequences |
---|
789 | n/a | save = self.save |
---|
790 | n/a | write = self.write |
---|
791 | n/a | |
---|
792 | n/a | if not self.bin: |
---|
793 | n/a | for x in items: |
---|
794 | n/a | save(x) |
---|
795 | n/a | write(APPEND) |
---|
796 | n/a | return |
---|
797 | n/a | |
---|
798 | n/a | it = iter(items) |
---|
799 | n/a | while True: |
---|
800 | n/a | tmp = list(islice(it, self._BATCHSIZE)) |
---|
801 | n/a | n = len(tmp) |
---|
802 | n/a | if n > 1: |
---|
803 | n/a | write(MARK) |
---|
804 | n/a | for x in tmp: |
---|
805 | n/a | save(x) |
---|
806 | n/a | write(APPENDS) |
---|
807 | n/a | elif n: |
---|
808 | n/a | save(tmp[0]) |
---|
809 | n/a | write(APPEND) |
---|
810 | n/a | # else tmp is empty, and we're done |
---|
811 | n/a | if n < self._BATCHSIZE: |
---|
812 | n/a | return |
---|
813 | n/a | |
---|
814 | n/a | def save_dict(self, obj): |
---|
815 | n/a | if self.bin: |
---|
816 | n/a | self.write(EMPTY_DICT) |
---|
817 | n/a | else: # proto 0 -- can't use EMPTY_DICT |
---|
818 | n/a | self.write(MARK + DICT) |
---|
819 | n/a | |
---|
820 | n/a | self.memoize(obj) |
---|
821 | n/a | self._batch_setitems(obj.items()) |
---|
822 | n/a | |
---|
823 | n/a | dispatch[dict] = save_dict |
---|
824 | n/a | if PyStringMap is not None: |
---|
825 | n/a | dispatch[PyStringMap] = save_dict |
---|
826 | n/a | |
---|
827 | n/a | def _batch_setitems(self, items): |
---|
828 | n/a | # Helper to batch up SETITEMS sequences; proto >= 1 only |
---|
829 | n/a | save = self.save |
---|
830 | n/a | write = self.write |
---|
831 | n/a | |
---|
832 | n/a | if not self.bin: |
---|
833 | n/a | for k, v in items: |
---|
834 | n/a | save(k) |
---|
835 | n/a | save(v) |
---|
836 | n/a | write(SETITEM) |
---|
837 | n/a | return |
---|
838 | n/a | |
---|
839 | n/a | it = iter(items) |
---|
840 | n/a | while True: |
---|
841 | n/a | tmp = list(islice(it, self._BATCHSIZE)) |
---|
842 | n/a | n = len(tmp) |
---|
843 | n/a | if n > 1: |
---|
844 | n/a | write(MARK) |
---|
845 | n/a | for k, v in tmp: |
---|
846 | n/a | save(k) |
---|
847 | n/a | save(v) |
---|
848 | n/a | write(SETITEMS) |
---|
849 | n/a | elif n: |
---|
850 | n/a | k, v = tmp[0] |
---|
851 | n/a | save(k) |
---|
852 | n/a | save(v) |
---|
853 | n/a | write(SETITEM) |
---|
854 | n/a | # else tmp is empty, and we're done |
---|
855 | n/a | if n < self._BATCHSIZE: |
---|
856 | n/a | return |
---|
857 | n/a | |
---|
858 | n/a | def save_set(self, obj): |
---|
859 | n/a | save = self.save |
---|
860 | n/a | write = self.write |
---|
861 | n/a | |
---|
862 | n/a | if self.proto < 4: |
---|
863 | n/a | self.save_reduce(set, (list(obj),), obj=obj) |
---|
864 | n/a | return |
---|
865 | n/a | |
---|
866 | n/a | write(EMPTY_SET) |
---|
867 | n/a | self.memoize(obj) |
---|
868 | n/a | |
---|
869 | n/a | it = iter(obj) |
---|
870 | n/a | while True: |
---|
871 | n/a | batch = list(islice(it, self._BATCHSIZE)) |
---|
872 | n/a | n = len(batch) |
---|
873 | n/a | if n > 0: |
---|
874 | n/a | write(MARK) |
---|
875 | n/a | for item in batch: |
---|
876 | n/a | save(item) |
---|
877 | n/a | write(ADDITEMS) |
---|
878 | n/a | if n < self._BATCHSIZE: |
---|
879 | n/a | return |
---|
880 | n/a | dispatch[set] = save_set |
---|
881 | n/a | |
---|
882 | n/a | def save_frozenset(self, obj): |
---|
883 | n/a | save = self.save |
---|
884 | n/a | write = self.write |
---|
885 | n/a | |
---|
886 | n/a | if self.proto < 4: |
---|
887 | n/a | self.save_reduce(frozenset, (list(obj),), obj=obj) |
---|
888 | n/a | return |
---|
889 | n/a | |
---|
890 | n/a | write(MARK) |
---|
891 | n/a | for item in obj: |
---|
892 | n/a | save(item) |
---|
893 | n/a | |
---|
894 | n/a | if id(obj) in self.memo: |
---|
895 | n/a | # If the object is already in the memo, this means it is |
---|
896 | n/a | # recursive. In this case, throw away everything we put on the |
---|
897 | n/a | # stack, and fetch the object back from the memo. |
---|
898 | n/a | write(POP_MARK + self.get(self.memo[id(obj)][0])) |
---|
899 | n/a | return |
---|
900 | n/a | |
---|
901 | n/a | write(FROZENSET) |
---|
902 | n/a | self.memoize(obj) |
---|
903 | n/a | dispatch[frozenset] = save_frozenset |
---|
904 | n/a | |
---|
905 | n/a | def save_global(self, obj, name=None): |
---|
906 | n/a | write = self.write |
---|
907 | n/a | memo = self.memo |
---|
908 | n/a | |
---|
909 | n/a | if name is None: |
---|
910 | n/a | name = getattr(obj, '__qualname__', None) |
---|
911 | n/a | if name is None: |
---|
912 | n/a | name = obj.__name__ |
---|
913 | n/a | |
---|
914 | n/a | module_name = whichmodule(obj, name) |
---|
915 | n/a | try: |
---|
916 | n/a | __import__(module_name, level=0) |
---|
917 | n/a | module = sys.modules[module_name] |
---|
918 | n/a | obj2, parent = _getattribute(module, name) |
---|
919 | n/a | except (ImportError, KeyError, AttributeError): |
---|
920 | n/a | raise PicklingError( |
---|
921 | n/a | "Can't pickle %r: it's not found as %s.%s" % |
---|
922 | n/a | (obj, module_name, name)) |
---|
923 | n/a | else: |
---|
924 | n/a | if obj2 is not obj: |
---|
925 | n/a | raise PicklingError( |
---|
926 | n/a | "Can't pickle %r: it's not the same object as %s.%s" % |
---|
927 | n/a | (obj, module_name, name)) |
---|
928 | n/a | |
---|
929 | n/a | if self.proto >= 2: |
---|
930 | n/a | code = _extension_registry.get((module_name, name)) |
---|
931 | n/a | if code: |
---|
932 | n/a | assert code > 0 |
---|
933 | n/a | if code <= 0xff: |
---|
934 | n/a | write(EXT1 + pack("<B", code)) |
---|
935 | n/a | elif code <= 0xffff: |
---|
936 | n/a | write(EXT2 + pack("<H", code)) |
---|
937 | n/a | else: |
---|
938 | n/a | write(EXT4 + pack("<i", code)) |
---|
939 | n/a | return |
---|
940 | n/a | lastname = name.rpartition('.')[2] |
---|
941 | n/a | if parent is module: |
---|
942 | n/a | name = lastname |
---|
943 | n/a | # Non-ASCII identifiers are supported only with protocols >= 3. |
---|
944 | n/a | if self.proto >= 4: |
---|
945 | n/a | self.save(module_name) |
---|
946 | n/a | self.save(name) |
---|
947 | n/a | write(STACK_GLOBAL) |
---|
948 | n/a | elif parent is not module: |
---|
949 | n/a | self.save_reduce(getattr, (parent, lastname)) |
---|
950 | n/a | elif self.proto >= 3: |
---|
951 | n/a | write(GLOBAL + bytes(module_name, "utf-8") + b'\n' + |
---|
952 | n/a | bytes(name, "utf-8") + b'\n') |
---|
953 | n/a | else: |
---|
954 | n/a | if self.fix_imports: |
---|
955 | n/a | r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING |
---|
956 | n/a | r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING |
---|
957 | n/a | if (module_name, name) in r_name_mapping: |
---|
958 | n/a | module_name, name = r_name_mapping[(module_name, name)] |
---|
959 | n/a | elif module_name in r_import_mapping: |
---|
960 | n/a | module_name = r_import_mapping[module_name] |
---|
961 | n/a | try: |
---|
962 | n/a | write(GLOBAL + bytes(module_name, "ascii") + b'\n' + |
---|
963 | n/a | bytes(name, "ascii") + b'\n') |
---|
964 | n/a | except UnicodeEncodeError: |
---|
965 | n/a | raise PicklingError( |
---|
966 | n/a | "can't pickle global identifier '%s.%s' using " |
---|
967 | n/a | "pickle protocol %i" % (module, name, self.proto)) |
---|
968 | n/a | |
---|
969 | n/a | self.memoize(obj) |
---|
970 | n/a | |
---|
971 | n/a | def save_type(self, obj): |
---|
972 | n/a | if obj is type(None): |
---|
973 | n/a | return self.save_reduce(type, (None,), obj=obj) |
---|
974 | n/a | elif obj is type(NotImplemented): |
---|
975 | n/a | return self.save_reduce(type, (NotImplemented,), obj=obj) |
---|
976 | n/a | elif obj is type(...): |
---|
977 | n/a | return self.save_reduce(type, (...,), obj=obj) |
---|
978 | n/a | return self.save_global(obj) |
---|
979 | n/a | |
---|
980 | n/a | dispatch[FunctionType] = save_global |
---|
981 | n/a | dispatch[type] = save_type |
---|
982 | n/a | |
---|
983 | n/a | |
---|
984 | n/a | # Unpickling machinery |
---|
985 | n/a | |
---|
986 | n/a | class _Unpickler: |
---|
987 | n/a | |
---|
988 | n/a | def __init__(self, file, *, fix_imports=True, |
---|
989 | n/a | encoding="ASCII", errors="strict"): |
---|
990 | n/a | """This takes a binary file for reading a pickle data stream. |
---|
991 | n/a | |
---|
992 | n/a | The protocol version of the pickle is detected automatically, so |
---|
993 | n/a | no proto argument is needed. |
---|
994 | n/a | |
---|
995 | n/a | The argument *file* must have two methods, a read() method that |
---|
996 | n/a | takes an integer argument, and a readline() method that requires |
---|
997 | n/a | no arguments. Both methods should return bytes. Thus *file* |
---|
998 | n/a | can be a binary file object opened for reading, an io.BytesIO |
---|
999 | n/a | object, or any other custom object that meets this interface. |
---|
1000 | n/a | |
---|
1001 | n/a | The file-like object must have two methods, a read() method |
---|
1002 | n/a | that takes an integer argument, and a readline() method that |
---|
1003 | n/a | requires no arguments. Both methods should return bytes. |
---|
1004 | n/a | Thus file-like object can be a binary file object opened for |
---|
1005 | n/a | reading, a BytesIO object, or any other custom object that |
---|
1006 | n/a | meets this interface. |
---|
1007 | n/a | |
---|
1008 | n/a | Optional keyword arguments are *fix_imports*, *encoding* and |
---|
1009 | n/a | *errors*, which are used to control compatibility support for |
---|
1010 | n/a | pickle stream generated by Python 2. If *fix_imports* is True, |
---|
1011 | n/a | pickle will try to map the old Python 2 names to the new names |
---|
1012 | n/a | used in Python 3. The *encoding* and *errors* tell pickle how |
---|
1013 | n/a | to decode 8-bit string instances pickled by Python 2; these |
---|
1014 | n/a | default to 'ASCII' and 'strict', respectively. *encoding* can be |
---|
1015 | n/a | 'bytes' to read theses 8-bit string instances as bytes objects. |
---|
1016 | n/a | """ |
---|
1017 | n/a | self._file_readline = file.readline |
---|
1018 | n/a | self._file_read = file.read |
---|
1019 | n/a | self.memo = {} |
---|
1020 | n/a | self.encoding = encoding |
---|
1021 | n/a | self.errors = errors |
---|
1022 | n/a | self.proto = 0 |
---|
1023 | n/a | self.fix_imports = fix_imports |
---|
1024 | n/a | |
---|
1025 | n/a | def load(self): |
---|
1026 | n/a | """Read a pickled object representation from the open file. |
---|
1027 | n/a | |
---|
1028 | n/a | Return the reconstituted object hierarchy specified in the file. |
---|
1029 | n/a | """ |
---|
1030 | n/a | # Check whether Unpickler was initialized correctly. This is |
---|
1031 | n/a | # only needed to mimic the behavior of _pickle.Unpickler.dump(). |
---|
1032 | n/a | if not hasattr(self, "_file_read"): |
---|
1033 | n/a | raise UnpicklingError("Unpickler.__init__() was not called by " |
---|
1034 | n/a | "%s.__init__()" % (self.__class__.__name__,)) |
---|
1035 | n/a | self._unframer = _Unframer(self._file_read, self._file_readline) |
---|
1036 | n/a | self.read = self._unframer.read |
---|
1037 | n/a | self.readline = self._unframer.readline |
---|
1038 | n/a | self.metastack = [] |
---|
1039 | n/a | self.stack = [] |
---|
1040 | n/a | self.append = self.stack.append |
---|
1041 | n/a | self.proto = 0 |
---|
1042 | n/a | read = self.read |
---|
1043 | n/a | dispatch = self.dispatch |
---|
1044 | n/a | try: |
---|
1045 | n/a | while True: |
---|
1046 | n/a | key = read(1) |
---|
1047 | n/a | if not key: |
---|
1048 | n/a | raise EOFError |
---|
1049 | n/a | assert isinstance(key, bytes_types) |
---|
1050 | n/a | dispatch[key[0]](self) |
---|
1051 | n/a | except _Stop as stopinst: |
---|
1052 | n/a | return stopinst.value |
---|
1053 | n/a | |
---|
1054 | n/a | # Return a list of items pushed in the stack after last MARK instruction. |
---|
1055 | n/a | def pop_mark(self): |
---|
1056 | n/a | items = self.stack |
---|
1057 | n/a | self.stack = self.metastack.pop() |
---|
1058 | n/a | self.append = self.stack.append |
---|
1059 | n/a | return items |
---|
1060 | n/a | |
---|
1061 | n/a | def persistent_load(self, pid): |
---|
1062 | n/a | raise UnpicklingError("unsupported persistent id encountered") |
---|
1063 | n/a | |
---|
1064 | n/a | dispatch = {} |
---|
1065 | n/a | |
---|
1066 | n/a | def load_proto(self): |
---|
1067 | n/a | proto = self.read(1)[0] |
---|
1068 | n/a | if not 0 <= proto <= HIGHEST_PROTOCOL: |
---|
1069 | n/a | raise ValueError("unsupported pickle protocol: %d" % proto) |
---|
1070 | n/a | self.proto = proto |
---|
1071 | n/a | dispatch[PROTO[0]] = load_proto |
---|
1072 | n/a | |
---|
1073 | n/a | def load_frame(self): |
---|
1074 | n/a | frame_size, = unpack('<Q', self.read(8)) |
---|
1075 | n/a | if frame_size > sys.maxsize: |
---|
1076 | n/a | raise ValueError("frame size > sys.maxsize: %d" % frame_size) |
---|
1077 | n/a | self._unframer.load_frame(frame_size) |
---|
1078 | n/a | dispatch[FRAME[0]] = load_frame |
---|
1079 | n/a | |
---|
1080 | n/a | def load_persid(self): |
---|
1081 | n/a | try: |
---|
1082 | n/a | pid = self.readline()[:-1].decode("ascii") |
---|
1083 | n/a | except UnicodeDecodeError: |
---|
1084 | n/a | raise UnpicklingError( |
---|
1085 | n/a | "persistent IDs in protocol 0 must be ASCII strings") |
---|
1086 | n/a | self.append(self.persistent_load(pid)) |
---|
1087 | n/a | dispatch[PERSID[0]] = load_persid |
---|
1088 | n/a | |
---|
1089 | n/a | def load_binpersid(self): |
---|
1090 | n/a | pid = self.stack.pop() |
---|
1091 | n/a | self.append(self.persistent_load(pid)) |
---|
1092 | n/a | dispatch[BINPERSID[0]] = load_binpersid |
---|
1093 | n/a | |
---|
1094 | n/a | def load_none(self): |
---|
1095 | n/a | self.append(None) |
---|
1096 | n/a | dispatch[NONE[0]] = load_none |
---|
1097 | n/a | |
---|
1098 | n/a | def load_false(self): |
---|
1099 | n/a | self.append(False) |
---|
1100 | n/a | dispatch[NEWFALSE[0]] = load_false |
---|
1101 | n/a | |
---|
1102 | n/a | def load_true(self): |
---|
1103 | n/a | self.append(True) |
---|
1104 | n/a | dispatch[NEWTRUE[0]] = load_true |
---|
1105 | n/a | |
---|
1106 | n/a | def load_int(self): |
---|
1107 | n/a | data = self.readline() |
---|
1108 | n/a | if data == FALSE[1:]: |
---|
1109 | n/a | val = False |
---|
1110 | n/a | elif data == TRUE[1:]: |
---|
1111 | n/a | val = True |
---|
1112 | n/a | else: |
---|
1113 | n/a | val = int(data, 0) |
---|
1114 | n/a | self.append(val) |
---|
1115 | n/a | dispatch[INT[0]] = load_int |
---|
1116 | n/a | |
---|
1117 | n/a | def load_binint(self): |
---|
1118 | n/a | self.append(unpack('<i', self.read(4))[0]) |
---|
1119 | n/a | dispatch[BININT[0]] = load_binint |
---|
1120 | n/a | |
---|
1121 | n/a | def load_binint1(self): |
---|
1122 | n/a | self.append(self.read(1)[0]) |
---|
1123 | n/a | dispatch[BININT1[0]] = load_binint1 |
---|
1124 | n/a | |
---|
1125 | n/a | def load_binint2(self): |
---|
1126 | n/a | self.append(unpack('<H', self.read(2))[0]) |
---|
1127 | n/a | dispatch[BININT2[0]] = load_binint2 |
---|
1128 | n/a | |
---|
1129 | n/a | def load_long(self): |
---|
1130 | n/a | val = self.readline()[:-1] |
---|
1131 | n/a | if val and val[-1] == b'L'[0]: |
---|
1132 | n/a | val = val[:-1] |
---|
1133 | n/a | self.append(int(val, 0)) |
---|
1134 | n/a | dispatch[LONG[0]] = load_long |
---|
1135 | n/a | |
---|
1136 | n/a | def load_long1(self): |
---|
1137 | n/a | n = self.read(1)[0] |
---|
1138 | n/a | data = self.read(n) |
---|
1139 | n/a | self.append(decode_long(data)) |
---|
1140 | n/a | dispatch[LONG1[0]] = load_long1 |
---|
1141 | n/a | |
---|
1142 | n/a | def load_long4(self): |
---|
1143 | n/a | n, = unpack('<i', self.read(4)) |
---|
1144 | n/a | if n < 0: |
---|
1145 | n/a | # Corrupt or hostile pickle -- we never write one like this |
---|
1146 | n/a | raise UnpicklingError("LONG pickle has negative byte count") |
---|
1147 | n/a | data = self.read(n) |
---|
1148 | n/a | self.append(decode_long(data)) |
---|
1149 | n/a | dispatch[LONG4[0]] = load_long4 |
---|
1150 | n/a | |
---|
1151 | n/a | def load_float(self): |
---|
1152 | n/a | self.append(float(self.readline()[:-1])) |
---|
1153 | n/a | dispatch[FLOAT[0]] = load_float |
---|
1154 | n/a | |
---|
1155 | n/a | def load_binfloat(self): |
---|
1156 | n/a | self.append(unpack('>d', self.read(8))[0]) |
---|
1157 | n/a | dispatch[BINFLOAT[0]] = load_binfloat |
---|
1158 | n/a | |
---|
1159 | n/a | def _decode_string(self, value): |
---|
1160 | n/a | # Used to allow strings from Python 2 to be decoded either as |
---|
1161 | n/a | # bytes or Unicode strings. This should be used only with the |
---|
1162 | n/a | # STRING, BINSTRING and SHORT_BINSTRING opcodes. |
---|
1163 | n/a | if self.encoding == "bytes": |
---|
1164 | n/a | return value |
---|
1165 | n/a | else: |
---|
1166 | n/a | return value.decode(self.encoding, self.errors) |
---|
1167 | n/a | |
---|
1168 | n/a | def load_string(self): |
---|
1169 | n/a | data = self.readline()[:-1] |
---|
1170 | n/a | # Strip outermost quotes |
---|
1171 | n/a | if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'': |
---|
1172 | n/a | data = data[1:-1] |
---|
1173 | n/a | else: |
---|
1174 | n/a | raise UnpicklingError("the STRING opcode argument must be quoted") |
---|
1175 | n/a | self.append(self._decode_string(codecs.escape_decode(data)[0])) |
---|
1176 | n/a | dispatch[STRING[0]] = load_string |
---|
1177 | n/a | |
---|
1178 | n/a | def load_binstring(self): |
---|
1179 | n/a | # Deprecated BINSTRING uses signed 32-bit length |
---|
1180 | n/a | len, = unpack('<i', self.read(4)) |
---|
1181 | n/a | if len < 0: |
---|
1182 | n/a | raise UnpicklingError("BINSTRING pickle has negative byte count") |
---|
1183 | n/a | data = self.read(len) |
---|
1184 | n/a | self.append(self._decode_string(data)) |
---|
1185 | n/a | dispatch[BINSTRING[0]] = load_binstring |
---|
1186 | n/a | |
---|
1187 | n/a | def load_binbytes(self): |
---|
1188 | n/a | len, = unpack('<I', self.read(4)) |
---|
1189 | n/a | if len > maxsize: |
---|
1190 | n/a | raise UnpicklingError("BINBYTES exceeds system's maximum size " |
---|
1191 | n/a | "of %d bytes" % maxsize) |
---|
1192 | n/a | self.append(self.read(len)) |
---|
1193 | n/a | dispatch[BINBYTES[0]] = load_binbytes |
---|
1194 | n/a | |
---|
1195 | n/a | def load_unicode(self): |
---|
1196 | n/a | self.append(str(self.readline()[:-1], 'raw-unicode-escape')) |
---|
1197 | n/a | dispatch[UNICODE[0]] = load_unicode |
---|
1198 | n/a | |
---|
1199 | n/a | def load_binunicode(self): |
---|
1200 | n/a | len, = unpack('<I', self.read(4)) |
---|
1201 | n/a | if len > maxsize: |
---|
1202 | n/a | raise UnpicklingError("BINUNICODE exceeds system's maximum size " |
---|
1203 | n/a | "of %d bytes" % maxsize) |
---|
1204 | n/a | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
---|
1205 | n/a | dispatch[BINUNICODE[0]] = load_binunicode |
---|
1206 | n/a | |
---|
1207 | n/a | def load_binunicode8(self): |
---|
1208 | n/a | len, = unpack('<Q', self.read(8)) |
---|
1209 | n/a | if len > maxsize: |
---|
1210 | n/a | raise UnpicklingError("BINUNICODE8 exceeds system's maximum size " |
---|
1211 | n/a | "of %d bytes" % maxsize) |
---|
1212 | n/a | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
---|
1213 | n/a | dispatch[BINUNICODE8[0]] = load_binunicode8 |
---|
1214 | n/a | |
---|
1215 | n/a | def load_binbytes8(self): |
---|
1216 | n/a | len, = unpack('<Q', self.read(8)) |
---|
1217 | n/a | if len > maxsize: |
---|
1218 | n/a | raise UnpicklingError("BINBYTES8 exceeds system's maximum size " |
---|
1219 | n/a | "of %d bytes" % maxsize) |
---|
1220 | n/a | self.append(self.read(len)) |
---|
1221 | n/a | dispatch[BINBYTES8[0]] = load_binbytes8 |
---|
1222 | n/a | |
---|
1223 | n/a | def load_short_binstring(self): |
---|
1224 | n/a | len = self.read(1)[0] |
---|
1225 | n/a | data = self.read(len) |
---|
1226 | n/a | self.append(self._decode_string(data)) |
---|
1227 | n/a | dispatch[SHORT_BINSTRING[0]] = load_short_binstring |
---|
1228 | n/a | |
---|
1229 | n/a | def load_short_binbytes(self): |
---|
1230 | n/a | len = self.read(1)[0] |
---|
1231 | n/a | self.append(self.read(len)) |
---|
1232 | n/a | dispatch[SHORT_BINBYTES[0]] = load_short_binbytes |
---|
1233 | n/a | |
---|
1234 | n/a | def load_short_binunicode(self): |
---|
1235 | n/a | len = self.read(1)[0] |
---|
1236 | n/a | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
---|
1237 | n/a | dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode |
---|
1238 | n/a | |
---|
1239 | n/a | def load_tuple(self): |
---|
1240 | n/a | items = self.pop_mark() |
---|
1241 | n/a | self.append(tuple(items)) |
---|
1242 | n/a | dispatch[TUPLE[0]] = load_tuple |
---|
1243 | n/a | |
---|
1244 | n/a | def load_empty_tuple(self): |
---|
1245 | n/a | self.append(()) |
---|
1246 | n/a | dispatch[EMPTY_TUPLE[0]] = load_empty_tuple |
---|
1247 | n/a | |
---|
1248 | n/a | def load_tuple1(self): |
---|
1249 | n/a | self.stack[-1] = (self.stack[-1],) |
---|
1250 | n/a | dispatch[TUPLE1[0]] = load_tuple1 |
---|
1251 | n/a | |
---|
1252 | n/a | def load_tuple2(self): |
---|
1253 | n/a | self.stack[-2:] = [(self.stack[-2], self.stack[-1])] |
---|
1254 | n/a | dispatch[TUPLE2[0]] = load_tuple2 |
---|
1255 | n/a | |
---|
1256 | n/a | def load_tuple3(self): |
---|
1257 | n/a | self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])] |
---|
1258 | n/a | dispatch[TUPLE3[0]] = load_tuple3 |
---|
1259 | n/a | |
---|
1260 | n/a | def load_empty_list(self): |
---|
1261 | n/a | self.append([]) |
---|
1262 | n/a | dispatch[EMPTY_LIST[0]] = load_empty_list |
---|
1263 | n/a | |
---|
1264 | n/a | def load_empty_dictionary(self): |
---|
1265 | n/a | self.append({}) |
---|
1266 | n/a | dispatch[EMPTY_DICT[0]] = load_empty_dictionary |
---|
1267 | n/a | |
---|
1268 | n/a | def load_empty_set(self): |
---|
1269 | n/a | self.append(set()) |
---|
1270 | n/a | dispatch[EMPTY_SET[0]] = load_empty_set |
---|
1271 | n/a | |
---|
1272 | n/a | def load_frozenset(self): |
---|
1273 | n/a | items = self.pop_mark() |
---|
1274 | n/a | self.append(frozenset(items)) |
---|
1275 | n/a | dispatch[FROZENSET[0]] = load_frozenset |
---|
1276 | n/a | |
---|
1277 | n/a | def load_list(self): |
---|
1278 | n/a | items = self.pop_mark() |
---|
1279 | n/a | self.append(items) |
---|
1280 | n/a | dispatch[LIST[0]] = load_list |
---|
1281 | n/a | |
---|
1282 | n/a | def load_dict(self): |
---|
1283 | n/a | items = self.pop_mark() |
---|
1284 | n/a | d = {items[i]: items[i+1] |
---|
1285 | n/a | for i in range(0, len(items), 2)} |
---|
1286 | n/a | self.append(d) |
---|
1287 | n/a | dispatch[DICT[0]] = load_dict |
---|
1288 | n/a | |
---|
1289 | n/a | # INST and OBJ differ only in how they get a class object. It's not |
---|
1290 | n/a | # only sensible to do the rest in a common routine, the two routines |
---|
1291 | n/a | # previously diverged and grew different bugs. |
---|
1292 | n/a | # klass is the class to instantiate, and k points to the topmost mark |
---|
1293 | n/a | # object, following which are the arguments for klass.__init__. |
---|
1294 | n/a | def _instantiate(self, klass, args): |
---|
1295 | n/a | if (args or not isinstance(klass, type) or |
---|
1296 | n/a | hasattr(klass, "__getinitargs__")): |
---|
1297 | n/a | try: |
---|
1298 | n/a | value = klass(*args) |
---|
1299 | n/a | except TypeError as err: |
---|
1300 | n/a | raise TypeError("in constructor for %s: %s" % |
---|
1301 | n/a | (klass.__name__, str(err)), sys.exc_info()[2]) |
---|
1302 | n/a | else: |
---|
1303 | n/a | value = klass.__new__(klass) |
---|
1304 | n/a | self.append(value) |
---|
1305 | n/a | |
---|
1306 | n/a | def load_inst(self): |
---|
1307 | n/a | module = self.readline()[:-1].decode("ascii") |
---|
1308 | n/a | name = self.readline()[:-1].decode("ascii") |
---|
1309 | n/a | klass = self.find_class(module, name) |
---|
1310 | n/a | self._instantiate(klass, self.pop_mark()) |
---|
1311 | n/a | dispatch[INST[0]] = load_inst |
---|
1312 | n/a | |
---|
1313 | n/a | def load_obj(self): |
---|
1314 | n/a | # Stack is ... markobject classobject arg1 arg2 ... |
---|
1315 | n/a | args = self.pop_mark() |
---|
1316 | n/a | cls = args.pop(0) |
---|
1317 | n/a | self._instantiate(cls, args) |
---|
1318 | n/a | dispatch[OBJ[0]] = load_obj |
---|
1319 | n/a | |
---|
1320 | n/a | def load_newobj(self): |
---|
1321 | n/a | args = self.stack.pop() |
---|
1322 | n/a | cls = self.stack.pop() |
---|
1323 | n/a | obj = cls.__new__(cls, *args) |
---|
1324 | n/a | self.append(obj) |
---|
1325 | n/a | dispatch[NEWOBJ[0]] = load_newobj |
---|
1326 | n/a | |
---|
1327 | n/a | def load_newobj_ex(self): |
---|
1328 | n/a | kwargs = self.stack.pop() |
---|
1329 | n/a | args = self.stack.pop() |
---|
1330 | n/a | cls = self.stack.pop() |
---|
1331 | n/a | obj = cls.__new__(cls, *args, **kwargs) |
---|
1332 | n/a | self.append(obj) |
---|
1333 | n/a | dispatch[NEWOBJ_EX[0]] = load_newobj_ex |
---|
1334 | n/a | |
---|
1335 | n/a | def load_global(self): |
---|
1336 | n/a | module = self.readline()[:-1].decode("utf-8") |
---|
1337 | n/a | name = self.readline()[:-1].decode("utf-8") |
---|
1338 | n/a | klass = self.find_class(module, name) |
---|
1339 | n/a | self.append(klass) |
---|
1340 | n/a | dispatch[GLOBAL[0]] = load_global |
---|
1341 | n/a | |
---|
1342 | n/a | def load_stack_global(self): |
---|
1343 | n/a | name = self.stack.pop() |
---|
1344 | n/a | module = self.stack.pop() |
---|
1345 | n/a | if type(name) is not str or type(module) is not str: |
---|
1346 | n/a | raise UnpicklingError("STACK_GLOBAL requires str") |
---|
1347 | n/a | self.append(self.find_class(module, name)) |
---|
1348 | n/a | dispatch[STACK_GLOBAL[0]] = load_stack_global |
---|
1349 | n/a | |
---|
1350 | n/a | def load_ext1(self): |
---|
1351 | n/a | code = self.read(1)[0] |
---|
1352 | n/a | self.get_extension(code) |
---|
1353 | n/a | dispatch[EXT1[0]] = load_ext1 |
---|
1354 | n/a | |
---|
1355 | n/a | def load_ext2(self): |
---|
1356 | n/a | code, = unpack('<H', self.read(2)) |
---|
1357 | n/a | self.get_extension(code) |
---|
1358 | n/a | dispatch[EXT2[0]] = load_ext2 |
---|
1359 | n/a | |
---|
1360 | n/a | def load_ext4(self): |
---|
1361 | n/a | code, = unpack('<i', self.read(4)) |
---|
1362 | n/a | self.get_extension(code) |
---|
1363 | n/a | dispatch[EXT4[0]] = load_ext4 |
---|
1364 | n/a | |
---|
1365 | n/a | def get_extension(self, code): |
---|
1366 | n/a | nil = [] |
---|
1367 | n/a | obj = _extension_cache.get(code, nil) |
---|
1368 | n/a | if obj is not nil: |
---|
1369 | n/a | self.append(obj) |
---|
1370 | n/a | return |
---|
1371 | n/a | key = _inverted_registry.get(code) |
---|
1372 | n/a | if not key: |
---|
1373 | n/a | if code <= 0: # note that 0 is forbidden |
---|
1374 | n/a | # Corrupt or hostile pickle. |
---|
1375 | n/a | raise UnpicklingError("EXT specifies code <= 0") |
---|
1376 | n/a | raise ValueError("unregistered extension code %d" % code) |
---|
1377 | n/a | obj = self.find_class(*key) |
---|
1378 | n/a | _extension_cache[code] = obj |
---|
1379 | n/a | self.append(obj) |
---|
1380 | n/a | |
---|
1381 | n/a | def find_class(self, module, name): |
---|
1382 | n/a | # Subclasses may override this. |
---|
1383 | n/a | if self.proto < 3 and self.fix_imports: |
---|
1384 | n/a | if (module, name) in _compat_pickle.NAME_MAPPING: |
---|
1385 | n/a | module, name = _compat_pickle.NAME_MAPPING[(module, name)] |
---|
1386 | n/a | elif module in _compat_pickle.IMPORT_MAPPING: |
---|
1387 | n/a | module = _compat_pickle.IMPORT_MAPPING[module] |
---|
1388 | n/a | __import__(module, level=0) |
---|
1389 | n/a | if self.proto >= 4: |
---|
1390 | n/a | return _getattribute(sys.modules[module], name)[0] |
---|
1391 | n/a | else: |
---|
1392 | n/a | return getattr(sys.modules[module], name) |
---|
1393 | n/a | |
---|
1394 | n/a | def load_reduce(self): |
---|
1395 | n/a | stack = self.stack |
---|
1396 | n/a | args = stack.pop() |
---|
1397 | n/a | func = stack[-1] |
---|
1398 | n/a | stack[-1] = func(*args) |
---|
1399 | n/a | dispatch[REDUCE[0]] = load_reduce |
---|
1400 | n/a | |
---|
1401 | n/a | def load_pop(self): |
---|
1402 | n/a | if self.stack: |
---|
1403 | n/a | del self.stack[-1] |
---|
1404 | n/a | else: |
---|
1405 | n/a | self.pop_mark() |
---|
1406 | n/a | dispatch[POP[0]] = load_pop |
---|
1407 | n/a | |
---|
1408 | n/a | def load_pop_mark(self): |
---|
1409 | n/a | self.pop_mark() |
---|
1410 | n/a | dispatch[POP_MARK[0]] = load_pop_mark |
---|
1411 | n/a | |
---|
1412 | n/a | def load_dup(self): |
---|
1413 | n/a | self.append(self.stack[-1]) |
---|
1414 | n/a | dispatch[DUP[0]] = load_dup |
---|
1415 | n/a | |
---|
1416 | n/a | def load_get(self): |
---|
1417 | n/a | i = int(self.readline()[:-1]) |
---|
1418 | n/a | self.append(self.memo[i]) |
---|
1419 | n/a | dispatch[GET[0]] = load_get |
---|
1420 | n/a | |
---|
1421 | n/a | def load_binget(self): |
---|
1422 | n/a | i = self.read(1)[0] |
---|
1423 | n/a | self.append(self.memo[i]) |
---|
1424 | n/a | dispatch[BINGET[0]] = load_binget |
---|
1425 | n/a | |
---|
1426 | n/a | def load_long_binget(self): |
---|
1427 | n/a | i, = unpack('<I', self.read(4)) |
---|
1428 | n/a | self.append(self.memo[i]) |
---|
1429 | n/a | dispatch[LONG_BINGET[0]] = load_long_binget |
---|
1430 | n/a | |
---|
1431 | n/a | def load_put(self): |
---|
1432 | n/a | i = int(self.readline()[:-1]) |
---|
1433 | n/a | if i < 0: |
---|
1434 | n/a | raise ValueError("negative PUT argument") |
---|
1435 | n/a | self.memo[i] = self.stack[-1] |
---|
1436 | n/a | dispatch[PUT[0]] = load_put |
---|
1437 | n/a | |
---|
1438 | n/a | def load_binput(self): |
---|
1439 | n/a | i = self.read(1)[0] |
---|
1440 | n/a | if i < 0: |
---|
1441 | n/a | raise ValueError("negative BINPUT argument") |
---|
1442 | n/a | self.memo[i] = self.stack[-1] |
---|
1443 | n/a | dispatch[BINPUT[0]] = load_binput |
---|
1444 | n/a | |
---|
1445 | n/a | def load_long_binput(self): |
---|
1446 | n/a | i, = unpack('<I', self.read(4)) |
---|
1447 | n/a | if i > maxsize: |
---|
1448 | n/a | raise ValueError("negative LONG_BINPUT argument") |
---|
1449 | n/a | self.memo[i] = self.stack[-1] |
---|
1450 | n/a | dispatch[LONG_BINPUT[0]] = load_long_binput |
---|
1451 | n/a | |
---|
1452 | n/a | def load_memoize(self): |
---|
1453 | n/a | memo = self.memo |
---|
1454 | n/a | memo[len(memo)] = self.stack[-1] |
---|
1455 | n/a | dispatch[MEMOIZE[0]] = load_memoize |
---|
1456 | n/a | |
---|
1457 | n/a | def load_append(self): |
---|
1458 | n/a | stack = self.stack |
---|
1459 | n/a | value = stack.pop() |
---|
1460 | n/a | list = stack[-1] |
---|
1461 | n/a | list.append(value) |
---|
1462 | n/a | dispatch[APPEND[0]] = load_append |
---|
1463 | n/a | |
---|
1464 | n/a | def load_appends(self): |
---|
1465 | n/a | items = self.pop_mark() |
---|
1466 | n/a | list_obj = self.stack[-1] |
---|
1467 | n/a | try: |
---|
1468 | n/a | extend = list_obj.extend |
---|
1469 | n/a | except AttributeError: |
---|
1470 | n/a | pass |
---|
1471 | n/a | else: |
---|
1472 | n/a | extend(items) |
---|
1473 | n/a | return |
---|
1474 | n/a | # Even if the PEP 307 requires extend() and append() methods, |
---|
1475 | n/a | # fall back on append() if the object has no extend() method |
---|
1476 | n/a | # for backward compatibility. |
---|
1477 | n/a | append = list_obj.append |
---|
1478 | n/a | for item in items: |
---|
1479 | n/a | append(item) |
---|
1480 | n/a | dispatch[APPENDS[0]] = load_appends |
---|
1481 | n/a | |
---|
1482 | n/a | def load_setitem(self): |
---|
1483 | n/a | stack = self.stack |
---|
1484 | n/a | value = stack.pop() |
---|
1485 | n/a | key = stack.pop() |
---|
1486 | n/a | dict = stack[-1] |
---|
1487 | n/a | dict[key] = value |
---|
1488 | n/a | dispatch[SETITEM[0]] = load_setitem |
---|
1489 | n/a | |
---|
1490 | n/a | def load_setitems(self): |
---|
1491 | n/a | items = self.pop_mark() |
---|
1492 | n/a | dict = self.stack[-1] |
---|
1493 | n/a | for i in range(0, len(items), 2): |
---|
1494 | n/a | dict[items[i]] = items[i + 1] |
---|
1495 | n/a | dispatch[SETITEMS[0]] = load_setitems |
---|
1496 | n/a | |
---|
1497 | n/a | def load_additems(self): |
---|
1498 | n/a | items = self.pop_mark() |
---|
1499 | n/a | set_obj = self.stack[-1] |
---|
1500 | n/a | if isinstance(set_obj, set): |
---|
1501 | n/a | set_obj.update(items) |
---|
1502 | n/a | else: |
---|
1503 | n/a | add = set_obj.add |
---|
1504 | n/a | for item in items: |
---|
1505 | n/a | add(item) |
---|
1506 | n/a | dispatch[ADDITEMS[0]] = load_additems |
---|
1507 | n/a | |
---|
1508 | n/a | def load_build(self): |
---|
1509 | n/a | stack = self.stack |
---|
1510 | n/a | state = stack.pop() |
---|
1511 | n/a | inst = stack[-1] |
---|
1512 | n/a | setstate = getattr(inst, "__setstate__", None) |
---|
1513 | n/a | if setstate is not None: |
---|
1514 | n/a | setstate(state) |
---|
1515 | n/a | return |
---|
1516 | n/a | slotstate = None |
---|
1517 | n/a | if isinstance(state, tuple) and len(state) == 2: |
---|
1518 | n/a | state, slotstate = state |
---|
1519 | n/a | if state: |
---|
1520 | n/a | inst_dict = inst.__dict__ |
---|
1521 | n/a | intern = sys.intern |
---|
1522 | n/a | for k, v in state.items(): |
---|
1523 | n/a | if type(k) is str: |
---|
1524 | n/a | inst_dict[intern(k)] = v |
---|
1525 | n/a | else: |
---|
1526 | n/a | inst_dict[k] = v |
---|
1527 | n/a | if slotstate: |
---|
1528 | n/a | for k, v in slotstate.items(): |
---|
1529 | n/a | setattr(inst, k, v) |
---|
1530 | n/a | dispatch[BUILD[0]] = load_build |
---|
1531 | n/a | |
---|
1532 | n/a | def load_mark(self): |
---|
1533 | n/a | self.metastack.append(self.stack) |
---|
1534 | n/a | self.stack = [] |
---|
1535 | n/a | self.append = self.stack.append |
---|
1536 | n/a | dispatch[MARK[0]] = load_mark |
---|
1537 | n/a | |
---|
1538 | n/a | def load_stop(self): |
---|
1539 | n/a | value = self.stack.pop() |
---|
1540 | n/a | raise _Stop(value) |
---|
1541 | n/a | dispatch[STOP[0]] = load_stop |
---|
1542 | n/a | |
---|
1543 | n/a | |
---|
1544 | n/a | # Shorthands |
---|
1545 | n/a | |
---|
1546 | n/a | def _dump(obj, file, protocol=None, *, fix_imports=True): |
---|
1547 | n/a | _Pickler(file, protocol, fix_imports=fix_imports).dump(obj) |
---|
1548 | n/a | |
---|
1549 | n/a | def _dumps(obj, protocol=None, *, fix_imports=True): |
---|
1550 | n/a | f = io.BytesIO() |
---|
1551 | n/a | _Pickler(f, protocol, fix_imports=fix_imports).dump(obj) |
---|
1552 | n/a | res = f.getvalue() |
---|
1553 | n/a | assert isinstance(res, bytes_types) |
---|
1554 | n/a | return res |
---|
1555 | n/a | |
---|
1556 | n/a | def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"): |
---|
1557 | n/a | return _Unpickler(file, fix_imports=fix_imports, |
---|
1558 | n/a | encoding=encoding, errors=errors).load() |
---|
1559 | n/a | |
---|
1560 | n/a | def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"): |
---|
1561 | n/a | if isinstance(s, str): |
---|
1562 | n/a | raise TypeError("Can't load pickle from unicode string") |
---|
1563 | n/a | file = io.BytesIO(s) |
---|
1564 | n/a | return _Unpickler(file, fix_imports=fix_imports, |
---|
1565 | n/a | encoding=encoding, errors=errors).load() |
---|
1566 | n/a | |
---|
1567 | n/a | # Use the faster _pickle if possible |
---|
1568 | n/a | try: |
---|
1569 | n/a | from _pickle import ( |
---|
1570 | n/a | PickleError, |
---|
1571 | n/a | PicklingError, |
---|
1572 | n/a | UnpicklingError, |
---|
1573 | n/a | Pickler, |
---|
1574 | n/a | Unpickler, |
---|
1575 | n/a | dump, |
---|
1576 | n/a | dumps, |
---|
1577 | n/a | load, |
---|
1578 | n/a | loads |
---|
1579 | n/a | ) |
---|
1580 | n/a | except ImportError: |
---|
1581 | n/a | Pickler, Unpickler = _Pickler, _Unpickler |
---|
1582 | n/a | dump, dumps, load, loads = _dump, _dumps, _load, _loads |
---|
1583 | n/a | |
---|
1584 | n/a | # Doctest |
---|
1585 | n/a | def _test(): |
---|
1586 | n/a | import doctest |
---|
1587 | n/a | return doctest.testmod() |
---|
1588 | n/a | |
---|
1589 | n/a | if __name__ == "__main__": |
---|
1590 | n/a | import argparse |
---|
1591 | n/a | parser = argparse.ArgumentParser( |
---|
1592 | n/a | description='display contents of the pickle files') |
---|
1593 | n/a | parser.add_argument( |
---|
1594 | n/a | 'pickle_file', type=argparse.FileType('br'), |
---|
1595 | n/a | nargs='*', help='the pickle file') |
---|
1596 | n/a | parser.add_argument( |
---|
1597 | n/a | '-t', '--test', action='store_true', |
---|
1598 | n/a | help='run self-test suite') |
---|
1599 | n/a | parser.add_argument( |
---|
1600 | n/a | '-v', action='store_true', |
---|
1601 | n/a | help='run verbosely; only affects self-test run') |
---|
1602 | n/a | args = parser.parse_args() |
---|
1603 | n/a | if args.test: |
---|
1604 | n/a | _test() |
---|
1605 | n/a | else: |
---|
1606 | n/a | if not args.pickle_file: |
---|
1607 | n/a | parser.print_help() |
---|
1608 | n/a | else: |
---|
1609 | n/a | import pprint |
---|
1610 | n/a | for f in args.pickle_file: |
---|
1611 | n/a | obj = load(f) |
---|
1612 | n/a | pprint.pprint(obj) |
---|