blob: d41753d2d25bd5da02337024495f9707dc978e98 [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.
Guido van Rossumf4169812008-03-17 22:56:06 +000060# We intentionally write a protocol that Python 2.x cannot read;
61# there are too many issues with that.
62DEFAULT_PROTOCOL = 3
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
186 _FRAME_SIZE_TARGET = 64 * 1024
187
188 def __init__(self, file_write):
189 self.file_write = file_write
190 self.current_frame = None
191
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100192 def start_framing(self):
193 self.current_frame = io.BytesIO()
194
195 def end_framing(self):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800196 if self.current_frame and self.current_frame.tell() > 0:
197 self.commit_frame(force=True)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100198 self.current_frame = None
199
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800200 def commit_frame(self, force=False):
201 if self.current_frame:
202 f = self.current_frame
203 if f.tell() >= self._FRAME_SIZE_TARGET or force:
204 with f.getbuffer() as data:
205 n = len(data)
206 write = self.file_write
207 write(FRAME)
208 write(pack("<Q", n))
209 write(data)
210 f.seek(0)
211 f.truncate()
212
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100213 def write(self, data):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800214 if self.current_frame:
215 return self.current_frame.write(data)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100216 else:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800217 return self.file_write(data)
218
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100219
220class _Unframer:
221
222 def __init__(self, file_read, file_readline, file_tell=None):
223 self.file_read = file_read
224 self.file_readline = file_readline
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100225 self.current_frame = None
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100226
227 def read(self, n):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800228 if self.current_frame:
229 data = self.current_frame.read(n)
230 if not data and n != 0:
231 self.current_frame = None
232 return self.file_read(n)
233 if len(data) < n:
234 raise UnpicklingError(
235 "pickle exhausted before end of frame")
236 return data
237 else:
238 return self.file_read(n)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100239
240 def readline(self):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800241 if self.current_frame:
242 data = self.current_frame.readline()
243 if not data:
244 self.current_frame = None
245 return self.file_readline()
Serhiy Storchaka21d75332015-01-26 10:37:01 +0200246 if data[-1] != b'\n'[0]:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800247 raise UnpicklingError(
248 "pickle exhausted before end of frame")
249 return data
250 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100251 return self.file_readline()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100252
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800253 def load_frame(self, frame_size):
254 if self.current_frame and self.current_frame.read() != b'':
255 raise UnpicklingError(
256 "beginning of a new frame before end of current frame")
257 self.current_frame = io.BytesIO(self.file_read(frame_size))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100258
259
260# Tools used for pickling.
261
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300262def _getattribute(obj, name):
263 for subpath in name.split('.'):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100264 if subpath == '<locals>':
265 raise AttributeError("Can't get local attribute {!r} on {!r}"
266 .format(name, obj))
267 try:
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300268 parent = obj
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100269 obj = getattr(obj, subpath)
270 except AttributeError:
271 raise AttributeError("Can't get attribute {!r} on {!r}"
272 .format(name, obj))
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300273 return obj, parent
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100274
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300275def whichmodule(obj, name):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100276 """Find the module an object belong to."""
277 module_name = getattr(obj, '__module__', None)
278 if module_name is not None:
279 return module_name
Antoine Pitroue1618492014-10-04 22:15:27 +0200280 # Protect the iteration by using a list copy of sys.modules against dynamic
281 # modules that trigger imports of other modules upon calls to getattr.
282 for module_name, module in list(sys.modules.items()):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100283 if module_name == '__main__' or module is None:
284 continue
285 try:
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300286 if _getattribute(module, name)[0] is obj:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100287 return module_name
288 except AttributeError:
289 pass
290 return '__main__'
291
292def encode_long(x):
293 r"""Encode a long to a two's complement little-endian binary string.
294 Note that 0 is a special case, returning an empty string, to save a
295 byte in the LONG1 pickling context.
296
297 >>> encode_long(0)
298 b''
299 >>> encode_long(255)
300 b'\xff\x00'
301 >>> encode_long(32767)
302 b'\xff\x7f'
303 >>> encode_long(-256)
304 b'\x00\xff'
305 >>> encode_long(-32768)
306 b'\x00\x80'
307 >>> encode_long(-128)
308 b'\x80'
309 >>> encode_long(127)
310 b'\x7f'
311 >>>
312 """
313 if x == 0:
314 return b''
315 nbytes = (x.bit_length() >> 3) + 1
316 result = x.to_bytes(nbytes, byteorder='little', signed=True)
317 if x < 0 and nbytes > 1:
318 if result[-1] == 0xff and (result[-2] & 0x80) != 0:
319 result = result[:-1]
320 return result
321
322def decode_long(data):
323 r"""Decode a long from a two's complement little-endian binary string.
324
325 >>> decode_long(b'')
326 0
327 >>> decode_long(b"\xff\x00")
328 255
329 >>> decode_long(b"\xff\x7f")
330 32767
331 >>> decode_long(b"\x00\xff")
332 -256
333 >>> decode_long(b"\x00\x80")
334 -32768
335 >>> decode_long(b"\x80")
336 -128
337 >>> decode_long(b"\x7f")
338 127
339 """
340 return int.from_bytes(data, byteorder='little', signed=True)
341
Skip Montanaro23bafc62001-02-18 03:10:09 +0000342
Guido van Rossum1be31752003-01-28 15:19:53 +0000343# Pickling machinery
344
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000345class _Pickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +0000346
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000347 def __init__(self, file, protocol=None, *, fix_imports=True):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000348 """This takes a binary file for writing a pickle data stream.
349
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800350 The optional *protocol* argument tells the pickler to use the
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100351 given protocol; supported protocols are 0, 1, 2, 3 and 4. The
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800352 default protocol is 3; a backward-incompatible protocol designed
353 for Python 3.
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000354
Guido van Rossum7eff63a2003-01-31 19:42:31 +0000355 Specifying a negative protocol version selects the highest
Tim Peters5bd2a792003-02-01 16:45:06 +0000356 protocol version supported. The higher the protocol used, the
357 more recent the version of Python needed to read the pickle
358 produced.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000359
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800360 The *file* argument must have a write() method that accepts a
361 single bytes argument. It can thus be a file object opened for
362 binary writing, a io.BytesIO instance, or any other custom
363 object that meets this interface.
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000364
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800365 If *fix_imports* is True and *protocol* is less than 3, pickle
366 will try to map the new Python 3 names to the old module names
367 used in Python 2, so that the pickle data stream is readable
368 with Python 2.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000369 """
Guido van Rossumcf117b02003-02-09 17:19:41 +0000370 if protocol is None:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000371 protocol = DEFAULT_PROTOCOL
Guido van Rossumcf117b02003-02-09 17:19:41 +0000372 if protocol < 0:
Tim Peters8587b3c2003-02-13 15:44:41 +0000373 protocol = HIGHEST_PROTOCOL
374 elif not 0 <= protocol <= HIGHEST_PROTOCOL:
375 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100377 self._file_write = file.write
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000378 except AttributeError:
379 raise TypeError("file must have a 'write' attribute")
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800380 self.framer = _Framer(self._file_write)
381 self.write = self.framer.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000382 self.memo = {}
Guido van Rossumcf117b02003-02-09 17:19:41 +0000383 self.proto = int(protocol)
384 self.bin = protocol >= 1
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000385 self.fast = 0
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000386 self.fix_imports = fix_imports and protocol < 3
Guido van Rossuma48061a1995-01-10 00:31:14 +0000387
Fred Drake7f781c92002-05-01 20:33:53 +0000388 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000389 """Clears the pickler's "memo".
390
391 The memo is the data structure that remembers which objects the
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800392 pickler has already seen, so that shared or recursive objects
393 are pickled by reference and not by value. This method is
394 useful when re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000395 """
Fred Drake7f781c92002-05-01 20:33:53 +0000396 self.memo.clear()
397
Guido van Rossum3a41c612003-01-28 15:10:22 +0000398 def dump(self, obj):
Tim Peters5bd2a792003-02-01 16:45:06 +0000399 """Write a pickled representation of obj to the open file."""
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000400 # Check whether Pickler was initialized correctly. This is
401 # only needed to mimic the behavior of _pickle.Pickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100402 if not hasattr(self, "_file_write"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000403 raise PicklingError("Pickler.__init__() was not called by "
404 "%s.__init__()" % (self.__class__.__name__,))
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000405 if self.proto >= 2:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800406 self.write(PROTO + pack("<B", self.proto))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100407 if self.proto >= 4:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800408 self.framer.start_framing()
Guido van Rossum3a41c612003-01-28 15:10:22 +0000409 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000410 self.write(STOP)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800411 self.framer.end_framing()
Guido van Rossuma48061a1995-01-10 00:31:14 +0000412
Jeremy Hylton3422c992003-01-24 19:29:52 +0000413 def memoize(self, obj):
414 """Store an object in the memo."""
415
Tim Peterse46b73f2003-01-27 21:22:10 +0000416 # The Pickler memo is a dictionary mapping object ids to 2-tuples
417 # that contain the Unpickler memo key and the object being memoized.
418 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000419 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000420 # Pickler memo so that transient objects are kept alive during
421 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000422
Tim Peterse46b73f2003-01-27 21:22:10 +0000423 # The use of the Unpickler memo length as the memo key is just a
424 # convention. The only requirement is that the memo values be unique.
425 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000426 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000427 # growable) array, indexed by memo key.
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000428 if self.fast:
429 return
Guido van Rossum9b40e802003-01-30 06:37:41 +0000430 assert id(obj) not in self.memo
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100431 idx = len(self.memo)
432 self.write(self.put(idx))
433 self.memo[id(obj)] = idx, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000434
Tim Petersbb38e302003-01-27 21:25:41 +0000435 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100436 def put(self, idx):
437 if self.proto >= 4:
438 return MEMOIZE
439 elif self.bin:
440 if idx < 256:
441 return BINPUT + pack("<B", idx)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000442 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100443 return LONG_BINPUT + pack("<I", idx)
444 else:
445 return PUT + repr(idx).encode("ascii") + b'\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000446
Tim Petersbb38e302003-01-27 21:25:41 +0000447 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300448 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000449 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000450 if i < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300451 return BINGET + pack("<B", i)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000452 else:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +0100453 return LONG_BINGET + pack("<I", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000454
Guido van Rossum39478e82007-08-27 17:23:59 +0000455 return GET + repr(i).encode("ascii") + b'\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000456
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457 def save(self, obj, save_persistent_id=True):
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -0800458 self.framer.commit_frame()
459
Guido van Rossumbc64e222003-01-28 16:34:19 +0000460 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000461 pid = self.persistent_id(obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000462 if pid is not None and save_persistent_id:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000463 self.save_pers(pid)
464 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000465
Guido van Rossumbc64e222003-01-28 16:34:19 +0000466 # Check the memo
467 x = self.memo.get(id(obj))
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300468 if x is not None:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000469 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000470 return
471
Guido van Rossumbc64e222003-01-28 16:34:19 +0000472 # Check the type dispatch table
Guido van Rossum3a41c612003-01-28 15:10:22 +0000473 t = type(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000474 f = self.dispatch.get(t)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300475 if f is not None:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000476 f(self, obj) # Call unbound method with explicit self
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000477 return
478
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100479 # Check private dispatch table if any, or else copyreg.dispatch_table
480 reduce = getattr(self, 'dispatch_table', dispatch_table).get(t)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300481 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000482 rv = reduce(obj)
483 else:
Antoine Pitrouffd41d92011-10-04 09:23:04 +0200484 # Check for a class with a custom metaclass; treat as regular class
485 try:
486 issc = issubclass(t, type)
487 except TypeError: # t is not a class (old Boost; see SF #502085)
488 issc = False
489 if issc:
490 self.save_global(obj)
491 return
492
Guido van Rossumc53f0092003-02-18 22:05:12 +0000493 # Check for a __reduce_ex__ method, fall back to __reduce__
494 reduce = getattr(obj, "__reduce_ex__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300495 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000496 rv = reduce(self.proto)
497 else:
498 reduce = getattr(obj, "__reduce__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300499 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000500 rv = reduce()
501 else:
502 raise PicklingError("Can't pickle %r object: %r" %
503 (t.__name__, obj))
Tim Petersb32a8312003-01-28 00:48:09 +0000504
Guido van Rossumbc64e222003-01-28 16:34:19 +0000505 # Check for string returned by reduce(), meaning "save as global"
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000506 if isinstance(rv, str):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000507 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000508 return
509
Guido van Rossumbc64e222003-01-28 16:34:19 +0000510 # Assert that reduce() returned a tuple
Guido van Rossum13257902007-06-07 23:15:56 +0000511 if not isinstance(rv, tuple):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000512 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000513
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000514 # Assert that it returned an appropriately sized tuple
Guido van Rossumbc64e222003-01-28 16:34:19 +0000515 l = len(rv)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000516 if not (2 <= l <= 5):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000517 raise PicklingError("Tuple returned by %s must have "
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000518 "two to five elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000519
Guido van Rossumbc64e222003-01-28 16:34:19 +0000520 # Save the reduce() output and finally memoize the object
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000521 self.save_reduce(obj=obj, *rv)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000522
Guido van Rossum3a41c612003-01-28 15:10:22 +0000523 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000524 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000525 return None
526
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000527 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000528 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000529 if self.bin:
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 self.save(pid, save_persistent_id=False)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000531 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000532 else:
Guido van Rossum39478e82007-08-27 17:23:59 +0000533 self.write(PERSID + str(pid).encode("ascii") + b'\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000534
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100535 def save_reduce(self, func, args, state=None, listitems=None,
536 dictitems=None, obj=None):
Jeremy Hyltone3a565e2003-06-29 16:59:59 +0000537 # This API is called by some subclasses
Guido van Rossumbc64e222003-01-28 16:34:19 +0000538
Guido van Rossum13257902007-06-07 23:15:56 +0000539 if not isinstance(args, tuple):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100540 raise PicklingError("args from save_reduce() must be a tuple")
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200541 if not callable(func):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100542 raise PicklingError("func from save_reduce() must be callable")
Guido van Rossumbc64e222003-01-28 16:34:19 +0000543
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000544 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000545 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000546
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100547 func_name = getattr(func, "__name__", "")
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300548 if self.proto >= 2 and func_name == "__newobj_ex__":
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100549 cls, args, kwargs = args
550 if not hasattr(cls, "__new__"):
551 raise PicklingError("args[0] from {} args has no __new__"
552 .format(func_name))
553 if obj is not None and cls is not obj.__class__:
554 raise PicklingError("args[0] from {} args has the wrong class"
555 .format(func_name))
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300556 if self.proto >= 4:
557 save(cls)
558 save(args)
559 save(kwargs)
560 write(NEWOBJ_EX)
561 else:
562 func = partial(cls.__new__, cls, *args, **kwargs)
563 save(func)
564 save(())
565 write(REDUCE)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100566 elif self.proto >= 2 and func_name == "__newobj__":
567 # A __reduce__ implementation can direct protocol 2 or newer to
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000568 # use the more efficient NEWOBJ opcode, while still
569 # allowing protocol 0 and 1 to work normally. For this to
570 # work, the function returned by __reduce__ should be
571 # called __newobj__, and its first argument should be a
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100572 # class. The implementation for __newobj__
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000573 # should be as follows, although pickle has no way to
574 # verify this:
575 #
576 # def __newobj__(cls, *args):
577 # return cls.__new__(cls, *args)
578 #
579 # Protocols 0 and 1 will pickle a reference to __newobj__,
580 # while protocol 2 (and above) will pickle a reference to
581 # cls, the remaining args tuple, and the NEWOBJ code,
582 # which calls cls.__new__(cls, *args) at unpickling time
583 # (see load_newobj below). If __reduce__ returns a
584 # three-tuple, the state from the third tuple item will be
585 # pickled regardless of the protocol, calling __setstate__
586 # at unpickling time (see load_build below).
587 #
588 # Note that no standard __newobj__ implementation exists;
589 # you have to provide your own. This is to enforce
590 # compatibility with Python 2.2 (pickles written using
591 # protocol 0 or 1 in Python 2.3 should be unpicklable by
592 # Python 2.2).
593 cls = args[0]
594 if not hasattr(cls, "__new__"):
595 raise PicklingError(
596 "args[0] from __newobj__ args has no __new__")
Guido van Rossumf7f45172003-01-31 17:17:49 +0000597 if obj is not None and cls is not obj.__class__:
598 raise PicklingError(
599 "args[0] from __newobj__ args has the wrong class")
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000600 args = args[1:]
601 save(cls)
602 save(args)
603 write(NEWOBJ)
604 else:
605 save(func)
606 save(args)
607 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000608
Guido van Rossumf7f45172003-01-31 17:17:49 +0000609 if obj is not None:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100610 # If the object is already in the memo, this means it is
611 # recursive. In this case, throw away everything we put on the
612 # stack, and fetch the object back from the memo.
613 if id(obj) in self.memo:
614 write(POP + self.get(self.memo[id(obj)][0]))
615 else:
616 self.memoize(obj)
Guido van Rossumf7f45172003-01-31 17:17:49 +0000617
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000618 # More new special cases (that work with older protocols as
619 # well): when __reduce__ returns a tuple with 4 or 5 items,
620 # the 4th and 5th item should be iterators that provide list
621 # items and dict items (as (key, value) tuples), or None.
622
623 if listitems is not None:
624 self._batch_appends(listitems)
625
626 if dictitems is not None:
627 self._batch_setitems(dictitems)
628
Tim Petersc32d8242001-04-10 02:48:53 +0000629 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000630 save(state)
631 write(BUILD)
632
Guido van Rossumbc64e222003-01-28 16:34:19 +0000633 # Methods below this point are dispatched through the dispatch table
634
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000635 dispatch = {}
636
Guido van Rossum3a41c612003-01-28 15:10:22 +0000637 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000638 self.write(NONE)
Guido van Rossum13257902007-06-07 23:15:56 +0000639 dispatch[type(None)] = save_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000640
Guido van Rossum3a41c612003-01-28 15:10:22 +0000641 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000642 if self.proto >= 2:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300643 self.write(NEWTRUE if obj else NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000644 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300645 self.write(TRUE if obj else FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000646 dispatch[bool] = save_bool
647
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300648 def save_long(self, obj):
Guido van Rossumddefaf32007-01-14 03:31:43 +0000649 if self.bin:
650 # If the int is small enough to fit in a signed 4-byte 2's-comp
651 # format, we can store it more efficiently than the general
652 # case.
653 # First one- and two-byte unsigned ints:
654 if obj >= 0:
655 if obj <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300656 self.write(BININT1 + pack("<B", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000657 return
658 if obj <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300659 self.write(BININT2 + pack("<H", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000660 return
661 # Next check for 4-byte signed ints:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300662 if -0x80000000 <= obj <= 0x7fffffff:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000663 self.write(BININT + pack("<i", obj))
664 return
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000665 if self.proto >= 2:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000666 encoded = encode_long(obj)
667 n = len(encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000668 if n < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300669 self.write(LONG1 + pack("<B", n) + encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000670 else:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000671 self.write(LONG4 + pack("<i", n) + encoded)
Tim Petersee1a53c2003-02-02 02:57:53 +0000672 return
Mark Dickinson8dd05142009-01-20 20:43:58 +0000673 self.write(LONG + repr(obj).encode("ascii") + b'L\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000674 dispatch[int] = save_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000675
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300676 def save_float(self, obj):
Guido van Rossumd3703791998-10-22 20:15:36 +0000677 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000678 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000679 else:
Guido van Rossum39478e82007-08-27 17:23:59 +0000680 self.write(FLOAT + repr(obj).encode("ascii") + b'\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000681 dispatch[float] = save_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000682
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300683 def save_bytes(self, obj):
Guido van Rossumf4169812008-03-17 22:56:06 +0000684 if self.proto < 3:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300685 if not obj: # bytes object is empty
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500686 self.save_reduce(bytes, (), obj=obj)
687 else:
688 self.save_reduce(codecs.encode,
689 (str(obj, 'latin1'), 'latin1'), obj=obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000690 return
691 n = len(obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100692 if n <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300693 self.write(SHORT_BINBYTES + pack("<B", n) + obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100694 elif n > 0xffffffff and self.proto >= 4:
695 self.write(BINBYTES8 + pack("<Q", n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000696 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300697 self.write(BINBYTES + pack("<I", n) + obj)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000698 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000699 dispatch[bytes] = save_bytes
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000700
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300701 def save_str(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000702 if self.bin:
Victor Stinner485fb562010-04-13 11:07:24 +0000703 encoded = obj.encode('utf-8', 'surrogatepass')
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000704 n = len(encoded)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100705 if n <= 0xff and self.proto >= 4:
706 self.write(SHORT_BINUNICODE + pack("<B", n) + encoded)
707 elif n > 0xffffffff and self.proto >= 4:
708 self.write(BINUNICODE8 + pack("<Q", n) + encoded)
709 else:
710 self.write(BINUNICODE + pack("<I", n) + encoded)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000711 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000712 obj = obj.replace("\\", "\\u005c")
713 obj = obj.replace("\n", "\\u000a")
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100714 self.write(UNICODE + obj.encode('raw-unicode-escape') +
715 b'\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000716 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000717 dispatch[str] = save_str
Tim Peters658cba62001-02-09 20:06:00 +0000718
Guido van Rossum3a41c612003-01-28 15:10:22 +0000719 def save_tuple(self, obj):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300720 if not obj: # tuple is empty
721 if self.bin:
722 self.write(EMPTY_TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000723 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300724 self.write(MARK + TUPLE)
Tim Petersd97da802003-01-28 05:48:29 +0000725 return
726
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300727 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000728 save = self.save
729 memo = self.memo
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300730 if n <= 3 and self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000731 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000732 save(element)
733 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000734 if id(obj) in memo:
735 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300736 self.write(POP * n + get)
Tim Petersd97da802003-01-28 05:48:29 +0000737 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300738 self.write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000739 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000740 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000741
Tim Peters1d63c9f2003-02-02 20:29:39 +0000742 # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple
Tim Petersff57bff2003-01-28 05:34:53 +0000743 # has more than 3 elements.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300744 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000745 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000746 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000747 save(element)
748
Tim Peters1d63c9f2003-02-02 20:29:39 +0000749 if id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000750 # Subtle. d was not in memo when we entered save_tuple(), so
751 # the process of saving the tuple's elements must have saved
752 # the tuple itself: the tuple is recursive. The proper action
753 # now is to throw away everything we put on the stack, and
754 # simply GET the tuple (it's already constructed). This check
755 # could have been done in the "for element" loop instead, but
756 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000757 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300758 if self.bin:
Tim Petersf558da02003-01-28 02:09:55 +0000759 write(POP_MARK + get)
760 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000761 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000762 return
763
Tim Peters1d63c9f2003-02-02 20:29:39 +0000764 # No recursion.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300765 write(TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000766 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000767
Guido van Rossum13257902007-06-07 23:15:56 +0000768 dispatch[tuple] = save_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769
Guido van Rossum3a41c612003-01-28 15:10:22 +0000770 def save_list(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000771 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300772 self.write(EMPTY_LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000773 else: # proto 0 -- can't use EMPTY_LIST
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300774 self.write(MARK + LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000775
776 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000777 self._batch_appends(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000778
Guido van Rossum13257902007-06-07 23:15:56 +0000779 dispatch[list] = save_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000780
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000781 _BATCHSIZE = 1000
782
783 def _batch_appends(self, items):
784 # Helper to batch up APPENDS sequences
785 save = self.save
786 write = self.write
787
788 if not self.bin:
789 for x in items:
790 save(x)
791 write(APPEND)
792 return
793
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300794 it = iter(items)
795 while True:
796 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000797 n = len(tmp)
798 if n > 1:
799 write(MARK)
800 for x in tmp:
801 save(x)
802 write(APPENDS)
803 elif n:
804 save(tmp[0])
805 write(APPEND)
806 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300807 if n < self._BATCHSIZE:
808 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000809
Guido van Rossum3a41c612003-01-28 15:10:22 +0000810 def save_dict(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000811 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300812 self.write(EMPTY_DICT)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000813 else: # proto 0 -- can't use EMPTY_DICT
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300814 self.write(MARK + DICT)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000815
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000816 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000817 self._batch_setitems(obj.items())
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000818
Guido van Rossum13257902007-06-07 23:15:56 +0000819 dispatch[dict] = save_dict
820 if PyStringMap is not None:
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000821 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000822
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000823 def _batch_setitems(self, items):
824 # Helper to batch up SETITEMS sequences; proto >= 1 only
825 save = self.save
826 write = self.write
827
828 if not self.bin:
829 for k, v in items:
830 save(k)
831 save(v)
832 write(SETITEM)
833 return
834
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300835 it = iter(items)
836 while True:
837 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000838 n = len(tmp)
839 if n > 1:
840 write(MARK)
841 for k, v in tmp:
842 save(k)
843 save(v)
844 write(SETITEMS)
845 elif n:
846 k, v = tmp[0]
847 save(k)
848 save(v)
849 write(SETITEM)
850 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300851 if n < self._BATCHSIZE:
852 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000853
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100854 def save_set(self, obj):
855 save = self.save
856 write = self.write
857
858 if self.proto < 4:
859 self.save_reduce(set, (list(obj),), obj=obj)
860 return
861
862 write(EMPTY_SET)
863 self.memoize(obj)
864
865 it = iter(obj)
866 while True:
867 batch = list(islice(it, self._BATCHSIZE))
868 n = len(batch)
869 if n > 0:
870 write(MARK)
871 for item in batch:
872 save(item)
873 write(ADDITEMS)
874 if n < self._BATCHSIZE:
875 return
876 dispatch[set] = save_set
877
878 def save_frozenset(self, obj):
879 save = self.save
880 write = self.write
881
882 if self.proto < 4:
883 self.save_reduce(frozenset, (list(obj),), obj=obj)
884 return
885
886 write(MARK)
887 for item in obj:
888 save(item)
889
890 if id(obj) in self.memo:
891 # If the object is already in the memo, this means it is
892 # recursive. In this case, throw away everything we put on the
893 # stack, and fetch the object back from the memo.
894 write(POP_MARK + self.get(self.memo[id(obj)][0]))
895 return
896
897 write(FROZENSET)
898 self.memoize(obj)
899 dispatch[frozenset] = save_frozenset
900
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300901 def save_global(self, obj, name=None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000902 write = self.write
903 memo = self.memo
904
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300905 if name is None:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100906 name = getattr(obj, '__qualname__', None)
Tim Petersc32d8242001-04-10 02:48:53 +0000907 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000908 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000909
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300910 module_name = whichmodule(obj, name)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000911 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100912 __import__(module_name, level=0)
913 module = sys.modules[module_name]
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300914 obj2, parent = _getattribute(module, name)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000915 except (ImportError, KeyError, AttributeError):
916 raise PicklingError(
917 "Can't pickle %r: it's not found as %s.%s" %
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100918 (obj, module_name, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000919 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100920 if obj2 is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000921 raise PicklingError(
922 "Can't pickle %r: it's not the same object as %s.%s" %
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100923 (obj, module_name, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000924
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000925 if self.proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100926 code = _extension_registry.get((module_name, name))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000927 if code:
928 assert code > 0
929 if code <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300930 write(EXT1 + pack("<B", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000931 elif code <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300932 write(EXT2 + pack("<H", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000933 else:
934 write(EXT4 + pack("<i", code))
935 return
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300936 lastname = name.rpartition('.')[2]
937 if parent is module:
938 name = lastname
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000939 # Non-ASCII identifiers are supported only with protocols >= 3.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100940 if self.proto >= 4:
941 self.save(module_name)
942 self.save(name)
943 write(STACK_GLOBAL)
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300944 elif parent is not module:
945 self.save_reduce(getattr, (parent, lastname))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 elif self.proto >= 3:
947 write(GLOBAL + bytes(module_name, "utf-8") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000948 bytes(name, "utf-8") + b'\n')
949 else:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000950 if self.fix_imports:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951 r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING
952 r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING
953 if (module_name, name) in r_name_mapping:
954 module_name, name = r_name_mapping[(module_name, name)]
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300955 elif module_name in r_import_mapping:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956 module_name = r_import_mapping[module_name]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000957 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100958 write(GLOBAL + bytes(module_name, "ascii") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000959 bytes(name, "ascii") + b'\n')
960 except UnicodeEncodeError:
961 raise PicklingError(
962 "can't pickle global identifier '%s.%s' using "
963 "pickle protocol %i" % (module, name, self.proto))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000964
Guido van Rossum3a41c612003-01-28 15:10:22 +0000965 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +0000966
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -0800967 def save_type(self, obj):
968 if obj is type(None):
969 return self.save_reduce(type, (None,), obj=obj)
970 elif obj is type(NotImplemented):
971 return self.save_reduce(type, (NotImplemented,), obj=obj)
972 elif obj is type(...):
973 return self.save_reduce(type, (...,), obj=obj)
974 return self.save_global(obj)
975
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000976 dispatch[FunctionType] = save_global
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -0800977 dispatch[type] = save_type
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000978
Guido van Rossuma48061a1995-01-10 00:31:14 +0000979
Guido van Rossum1be31752003-01-28 15:19:53 +0000980# Unpickling machinery
981
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000982class _Unpickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +0000983
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000984 def __init__(self, file, *, fix_imports=True,
985 encoding="ASCII", errors="strict"):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000986 """This takes a binary file for reading a pickle data stream.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000987
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -0800988 The protocol version of the pickle is detected automatically, so
989 no proto argument is needed.
990
991 The argument *file* must have two methods, a read() method that
992 takes an integer argument, and a readline() method that requires
993 no arguments. Both methods should return bytes. Thus *file*
994 can be a binary file object opened for reading, a io.BytesIO
995 object, or any other custom object that meets this interface.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000996
Guido van Rossumfeea0782007-10-10 18:00:50 +0000997 The file-like object must have two methods, a read() method
998 that takes an integer argument, and a readline() method that
999 requires no arguments. Both methods should return bytes.
1000 Thus file-like object can be a binary file object opened for
1001 reading, a BytesIO object, or any other custom object that
1002 meets this interface.
Guido van Rossumf4169812008-03-17 22:56:06 +00001003
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001004 Optional keyword arguments are *fix_imports*, *encoding* and
1005 *errors*, which are used to control compatiblity support for
1006 pickle stream generated by Python 2. If *fix_imports* is True,
1007 pickle will try to map the old Python 2 names to the new names
1008 used in Python 3. The *encoding* and *errors* tell pickle how
1009 to decode 8-bit string instances pickled by Python 2; these
1010 default to 'ASCII' and 'strict', respectively. *encoding* can be
1011 'bytes' to read theses 8-bit string instances as bytes objects.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001012 """
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001013 self._file_readline = file.readline
1014 self._file_read = file.read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001015 self.memo = {}
Guido van Rossumf4169812008-03-17 22:56:06 +00001016 self.encoding = encoding
1017 self.errors = errors
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001018 self.proto = 0
1019 self.fix_imports = fix_imports
Guido van Rossuma48061a1995-01-10 00:31:14 +00001020
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001021 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +00001022 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001023
Guido van Rossum3a41c612003-01-28 15:10:22 +00001024 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001025 """
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001026 # Check whether Unpickler was initialized correctly. This is
1027 # only needed to mimic the behavior of _pickle.Unpickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001028 if not hasattr(self, "_file_read"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001029 raise UnpicklingError("Unpickler.__init__() was not called by "
1030 "%s.__init__()" % (self.__class__.__name__,))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001031 self._unframer = _Unframer(self._file_read, self._file_readline)
1032 self.read = self._unframer.read
1033 self.readline = self._unframer.readline
Jeremy Hylton20747fa2001-11-09 16:15:04 +00001034 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001035 self.stack = []
1036 self.append = self.stack.append
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001037 self.proto = 0
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001038 read = self.read
1039 dispatch = self.dispatch
1040 try:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001041 while True:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001042 key = read(1)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001043 if not key:
1044 raise EOFError
Guido van Rossum98297ee2007-11-06 21:34:58 +00001045 assert isinstance(key, bytes_types)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001046 dispatch[key[0]](self)
Guido van Rossumb940e112007-01-10 16:19:56 +00001047 except _Stop as stopinst:
Guido van Rossumff871742000-12-13 18:11:56 +00001048 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001049
Tim Petersc23d18a2003-01-28 01:41:51 +00001050 # Return largest index k such that self.stack[k] is self.mark.
1051 # If the stack doesn't contain a mark, eventually raises IndexError.
1052 # This could be sped by maintaining another stack, of indices at which
1053 # the mark appears. For that matter, the latter stack would suffice,
1054 # and we wouldn't need to push mark objects on self.stack at all.
1055 # Doing so is probably a good thing, though, since if the pickle is
1056 # corrupt (or hostile) we may get a clue from finding self.mark embedded
1057 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001058 def marker(self):
1059 stack = self.stack
1060 mark = self.mark
1061 k = len(stack)-1
1062 while stack[k] is not mark: k = k-1
1063 return k
1064
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001065 def persistent_load(self, pid):
Benjamin Peterson49956b22009-01-10 17:05:44 +00001066 raise UnpicklingError("unsupported persistent id encountered")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001067
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001068 dispatch = {}
1069
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001070 def load_proto(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001071 proto = self.read(1)[0]
Guido van Rossumf4169812008-03-17 22:56:06 +00001072 if not 0 <= proto <= HIGHEST_PROTOCOL:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001073 raise ValueError("unsupported pickle protocol: %d" % proto)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001074 self.proto = proto
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001075 dispatch[PROTO[0]] = load_proto
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001076
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001077 def load_frame(self):
1078 frame_size, = unpack('<Q', self.read(8))
1079 if frame_size > sys.maxsize:
1080 raise ValueError("frame size > sys.maxsize: %d" % frame_size)
1081 self._unframer.load_frame(frame_size)
1082 dispatch[FRAME[0]] = load_frame
1083
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001084 def load_persid(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001085 pid = self.readline()[:-1].decode("ascii")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001086 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001087 dispatch[PERSID[0]] = load_persid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001088
1089 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001090 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001091 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001092 dispatch[BINPERSID[0]] = load_binpersid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001093
1094 def load_none(self):
1095 self.append(None)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001096 dispatch[NONE[0]] = load_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001097
Guido van Rossum7d97d312003-01-28 04:25:27 +00001098 def load_false(self):
1099 self.append(False)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001100 dispatch[NEWFALSE[0]] = load_false
Guido van Rossum7d97d312003-01-28 04:25:27 +00001101
1102 def load_true(self):
1103 self.append(True)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001104 dispatch[NEWTRUE[0]] = load_true
Guido van Rossum7d97d312003-01-28 04:25:27 +00001105
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001106 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +00001107 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +00001108 if data == FALSE[1:]:
1109 val = False
1110 elif data == TRUE[1:]:
1111 val = True
1112 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001113 val = int(data, 0)
Guido van Rossume2763392002-04-05 19:30:08 +00001114 self.append(val)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001115 dispatch[INT[0]] = load_int
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001116
1117 def load_binint(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001118 self.append(unpack('<i', self.read(4))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001119 dispatch[BININT[0]] = load_binint
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001120
1121 def load_binint1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001122 self.append(self.read(1)[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001123 dispatch[BININT1[0]] = load_binint1
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001124
1125 def load_binint2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001126 self.append(unpack('<H', self.read(2))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001127 dispatch[BININT2[0]] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +00001128
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001129 def load_long(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001130 val = self.readline()[:-1]
1131 if val and val[-1] == b'L'[0]:
Mark Dickinson8dd05142009-01-20 20:43:58 +00001132 val = val[:-1]
Guido van Rossumfeea0782007-10-10 18:00:50 +00001133 self.append(int(val, 0))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001134 dispatch[LONG[0]] = load_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001135
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001136 def load_long1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001137 n = self.read(1)[0]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001138 data = self.read(n)
1139 self.append(decode_long(data))
1140 dispatch[LONG1[0]] = load_long1
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001141
1142 def load_long4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001143 n, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001144 if n < 0:
1145 # Corrupt or hostile pickle -- we never write one like this
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001146 raise UnpicklingError("LONG pickle has negative byte count")
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001147 data = self.read(n)
1148 self.append(decode_long(data))
1149 dispatch[LONG4[0]] = load_long4
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001150
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001151 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +00001152 self.append(float(self.readline()[:-1]))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001153 dispatch[FLOAT[0]] = load_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001154
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001155 def load_binfloat(self):
Guido van Rossumd3703791998-10-22 20:15:36 +00001156 self.append(unpack('>d', self.read(8))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001157 dispatch[BINFLOAT[0]] = load_binfloat
Guido van Rossumd3703791998-10-22 20:15:36 +00001158
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001159 def _decode_string(self, value):
1160 # Used to allow strings from Python 2 to be decoded either as
1161 # bytes or Unicode strings. This should be used only with the
1162 # STRING, BINSTRING and SHORT_BINSTRING opcodes.
1163 if self.encoding == "bytes":
1164 return value
1165 else:
1166 return value.decode(self.encoding, self.errors)
1167
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001168 def load_string(self):
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001169 data = self.readline()[:-1]
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001170 # Strip outermost quotes
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001171 if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'':
1172 data = data[1:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +00001173 else:
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001174 raise UnpicklingError("the STRING opcode argument must be quoted")
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001175 self.append(self._decode_string(codecs.escape_decode(data)[0]))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001176 dispatch[STRING[0]] = load_string
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001177
1178 def load_binstring(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001179 # Deprecated BINSTRING uses signed 32-bit length
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001180 len, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001181 if len < 0:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001182 raise UnpicklingError("BINSTRING pickle has negative byte count")
Guido van Rossumf4169812008-03-17 22:56:06 +00001183 data = self.read(len)
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001184 self.append(self._decode_string(data))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001185 dispatch[BINSTRING[0]] = load_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001186
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001187 def load_binbytes(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001188 len, = unpack('<I', self.read(4))
1189 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001190 raise UnpicklingError("BINBYTES exceeds system's maximum size "
1191 "of %d bytes" % maxsize)
Guido van Rossumf4169812008-03-17 22:56:06 +00001192 self.append(self.read(len))
1193 dispatch[BINBYTES[0]] = load_binbytes
1194
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001195 def load_unicode(self):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001196 self.append(str(self.readline()[:-1], 'raw-unicode-escape'))
1197 dispatch[UNICODE[0]] = load_unicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001198
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001199 def load_binunicode(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001200 len, = unpack('<I', self.read(4))
1201 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001202 raise UnpicklingError("BINUNICODE exceeds system's maximum size "
1203 "of %d bytes" % maxsize)
Victor Stinner485fb562010-04-13 11:07:24 +00001204 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001205 dispatch[BINUNICODE[0]] = load_binunicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001206
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001207 def load_binunicode8(self):
1208 len, = unpack('<Q', self.read(8))
1209 if len > maxsize:
1210 raise UnpicklingError("BINUNICODE8 exceeds system's maximum size "
1211 "of %d bytes" % maxsize)
1212 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1213 dispatch[BINUNICODE8[0]] = load_binunicode8
1214
Serhiy Storchakae0606192015-09-29 22:10:07 +03001215 def load_binbytes8(self):
1216 len, = unpack('<Q', self.read(8))
1217 if len > maxsize:
1218 raise UnpicklingError("BINBYTES8 exceeds system's maximum size "
1219 "of %d bytes" % maxsize)
1220 self.append(self.read(len))
1221 dispatch[BINBYTES8[0]] = load_binbytes8
1222
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001223 def load_short_binstring(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001224 len = self.read(1)[0]
1225 data = self.read(len)
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08001226 self.append(self._decode_string(data))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001227 dispatch[SHORT_BINSTRING[0]] = load_short_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001228
Guido van Rossumf4169812008-03-17 22:56:06 +00001229 def load_short_binbytes(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001230 len = self.read(1)[0]
1231 self.append(self.read(len))
Guido van Rossumf4169812008-03-17 22:56:06 +00001232 dispatch[SHORT_BINBYTES[0]] = load_short_binbytes
1233
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001234 def load_short_binunicode(self):
1235 len = self.read(1)[0]
1236 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1237 dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode
1238
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001239 def load_tuple(self):
1240 k = self.marker()
1241 self.stack[k:] = [tuple(self.stack[k+1:])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001242 dispatch[TUPLE[0]] = load_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001243
1244 def load_empty_tuple(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245 self.append(())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001246 dispatch[EMPTY_TUPLE[0]] = load_empty_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001247
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001248 def load_tuple1(self):
1249 self.stack[-1] = (self.stack[-1],)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001250 dispatch[TUPLE1[0]] = load_tuple1
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001251
1252 def load_tuple2(self):
1253 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001254 dispatch[TUPLE2[0]] = load_tuple2
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001255
1256 def load_tuple3(self):
1257 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001258 dispatch[TUPLE3[0]] = load_tuple3
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001259
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001260 def load_empty_list(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001261 self.append([])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001262 dispatch[EMPTY_LIST[0]] = load_empty_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001263
1264 def load_empty_dictionary(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001265 self.append({})
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001266 dispatch[EMPTY_DICT[0]] = load_empty_dictionary
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001267
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001268 def load_empty_set(self):
1269 self.append(set())
1270 dispatch[EMPTY_SET[0]] = load_empty_set
1271
1272 def load_frozenset(self):
1273 k = self.marker()
1274 self.stack[k:] = [frozenset(self.stack[k+1:])]
1275 dispatch[FROZENSET[0]] = load_frozenset
1276
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001277 def load_list(self):
1278 k = self.marker()
1279 self.stack[k:] = [self.stack[k+1:]]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001280 dispatch[LIST[0]] = load_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001281
1282 def load_dict(self):
1283 k = self.marker()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001284 items = self.stack[k+1:]
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001285 d = {items[i]: items[i+1]
1286 for i in range(0, len(items), 2)}
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001287 self.stack[k:] = [d]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001288 dispatch[DICT[0]] = load_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001289
Tim Petersd01c1e92003-01-30 15:41:46 +00001290 # INST and OBJ differ only in how they get a class object. It's not
1291 # only sensible to do the rest in a common routine, the two routines
1292 # previously diverged and grew different bugs.
1293 # klass is the class to instantiate, and k points to the topmost mark
1294 # object, following which are the arguments for klass.__init__.
1295 def _instantiate(self, klass, k):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001296 args = tuple(self.stack[k+1:])
1297 del self.stack[k:]
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001298 if (args or not isinstance(klass, type) or
1299 hasattr(klass, "__getinitargs__")):
Guido van Rossum743d17e1998-09-15 20:25:57 +00001300 try:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001301 value = klass(*args)
Guido van Rossumb940e112007-01-10 16:19:56 +00001302 except TypeError as err:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001303 raise TypeError("in constructor for %s: %s" %
1304 (klass.__name__, str(err)), sys.exc_info()[2])
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001305 else:
1306 value = klass.__new__(klass)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001307 self.append(value)
Tim Petersd01c1e92003-01-30 15:41:46 +00001308
1309 def load_inst(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001310 module = self.readline()[:-1].decode("ascii")
1311 name = self.readline()[:-1].decode("ascii")
Tim Petersd01c1e92003-01-30 15:41:46 +00001312 klass = self.find_class(module, name)
1313 self._instantiate(klass, self.marker())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001314 dispatch[INST[0]] = load_inst
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001315
1316 def load_obj(self):
Tim Petersd01c1e92003-01-30 15:41:46 +00001317 # Stack is ... markobject classobject arg1 arg2 ...
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001318 k = self.marker()
Tim Petersd01c1e92003-01-30 15:41:46 +00001319 klass = self.stack.pop(k+1)
1320 self._instantiate(klass, k)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001321 dispatch[OBJ[0]] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001322
Guido van Rossum3a41c612003-01-28 15:10:22 +00001323 def load_newobj(self):
1324 args = self.stack.pop()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001325 cls = self.stack.pop()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001326 obj = cls.__new__(cls, *args)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001327 self.append(obj)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001328 dispatch[NEWOBJ[0]] = load_newobj
Guido van Rossum3a41c612003-01-28 15:10:22 +00001329
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001330 def load_newobj_ex(self):
1331 kwargs = self.stack.pop()
1332 args = self.stack.pop()
1333 cls = self.stack.pop()
1334 obj = cls.__new__(cls, *args, **kwargs)
1335 self.append(obj)
1336 dispatch[NEWOBJ_EX[0]] = load_newobj_ex
1337
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001338 def load_global(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001339 module = self.readline()[:-1].decode("utf-8")
1340 name = self.readline()[:-1].decode("utf-8")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001341 klass = self.find_class(module, name)
1342 self.append(klass)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001343 dispatch[GLOBAL[0]] = load_global
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001344
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001345 def load_stack_global(self):
1346 name = self.stack.pop()
1347 module = self.stack.pop()
1348 if type(name) is not str or type(module) is not str:
1349 raise UnpicklingError("STACK_GLOBAL requires str")
1350 self.append(self.find_class(module, name))
1351 dispatch[STACK_GLOBAL[0]] = load_stack_global
1352
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001353 def load_ext1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001354 code = self.read(1)[0]
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001355 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001356 dispatch[EXT1[0]] = load_ext1
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001357
1358 def load_ext2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001359 code, = unpack('<H', self.read(2))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001360 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001361 dispatch[EXT2[0]] = load_ext2
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001362
1363 def load_ext4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001364 code, = unpack('<i', self.read(4))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001365 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001366 dispatch[EXT4[0]] = load_ext4
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001367
1368 def get_extension(self, code):
1369 nil = []
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001370 obj = _extension_cache.get(code, nil)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001371 if obj is not nil:
1372 self.append(obj)
1373 return
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001374 key = _inverted_registry.get(code)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001375 if not key:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001376 if code <= 0: # note that 0 is forbidden
1377 # Corrupt or hostile pickle.
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001378 raise UnpicklingError("EXT specifies code <= 0")
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001379 raise ValueError("unregistered extension code %d" % code)
1380 obj = self.find_class(*key)
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001381 _extension_cache[code] = obj
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001382 self.append(obj)
1383
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001384 def find_class(self, module, name):
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001385 # Subclasses may override this.
1386 if self.proto < 3 and self.fix_imports:
1387 if (module, name) in _compat_pickle.NAME_MAPPING:
1388 module, name = _compat_pickle.NAME_MAPPING[(module, name)]
Serhiy Storchakabfe18242015-03-31 13:12:37 +03001389 elif module in _compat_pickle.IMPORT_MAPPING:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001390 module = _compat_pickle.IMPORT_MAPPING[module]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001391 __import__(module, level=0)
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001392 if self.proto >= 4:
1393 return _getattribute(sys.modules[module], name)[0]
1394 else:
1395 return getattr(sys.modules[module], name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001396
1397 def load_reduce(self):
1398 stack = self.stack
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001399 args = stack.pop()
1400 func = stack[-1]
Guido van Rossum99603b02007-07-20 00:22:32 +00001401 try:
1402 value = func(*args)
1403 except:
1404 print(sys.exc_info())
1405 print(func, args)
1406 raise
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001407 stack[-1] = value
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001408 dispatch[REDUCE[0]] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001409
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001410 def load_pop(self):
1411 del self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001412 dispatch[POP[0]] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001413
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001414 def load_pop_mark(self):
1415 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001416 del self.stack[k:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001417 dispatch[POP_MARK[0]] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001418
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001419 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001420 self.append(self.stack[-1])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001421 dispatch[DUP[0]] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001422
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001423 def load_get(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001424 i = int(self.readline()[:-1])
1425 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001426 dispatch[GET[0]] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001427
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001428 def load_binget(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001429 i = self.read(1)[0]
1430 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001431 dispatch[BINGET[0]] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001432
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001433 def load_long_binget(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001434 i, = unpack('<I', self.read(4))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001435 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001436 dispatch[LONG_BINGET[0]] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001437
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001438 def load_put(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001439 i = int(self.readline()[:-1])
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001440 if i < 0:
1441 raise ValueError("negative PUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001442 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001443 dispatch[PUT[0]] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001444
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001445 def load_binput(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001446 i = self.read(1)[0]
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001447 if i < 0:
1448 raise ValueError("negative BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001449 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001450 dispatch[BINPUT[0]] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001451
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001452 def load_long_binput(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001453 i, = unpack('<I', self.read(4))
1454 if i > maxsize:
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001455 raise ValueError("negative LONG_BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001456 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001457 dispatch[LONG_BINPUT[0]] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001458
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001459 def load_memoize(self):
1460 memo = self.memo
1461 memo[len(memo)] = self.stack[-1]
1462 dispatch[MEMOIZE[0]] = load_memoize
1463
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001464 def load_append(self):
1465 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001466 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001467 list = stack[-1]
1468 list.append(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001469 dispatch[APPEND[0]] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001470
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001471 def load_appends(self):
1472 stack = self.stack
1473 mark = self.marker()
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001474 list_obj = stack[mark - 1]
1475 items = stack[mark + 1:]
1476 if isinstance(list_obj, list):
1477 list_obj.extend(items)
1478 else:
1479 append = list_obj.append
1480 for item in items:
1481 append(item)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001482 del stack[mark:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001483 dispatch[APPENDS[0]] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001484
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001485 def load_setitem(self):
1486 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001487 value = stack.pop()
1488 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001489 dict = stack[-1]
1490 dict[key] = value
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001491 dispatch[SETITEM[0]] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001492
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001493 def load_setitems(self):
1494 stack = self.stack
1495 mark = self.marker()
1496 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001497 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001498 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001499
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001500 del stack[mark:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001501 dispatch[SETITEMS[0]] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001502
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001503 def load_additems(self):
1504 stack = self.stack
1505 mark = self.marker()
1506 set_obj = stack[mark - 1]
1507 items = stack[mark + 1:]
1508 if isinstance(set_obj, set):
1509 set_obj.update(items)
1510 else:
1511 add = set_obj.add
1512 for item in items:
1513 add(item)
1514 del stack[mark:]
1515 dispatch[ADDITEMS[0]] = load_additems
1516
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001517 def load_build(self):
1518 stack = self.stack
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001519 state = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001520 inst = stack[-1]
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001521 setstate = getattr(inst, "__setstate__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001522 if setstate is not None:
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001523 setstate(state)
1524 return
1525 slotstate = None
1526 if isinstance(state, tuple) and len(state) == 2:
1527 state, slotstate = state
1528 if state:
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001529 inst_dict = inst.__dict__
Antoine Pitroua9f48a02009-05-02 21:41:14 +00001530 intern = sys.intern
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001531 for k, v in state.items():
1532 if type(k) is str:
1533 inst_dict[intern(k)] = v
1534 else:
1535 inst_dict[k] = v
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001536 if slotstate:
1537 for k, v in slotstate.items():
1538 setattr(inst, k, v)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001539 dispatch[BUILD[0]] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001540
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001541 def load_mark(self):
1542 self.append(self.mark)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001543 dispatch[MARK[0]] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001544
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001545 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001546 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001547 raise _Stop(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001548 dispatch[STOP[0]] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001549
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001550
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001551# Shorthands
1552
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001553def _dump(obj, file, protocol=None, *, fix_imports=True):
1554 _Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001555
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001556def _dumps(obj, protocol=None, *, fix_imports=True):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001557 f = io.BytesIO()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001558 _Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001559 res = f.getvalue()
Guido van Rossum98297ee2007-11-06 21:34:58 +00001560 assert isinstance(res, bytes_types)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001561 return res
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001562
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001563def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"):
1564 return _Unpickler(file, fix_imports=fix_imports,
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001565 encoding=encoding, errors=errors).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001566
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001567def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001568 if isinstance(s, str):
1569 raise TypeError("Can't load pickle from unicode string")
1570 file = io.BytesIO(s)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 return _Unpickler(file, fix_imports=fix_imports,
1572 encoding=encoding, errors=errors).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001574# Use the faster _pickle if possible
1575try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001576 from _pickle import (
1577 PickleError,
1578 PicklingError,
1579 UnpicklingError,
1580 Pickler,
1581 Unpickler,
1582 dump,
1583 dumps,
1584 load,
1585 loads
1586 )
Brett Cannoncd171c82013-07-04 17:43:24 -04001587except ImportError:
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001588 Pickler, Unpickler = _Pickler, _Unpickler
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001589 dump, dumps, load, loads = _dump, _dumps, _load, _loads
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001590
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001591# Doctest
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001592def _test():
1593 import doctest
1594 return doctest.testmod()
1595
1596if __name__ == "__main__":
Florent Xicluna54540ec2011-11-04 08:29:17 +01001597 import argparse
Alexander Belopolsky455f7bd2010-07-27 23:02:38 +00001598 parser = argparse.ArgumentParser(
1599 description='display contents of the pickle files')
1600 parser.add_argument(
1601 'pickle_file', type=argparse.FileType('br'),
1602 nargs='*', help='the pickle file')
1603 parser.add_argument(
1604 '-t', '--test', action='store_true',
1605 help='run self-test suite')
1606 parser.add_argument(
1607 '-v', action='store_true',
1608 help='run verbosely; only affects self-test run')
1609 args = parser.parse_args()
1610 if args.test:
1611 _test()
1612 else:
1613 if not args.pickle_file:
1614 parser.print_help()
1615 else:
1616 import pprint
1617 for f in args.pickle_file:
1618 obj = load(f)
1619 pprint.pprint(obj)