blob: 595beda4765afec5a243a2c1e8bfcfe8da4e59f3 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Create portable serialized representations of Python objects.
Guido van Rossuma48061a1995-01-10 00:31:14 +00002
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00003See module copyreg for a mechanism for registering custom picklers.
Tim Peters22a449a2003-01-27 20:16:36 +00004See module pickletools source for extensive comments.
Guido van Rossuma48061a1995-01-10 00:31:14 +00005
Guido van Rossume467be61997-12-05 19:42:42 +00006Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00007
Guido van Rossume467be61997-12-05 19:42:42 +00008 Pickler
9 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000010
Guido van Rossume467be61997-12-05 19:42:42 +000011Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000012
Guido van Rossume467be61997-12-05 19:42:42 +000013 dump(object, file)
14 dumps(object) -> string
15 load(file) -> object
16 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000017
Guido van Rossume467be61997-12-05 19:42:42 +000018Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000019
Fred Drakefe82acc1998-02-13 03:24:48 +000020 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000021 format_version
22 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000023
Guido van Rossuma48061a1995-01-10 00:31:14 +000024"""
25
Victor Stinner7fa767e2014-03-20 09:16:38 +010026from types import FunctionType
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000027from copyreg import dispatch_table
28from copyreg import _extension_registry, _inverted_registry, _extension_cache
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +030029from itertools import islice
Serhiy Storchaka0d554d72015-10-10 22:42:18 +030030from functools import partial
Guido van Rossumd3703791998-10-22 20:15:36 +000031import sys
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +030032from sys import maxsize
33from struct import pack, unpack
Skip Montanaro23bafc62001-02-18 03:10:09 +000034import re
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000035import io
Walter Dörwald42748a82007-06-12 16:40:17 +000036import codecs
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000037import _compat_pickle
Guido van Rossuma48061a1995-01-10 00:31:14 +000038
Skip Montanaro352674d2001-02-07 23:14:30 +000039__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
40 "Unpickler", "dump", "dumps", "load", "loads"]
41
Guido van Rossum98297ee2007-11-06 21:34:58 +000042# Shortcut for use in isinstance testing
Alexandre Vassalotti8cb02b62008-05-03 01:42:49 +000043bytes_types = (bytes, bytearray)
Guido van Rossum98297ee2007-11-06 21:34:58 +000044
Tim Petersc0c12b52003-01-29 00:56:17 +000045# These are purely informational; no code uses these.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010046format_version = "4.0" # File format version we write
Guido van Rossumf29d3d62003-01-27 22:47:53 +000047compatible_formats = ["1.0", # Original protocol 0
Guido van Rossumbc64e222003-01-28 16:34:19 +000048 "1.1", # Protocol 0 with INST added
Guido van Rossumf29d3d62003-01-27 22:47:53 +000049 "1.2", # Original protocol 1
50 "1.3", # Protocol 1 with BINFLOAT added
51 "2.0", # Protocol 2
Guido van Rossumf4169812008-03-17 22:56:06 +000052 "3.0", # Protocol 3
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010053 "4.0", # Protocol 4
Guido van Rossumf29d3d62003-01-27 22:47:53 +000054 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000055
Guido van Rossum99603b02007-07-20 00:22:32 +000056# This is the highest protocol number we know how to read.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010057HIGHEST_PROTOCOL = 4
Tim Peters8587b3c2003-02-13 15:44:41 +000058
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000059# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
Łukasz Langac51d8c92018-04-03 23:06:53 -070060# Only bump this if the oldest still supported version of Python already
61# includes it.
62DEFAULT_PROTOCOL = 4
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000063
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000064class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000065 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000066 pass
67
68class PicklingError(PickleError):
69 """This exception is raised when an unpicklable object is passed to the
70 dump() method.
71
72 """
73 pass
74
75class UnpicklingError(PickleError):
76 """This exception is raised when there is a problem unpickling an object,
77 such as a security violation.
78
79 Note that other exceptions may also be raised during unpickling, including
80 (but not necessarily limited to) AttributeError, EOFError, ImportError,
81 and IndexError.
82
83 """
84 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000085
Tim Petersc0c12b52003-01-29 00:56:17 +000086# An instance of _Stop is raised by Unpickler.load_stop() in response to
87# the STOP opcode, passing the object that is the result of unpickling.
Guido van Rossumff871742000-12-13 18:11:56 +000088class _Stop(Exception):
89 def __init__(self, value):
90 self.value = value
91
Guido van Rossum533dbcf2003-01-28 17:55:05 +000092# Jython has PyStringMap; it's a dict subclass with string keys
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000093try:
94 from org.python.core import PyStringMap
Brett Cannoncd171c82013-07-04 17:43:24 -040095except ImportError:
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000096 PyStringMap = None
97
Tim Peters22a449a2003-01-27 20:16:36 +000098# Pickle opcodes. See pickletools.py for extensive docs. The listing
99# here is in kind-of alphabetical order of 1-character pickle code.
100# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000101
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000102MARK = b'(' # push special markobject on stack
103STOP = b'.' # every pickle ends with STOP
104POP = b'0' # discard topmost stack item
105POP_MARK = b'1' # discard stack top through topmost markobject
106DUP = b'2' # duplicate top stack item
107FLOAT = b'F' # push float object; decimal string argument
108INT = b'I' # push integer or bool; decimal string argument
109BININT = b'J' # push four-byte signed int
110BININT1 = b'K' # push 1-byte unsigned int
111LONG = b'L' # push long; decimal string argument
112BININT2 = b'M' # push 2-byte unsigned int
113NONE = b'N' # push None
114PERSID = b'P' # push persistent object; id is taken from string arg
115BINPERSID = b'Q' # " " " ; " " " " stack
116REDUCE = b'R' # apply callable to argtuple, both on stack
117STRING = b'S' # push string; NL-terminated string argument
118BINSTRING = b'T' # push string; counted binary string argument
119SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes
120UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument
121BINUNICODE = b'X' # " " " ; counted UTF-8 string argument
122APPEND = b'a' # append stack top to list below it
123BUILD = b'b' # call __setstate__ or __dict__.update()
124GLOBAL = b'c' # push self.find_class(modname, name); 2 string args
125DICT = b'd' # build a dict from stack items
126EMPTY_DICT = b'}' # push empty dict
127APPENDS = b'e' # extend list on stack by topmost stack slice
128GET = b'g' # push item from memo on stack; index is string arg
129BINGET = b'h' # " " " " " " ; " " 1-byte arg
130INST = b'i' # build & push class instance
131LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg
132LIST = b'l' # build list from topmost stack items
133EMPTY_LIST = b']' # push empty list
134OBJ = b'o' # build & push class instance
135PUT = b'p' # store stack top in memo; index is string arg
136BINPUT = b'q' # " " " " " ; " " 1-byte arg
137LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg
138SETITEM = b's' # add key+value pair to dict
139TUPLE = b't' # build tuple from topmost stack items
140EMPTY_TUPLE = b')' # push empty tuple
141SETITEMS = b'u' # modify dict by adding topmost key+value pairs
142BINFLOAT = b'G' # push float; arg is 8-byte float encoding
Tim Peters22a449a2003-01-27 20:16:36 +0000143
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000144TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py
145FALSE = b'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000146
Guido van Rossum586c9e82003-01-29 06:16:12 +0000147# Protocol 2
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000148
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000149PROTO = b'\x80' # identify pickle protocol
150NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple
151EXT1 = b'\x82' # push object from extension registry; 1-byte index
152EXT2 = b'\x83' # ditto, but 2-byte index
153EXT4 = b'\x84' # ditto, but 4-byte index
154TUPLE1 = b'\x85' # build 1-tuple from stack top
155TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items
156TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items
157NEWTRUE = b'\x88' # push True
158NEWFALSE = b'\x89' # push False
159LONG1 = b'\x8a' # push long from < 256 bytes
160LONG4 = b'\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000161
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000162_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
163
Guido van Rossumf4169812008-03-17 22:56:06 +0000164# Protocol 3 (Python 3.x)
165
166BINBYTES = b'B' # push bytes; counted binary string argument
167SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes
Guido van Rossuma48061a1995-01-10 00:31:14 +0000168
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100169# Protocol 4
170SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes
171BINUNICODE8 = b'\x8d' # push very long string
172BINBYTES8 = b'\x8e' # push very long bytes string
173EMPTY_SET = b'\x8f' # push empty set on the stack
174ADDITEMS = b'\x90' # modify set by adding topmost stack items
175FROZENSET = b'\x91' # build frozenset from topmost stack items
176NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments
177STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks
178MEMOIZE = b'\x94' # store top of the stack in memo
179FRAME = b'\x95' # indicate the beginning of a new frame
180
181__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)])
182
183
184class _Framer:
185
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200186 _FRAME_SIZE_MIN = 4
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100187 _FRAME_SIZE_TARGET = 64 * 1024
188
189 def __init__(self, file_write):
190 self.file_write = file_write
191 self.current_frame = None
192
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100193 def start_framing(self):
194 self.current_frame = io.BytesIO()
195
196 def end_framing(self):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800197 if self.current_frame and self.current_frame.tell() > 0:
198 self.commit_frame(force=True)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100199 self.current_frame = None
200
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800201 def commit_frame(self, force=False):
202 if self.current_frame:
203 f = self.current_frame
204 if f.tell() >= self._FRAME_SIZE_TARGET or force:
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100205 data = f.getbuffer()
206 write = self.file_write
Serhiy Storchaka1211c9a2018-01-20 16:42:44 +0200207 if len(data) >= self._FRAME_SIZE_MIN:
208 # Issue a single call to the write method of the underlying
209 # file object for the frame opcode with the size of the
210 # frame. The concatenation is expected to be less expensive
211 # than issuing an additional call to write.
212 write(FRAME + pack("<Q", len(data)))
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100213
214 # Issue a separate call to write to append the frame
215 # contents without concatenation to the above to avoid a
216 # memory copy.
217 write(data)
218
219 # Start the new frame with a new io.BytesIO instance so that
220 # the file object can have delayed access to the previous frame
221 # contents via an unreleased memoryview of the previous
222 # io.BytesIO instance.
223 self.current_frame = io.BytesIO()
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800224
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100225 def write(self, data):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800226 if self.current_frame:
227 return self.current_frame.write(data)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100228 else:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800229 return self.file_write(data)
230
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100231 def write_large_bytes(self, header, payload):
232 write = self.file_write
233 if self.current_frame:
234 # Terminate the current frame and flush it to the file.
235 self.commit_frame(force=True)
236
237 # Perform direct write of the header and payload of the large binary
238 # object. Be careful not to concatenate the header and the payload
239 # prior to calling 'write' as we do not want to allocate a large
240 # temporary bytes object.
241 # We intentionally do not insert a protocol 4 frame opcode to make
242 # it possible to optimize file.read calls in the loader.
243 write(header)
244 write(payload)
245
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100246
247class _Unframer:
248
249 def __init__(self, file_read, file_readline, file_tell=None):
250 self.file_read = file_read
251 self.file_readline = file_readline
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100252 self.current_frame = None
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100253
254 def read(self, n):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800255 if self.current_frame:
256 data = self.current_frame.read(n)
257 if not data and n != 0:
258 self.current_frame = None
259 return self.file_read(n)
260 if len(data) < n:
261 raise UnpicklingError(
262 "pickle exhausted before end of frame")
263 return data
264 else:
265 return self.file_read(n)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100266
267 def readline(self):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800268 if self.current_frame:
269 data = self.current_frame.readline()
270 if not data:
271 self.current_frame = None
272 return self.file_readline()
Serhiy Storchaka21d75332015-01-26 10:37:01 +0200273 if data[-1] != b'\n'[0]:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800274 raise UnpicklingError(
275 "pickle exhausted before end of frame")
276 return data
277 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100278 return self.file_readline()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100279
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800280 def load_frame(self, frame_size):
281 if self.current_frame and self.current_frame.read() != b'':
282 raise UnpicklingError(
283 "beginning of a new frame before end of current frame")
284 self.current_frame = io.BytesIO(self.file_read(frame_size))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100285
286
287# Tools used for pickling.
288
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300289def _getattribute(obj, name):
290 for subpath in name.split('.'):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100291 if subpath == '<locals>':
292 raise AttributeError("Can't get local attribute {!r} on {!r}"
293 .format(name, obj))
294 try:
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300295 parent = obj
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100296 obj = getattr(obj, subpath)
297 except AttributeError:
298 raise AttributeError("Can't get attribute {!r} on {!r}"
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300299 .format(name, obj)) from None
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300300 return obj, parent
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100301
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300302def whichmodule(obj, name):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100303 """Find the module an object belong to."""
304 module_name = getattr(obj, '__module__', None)
305 if module_name is not None:
306 return module_name
Antoine Pitroue1618492014-10-04 22:15:27 +0200307 # Protect the iteration by using a list copy of sys.modules against dynamic
308 # modules that trigger imports of other modules upon calls to getattr.
309 for module_name, module in list(sys.modules.items()):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100310 if module_name == '__main__' or module is None:
311 continue
312 try:
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300313 if _getattribute(module, name)[0] is obj:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100314 return module_name
315 except AttributeError:
316 pass
317 return '__main__'
318
319def encode_long(x):
320 r"""Encode a long to a two's complement little-endian binary string.
321 Note that 0 is a special case, returning an empty string, to save a
322 byte in the LONG1 pickling context.
323
324 >>> encode_long(0)
325 b''
326 >>> encode_long(255)
327 b'\xff\x00'
328 >>> encode_long(32767)
329 b'\xff\x7f'
330 >>> encode_long(-256)
331 b'\x00\xff'
332 >>> encode_long(-32768)
333 b'\x00\x80'
334 >>> encode_long(-128)
335 b'\x80'
336 >>> encode_long(127)
337 b'\x7f'
338 >>>
339 """
340 if x == 0:
341 return b''
342 nbytes = (x.bit_length() >> 3) + 1
343 result = x.to_bytes(nbytes, byteorder='little', signed=True)
344 if x < 0 and nbytes > 1:
345 if result[-1] == 0xff and (result[-2] & 0x80) != 0:
346 result = result[:-1]
347 return result
348
349def decode_long(data):
350 r"""Decode a long from a two's complement little-endian binary string.
351
352 >>> decode_long(b'')
353 0
354 >>> decode_long(b"\xff\x00")
355 255
356 >>> decode_long(b"\xff\x7f")
357 32767
358 >>> decode_long(b"\x00\xff")
359 -256
360 >>> decode_long(b"\x00\x80")
361 -32768
362 >>> decode_long(b"\x80")
363 -128
364 >>> decode_long(b"\x7f")
365 127
366 """
367 return int.from_bytes(data, byteorder='little', signed=True)
368
Skip Montanaro23bafc62001-02-18 03:10:09 +0000369
Guido van Rossum1be31752003-01-28 15:19:53 +0000370# Pickling machinery
371
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372class _Pickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +0000373
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000374 def __init__(self, file, protocol=None, *, fix_imports=True):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000375 """This takes a binary file for writing a pickle data stream.
376
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800377 The optional *protocol* argument tells the pickler to use the
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100378 given protocol; supported protocols are 0, 1, 2, 3 and 4. The
Łukasz Langac51d8c92018-04-03 23:06:53 -0700379 default protocol is 4. It was introduced in Python 3.4, it is
380 incompatible with previous versions.
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000381
Guido van Rossum7eff63a2003-01-31 19:42:31 +0000382 Specifying a negative protocol version selects the highest
Tim Peters5bd2a792003-02-01 16:45:06 +0000383 protocol version supported. The higher the protocol used, the
384 more recent the version of Python needed to read the pickle
385 produced.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000386
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800387 The *file* argument must have a write() method that accepts a
388 single bytes argument. It can thus be a file object opened for
Martin Panter7462b6492015-11-02 03:37:02 +0000389 binary writing, an io.BytesIO instance, or any other custom
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800390 object that meets this interface.
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000391
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800392 If *fix_imports* is True and *protocol* is less than 3, pickle
393 will try to map the new Python 3 names to the old module names
394 used in Python 2, so that the pickle data stream is readable
395 with Python 2.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000396 """
Guido van Rossumcf117b02003-02-09 17:19:41 +0000397 if protocol is None:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000398 protocol = DEFAULT_PROTOCOL
Guido van Rossumcf117b02003-02-09 17:19:41 +0000399 if protocol < 0:
Tim Peters8587b3c2003-02-13 15:44:41 +0000400 protocol = HIGHEST_PROTOCOL
401 elif not 0 <= protocol <= HIGHEST_PROTOCOL:
402 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100404 self._file_write = file.write
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000405 except AttributeError:
406 raise TypeError("file must have a 'write' attribute")
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800407 self.framer = _Framer(self._file_write)
408 self.write = self.framer.write
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100409 self._write_large_bytes = self.framer.write_large_bytes
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000410 self.memo = {}
Guido van Rossumcf117b02003-02-09 17:19:41 +0000411 self.proto = int(protocol)
412 self.bin = protocol >= 1
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000413 self.fast = 0
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000414 self.fix_imports = fix_imports and protocol < 3
Guido van Rossuma48061a1995-01-10 00:31:14 +0000415
Fred Drake7f781c92002-05-01 20:33:53 +0000416 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000417 """Clears the pickler's "memo".
418
419 The memo is the data structure that remembers which objects the
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800420 pickler has already seen, so that shared or recursive objects
421 are pickled by reference and not by value. This method is
422 useful when re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000423 """
Fred Drake7f781c92002-05-01 20:33:53 +0000424 self.memo.clear()
425
Guido van Rossum3a41c612003-01-28 15:10:22 +0000426 def dump(self, obj):
Tim Peters5bd2a792003-02-01 16:45:06 +0000427 """Write a pickled representation of obj to the open file."""
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000428 # Check whether Pickler was initialized correctly. This is
429 # only needed to mimic the behavior of _pickle.Pickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100430 if not hasattr(self, "_file_write"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000431 raise PicklingError("Pickler.__init__() was not called by "
432 "%s.__init__()" % (self.__class__.__name__,))
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000433 if self.proto >= 2:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800434 self.write(PROTO + pack("<B", self.proto))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100435 if self.proto >= 4:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800436 self.framer.start_framing()
Guido van Rossum3a41c612003-01-28 15:10:22 +0000437 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000438 self.write(STOP)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800439 self.framer.end_framing()
Guido van Rossuma48061a1995-01-10 00:31:14 +0000440
Jeremy Hylton3422c992003-01-24 19:29:52 +0000441 def memoize(self, obj):
442 """Store an object in the memo."""
443
Tim Peterse46b73f2003-01-27 21:22:10 +0000444 # The Pickler memo is a dictionary mapping object ids to 2-tuples
445 # that contain the Unpickler memo key and the object being memoized.
446 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000447 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000448 # Pickler memo so that transient objects are kept alive during
449 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000450
Tim Peterse46b73f2003-01-27 21:22:10 +0000451 # The use of the Unpickler memo length as the memo key is just a
452 # convention. The only requirement is that the memo values be unique.
453 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000454 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000455 # growable) array, indexed by memo key.
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000456 if self.fast:
457 return
Guido van Rossum9b40e802003-01-30 06:37:41 +0000458 assert id(obj) not in self.memo
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100459 idx = len(self.memo)
460 self.write(self.put(idx))
461 self.memo[id(obj)] = idx, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000462
Tim Petersbb38e302003-01-27 21:25:41 +0000463 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100464 def put(self, idx):
465 if self.proto >= 4:
466 return MEMOIZE
467 elif self.bin:
468 if idx < 256:
469 return BINPUT + pack("<B", idx)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000470 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100471 return LONG_BINPUT + pack("<I", idx)
472 else:
473 return PUT + repr(idx).encode("ascii") + b'\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000474
Tim Petersbb38e302003-01-27 21:25:41 +0000475 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300476 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000477 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000478 if i < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300479 return BINGET + pack("<B", i)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000480 else:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +0100481 return LONG_BINGET + pack("<I", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000482
Guido van Rossum39478e82007-08-27 17:23:59 +0000483 return GET + repr(i).encode("ascii") + b'\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000485 def save(self, obj, save_persistent_id=True):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800486 self.framer.commit_frame()
487
Guido van Rossumbc64e222003-01-28 16:34:19 +0000488 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000489 pid = self.persistent_id(obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000490 if pid is not None and save_persistent_id:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000491 self.save_pers(pid)
492 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000493
Guido van Rossumbc64e222003-01-28 16:34:19 +0000494 # Check the memo
495 x = self.memo.get(id(obj))
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300496 if x is not None:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000497 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000498 return
499
Pierre Glaser289f1f82019-05-08 23:08:25 +0200500 rv = NotImplemented
501 reduce = getattr(self, "reducer_override", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300502 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000503 rv = reduce(obj)
Pierre Glaser289f1f82019-05-08 23:08:25 +0200504
505 if rv is NotImplemented:
506 # Check the type dispatch table
507 t = type(obj)
508 f = self.dispatch.get(t)
509 if f is not None:
510 f(self, obj) # Call unbound method with explicit self
Antoine Pitrouffd41d92011-10-04 09:23:04 +0200511 return
512
Pierre Glaser289f1f82019-05-08 23:08:25 +0200513 # Check private dispatch table if any, or else
514 # copyreg.dispatch_table
515 reduce = getattr(self, 'dispatch_table', dispatch_table).get(t)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300516 if reduce is not None:
Pierre Glaser289f1f82019-05-08 23:08:25 +0200517 rv = reduce(obj)
Guido van Rossumc53f0092003-02-18 22:05:12 +0000518 else:
Pierre Glaser289f1f82019-05-08 23:08:25 +0200519 # Check for a class with a custom metaclass; treat as regular
520 # class
521 if issubclass(t, type):
522 self.save_global(obj)
523 return
524
525 # Check for a __reduce_ex__ method, fall back to __reduce__
526 reduce = getattr(obj, "__reduce_ex__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300527 if reduce is not None:
Pierre Glaser289f1f82019-05-08 23:08:25 +0200528 rv = reduce(self.proto)
Guido van Rossumc53f0092003-02-18 22:05:12 +0000529 else:
Pierre Glaser289f1f82019-05-08 23:08:25 +0200530 reduce = getattr(obj, "__reduce__", None)
531 if reduce is not None:
532 rv = reduce()
533 else:
534 raise PicklingError("Can't pickle %r object: %r" %
535 (t.__name__, obj))
Tim Petersb32a8312003-01-28 00:48:09 +0000536
Guido van Rossumbc64e222003-01-28 16:34:19 +0000537 # Check for string returned by reduce(), meaning "save as global"
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000538 if isinstance(rv, str):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000539 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000540 return
541
Guido van Rossumbc64e222003-01-28 16:34:19 +0000542 # Assert that reduce() returned a tuple
Guido van Rossum13257902007-06-07 23:15:56 +0000543 if not isinstance(rv, tuple):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000544 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000545
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000546 # Assert that it returned an appropriately sized tuple
Guido van Rossumbc64e222003-01-28 16:34:19 +0000547 l = len(rv)
Pierre Glaser65d98d02019-05-08 21:40:25 +0200548 if not (2 <= l <= 6):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000549 raise PicklingError("Tuple returned by %s must have "
Pierre Glaser65d98d02019-05-08 21:40:25 +0200550 "two to six elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000551
Guido van Rossumbc64e222003-01-28 16:34:19 +0000552 # Save the reduce() output and finally memoize the object
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000553 self.save_reduce(obj=obj, *rv)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000554
Guido van Rossum3a41c612003-01-28 15:10:22 +0000555 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000556 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000557 return None
558
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000559 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000560 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000561 if self.bin:
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 self.save(pid, save_persistent_id=False)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000563 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000564 else:
Serhiy Storchakadec25af2016-07-17 11:24:17 +0300565 try:
566 self.write(PERSID + str(pid).encode("ascii") + b'\n')
567 except UnicodeEncodeError:
568 raise PicklingError(
569 "persistent IDs in protocol 0 must be ASCII strings")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100571 def save_reduce(self, func, args, state=None, listitems=None,
Pierre Glaser65d98d02019-05-08 21:40:25 +0200572 dictitems=None, state_setter=None, obj=None):
Jeremy Hyltone3a565e2003-06-29 16:59:59 +0000573 # This API is called by some subclasses
Guido van Rossumbc64e222003-01-28 16:34:19 +0000574
Guido van Rossum13257902007-06-07 23:15:56 +0000575 if not isinstance(args, tuple):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100576 raise PicklingError("args from save_reduce() must be a tuple")
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200577 if not callable(func):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100578 raise PicklingError("func from save_reduce() must be callable")
Guido van Rossumbc64e222003-01-28 16:34:19 +0000579
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000580 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000581 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000582
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100583 func_name = getattr(func, "__name__", "")
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300584 if self.proto >= 2 and func_name == "__newobj_ex__":
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100585 cls, args, kwargs = args
586 if not hasattr(cls, "__new__"):
587 raise PicklingError("args[0] from {} args has no __new__"
588 .format(func_name))
589 if obj is not None and cls is not obj.__class__:
590 raise PicklingError("args[0] from {} args has the wrong class"
591 .format(func_name))
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300592 if self.proto >= 4:
593 save(cls)
594 save(args)
595 save(kwargs)
596 write(NEWOBJ_EX)
597 else:
598 func = partial(cls.__new__, cls, *args, **kwargs)
599 save(func)
600 save(())
601 write(REDUCE)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100602 elif self.proto >= 2 and func_name == "__newobj__":
603 # A __reduce__ implementation can direct protocol 2 or newer to
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000604 # use the more efficient NEWOBJ opcode, while still
605 # allowing protocol 0 and 1 to work normally. For this to
606 # work, the function returned by __reduce__ should be
607 # called __newobj__, and its first argument should be a
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100608 # class. The implementation for __newobj__
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000609 # should be as follows, although pickle has no way to
610 # verify this:
611 #
612 # def __newobj__(cls, *args):
613 # return cls.__new__(cls, *args)
614 #
615 # Protocols 0 and 1 will pickle a reference to __newobj__,
616 # while protocol 2 (and above) will pickle a reference to
617 # cls, the remaining args tuple, and the NEWOBJ code,
618 # which calls cls.__new__(cls, *args) at unpickling time
619 # (see load_newobj below). If __reduce__ returns a
620 # three-tuple, the state from the third tuple item will be
621 # pickled regardless of the protocol, calling __setstate__
622 # at unpickling time (see load_build below).
623 #
624 # Note that no standard __newobj__ implementation exists;
625 # you have to provide your own. This is to enforce
626 # compatibility with Python 2.2 (pickles written using
627 # protocol 0 or 1 in Python 2.3 should be unpicklable by
628 # Python 2.2).
629 cls = args[0]
630 if not hasattr(cls, "__new__"):
631 raise PicklingError(
632 "args[0] from __newobj__ args has no __new__")
Guido van Rossumf7f45172003-01-31 17:17:49 +0000633 if obj is not None and cls is not obj.__class__:
634 raise PicklingError(
635 "args[0] from __newobj__ args has the wrong class")
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000636 args = args[1:]
637 save(cls)
638 save(args)
639 write(NEWOBJ)
640 else:
641 save(func)
642 save(args)
643 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000644
Guido van Rossumf7f45172003-01-31 17:17:49 +0000645 if obj is not None:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100646 # If the object is already in the memo, this means it is
647 # recursive. In this case, throw away everything we put on the
648 # stack, and fetch the object back from the memo.
649 if id(obj) in self.memo:
650 write(POP + self.get(self.memo[id(obj)][0]))
651 else:
652 self.memoize(obj)
Guido van Rossumf7f45172003-01-31 17:17:49 +0000653
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000654 # More new special cases (that work with older protocols as
655 # well): when __reduce__ returns a tuple with 4 or 5 items,
656 # the 4th and 5th item should be iterators that provide list
657 # items and dict items (as (key, value) tuples), or None.
658
659 if listitems is not None:
660 self._batch_appends(listitems)
661
662 if dictitems is not None:
663 self._batch_setitems(dictitems)
664
Tim Petersc32d8242001-04-10 02:48:53 +0000665 if state is not None:
Pierre Glaser65d98d02019-05-08 21:40:25 +0200666 if state_setter is None:
667 save(state)
668 write(BUILD)
669 else:
670 # If a state_setter is specified, call it instead of load_build
671 # to update obj's with its previous state.
672 # First, push state_setter and its tuple of expected arguments
673 # (obj, state) onto the stack.
674 save(state_setter)
675 save(obj) # simple BINGET opcode as obj is already memoized.
676 save(state)
677 write(TUPLE2)
678 # Trigger a state_setter(obj, state) function call.
679 write(REDUCE)
680 # The purpose of state_setter is to carry-out an
681 # inplace modification of obj. We do not care about what the
682 # method might return, so its output is eventually removed from
683 # the stack.
684 write(POP)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000685
Guido van Rossumbc64e222003-01-28 16:34:19 +0000686 # Methods below this point are dispatched through the dispatch table
687
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000688 dispatch = {}
689
Guido van Rossum3a41c612003-01-28 15:10:22 +0000690 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 self.write(NONE)
Guido van Rossum13257902007-06-07 23:15:56 +0000692 dispatch[type(None)] = save_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000693
Guido van Rossum3a41c612003-01-28 15:10:22 +0000694 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000695 if self.proto >= 2:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300696 self.write(NEWTRUE if obj else NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000697 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300698 self.write(TRUE if obj else FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000699 dispatch[bool] = save_bool
700
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300701 def save_long(self, obj):
Guido van Rossumddefaf32007-01-14 03:31:43 +0000702 if self.bin:
703 # If the int is small enough to fit in a signed 4-byte 2's-comp
704 # format, we can store it more efficiently than the general
705 # case.
706 # First one- and two-byte unsigned ints:
707 if obj >= 0:
708 if obj <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300709 self.write(BININT1 + pack("<B", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000710 return
711 if obj <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300712 self.write(BININT2 + pack("<H", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000713 return
714 # Next check for 4-byte signed ints:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300715 if -0x80000000 <= obj <= 0x7fffffff:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000716 self.write(BININT + pack("<i", obj))
717 return
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000718 if self.proto >= 2:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000719 encoded = encode_long(obj)
720 n = len(encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000721 if n < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300722 self.write(LONG1 + pack("<B", n) + encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000723 else:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000724 self.write(LONG4 + pack("<i", n) + encoded)
Tim Petersee1a53c2003-02-02 02:57:53 +0000725 return
Serhiy Storchaka3daaafb2017-11-16 09:44:43 +0200726 if -0x80000000 <= obj <= 0x7fffffff:
727 self.write(INT + repr(obj).encode("ascii") + b'\n')
728 else:
729 self.write(LONG + repr(obj).encode("ascii") + b'L\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000730 dispatch[int] = save_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000731
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300732 def save_float(self, obj):
Guido van Rossumd3703791998-10-22 20:15:36 +0000733 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000734 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000735 else:
Guido van Rossum39478e82007-08-27 17:23:59 +0000736 self.write(FLOAT + repr(obj).encode("ascii") + b'\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000737 dispatch[float] = save_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000738
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300739 def save_bytes(self, obj):
Guido van Rossumf4169812008-03-17 22:56:06 +0000740 if self.proto < 3:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300741 if not obj: # bytes object is empty
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500742 self.save_reduce(bytes, (), obj=obj)
743 else:
744 self.save_reduce(codecs.encode,
745 (str(obj, 'latin1'), 'latin1'), obj=obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000746 return
747 n = len(obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100748 if n <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300749 self.write(SHORT_BINBYTES + pack("<B", n) + obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100750 elif n > 0xffffffff and self.proto >= 4:
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100751 self._write_large_bytes(BINBYTES8 + pack("<Q", n), obj)
752 elif n >= self.framer._FRAME_SIZE_TARGET:
753 self._write_large_bytes(BINBYTES + pack("<I", n), obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000754 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300755 self.write(BINBYTES + pack("<I", n) + obj)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000756 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000757 dispatch[bytes] = save_bytes
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000758
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300759 def save_str(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000760 if self.bin:
Victor Stinner485fb562010-04-13 11:07:24 +0000761 encoded = obj.encode('utf-8', 'surrogatepass')
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000762 n = len(encoded)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100763 if n <= 0xff and self.proto >= 4:
764 self.write(SHORT_BINUNICODE + pack("<B", n) + encoded)
765 elif n > 0xffffffff and self.proto >= 4:
Olivier Grisel3cd7c6e2018-01-06 16:18:54 +0100766 self._write_large_bytes(BINUNICODE8 + pack("<Q", n), encoded)
767 elif n >= self.framer._FRAME_SIZE_TARGET:
768 self._write_large_bytes(BINUNICODE + pack("<I", n), encoded)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100769 else:
770 self.write(BINUNICODE + pack("<I", n) + encoded)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000771 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000772 obj = obj.replace("\\", "\\u005c")
773 obj = obj.replace("\n", "\\u000a")
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100774 self.write(UNICODE + obj.encode('raw-unicode-escape') +
775 b'\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000776 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000777 dispatch[str] = save_str
Tim Peters658cba62001-02-09 20:06:00 +0000778
Guido van Rossum3a41c612003-01-28 15:10:22 +0000779 def save_tuple(self, obj):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300780 if not obj: # tuple is empty
781 if self.bin:
782 self.write(EMPTY_TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000783 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300784 self.write(MARK + TUPLE)
Tim Petersd97da802003-01-28 05:48:29 +0000785 return
786
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300787 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000788 save = self.save
789 memo = self.memo
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300790 if n <= 3 and self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000791 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000792 save(element)
793 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000794 if id(obj) in memo:
795 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300796 self.write(POP * n + get)
Tim Petersd97da802003-01-28 05:48:29 +0000797 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300798 self.write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000799 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000800 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000801
Tim Peters1d63c9f2003-02-02 20:29:39 +0000802 # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple
Tim Petersff57bff2003-01-28 05:34:53 +0000803 # has more than 3 elements.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300804 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000805 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000806 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000807 save(element)
808
Tim Peters1d63c9f2003-02-02 20:29:39 +0000809 if id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000810 # Subtle. d was not in memo when we entered save_tuple(), so
811 # the process of saving the tuple's elements must have saved
812 # the tuple itself: the tuple is recursive. The proper action
813 # now is to throw away everything we put on the stack, and
814 # simply GET the tuple (it's already constructed). This check
815 # could have been done in the "for element" loop instead, but
816 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000817 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300818 if self.bin:
Tim Petersf558da02003-01-28 02:09:55 +0000819 write(POP_MARK + get)
820 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000821 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000822 return
823
Tim Peters1d63c9f2003-02-02 20:29:39 +0000824 # No recursion.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300825 write(TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000826 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000827
Guido van Rossum13257902007-06-07 23:15:56 +0000828 dispatch[tuple] = save_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000829
Guido van Rossum3a41c612003-01-28 15:10:22 +0000830 def save_list(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000831 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300832 self.write(EMPTY_LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000833 else: # proto 0 -- can't use EMPTY_LIST
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300834 self.write(MARK + LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000835
836 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000837 self._batch_appends(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000838
Guido van Rossum13257902007-06-07 23:15:56 +0000839 dispatch[list] = save_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000840
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000841 _BATCHSIZE = 1000
842
843 def _batch_appends(self, items):
844 # Helper to batch up APPENDS sequences
845 save = self.save
846 write = self.write
847
848 if not self.bin:
849 for x in items:
850 save(x)
851 write(APPEND)
852 return
853
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300854 it = iter(items)
855 while True:
856 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000857 n = len(tmp)
858 if n > 1:
859 write(MARK)
860 for x in tmp:
861 save(x)
862 write(APPENDS)
863 elif n:
864 save(tmp[0])
865 write(APPEND)
866 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300867 if n < self._BATCHSIZE:
868 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000869
Guido van Rossum3a41c612003-01-28 15:10:22 +0000870 def save_dict(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000871 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300872 self.write(EMPTY_DICT)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000873 else: # proto 0 -- can't use EMPTY_DICT
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300874 self.write(MARK + DICT)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000875
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000876 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000877 self._batch_setitems(obj.items())
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000878
Guido van Rossum13257902007-06-07 23:15:56 +0000879 dispatch[dict] = save_dict
880 if PyStringMap is not None:
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000881 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000882
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000883 def _batch_setitems(self, items):
884 # Helper to batch up SETITEMS sequences; proto >= 1 only
885 save = self.save
886 write = self.write
887
888 if not self.bin:
889 for k, v in items:
890 save(k)
891 save(v)
892 write(SETITEM)
893 return
894
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300895 it = iter(items)
896 while True:
897 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000898 n = len(tmp)
899 if n > 1:
900 write(MARK)
901 for k, v in tmp:
902 save(k)
903 save(v)
904 write(SETITEMS)
905 elif n:
906 k, v = tmp[0]
907 save(k)
908 save(v)
909 write(SETITEM)
910 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300911 if n < self._BATCHSIZE:
912 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000913
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100914 def save_set(self, obj):
915 save = self.save
916 write = self.write
917
918 if self.proto < 4:
919 self.save_reduce(set, (list(obj),), obj=obj)
920 return
921
922 write(EMPTY_SET)
923 self.memoize(obj)
924
925 it = iter(obj)
926 while True:
927 batch = list(islice(it, self._BATCHSIZE))
928 n = len(batch)
929 if n > 0:
930 write(MARK)
931 for item in batch:
932 save(item)
933 write(ADDITEMS)
934 if n < self._BATCHSIZE:
935 return
936 dispatch[set] = save_set
937
938 def save_frozenset(self, obj):
939 save = self.save
940 write = self.write
941
942 if self.proto < 4:
943 self.save_reduce(frozenset, (list(obj),), obj=obj)
944 return
945
946 write(MARK)
947 for item in obj:
948 save(item)
949
950 if id(obj) in self.memo:
951 # If the object is already in the memo, this means it is
952 # recursive. In this case, throw away everything we put on the
953 # stack, and fetch the object back from the memo.
954 write(POP_MARK + self.get(self.memo[id(obj)][0]))
955 return
956
957 write(FROZENSET)
958 self.memoize(obj)
959 dispatch[frozenset] = save_frozenset
960
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300961 def save_global(self, obj, name=None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000962 write = self.write
963 memo = self.memo
964
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300965 if name is None:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100966 name = getattr(obj, '__qualname__', None)
Tim Petersc32d8242001-04-10 02:48:53 +0000967 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000968 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000969
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300970 module_name = whichmodule(obj, name)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000971 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 __import__(module_name, level=0)
973 module = sys.modules[module_name]
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300974 obj2, parent = _getattribute(module, name)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000975 except (ImportError, KeyError, AttributeError):
976 raise PicklingError(
977 "Can't pickle %r: it's not found as %s.%s" %
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300978 (obj, module_name, name)) from None
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000979 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100980 if obj2 is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000981 raise PicklingError(
982 "Can't pickle %r: it's not the same object as %s.%s" %
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100983 (obj, module_name, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000984
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000985 if self.proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100986 code = _extension_registry.get((module_name, name))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000987 if code:
988 assert code > 0
989 if code <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300990 write(EXT1 + pack("<B", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000991 elif code <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300992 write(EXT2 + pack("<H", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000993 else:
994 write(EXT4 + pack("<i", code))
995 return
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300996 lastname = name.rpartition('.')[2]
997 if parent is module:
998 name = lastname
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000999 # Non-ASCII identifiers are supported only with protocols >= 3.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001000 if self.proto >= 4:
1001 self.save(module_name)
1002 self.save(name)
1003 write(STACK_GLOBAL)
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001004 elif parent is not module:
1005 self.save_reduce(getattr, (parent, lastname))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001006 elif self.proto >= 3:
1007 write(GLOBAL + bytes(module_name, "utf-8") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001008 bytes(name, "utf-8") + b'\n')
1009 else:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001010 if self.fix_imports:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001011 r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING
1012 r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING
1013 if (module_name, name) in r_name_mapping:
1014 module_name, name = r_name_mapping[(module_name, name)]
Serhiy Storchakabfe18242015-03-31 13:12:37 +03001015 elif module_name in r_import_mapping:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001016 module_name = r_import_mapping[module_name]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001017 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001018 write(GLOBAL + bytes(module_name, "ascii") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001019 bytes(name, "ascii") + b'\n')
1020 except UnicodeEncodeError:
1021 raise PicklingError(
1022 "can't pickle global identifier '%s.%s' using "
Serhiy Storchaka5affd232017-04-05 09:37:24 +03001023 "pickle protocol %i" % (module, name, self.proto)) from None
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001024
Guido van Rossum3a41c612003-01-28 15:10:22 +00001025 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +00001026
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08001027 def save_type(self, obj):
1028 if obj is type(None):
1029 return self.save_reduce(type, (None,), obj=obj)
1030 elif obj is type(NotImplemented):
1031 return self.save_reduce(type, (NotImplemented,), obj=obj)
1032 elif obj is type(...):
1033 return self.save_reduce(type, (...,), obj=obj)
1034 return self.save_global(obj)
1035
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001036 dispatch[FunctionType] = save_global
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08001037 dispatch[type] = save_type
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001038
Guido van Rossuma48061a1995-01-10 00:31:14 +00001039
Guido van Rossum1be31752003-01-28 15:19:53 +00001040# Unpickling machinery
1041
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001042class _Unpickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +00001043
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001044 def __init__(self, file, *, fix_imports=True,
1045 encoding="ASCII", errors="strict"):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001046 """This takes a binary file for reading a pickle data stream.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001047
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001048 The protocol version of the pickle is detected automatically, so
1049 no proto argument is needed.
1050
1051 The argument *file* must have two methods, a read() method that
1052 takes an integer argument, and a readline() method that requires
1053 no arguments. Both methods should return bytes. Thus *file*
Martin Panter7462b6492015-11-02 03:37:02 +00001054 can be a binary file object opened for reading, an io.BytesIO
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001055 object, or any other custom object that meets this interface.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001056
Guido van Rossumfeea0782007-10-10 18:00:50 +00001057 The file-like object must have two methods, a read() method
1058 that takes an integer argument, and a readline() method that
1059 requires no arguments. Both methods should return bytes.
1060 Thus file-like object can be a binary file object opened for
1061 reading, a BytesIO object, or any other custom object that
1062 meets this interface.
Guido van Rossumf4169812008-03-17 22:56:06 +00001063
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001064 Optional keyword arguments are *fix_imports*, *encoding* and
Martin Panter46f50722016-05-26 05:35:26 +00001065 *errors*, which are used to control compatibility support for
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001066 pickle stream generated by Python 2. If *fix_imports* is True,
1067 pickle will try to map the old Python 2 names to the new names
1068 used in Python 3. The *encoding* and *errors* tell pickle how
1069 to decode 8-bit string instances pickled by Python 2; these
1070 default to 'ASCII' and 'strict', respectively. *encoding* can be
1071 'bytes' to read theses 8-bit string instances as bytes objects.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001072 """
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001073 self._file_readline = file.readline
1074 self._file_read = file.read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001075 self.memo = {}
Guido van Rossumf4169812008-03-17 22:56:06 +00001076 self.encoding = encoding
1077 self.errors = errors
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001078 self.proto = 0
1079 self.fix_imports = fix_imports
Guido van Rossuma48061a1995-01-10 00:31:14 +00001080
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001081 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +00001082 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001083
Guido van Rossum3a41c612003-01-28 15:10:22 +00001084 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001085 """
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001086 # Check whether Unpickler was initialized correctly. This is
1087 # only needed to mimic the behavior of _pickle.Unpickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001088 if not hasattr(self, "_file_read"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001089 raise UnpicklingError("Unpickler.__init__() was not called by "
1090 "%s.__init__()" % (self.__class__.__name__,))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001091 self._unframer = _Unframer(self._file_read, self._file_readline)
1092 self.read = self._unframer.read
1093 self.readline = self._unframer.readline
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001094 self.metastack = []
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001095 self.stack = []
1096 self.append = self.stack.append
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001097 self.proto = 0
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001098 read = self.read
1099 dispatch = self.dispatch
1100 try:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001101 while True:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001102 key = read(1)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001103 if not key:
1104 raise EOFError
Guido van Rossum98297ee2007-11-06 21:34:58 +00001105 assert isinstance(key, bytes_types)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001106 dispatch[key[0]](self)
Guido van Rossumb940e112007-01-10 16:19:56 +00001107 except _Stop as stopinst:
Guido van Rossumff871742000-12-13 18:11:56 +00001108 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001109
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001110 # Return a list of items pushed in the stack after last MARK instruction.
1111 def pop_mark(self):
1112 items = self.stack
1113 self.stack = self.metastack.pop()
1114 self.append = self.stack.append
1115 return items
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001116
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001117 def persistent_load(self, pid):
Benjamin Peterson49956b22009-01-10 17:05:44 +00001118 raise UnpicklingError("unsupported persistent id encountered")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001119
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001120 dispatch = {}
1121
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001122 def load_proto(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001123 proto = self.read(1)[0]
Guido van Rossumf4169812008-03-17 22:56:06 +00001124 if not 0 <= proto <= HIGHEST_PROTOCOL:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001125 raise ValueError("unsupported pickle protocol: %d" % proto)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001126 self.proto = proto
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001127 dispatch[PROTO[0]] = load_proto
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001128
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001129 def load_frame(self):
1130 frame_size, = unpack('<Q', self.read(8))
1131 if frame_size > sys.maxsize:
1132 raise ValueError("frame size > sys.maxsize: %d" % frame_size)
1133 self._unframer.load_frame(frame_size)
1134 dispatch[FRAME[0]] = load_frame
1135
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001136 def load_persid(self):
Serhiy Storchakadec25af2016-07-17 11:24:17 +03001137 try:
1138 pid = self.readline()[:-1].decode("ascii")
1139 except UnicodeDecodeError:
1140 raise UnpicklingError(
1141 "persistent IDs in protocol 0 must be ASCII strings")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001142 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001143 dispatch[PERSID[0]] = load_persid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001144
1145 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001146 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001147 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001148 dispatch[BINPERSID[0]] = load_binpersid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001149
1150 def load_none(self):
1151 self.append(None)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001152 dispatch[NONE[0]] = load_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001153
Guido van Rossum7d97d312003-01-28 04:25:27 +00001154 def load_false(self):
1155 self.append(False)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001156 dispatch[NEWFALSE[0]] = load_false
Guido van Rossum7d97d312003-01-28 04:25:27 +00001157
1158 def load_true(self):
1159 self.append(True)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001160 dispatch[NEWTRUE[0]] = load_true
Guido van Rossum7d97d312003-01-28 04:25:27 +00001161
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001162 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +00001163 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +00001164 if data == FALSE[1:]:
1165 val = False
1166 elif data == TRUE[1:]:
1167 val = True
1168 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001169 val = int(data, 0)
Guido van Rossume2763392002-04-05 19:30:08 +00001170 self.append(val)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001171 dispatch[INT[0]] = load_int
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001172
1173 def load_binint(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001174 self.append(unpack('<i', self.read(4))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001175 dispatch[BININT[0]] = load_binint
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001176
1177 def load_binint1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001178 self.append(self.read(1)[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001179 dispatch[BININT1[0]] = load_binint1
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001180
1181 def load_binint2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001182 self.append(unpack('<H', self.read(2))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001183 dispatch[BININT2[0]] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +00001184
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001185 def load_long(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001186 val = self.readline()[:-1]
1187 if val and val[-1] == b'L'[0]:
Mark Dickinson8dd05142009-01-20 20:43:58 +00001188 val = val[:-1]
Guido van Rossumfeea0782007-10-10 18:00:50 +00001189 self.append(int(val, 0))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001190 dispatch[LONG[0]] = load_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001191
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001192 def load_long1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001193 n = self.read(1)[0]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001194 data = self.read(n)
1195 self.append(decode_long(data))
1196 dispatch[LONG1[0]] = load_long1
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001197
1198 def load_long4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001199 n, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001200 if n < 0:
1201 # Corrupt or hostile pickle -- we never write one like this
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001202 raise UnpicklingError("LONG pickle has negative byte count")
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001203 data = self.read(n)
1204 self.append(decode_long(data))
1205 dispatch[LONG4[0]] = load_long4
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001206
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001207 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +00001208 self.append(float(self.readline()[:-1]))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001209 dispatch[FLOAT[0]] = load_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001210
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001211 def load_binfloat(self):
Guido van Rossumd3703791998-10-22 20:15:36 +00001212 self.append(unpack('>d', self.read(8))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001213 dispatch[BINFLOAT[0]] = load_binfloat
Guido van Rossumd3703791998-10-22 20:15:36 +00001214
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001215 def _decode_string(self, value):
1216 # Used to allow strings from Python 2 to be decoded either as
1217 # bytes or Unicode strings. This should be used only with the
1218 # STRING, BINSTRING and SHORT_BINSTRING opcodes.
1219 if self.encoding == "bytes":
1220 return value
1221 else:
1222 return value.decode(self.encoding, self.errors)
1223
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001224 def load_string(self):
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001225 data = self.readline()[:-1]
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001226 # Strip outermost quotes
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001227 if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'':
1228 data = data[1:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +00001229 else:
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001230 raise UnpicklingError("the STRING opcode argument must be quoted")
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001231 self.append(self._decode_string(codecs.escape_decode(data)[0]))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001232 dispatch[STRING[0]] = load_string
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001233
1234 def load_binstring(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001235 # Deprecated BINSTRING uses signed 32-bit length
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001236 len, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001237 if len < 0:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001238 raise UnpicklingError("BINSTRING pickle has negative byte count")
Guido van Rossumf4169812008-03-17 22:56:06 +00001239 data = self.read(len)
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001240 self.append(self._decode_string(data))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001241 dispatch[BINSTRING[0]] = load_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001242
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001243 def load_binbytes(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001244 len, = unpack('<I', self.read(4))
1245 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001246 raise UnpicklingError("BINBYTES exceeds system's maximum size "
1247 "of %d bytes" % maxsize)
Guido van Rossumf4169812008-03-17 22:56:06 +00001248 self.append(self.read(len))
1249 dispatch[BINBYTES[0]] = load_binbytes
1250
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001251 def load_unicode(self):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001252 self.append(str(self.readline()[:-1], 'raw-unicode-escape'))
1253 dispatch[UNICODE[0]] = load_unicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001254
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001255 def load_binunicode(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001256 len, = unpack('<I', self.read(4))
1257 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001258 raise UnpicklingError("BINUNICODE exceeds system's maximum size "
1259 "of %d bytes" % maxsize)
Victor Stinner485fb562010-04-13 11:07:24 +00001260 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001261 dispatch[BINUNICODE[0]] = load_binunicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001262
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001263 def load_binunicode8(self):
1264 len, = unpack('<Q', self.read(8))
1265 if len > maxsize:
1266 raise UnpicklingError("BINUNICODE8 exceeds system's maximum size "
1267 "of %d bytes" % maxsize)
1268 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1269 dispatch[BINUNICODE8[0]] = load_binunicode8
1270
Serhiy Storchakae0606192015-09-29 22:10:07 +03001271 def load_binbytes8(self):
1272 len, = unpack('<Q', self.read(8))
1273 if len > maxsize:
1274 raise UnpicklingError("BINBYTES8 exceeds system's maximum size "
1275 "of %d bytes" % maxsize)
1276 self.append(self.read(len))
1277 dispatch[BINBYTES8[0]] = load_binbytes8
1278
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001279 def load_short_binstring(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001280 len = self.read(1)[0]
1281 data = self.read(len)
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001282 self.append(self._decode_string(data))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001283 dispatch[SHORT_BINSTRING[0]] = load_short_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001284
Guido van Rossumf4169812008-03-17 22:56:06 +00001285 def load_short_binbytes(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001286 len = self.read(1)[0]
1287 self.append(self.read(len))
Guido van Rossumf4169812008-03-17 22:56:06 +00001288 dispatch[SHORT_BINBYTES[0]] = load_short_binbytes
1289
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001290 def load_short_binunicode(self):
1291 len = self.read(1)[0]
1292 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1293 dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode
1294
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001295 def load_tuple(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001296 items = self.pop_mark()
1297 self.append(tuple(items))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001298 dispatch[TUPLE[0]] = load_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001299
1300 def load_empty_tuple(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001301 self.append(())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001302 dispatch[EMPTY_TUPLE[0]] = load_empty_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001303
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001304 def load_tuple1(self):
1305 self.stack[-1] = (self.stack[-1],)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001306 dispatch[TUPLE1[0]] = load_tuple1
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001307
1308 def load_tuple2(self):
1309 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001310 dispatch[TUPLE2[0]] = load_tuple2
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001311
1312 def load_tuple3(self):
1313 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001314 dispatch[TUPLE3[0]] = load_tuple3
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001315
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001316 def load_empty_list(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001317 self.append([])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001318 dispatch[EMPTY_LIST[0]] = load_empty_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001319
1320 def load_empty_dictionary(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001321 self.append({})
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001322 dispatch[EMPTY_DICT[0]] = load_empty_dictionary
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001323
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001324 def load_empty_set(self):
1325 self.append(set())
1326 dispatch[EMPTY_SET[0]] = load_empty_set
1327
1328 def load_frozenset(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001329 items = self.pop_mark()
1330 self.append(frozenset(items))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001331 dispatch[FROZENSET[0]] = load_frozenset
1332
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001333 def load_list(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001334 items = self.pop_mark()
1335 self.append(items)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001336 dispatch[LIST[0]] = load_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001337
1338 def load_dict(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001339 items = self.pop_mark()
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001340 d = {items[i]: items[i+1]
1341 for i in range(0, len(items), 2)}
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001342 self.append(d)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001343 dispatch[DICT[0]] = load_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001344
Tim Petersd01c1e92003-01-30 15:41:46 +00001345 # INST and OBJ differ only in how they get a class object. It's not
1346 # only sensible to do the rest in a common routine, the two routines
1347 # previously diverged and grew different bugs.
1348 # klass is the class to instantiate, and k points to the topmost mark
1349 # object, following which are the arguments for klass.__init__.
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001350 def _instantiate(self, klass, args):
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001351 if (args or not isinstance(klass, type) or
1352 hasattr(klass, "__getinitargs__")):
Guido van Rossum743d17e1998-09-15 20:25:57 +00001353 try:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001354 value = klass(*args)
Guido van Rossumb940e112007-01-10 16:19:56 +00001355 except TypeError as err:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001356 raise TypeError("in constructor for %s: %s" %
1357 (klass.__name__, str(err)), sys.exc_info()[2])
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001358 else:
1359 value = klass.__new__(klass)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001360 self.append(value)
Tim Petersd01c1e92003-01-30 15:41:46 +00001361
1362 def load_inst(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001363 module = self.readline()[:-1].decode("ascii")
1364 name = self.readline()[:-1].decode("ascii")
Tim Petersd01c1e92003-01-30 15:41:46 +00001365 klass = self.find_class(module, name)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001366 self._instantiate(klass, self.pop_mark())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001367 dispatch[INST[0]] = load_inst
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001368
1369 def load_obj(self):
Tim Petersd01c1e92003-01-30 15:41:46 +00001370 # Stack is ... markobject classobject arg1 arg2 ...
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001371 args = self.pop_mark()
1372 cls = args.pop(0)
1373 self._instantiate(cls, args)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001374 dispatch[OBJ[0]] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001375
Guido van Rossum3a41c612003-01-28 15:10:22 +00001376 def load_newobj(self):
1377 args = self.stack.pop()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001378 cls = self.stack.pop()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001379 obj = cls.__new__(cls, *args)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001380 self.append(obj)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001381 dispatch[NEWOBJ[0]] = load_newobj
Guido van Rossum3a41c612003-01-28 15:10:22 +00001382
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001383 def load_newobj_ex(self):
1384 kwargs = self.stack.pop()
1385 args = self.stack.pop()
1386 cls = self.stack.pop()
1387 obj = cls.__new__(cls, *args, **kwargs)
1388 self.append(obj)
1389 dispatch[NEWOBJ_EX[0]] = load_newobj_ex
1390
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001391 def load_global(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001392 module = self.readline()[:-1].decode("utf-8")
1393 name = self.readline()[:-1].decode("utf-8")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001394 klass = self.find_class(module, name)
1395 self.append(klass)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001396 dispatch[GLOBAL[0]] = load_global
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001397
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001398 def load_stack_global(self):
1399 name = self.stack.pop()
1400 module = self.stack.pop()
1401 if type(name) is not str or type(module) is not str:
1402 raise UnpicklingError("STACK_GLOBAL requires str")
1403 self.append(self.find_class(module, name))
1404 dispatch[STACK_GLOBAL[0]] = load_stack_global
1405
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001406 def load_ext1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001407 code = self.read(1)[0]
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001408 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001409 dispatch[EXT1[0]] = load_ext1
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001410
1411 def load_ext2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001412 code, = unpack('<H', self.read(2))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001413 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001414 dispatch[EXT2[0]] = load_ext2
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001415
1416 def load_ext4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001417 code, = unpack('<i', self.read(4))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001418 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001419 dispatch[EXT4[0]] = load_ext4
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001420
1421 def get_extension(self, code):
1422 nil = []
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001423 obj = _extension_cache.get(code, nil)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001424 if obj is not nil:
1425 self.append(obj)
1426 return
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001427 key = _inverted_registry.get(code)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001428 if not key:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001429 if code <= 0: # note that 0 is forbidden
1430 # Corrupt or hostile pickle.
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001431 raise UnpicklingError("EXT specifies code <= 0")
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001432 raise ValueError("unregistered extension code %d" % code)
1433 obj = self.find_class(*key)
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001434 _extension_cache[code] = obj
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001435 self.append(obj)
1436
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001437 def find_class(self, module, name):
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001438 # Subclasses may override this.
1439 if self.proto < 3 and self.fix_imports:
1440 if (module, name) in _compat_pickle.NAME_MAPPING:
1441 module, name = _compat_pickle.NAME_MAPPING[(module, name)]
Serhiy Storchakabfe18242015-03-31 13:12:37 +03001442 elif module in _compat_pickle.IMPORT_MAPPING:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001443 module = _compat_pickle.IMPORT_MAPPING[module]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001444 __import__(module, level=0)
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001445 if self.proto >= 4:
1446 return _getattribute(sys.modules[module], name)[0]
1447 else:
1448 return getattr(sys.modules[module], name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001449
1450 def load_reduce(self):
1451 stack = self.stack
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001452 args = stack.pop()
1453 func = stack[-1]
Serhiy Storchakaa8d83f52015-12-01 00:39:25 +02001454 stack[-1] = func(*args)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001455 dispatch[REDUCE[0]] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001456
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001457 def load_pop(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001458 if self.stack:
1459 del self.stack[-1]
1460 else:
1461 self.pop_mark()
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001462 dispatch[POP[0]] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001463
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001464 def load_pop_mark(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001465 self.pop_mark()
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001466 dispatch[POP_MARK[0]] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001467
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001468 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001469 self.append(self.stack[-1])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001470 dispatch[DUP[0]] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001471
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001472 def load_get(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 i = int(self.readline()[:-1])
1474 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001475 dispatch[GET[0]] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001476
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001477 def load_binget(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001478 i = self.read(1)[0]
1479 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001480 dispatch[BINGET[0]] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001481
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001482 def load_long_binget(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001483 i, = unpack('<I', self.read(4))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001484 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001485 dispatch[LONG_BINGET[0]] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001486
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001487 def load_put(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 i = int(self.readline()[:-1])
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001489 if i < 0:
1490 raise ValueError("negative PUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001492 dispatch[PUT[0]] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001493
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001494 def load_binput(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001495 i = self.read(1)[0]
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001496 if i < 0:
1497 raise ValueError("negative BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001498 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001499 dispatch[BINPUT[0]] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001500
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001501 def load_long_binput(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001502 i, = unpack('<I', self.read(4))
1503 if i > maxsize:
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001504 raise ValueError("negative LONG_BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001505 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001506 dispatch[LONG_BINPUT[0]] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001507
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001508 def load_memoize(self):
1509 memo = self.memo
1510 memo[len(memo)] = self.stack[-1]
1511 dispatch[MEMOIZE[0]] = load_memoize
1512
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001513 def load_append(self):
1514 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001515 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001516 list = stack[-1]
1517 list.append(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001518 dispatch[APPEND[0]] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001519
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001520 def load_appends(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001521 items = self.pop_mark()
1522 list_obj = self.stack[-1]
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02001523 try:
1524 extend = list_obj.extend
1525 except AttributeError:
1526 pass
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001527 else:
Serhiy Storchakabee09ae2017-02-02 11:12:47 +02001528 extend(items)
1529 return
1530 # Even if the PEP 307 requires extend() and append() methods,
1531 # fall back on append() if the object has no extend() method
1532 # for backward compatibility.
1533 append = list_obj.append
1534 for item in items:
1535 append(item)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001536 dispatch[APPENDS[0]] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001537
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001538 def load_setitem(self):
1539 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001540 value = stack.pop()
1541 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001542 dict = stack[-1]
1543 dict[key] = value
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001544 dispatch[SETITEM[0]] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001545
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001546 def load_setitems(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001547 items = self.pop_mark()
1548 dict = self.stack[-1]
1549 for i in range(0, len(items), 2):
1550 dict[items[i]] = items[i + 1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001551 dispatch[SETITEMS[0]] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001552
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001553 def load_additems(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001554 items = self.pop_mark()
1555 set_obj = self.stack[-1]
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001556 if isinstance(set_obj, set):
1557 set_obj.update(items)
1558 else:
1559 add = set_obj.add
1560 for item in items:
1561 add(item)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001562 dispatch[ADDITEMS[0]] = load_additems
1563
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001564 def load_build(self):
1565 stack = self.stack
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001566 state = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001567 inst = stack[-1]
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001568 setstate = getattr(inst, "__setstate__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001569 if setstate is not None:
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001570 setstate(state)
1571 return
1572 slotstate = None
1573 if isinstance(state, tuple) and len(state) == 2:
1574 state, slotstate = state
1575 if state:
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001576 inst_dict = inst.__dict__
Antoine Pitroua9f48a02009-05-02 21:41:14 +00001577 intern = sys.intern
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001578 for k, v in state.items():
1579 if type(k) is str:
1580 inst_dict[intern(k)] = v
1581 else:
1582 inst_dict[k] = v
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001583 if slotstate:
1584 for k, v in slotstate.items():
1585 setattr(inst, k, v)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001586 dispatch[BUILD[0]] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001587
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001588 def load_mark(self):
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02001589 self.metastack.append(self.stack)
1590 self.stack = []
1591 self.append = self.stack.append
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001592 dispatch[MARK[0]] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001593
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001594 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001595 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001596 raise _Stop(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001597 dispatch[STOP[0]] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001598
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001599
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001600# Shorthands
1601
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001602def _dump(obj, file, protocol=None, *, fix_imports=True):
1603 _Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001604
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001605def _dumps(obj, protocol=None, *, fix_imports=True):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001606 f = io.BytesIO()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001607 _Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001608 res = f.getvalue()
Guido van Rossum98297ee2007-11-06 21:34:58 +00001609 assert isinstance(res, bytes_types)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001610 return res
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001611
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001612def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"):
1613 return _Unpickler(file, fix_imports=fix_imports,
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001614 encoding=encoding, errors=errors).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001615
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001616def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001617 if isinstance(s, str):
1618 raise TypeError("Can't load pickle from unicode string")
1619 file = io.BytesIO(s)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001620 return _Unpickler(file, fix_imports=fix_imports,
1621 encoding=encoding, errors=errors).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001622
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001623# Use the faster _pickle if possible
1624try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 from _pickle import (
1626 PickleError,
1627 PicklingError,
1628 UnpicklingError,
1629 Pickler,
1630 Unpickler,
1631 dump,
1632 dumps,
1633 load,
1634 loads
1635 )
Brett Cannoncd171c82013-07-04 17:43:24 -04001636except ImportError:
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001637 Pickler, Unpickler = _Pickler, _Unpickler
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001638 dump, dumps, load, loads = _dump, _dumps, _load, _loads
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001639
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001640# Doctest
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001641def _test():
1642 import doctest
1643 return doctest.testmod()
1644
1645if __name__ == "__main__":
Florent Xicluna54540ec2011-11-04 08:29:17 +01001646 import argparse
Alexander Belopolsky455f7bd2010-07-27 23:02:38 +00001647 parser = argparse.ArgumentParser(
1648 description='display contents of the pickle files')
1649 parser.add_argument(
1650 'pickle_file', type=argparse.FileType('br'),
1651 nargs='*', help='the pickle file')
1652 parser.add_argument(
1653 '-t', '--test', action='store_true',
1654 help='run self-test suite')
1655 parser.add_argument(
1656 '-v', action='store_true',
1657 help='run verbosely; only affects self-test run')
1658 args = parser.parse_args()
1659 if args.test:
1660 _test()
1661 else:
1662 if not args.pickle_file:
1663 parser.print_help()
1664 else:
1665 import pprint
1666 for f in args.pickle_file:
1667 obj = load(f)
1668 pprint.pprint(obj)