blob: d1f1538b8bb17057ee8645705b0aa75272ab8a90 [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
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010026from types import FunctionType, BuiltinFunctionType, ModuleType
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
Guido van Rossumd3703791998-10-22 20:15:36 +000030import sys
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +030031from sys import maxsize
32from struct import pack, unpack
Skip Montanaro23bafc62001-02-18 03:10:09 +000033import re
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000034import io
Walter Dörwald42748a82007-06-12 16:40:17 +000035import codecs
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000036import _compat_pickle
Guido van Rossuma48061a1995-01-10 00:31:14 +000037
Skip Montanaro352674d2001-02-07 23:14:30 +000038__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
39 "Unpickler", "dump", "dumps", "load", "loads"]
40
Guido van Rossum98297ee2007-11-06 21:34:58 +000041# Shortcut for use in isinstance testing
Alexandre Vassalotti8cb02b62008-05-03 01:42:49 +000042bytes_types = (bytes, bytearray)
Guido van Rossum98297ee2007-11-06 21:34:58 +000043
Tim Petersc0c12b52003-01-29 00:56:17 +000044# These are purely informational; no code uses these.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010045format_version = "4.0" # File format version we write
Guido van Rossumf29d3d62003-01-27 22:47:53 +000046compatible_formats = ["1.0", # Original protocol 0
Guido van Rossumbc64e222003-01-28 16:34:19 +000047 "1.1", # Protocol 0 with INST added
Guido van Rossumf29d3d62003-01-27 22:47:53 +000048 "1.2", # Original protocol 1
49 "1.3", # Protocol 1 with BINFLOAT added
50 "2.0", # Protocol 2
Guido van Rossumf4169812008-03-17 22:56:06 +000051 "3.0", # Protocol 3
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010052 "4.0", # Protocol 4
Guido van Rossumf29d3d62003-01-27 22:47:53 +000053 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000054
Guido van Rossum99603b02007-07-20 00:22:32 +000055# This is the highest protocol number we know how to read.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010056HIGHEST_PROTOCOL = 4
Tim Peters8587b3c2003-02-13 15:44:41 +000057
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000058# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
Guido van Rossumf4169812008-03-17 22:56:06 +000059# We intentionally write a protocol that Python 2.x cannot read;
60# there are too many issues with that.
61DEFAULT_PROTOCOL = 3
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000062
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000063class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000064 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000065 pass
66
67class PicklingError(PickleError):
68 """This exception is raised when an unpicklable object is passed to the
69 dump() method.
70
71 """
72 pass
73
74class UnpicklingError(PickleError):
75 """This exception is raised when there is a problem unpickling an object,
76 such as a security violation.
77
78 Note that other exceptions may also be raised during unpickling, including
79 (but not necessarily limited to) AttributeError, EOFError, ImportError,
80 and IndexError.
81
82 """
83 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000084
Tim Petersc0c12b52003-01-29 00:56:17 +000085# An instance of _Stop is raised by Unpickler.load_stop() in response to
86# the STOP opcode, passing the object that is the result of unpickling.
Guido van Rossumff871742000-12-13 18:11:56 +000087class _Stop(Exception):
88 def __init__(self, value):
89 self.value = value
90
Guido van Rossum533dbcf2003-01-28 17:55:05 +000091# Jython has PyStringMap; it's a dict subclass with string keys
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000092try:
93 from org.python.core import PyStringMap
Brett Cannoncd171c82013-07-04 17:43:24 -040094except ImportError:
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000095 PyStringMap = None
96
Tim Peters22a449a2003-01-27 20:16:36 +000097# Pickle opcodes. See pickletools.py for extensive docs. The listing
98# here is in kind-of alphabetical order of 1-character pickle code.
99# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000100
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000101MARK = b'(' # push special markobject on stack
102STOP = b'.' # every pickle ends with STOP
103POP = b'0' # discard topmost stack item
104POP_MARK = b'1' # discard stack top through topmost markobject
105DUP = b'2' # duplicate top stack item
106FLOAT = b'F' # push float object; decimal string argument
107INT = b'I' # push integer or bool; decimal string argument
108BININT = b'J' # push four-byte signed int
109BININT1 = b'K' # push 1-byte unsigned int
110LONG = b'L' # push long; decimal string argument
111BININT2 = b'M' # push 2-byte unsigned int
112NONE = b'N' # push None
113PERSID = b'P' # push persistent object; id is taken from string arg
114BINPERSID = b'Q' # " " " ; " " " " stack
115REDUCE = b'R' # apply callable to argtuple, both on stack
116STRING = b'S' # push string; NL-terminated string argument
117BINSTRING = b'T' # push string; counted binary string argument
118SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes
119UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument
120BINUNICODE = b'X' # " " " ; counted UTF-8 string argument
121APPEND = b'a' # append stack top to list below it
122BUILD = b'b' # call __setstate__ or __dict__.update()
123GLOBAL = b'c' # push self.find_class(modname, name); 2 string args
124DICT = b'd' # build a dict from stack items
125EMPTY_DICT = b'}' # push empty dict
126APPENDS = b'e' # extend list on stack by topmost stack slice
127GET = b'g' # push item from memo on stack; index is string arg
128BINGET = b'h' # " " " " " " ; " " 1-byte arg
129INST = b'i' # build & push class instance
130LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg
131LIST = b'l' # build list from topmost stack items
132EMPTY_LIST = b']' # push empty list
133OBJ = b'o' # build & push class instance
134PUT = b'p' # store stack top in memo; index is string arg
135BINPUT = b'q' # " " " " " ; " " 1-byte arg
136LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg
137SETITEM = b's' # add key+value pair to dict
138TUPLE = b't' # build tuple from topmost stack items
139EMPTY_TUPLE = b')' # push empty tuple
140SETITEMS = b'u' # modify dict by adding topmost key+value pairs
141BINFLOAT = b'G' # push float; arg is 8-byte float encoding
Tim Peters22a449a2003-01-27 20:16:36 +0000142
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000143TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py
144FALSE = b'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000145
Guido van Rossum586c9e82003-01-29 06:16:12 +0000146# Protocol 2
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000147
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000148PROTO = b'\x80' # identify pickle protocol
149NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple
150EXT1 = b'\x82' # push object from extension registry; 1-byte index
151EXT2 = b'\x83' # ditto, but 2-byte index
152EXT4 = b'\x84' # ditto, but 4-byte index
153TUPLE1 = b'\x85' # build 1-tuple from stack top
154TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items
155TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items
156NEWTRUE = b'\x88' # push True
157NEWFALSE = b'\x89' # push False
158LONG1 = b'\x8a' # push long from < 256 bytes
159LONG4 = b'\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000160
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000161_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
162
Guido van Rossumf4169812008-03-17 22:56:06 +0000163# Protocol 3 (Python 3.x)
164
165BINBYTES = b'B' # push bytes; counted binary string argument
166SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes
Guido van Rossuma48061a1995-01-10 00:31:14 +0000167
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100168# Protocol 4
169SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes
170BINUNICODE8 = b'\x8d' # push very long string
171BINBYTES8 = b'\x8e' # push very long bytes string
172EMPTY_SET = b'\x8f' # push empty set on the stack
173ADDITEMS = b'\x90' # modify set by adding topmost stack items
174FROZENSET = b'\x91' # build frozenset from topmost stack items
175NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments
176STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks
177MEMOIZE = b'\x94' # store top of the stack in memo
178FRAME = b'\x95' # indicate the beginning of a new frame
179
180__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)])
181
182
183class _Framer:
184
185 _FRAME_SIZE_TARGET = 64 * 1024
186
187 def __init__(self, file_write):
188 self.file_write = file_write
189 self.current_frame = None
190
191 def _commit_frame(self):
192 f = self.current_frame
193 with f.getbuffer() as data:
194 n = len(data)
195 write = self.file_write
196 write(FRAME)
197 write(pack("<Q", n))
198 write(data)
199 f.seek(0)
200 f.truncate()
201
202 def start_framing(self):
203 self.current_frame = io.BytesIO()
204
205 def end_framing(self):
206 if self.current_frame is not None:
207 self._commit_frame()
208 self.current_frame = None
209
210 def write(self, data):
211 f = self.current_frame
212 if f is None:
213 return self.file_write(data)
214 else:
215 n = len(data)
216 if f.tell() >= self._FRAME_SIZE_TARGET:
217 self._commit_frame()
218 return f.write(data)
219
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
225 self.file_tell = file_tell
226 self.framing_enabled = False
227 self.current_frame = None
228 self.frame_start = None
229
230 def read(self, n):
231 if n == 0:
232 return b''
233 _file_read = self.file_read
234 if not self.framing_enabled:
235 return _file_read(n)
236 f = self.current_frame
237 if f is not None:
238 data = f.read(n)
239 if data:
240 if len(data) < n:
241 raise UnpicklingError(
242 "pickle exhausted before end of frame")
243 return data
244 frame_opcode = _file_read(1)
245 if frame_opcode != FRAME:
246 raise UnpicklingError(
247 "expected a FRAME opcode, got {} instead".format(frame_opcode))
248 frame_size, = unpack("<Q", _file_read(8))
249 if frame_size > sys.maxsize:
250 raise ValueError("frame size > sys.maxsize: %d" % frame_size)
251 if self.file_tell is not None:
252 self.frame_start = self.file_tell()
253 f = self.current_frame = io.BytesIO(_file_read(frame_size))
254 self.readline = f.readline
255 data = f.read(n)
256 assert len(data) == n, (len(data), n)
257 return data
258
259 def readline(self):
260 if not self.framing_enabled:
261 return self.file_readline()
262 else:
263 return self.current_frame.readline()
264
265 def tell(self):
266 if self.file_tell is None:
267 return None
268 elif self.current_frame is None:
269 return self.file_tell()
270 else:
271 return self.frame_start + self.current_frame.tell()
272
273
274# Tools used for pickling.
275
276def _getattribute(obj, name, allow_qualname=False):
277 dotted_path = name.split(".")
278 if not allow_qualname and len(dotted_path) > 1:
279 raise AttributeError("Can't get qualified attribute {!r} on {!r}; " +
280 "use protocols >= 4 to enable support"
281 .format(name, obj))
282 for subpath in dotted_path:
283 if subpath == '<locals>':
284 raise AttributeError("Can't get local attribute {!r} on {!r}"
285 .format(name, obj))
286 try:
287 obj = getattr(obj, subpath)
288 except AttributeError:
289 raise AttributeError("Can't get attribute {!r} on {!r}"
290 .format(name, obj))
291 return obj
292
293def whichmodule(obj, name, allow_qualname=False):
294 """Find the module an object belong to."""
295 module_name = getattr(obj, '__module__', None)
296 if module_name is not None:
297 return module_name
298 for module_name, module in sys.modules.items():
299 if module_name == '__main__' or module is None:
300 continue
301 try:
302 if _getattribute(module, name, allow_qualname) is obj:
303 return module_name
304 except AttributeError:
305 pass
306 return '__main__'
307
308def encode_long(x):
309 r"""Encode a long to a two's complement little-endian binary string.
310 Note that 0 is a special case, returning an empty string, to save a
311 byte in the LONG1 pickling context.
312
313 >>> encode_long(0)
314 b''
315 >>> encode_long(255)
316 b'\xff\x00'
317 >>> encode_long(32767)
318 b'\xff\x7f'
319 >>> encode_long(-256)
320 b'\x00\xff'
321 >>> encode_long(-32768)
322 b'\x00\x80'
323 >>> encode_long(-128)
324 b'\x80'
325 >>> encode_long(127)
326 b'\x7f'
327 >>>
328 """
329 if x == 0:
330 return b''
331 nbytes = (x.bit_length() >> 3) + 1
332 result = x.to_bytes(nbytes, byteorder='little', signed=True)
333 if x < 0 and nbytes > 1:
334 if result[-1] == 0xff and (result[-2] & 0x80) != 0:
335 result = result[:-1]
336 return result
337
338def decode_long(data):
339 r"""Decode a long from a two's complement little-endian binary string.
340
341 >>> decode_long(b'')
342 0
343 >>> decode_long(b"\xff\x00")
344 255
345 >>> decode_long(b"\xff\x7f")
346 32767
347 >>> decode_long(b"\x00\xff")
348 -256
349 >>> decode_long(b"\x00\x80")
350 -32768
351 >>> decode_long(b"\x80")
352 -128
353 >>> decode_long(b"\x7f")
354 127
355 """
356 return int.from_bytes(data, byteorder='little', signed=True)
357
Skip Montanaro23bafc62001-02-18 03:10:09 +0000358
Guido van Rossum1be31752003-01-28 15:19:53 +0000359# Pickling machinery
360
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000361class _Pickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +0000362
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000363 def __init__(self, file, protocol=None, *, fix_imports=True):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000364 """This takes a binary file for writing a pickle data stream.
365
Guido van Rossumcf117b02003-02-09 17:19:41 +0000366 The optional protocol argument tells the pickler to use the
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100367 given protocol; supported protocols are 0, 1, 2, 3 and 4. The
368 default protocol is 3; a backward-incompatible protocol designed for
369 Python 3.
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000370
Guido van Rossum7eff63a2003-01-31 19:42:31 +0000371 Specifying a negative protocol version selects the highest
Tim Peters5bd2a792003-02-01 16:45:06 +0000372 protocol version supported. The higher the protocol used, the
373 more recent the version of Python needed to read the pickle
374 produced.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000375
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 The file argument must have a write() method that accepts a single
377 bytes argument. It can thus be a file object opened for binary
378 writing, a io.BytesIO instance, or any other custom object that
379 meets this interface.
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000380
381 If fix_imports is True and protocol is less than 3, pickle will try to
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100382 map the new Python 3 names to the old module names used in Python 2,
383 so that the pickle data stream is readable with Python 2.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000384 """
Guido van Rossumcf117b02003-02-09 17:19:41 +0000385 if protocol is None:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000386 protocol = DEFAULT_PROTOCOL
Guido van Rossumcf117b02003-02-09 17:19:41 +0000387 if protocol < 0:
Tim Peters8587b3c2003-02-13 15:44:41 +0000388 protocol = HIGHEST_PROTOCOL
389 elif not 0 <= protocol <= HIGHEST_PROTOCOL:
390 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000391 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100392 self._file_write = file.write
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000393 except AttributeError:
394 raise TypeError("file must have a 'write' attribute")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000395 self.memo = {}
Guido van Rossumcf117b02003-02-09 17:19:41 +0000396 self.proto = int(protocol)
397 self.bin = protocol >= 1
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000398 self.fast = 0
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000399 self.fix_imports = fix_imports and protocol < 3
Guido van Rossuma48061a1995-01-10 00:31:14 +0000400
Fred Drake7f781c92002-05-01 20:33:53 +0000401 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000402 """Clears the pickler's "memo".
403
404 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000405 pickler has already seen, so that shared or recursive objects are
406 pickled by reference and not by value. This method is useful when
407 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000408
409 """
Fred Drake7f781c92002-05-01 20:33:53 +0000410 self.memo.clear()
411
Guido van Rossum3a41c612003-01-28 15:10:22 +0000412 def dump(self, obj):
Tim Peters5bd2a792003-02-01 16:45:06 +0000413 """Write a pickled representation of obj to the open file."""
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000414 # Check whether Pickler was initialized correctly. This is
415 # only needed to mimic the behavior of _pickle.Pickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100416 if not hasattr(self, "_file_write"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +0000417 raise PicklingError("Pickler.__init__() was not called by "
418 "%s.__init__()" % (self.__class__.__name__,))
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000419 if self.proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100420 self._file_write(PROTO + pack("<B", self.proto))
421 if self.proto >= 4:
422 framer = _Framer(self._file_write)
423 framer.start_framing()
424 self.write = framer.write
425 else:
426 framer = None
427 self.write = self._file_write
Guido van Rossum3a41c612003-01-28 15:10:22 +0000428 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000429 self.write(STOP)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100430 if framer is not None:
431 framer.end_framing()
Guido van Rossuma48061a1995-01-10 00:31:14 +0000432
Jeremy Hylton3422c992003-01-24 19:29:52 +0000433 def memoize(self, obj):
434 """Store an object in the memo."""
435
Tim Peterse46b73f2003-01-27 21:22:10 +0000436 # The Pickler memo is a dictionary mapping object ids to 2-tuples
437 # that contain the Unpickler memo key and the object being memoized.
438 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000439 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000440 # Pickler memo so that transient objects are kept alive during
441 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000442
Tim Peterse46b73f2003-01-27 21:22:10 +0000443 # The use of the Unpickler memo length as the memo key is just a
444 # convention. The only requirement is that the memo values be unique.
445 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000446 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000447 # growable) array, indexed by memo key.
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000448 if self.fast:
449 return
Guido van Rossum9b40e802003-01-30 06:37:41 +0000450 assert id(obj) not in self.memo
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100451 idx = len(self.memo)
452 self.write(self.put(idx))
453 self.memo[id(obj)] = idx, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000454
Tim Petersbb38e302003-01-27 21:25:41 +0000455 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100456 def put(self, idx):
457 if self.proto >= 4:
458 return MEMOIZE
459 elif self.bin:
460 if idx < 256:
461 return BINPUT + pack("<B", idx)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000462 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100463 return LONG_BINPUT + pack("<I", idx)
464 else:
465 return PUT + repr(idx).encode("ascii") + b'\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000466
Tim Petersbb38e302003-01-27 21:25:41 +0000467 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300468 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000469 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000470 if i < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300471 return BINGET + pack("<B", i)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000472 else:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +0100473 return LONG_BINGET + pack("<I", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000474
Guido van Rossum39478e82007-08-27 17:23:59 +0000475 return GET + repr(i).encode("ascii") + b'\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000476
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477 def save(self, obj, save_persistent_id=True):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000478 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000479 pid = self.persistent_id(obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 if pid is not None and save_persistent_id:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000481 self.save_pers(pid)
482 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000483
Guido van Rossumbc64e222003-01-28 16:34:19 +0000484 # Check the memo
485 x = self.memo.get(id(obj))
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300486 if x is not None:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000487 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000488 return
489
Guido van Rossumbc64e222003-01-28 16:34:19 +0000490 # Check the type dispatch table
Guido van Rossum3a41c612003-01-28 15:10:22 +0000491 t = type(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000492 f = self.dispatch.get(t)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300493 if f is not None:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000494 f(self, obj) # Call unbound method with explicit self
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000495 return
496
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100497 # Check private dispatch table if any, or else copyreg.dispatch_table
498 reduce = getattr(self, 'dispatch_table', dispatch_table).get(t)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300499 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000500 rv = reduce(obj)
501 else:
Antoine Pitrouffd41d92011-10-04 09:23:04 +0200502 # Check for a class with a custom metaclass; treat as regular class
503 try:
504 issc = issubclass(t, type)
505 except TypeError: # t is not a class (old Boost; see SF #502085)
506 issc = False
507 if issc:
508 self.save_global(obj)
509 return
510
Guido van Rossumc53f0092003-02-18 22:05:12 +0000511 # Check for a __reduce_ex__ method, fall back to __reduce__
512 reduce = getattr(obj, "__reduce_ex__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300513 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000514 rv = reduce(self.proto)
515 else:
516 reduce = getattr(obj, "__reduce__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300517 if reduce is not None:
Guido van Rossumc53f0092003-02-18 22:05:12 +0000518 rv = reduce()
519 else:
520 raise PicklingError("Can't pickle %r object: %r" %
521 (t.__name__, obj))
Tim Petersb32a8312003-01-28 00:48:09 +0000522
Guido van Rossumbc64e222003-01-28 16:34:19 +0000523 # Check for string returned by reduce(), meaning "save as global"
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000524 if isinstance(rv, str):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000525 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000526 return
527
Guido van Rossumbc64e222003-01-28 16:34:19 +0000528 # Assert that reduce() returned a tuple
Guido van Rossum13257902007-06-07 23:15:56 +0000529 if not isinstance(rv, tuple):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000530 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000531
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000532 # Assert that it returned an appropriately sized tuple
Guido van Rossumbc64e222003-01-28 16:34:19 +0000533 l = len(rv)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000534 if not (2 <= l <= 5):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000535 raise PicklingError("Tuple returned by %s must have "
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000536 "two to five elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000537
Guido van Rossumbc64e222003-01-28 16:34:19 +0000538 # Save the reduce() output and finally memoize the object
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000539 self.save_reduce(obj=obj, *rv)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000540
Guido van Rossum3a41c612003-01-28 15:10:22 +0000541 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000542 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000543 return None
544
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000545 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000546 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000547 if self.bin:
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000548 self.save(pid, save_persistent_id=False)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000549 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000550 else:
Guido van Rossum39478e82007-08-27 17:23:59 +0000551 self.write(PERSID + str(pid).encode("ascii") + b'\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000552
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100553 def save_reduce(self, func, args, state=None, listitems=None,
554 dictitems=None, obj=None):
Jeremy Hyltone3a565e2003-06-29 16:59:59 +0000555 # This API is called by some subclasses
Guido van Rossumbc64e222003-01-28 16:34:19 +0000556
Guido van Rossum13257902007-06-07 23:15:56 +0000557 if not isinstance(args, tuple):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100558 raise PicklingError("args from save_reduce() must be a tuple")
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200559 if not callable(func):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100560 raise PicklingError("func from save_reduce() must be callable")
Guido van Rossumbc64e222003-01-28 16:34:19 +0000561
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000562 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000563 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000564
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100565 func_name = getattr(func, "__name__", "")
566 if self.proto >= 4 and func_name == "__newobj_ex__":
567 cls, args, kwargs = args
568 if not hasattr(cls, "__new__"):
569 raise PicklingError("args[0] from {} args has no __new__"
570 .format(func_name))
571 if obj is not None and cls is not obj.__class__:
572 raise PicklingError("args[0] from {} args has the wrong class"
573 .format(func_name))
574 save(cls)
575 save(args)
576 save(kwargs)
577 write(NEWOBJ_EX)
578 elif self.proto >= 2 and func_name == "__newobj__":
579 # A __reduce__ implementation can direct protocol 2 or newer to
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000580 # use the more efficient NEWOBJ opcode, while still
581 # allowing protocol 0 and 1 to work normally. For this to
582 # work, the function returned by __reduce__ should be
583 # called __newobj__, and its first argument should be a
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100584 # class. The implementation for __newobj__
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000585 # should be as follows, although pickle has no way to
586 # verify this:
587 #
588 # def __newobj__(cls, *args):
589 # return cls.__new__(cls, *args)
590 #
591 # Protocols 0 and 1 will pickle a reference to __newobj__,
592 # while protocol 2 (and above) will pickle a reference to
593 # cls, the remaining args tuple, and the NEWOBJ code,
594 # which calls cls.__new__(cls, *args) at unpickling time
595 # (see load_newobj below). If __reduce__ returns a
596 # three-tuple, the state from the third tuple item will be
597 # pickled regardless of the protocol, calling __setstate__
598 # at unpickling time (see load_build below).
599 #
600 # Note that no standard __newobj__ implementation exists;
601 # you have to provide your own. This is to enforce
602 # compatibility with Python 2.2 (pickles written using
603 # protocol 0 or 1 in Python 2.3 should be unpicklable by
604 # Python 2.2).
605 cls = args[0]
606 if not hasattr(cls, "__new__"):
607 raise PicklingError(
608 "args[0] from __newobj__ args has no __new__")
Guido van Rossumf7f45172003-01-31 17:17:49 +0000609 if obj is not None and cls is not obj.__class__:
610 raise PicklingError(
611 "args[0] from __newobj__ args has the wrong class")
Guido van Rossumd053b4b2003-01-31 16:51:45 +0000612 args = args[1:]
613 save(cls)
614 save(args)
615 write(NEWOBJ)
616 else:
617 save(func)
618 save(args)
619 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000620
Guido van Rossumf7f45172003-01-31 17:17:49 +0000621 if obj is not None:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100622 # If the object is already in the memo, this means it is
623 # recursive. In this case, throw away everything we put on the
624 # stack, and fetch the object back from the memo.
625 if id(obj) in self.memo:
626 write(POP + self.get(self.memo[id(obj)][0]))
627 else:
628 self.memoize(obj)
Guido van Rossumf7f45172003-01-31 17:17:49 +0000629
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000630 # More new special cases (that work with older protocols as
631 # well): when __reduce__ returns a tuple with 4 or 5 items,
632 # the 4th and 5th item should be iterators that provide list
633 # items and dict items (as (key, value) tuples), or None.
634
635 if listitems is not None:
636 self._batch_appends(listitems)
637
638 if dictitems is not None:
639 self._batch_setitems(dictitems)
640
Tim Petersc32d8242001-04-10 02:48:53 +0000641 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000642 save(state)
643 write(BUILD)
644
Guido van Rossumbc64e222003-01-28 16:34:19 +0000645 # Methods below this point are dispatched through the dispatch table
646
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000647 dispatch = {}
648
Guido van Rossum3a41c612003-01-28 15:10:22 +0000649 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000650 self.write(NONE)
Guido van Rossum13257902007-06-07 23:15:56 +0000651 dispatch[type(None)] = save_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000652
Łukasz Langaf3078fb2012-03-12 19:46:12 +0100653 def save_ellipsis(self, obj):
654 self.save_global(Ellipsis, 'Ellipsis')
655 dispatch[type(Ellipsis)] = save_ellipsis
656
657 def save_notimplemented(self, obj):
658 self.save_global(NotImplemented, 'NotImplemented')
659 dispatch[type(NotImplemented)] = save_notimplemented
660
Guido van Rossum3a41c612003-01-28 15:10:22 +0000661 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000662 if self.proto >= 2:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300663 self.write(NEWTRUE if obj else NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000664 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300665 self.write(TRUE if obj else FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000666 dispatch[bool] = save_bool
667
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300668 def save_long(self, obj):
Guido van Rossumddefaf32007-01-14 03:31:43 +0000669 if self.bin:
670 # If the int is small enough to fit in a signed 4-byte 2's-comp
671 # format, we can store it more efficiently than the general
672 # case.
673 # First one- and two-byte unsigned ints:
674 if obj >= 0:
675 if obj <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300676 self.write(BININT1 + pack("<B", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000677 return
678 if obj <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300679 self.write(BININT2 + pack("<H", obj))
Guido van Rossumddefaf32007-01-14 03:31:43 +0000680 return
681 # Next check for 4-byte signed ints:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300682 if -0x80000000 <= obj <= 0x7fffffff:
Guido van Rossumddefaf32007-01-14 03:31:43 +0000683 self.write(BININT + pack("<i", obj))
684 return
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000685 if self.proto >= 2:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000686 encoded = encode_long(obj)
687 n = len(encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000688 if n < 256:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300689 self.write(LONG1 + pack("<B", n) + encoded)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000690 else:
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000691 self.write(LONG4 + pack("<i", n) + encoded)
Tim Petersee1a53c2003-02-02 02:57:53 +0000692 return
Mark Dickinson8dd05142009-01-20 20:43:58 +0000693 self.write(LONG + repr(obj).encode("ascii") + b'L\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000694 dispatch[int] = save_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000695
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300696 def save_float(self, obj):
Guido van Rossumd3703791998-10-22 20:15:36 +0000697 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000698 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000699 else:
Guido van Rossum39478e82007-08-27 17:23:59 +0000700 self.write(FLOAT + repr(obj).encode("ascii") + b'\n')
Guido van Rossum13257902007-06-07 23:15:56 +0000701 dispatch[float] = save_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000702
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300703 def save_bytes(self, obj):
Guido van Rossumf4169812008-03-17 22:56:06 +0000704 if self.proto < 3:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300705 if not obj: # bytes object is empty
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500706 self.save_reduce(bytes, (), obj=obj)
707 else:
708 self.save_reduce(codecs.encode,
709 (str(obj, 'latin1'), 'latin1'), obj=obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000710 return
711 n = len(obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100712 if n <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300713 self.write(SHORT_BINBYTES + pack("<B", n) + obj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100714 elif n > 0xffffffff and self.proto >= 4:
715 self.write(BINBYTES8 + pack("<Q", n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000716 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300717 self.write(BINBYTES + pack("<I", n) + obj)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000718 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000719 dispatch[bytes] = save_bytes
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000720
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300721 def save_str(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000722 if self.bin:
Victor Stinner485fb562010-04-13 11:07:24 +0000723 encoded = obj.encode('utf-8', 'surrogatepass')
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000724 n = len(encoded)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100725 if n <= 0xff and self.proto >= 4:
726 self.write(SHORT_BINUNICODE + pack("<B", n) + encoded)
727 elif n > 0xffffffff and self.proto >= 4:
728 self.write(BINUNICODE8 + pack("<Q", n) + encoded)
729 else:
730 self.write(BINUNICODE + pack("<I", n) + encoded)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000731 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000732 obj = obj.replace("\\", "\\u005c")
733 obj = obj.replace("\n", "\\u000a")
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100734 self.write(UNICODE + obj.encode('raw-unicode-escape') +
735 b'\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000736 self.memoize(obj)
Guido van Rossumf4169812008-03-17 22:56:06 +0000737 dispatch[str] = save_str
Tim Peters658cba62001-02-09 20:06:00 +0000738
Guido van Rossum3a41c612003-01-28 15:10:22 +0000739 def save_tuple(self, obj):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300740 if not obj: # tuple is empty
741 if self.bin:
742 self.write(EMPTY_TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000743 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300744 self.write(MARK + TUPLE)
Tim Petersd97da802003-01-28 05:48:29 +0000745 return
746
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300747 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000748 save = self.save
749 memo = self.memo
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300750 if n <= 3 and self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000751 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000752 save(element)
753 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000754 if id(obj) in memo:
755 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300756 self.write(POP * n + get)
Tim Petersd97da802003-01-28 05:48:29 +0000757 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300758 self.write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000759 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000760 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000761
Tim Peters1d63c9f2003-02-02 20:29:39 +0000762 # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple
Tim Petersff57bff2003-01-28 05:34:53 +0000763 # has more than 3 elements.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300764 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000766 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000767 save(element)
768
Tim Peters1d63c9f2003-02-02 20:29:39 +0000769 if id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000770 # Subtle. d was not in memo when we entered save_tuple(), so
771 # the process of saving the tuple's elements must have saved
772 # the tuple itself: the tuple is recursive. The proper action
773 # now is to throw away everything we put on the stack, and
774 # simply GET the tuple (it's already constructed). This check
775 # could have been done in the "for element" loop instead, but
776 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000777 get = self.get(memo[id(obj)][0])
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300778 if self.bin:
Tim Petersf558da02003-01-28 02:09:55 +0000779 write(POP_MARK + get)
780 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000781 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 return
783
Tim Peters1d63c9f2003-02-02 20:29:39 +0000784 # No recursion.
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300785 write(TUPLE)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000786 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000787
Guido van Rossum13257902007-06-07 23:15:56 +0000788 dispatch[tuple] = save_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000789
Guido van Rossum3a41c612003-01-28 15:10:22 +0000790 def save_list(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000791 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300792 self.write(EMPTY_LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000793 else: # proto 0 -- can't use EMPTY_LIST
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300794 self.write(MARK + LIST)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000795
796 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000797 self._batch_appends(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000798
Guido van Rossum13257902007-06-07 23:15:56 +0000799 dispatch[list] = save_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000800
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000801 _BATCHSIZE = 1000
802
803 def _batch_appends(self, items):
804 # Helper to batch up APPENDS sequences
805 save = self.save
806 write = self.write
807
808 if not self.bin:
809 for x in items:
810 save(x)
811 write(APPEND)
812 return
813
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300814 it = iter(items)
815 while True:
816 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000817 n = len(tmp)
818 if n > 1:
819 write(MARK)
820 for x in tmp:
821 save(x)
822 write(APPENDS)
823 elif n:
824 save(tmp[0])
825 write(APPEND)
826 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300827 if n < self._BATCHSIZE:
828 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000829
Guido van Rossum3a41c612003-01-28 15:10:22 +0000830 def save_dict(self, obj):
Tim Petersc32d8242001-04-10 02:48:53 +0000831 if self.bin:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300832 self.write(EMPTY_DICT)
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000833 else: # proto 0 -- can't use EMPTY_DICT
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300834 self.write(MARK + DICT)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000835
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000836 self.memoize(obj)
Alexandre Vassalottic7db1d62008-05-14 21:57:18 +0000837 self._batch_setitems(obj.items())
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000838
Guido van Rossum13257902007-06-07 23:15:56 +0000839 dispatch[dict] = save_dict
840 if PyStringMap is not None:
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000841 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000842
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000843 def _batch_setitems(self, items):
844 # Helper to batch up SETITEMS sequences; proto >= 1 only
845 save = self.save
846 write = self.write
847
848 if not self.bin:
849 for k, v in items:
850 save(k)
851 save(v)
852 write(SETITEM)
853 return
854
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300855 it = iter(items)
856 while True:
857 tmp = list(islice(it, self._BATCHSIZE))
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000858 n = len(tmp)
859 if n > 1:
860 write(MARK)
861 for k, v in tmp:
862 save(k)
863 save(v)
864 write(SETITEMS)
865 elif n:
866 k, v = tmp[0]
867 save(k)
868 save(v)
869 write(SETITEM)
870 # else tmp is empty, and we're done
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300871 if n < self._BATCHSIZE:
872 return
Guido van Rossum25cb7df2003-01-31 18:53:21 +0000873
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100874 def save_set(self, obj):
875 save = self.save
876 write = self.write
877
878 if self.proto < 4:
879 self.save_reduce(set, (list(obj),), obj=obj)
880 return
881
882 write(EMPTY_SET)
883 self.memoize(obj)
884
885 it = iter(obj)
886 while True:
887 batch = list(islice(it, self._BATCHSIZE))
888 n = len(batch)
889 if n > 0:
890 write(MARK)
891 for item in batch:
892 save(item)
893 write(ADDITEMS)
894 if n < self._BATCHSIZE:
895 return
896 dispatch[set] = save_set
897
898 def save_frozenset(self, obj):
899 save = self.save
900 write = self.write
901
902 if self.proto < 4:
903 self.save_reduce(frozenset, (list(obj),), obj=obj)
904 return
905
906 write(MARK)
907 for item in obj:
908 save(item)
909
910 if id(obj) in self.memo:
911 # If the object is already in the memo, this means it is
912 # recursive. In this case, throw away everything we put on the
913 # stack, and fetch the object back from the memo.
914 write(POP_MARK + self.get(self.memo[id(obj)][0]))
915 return
916
917 write(FROZENSET)
918 self.memoize(obj)
919 dispatch[frozenset] = save_frozenset
920
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300921 def save_global(self, obj, name=None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000922 write = self.write
923 memo = self.memo
924
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100925 if name is None and self.proto >= 4:
926 name = getattr(obj, '__qualname__', None)
Tim Petersc32d8242001-04-10 02:48:53 +0000927 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000928 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000929
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100930 module_name = whichmodule(obj, name, allow_qualname=self.proto >= 4)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000931 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100932 __import__(module_name, level=0)
933 module = sys.modules[module_name]
934 obj2 = _getattribute(module, name, allow_qualname=self.proto >= 4)
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000935 except (ImportError, KeyError, AttributeError):
936 raise PicklingError(
937 "Can't pickle %r: it's not found as %s.%s" %
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938 (obj, module_name, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000939 else:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100940 if obj2 is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000941 raise PicklingError(
942 "Can't pickle %r: it's not the same object as %s.%s" %
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100943 (obj, module_name, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000944
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000945 if self.proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 code = _extension_registry.get((module_name, name))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000947 if code:
948 assert code > 0
949 if code <= 0xff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300950 write(EXT1 + pack("<B", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000951 elif code <= 0xffff:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +0300952 write(EXT2 + pack("<H", code))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000953 else:
954 write(EXT4 + pack("<i", code))
955 return
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000956 # Non-ASCII identifiers are supported only with protocols >= 3.
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100957 if self.proto >= 4:
958 self.save(module_name)
959 self.save(name)
960 write(STACK_GLOBAL)
961 elif self.proto >= 3:
962 write(GLOBAL + bytes(module_name, "utf-8") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000963 bytes(name, "utf-8") + b'\n')
964 else:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000965 if self.fix_imports:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100966 r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING
967 r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING
968 if (module_name, name) in r_name_mapping:
969 module_name, name = r_name_mapping[(module_name, name)]
970 if module_name in r_import_mapping:
971 module_name = r_import_mapping[module_name]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000972 try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100973 write(GLOBAL + bytes(module_name, "ascii") + b'\n' +
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000974 bytes(name, "ascii") + b'\n')
975 except UnicodeEncodeError:
976 raise PicklingError(
977 "can't pickle global identifier '%s.%s' using "
978 "pickle protocol %i" % (module, name, self.proto))
Guido van Rossum255f3ee2003-01-29 06:14:11 +0000979
Guido van Rossum3a41c612003-01-28 15:10:22 +0000980 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +0000981
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 def save_method(self, obj):
983 if obj.__self__ is None or type(obj.__self__) is ModuleType:
984 self.save_global(obj)
985 else:
986 self.save_reduce(getattr, (obj.__self__, obj.__name__), obj=obj)
987
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000988 dispatch[FunctionType] = save_global
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100989 dispatch[BuiltinFunctionType] = save_method
Guido van Rossum13257902007-06-07 23:15:56 +0000990 dispatch[type] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000991
Guido van Rossuma48061a1995-01-10 00:31:14 +0000992
Guido van Rossum1be31752003-01-28 15:19:53 +0000993# Unpickling machinery
994
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000995class _Unpickler:
Guido van Rossuma48061a1995-01-10 00:31:14 +0000996
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000997 def __init__(self, file, *, fix_imports=True,
998 encoding="ASCII", errors="strict"):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +0000999 """This takes a binary file for reading a pickle data stream.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001000
Tim Peters5bd2a792003-02-01 16:45:06 +00001001 The protocol version of the pickle is detected automatically, so no
1002 proto argument is needed.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001003
Guido van Rossumfeea0782007-10-10 18:00:50 +00001004 The file-like object must have two methods, a read() method
1005 that takes an integer argument, and a readline() method that
1006 requires no arguments. Both methods should return bytes.
1007 Thus file-like object can be a binary file object opened for
1008 reading, a BytesIO object, or any other custom object that
1009 meets this interface.
Guido van Rossumf4169812008-03-17 22:56:06 +00001010
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001011 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
1012 which are used to control compatiblity support for pickle stream
1013 generated by Python 2.x. If *fix_imports* is True, pickle will try to
1014 map the old Python 2.x names to the new names used in Python 3.x. The
1015 *encoding* and *errors* tell pickle how to decode 8-bit string
1016 instances pickled by Python 2.x; these default to 'ASCII' and
1017 'strict', respectively.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001018 """
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001019 self._file_readline = file.readline
1020 self._file_read = file.read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001021 self.memo = {}
Guido van Rossumf4169812008-03-17 22:56:06 +00001022 self.encoding = encoding
1023 self.errors = errors
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001024 self.proto = 0
1025 self.fix_imports = fix_imports
Guido van Rossuma48061a1995-01-10 00:31:14 +00001026
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001027 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +00001028 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001029
Guido van Rossum3a41c612003-01-28 15:10:22 +00001030 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +00001031 """
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001032 # Check whether Unpickler was initialized correctly. This is
1033 # only needed to mimic the behavior of _pickle.Unpickler.dump().
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001034 if not hasattr(self, "_file_read"):
Alexandre Vassalotti3cfcab92008-12-27 09:30:39 +00001035 raise UnpicklingError("Unpickler.__init__() was not called by "
1036 "%s.__init__()" % (self.__class__.__name__,))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001037 self._unframer = _Unframer(self._file_read, self._file_readline)
1038 self.read = self._unframer.read
1039 self.readline = self._unframer.readline
Jeremy Hylton20747fa2001-11-09 16:15:04 +00001040 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001041 self.stack = []
1042 self.append = self.stack.append
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001043 self.proto = 0
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001044 read = self.read
1045 dispatch = self.dispatch
1046 try:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001047 while True:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001048 key = read(1)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001049 if not key:
1050 raise EOFError
Guido van Rossum98297ee2007-11-06 21:34:58 +00001051 assert isinstance(key, bytes_types)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001052 dispatch[key[0]](self)
Guido van Rossumb940e112007-01-10 16:19:56 +00001053 except _Stop as stopinst:
Guido van Rossumff871742000-12-13 18:11:56 +00001054 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001055
Tim Petersc23d18a2003-01-28 01:41:51 +00001056 # Return largest index k such that self.stack[k] is self.mark.
1057 # If the stack doesn't contain a mark, eventually raises IndexError.
1058 # This could be sped by maintaining another stack, of indices at which
1059 # the mark appears. For that matter, the latter stack would suffice,
1060 # and we wouldn't need to push mark objects on self.stack at all.
1061 # Doing so is probably a good thing, though, since if the pickle is
1062 # corrupt (or hostile) we may get a clue from finding self.mark embedded
1063 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001064 def marker(self):
1065 stack = self.stack
1066 mark = self.mark
1067 k = len(stack)-1
1068 while stack[k] is not mark: k = k-1
1069 return k
1070
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001071 def persistent_load(self, pid):
Benjamin Peterson49956b22009-01-10 17:05:44 +00001072 raise UnpicklingError("unsupported persistent id encountered")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001073
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001074 dispatch = {}
1075
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001076 def load_proto(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001077 proto = self.read(1)[0]
Guido van Rossumf4169812008-03-17 22:56:06 +00001078 if not 0 <= proto <= HIGHEST_PROTOCOL:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001079 raise ValueError("unsupported pickle protocol: %d" % proto)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001080 self.proto = proto
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001081 if proto >= 4:
1082 self._unframer.framing_enabled = True
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001083 dispatch[PROTO[0]] = load_proto
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001084
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001085 def load_persid(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001086 pid = self.readline()[:-1].decode("ascii")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001087 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001088 dispatch[PERSID[0]] = load_persid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001089
1090 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001091 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001092 self.append(self.persistent_load(pid))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001093 dispatch[BINPERSID[0]] = load_binpersid
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001094
1095 def load_none(self):
1096 self.append(None)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001097 dispatch[NONE[0]] = load_none
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001098
Guido van Rossum7d97d312003-01-28 04:25:27 +00001099 def load_false(self):
1100 self.append(False)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001101 dispatch[NEWFALSE[0]] = load_false
Guido van Rossum7d97d312003-01-28 04:25:27 +00001102
1103 def load_true(self):
1104 self.append(True)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001105 dispatch[NEWTRUE[0]] = load_true
Guido van Rossum7d97d312003-01-28 04:25:27 +00001106
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001107 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +00001108 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +00001109 if data == FALSE[1:]:
1110 val = False
1111 elif data == TRUE[1:]:
1112 val = True
1113 else:
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001114 val = int(data, 0)
Guido van Rossume2763392002-04-05 19:30:08 +00001115 self.append(val)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001116 dispatch[INT[0]] = load_int
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001117
1118 def load_binint(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001119 self.append(unpack('<i', self.read(4))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001120 dispatch[BININT[0]] = load_binint
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001121
1122 def load_binint1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001123 self.append(self.read(1)[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001124 dispatch[BININT1[0]] = load_binint1
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001125
1126 def load_binint2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001127 self.append(unpack('<H', self.read(2))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001128 dispatch[BININT2[0]] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +00001129
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001130 def load_long(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001131 val = self.readline()[:-1]
1132 if val and val[-1] == b'L'[0]:
Mark Dickinson8dd05142009-01-20 20:43:58 +00001133 val = val[:-1]
Guido van Rossumfeea0782007-10-10 18:00:50 +00001134 self.append(int(val, 0))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001135 dispatch[LONG[0]] = load_long
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001136
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001137 def load_long1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001138 n = self.read(1)[0]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001139 data = self.read(n)
1140 self.append(decode_long(data))
1141 dispatch[LONG1[0]] = load_long1
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001142
1143 def load_long4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001144 n, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001145 if n < 0:
1146 # Corrupt or hostile pickle -- we never write one like this
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001147 raise UnpicklingError("LONG pickle has negative byte count")
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001148 data = self.read(n)
1149 self.append(decode_long(data))
1150 dispatch[LONG4[0]] = load_long4
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001151
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001152 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +00001153 self.append(float(self.readline()[:-1]))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001154 dispatch[FLOAT[0]] = load_float
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001155
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001156 def load_binfloat(self):
Guido van Rossumd3703791998-10-22 20:15:36 +00001157 self.append(unpack('>d', self.read(8))[0])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001158 dispatch[BINFLOAT[0]] = load_binfloat
Guido van Rossumd3703791998-10-22 20:15:36 +00001159
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001160 def load_string(self):
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001161 data = self.readline()[:-1]
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001162 # Strip outermost quotes
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001163 if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'':
1164 data = data[1:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +00001165 else:
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07001166 raise UnpicklingError("the STRING opcode argument must be quoted")
1167 self.append(codecs.escape_decode(data)[0]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001168 .decode(self.encoding, self.errors))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001169 dispatch[STRING[0]] = load_string
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001170
1171 def load_binstring(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001172 # Deprecated BINSTRING uses signed 32-bit length
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001173 len, = unpack('<i', self.read(4))
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001174 if len < 0:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001175 raise UnpicklingError("BINSTRING pickle has negative byte count")
Guido van Rossumf4169812008-03-17 22:56:06 +00001176 data = self.read(len)
1177 value = str(data, self.encoding, self.errors)
1178 self.append(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001179 dispatch[BINSTRING[0]] = load_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001180
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001181 def load_binbytes(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001182 len, = unpack('<I', self.read(4))
1183 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001184 raise UnpicklingError("BINBYTES exceeds system's maximum size "
1185 "of %d bytes" % maxsize)
Guido van Rossumf4169812008-03-17 22:56:06 +00001186 self.append(self.read(len))
1187 dispatch[BINBYTES[0]] = load_binbytes
1188
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001189 def load_unicode(self):
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001190 self.append(str(self.readline()[:-1], 'raw-unicode-escape'))
1191 dispatch[UNICODE[0]] = load_unicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001192
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001193 def load_binunicode(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001194 len, = unpack('<I', self.read(4))
1195 if len > maxsize:
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001196 raise UnpicklingError("BINUNICODE exceeds system's maximum size "
1197 "of %d bytes" % maxsize)
Victor Stinner485fb562010-04-13 11:07:24 +00001198 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001199 dispatch[BINUNICODE[0]] = load_binunicode
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +00001200
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001201 def load_binunicode8(self):
1202 len, = unpack('<Q', self.read(8))
1203 if len > maxsize:
1204 raise UnpicklingError("BINUNICODE8 exceeds system's maximum size "
1205 "of %d bytes" % maxsize)
1206 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1207 dispatch[BINUNICODE8[0]] = load_binunicode8
1208
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001209 def load_short_binstring(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001210 len = self.read(1)[0]
1211 data = self.read(len)
Guido van Rossumf4169812008-03-17 22:56:06 +00001212 value = str(data, self.encoding, self.errors)
1213 self.append(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001214 dispatch[SHORT_BINSTRING[0]] = load_short_binstring
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001215
Guido van Rossumf4169812008-03-17 22:56:06 +00001216 def load_short_binbytes(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001217 len = self.read(1)[0]
1218 self.append(self.read(len))
Guido van Rossumf4169812008-03-17 22:56:06 +00001219 dispatch[SHORT_BINBYTES[0]] = load_short_binbytes
1220
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001221 def load_short_binunicode(self):
1222 len = self.read(1)[0]
1223 self.append(str(self.read(len), 'utf-8', 'surrogatepass'))
1224 dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode
1225
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001226 def load_tuple(self):
1227 k = self.marker()
1228 self.stack[k:] = [tuple(self.stack[k+1:])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001229 dispatch[TUPLE[0]] = load_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001230
1231 def load_empty_tuple(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001232 self.append(())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001233 dispatch[EMPTY_TUPLE[0]] = load_empty_tuple
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001234
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001235 def load_tuple1(self):
1236 self.stack[-1] = (self.stack[-1],)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001237 dispatch[TUPLE1[0]] = load_tuple1
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001238
1239 def load_tuple2(self):
1240 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001241 dispatch[TUPLE2[0]] = load_tuple2
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001242
1243 def load_tuple3(self):
1244 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001245 dispatch[TUPLE3[0]] = load_tuple3
Guido van Rossum44f0ea52003-01-28 04:14:51 +00001246
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001247 def load_empty_list(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001248 self.append([])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001249 dispatch[EMPTY_LIST[0]] = load_empty_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001250
1251 def load_empty_dictionary(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252 self.append({})
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001253 dispatch[EMPTY_DICT[0]] = load_empty_dictionary
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001254
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001255 def load_empty_set(self):
1256 self.append(set())
1257 dispatch[EMPTY_SET[0]] = load_empty_set
1258
1259 def load_frozenset(self):
1260 k = self.marker()
1261 self.stack[k:] = [frozenset(self.stack[k+1:])]
1262 dispatch[FROZENSET[0]] = load_frozenset
1263
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001264 def load_list(self):
1265 k = self.marker()
1266 self.stack[k:] = [self.stack[k+1:]]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001267 dispatch[LIST[0]] = load_list
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001268
1269 def load_dict(self):
1270 k = self.marker()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001271 items = self.stack[k+1:]
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001272 d = {items[i]: items[i+1]
1273 for i in range(0, len(items), 2)}
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001274 self.stack[k:] = [d]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001275 dispatch[DICT[0]] = load_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001276
Tim Petersd01c1e92003-01-30 15:41:46 +00001277 # INST and OBJ differ only in how they get a class object. It's not
1278 # only sensible to do the rest in a common routine, the two routines
1279 # previously diverged and grew different bugs.
1280 # klass is the class to instantiate, and k points to the topmost mark
1281 # object, following which are the arguments for klass.__init__.
1282 def _instantiate(self, klass, k):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001283 args = tuple(self.stack[k+1:])
1284 del self.stack[k:]
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001285 if (args or not isinstance(klass, type) or
1286 hasattr(klass, "__getinitargs__")):
Guido van Rossum743d17e1998-09-15 20:25:57 +00001287 try:
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001288 value = klass(*args)
Guido van Rossumb940e112007-01-10 16:19:56 +00001289 except TypeError as err:
Guido van Rossum26d95c32007-08-27 23:18:54 +00001290 raise TypeError("in constructor for %s: %s" %
1291 (klass.__name__, str(err)), sys.exc_info()[2])
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00001292 else:
1293 value = klass.__new__(klass)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001294 self.append(value)
Tim Petersd01c1e92003-01-30 15:41:46 +00001295
1296 def load_inst(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001297 module = self.readline()[:-1].decode("ascii")
1298 name = self.readline()[:-1].decode("ascii")
Tim Petersd01c1e92003-01-30 15:41:46 +00001299 klass = self.find_class(module, name)
1300 self._instantiate(klass, self.marker())
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001301 dispatch[INST[0]] = load_inst
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001302
1303 def load_obj(self):
Tim Petersd01c1e92003-01-30 15:41:46 +00001304 # Stack is ... markobject classobject arg1 arg2 ...
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001305 k = self.marker()
Tim Petersd01c1e92003-01-30 15:41:46 +00001306 klass = self.stack.pop(k+1)
1307 self._instantiate(klass, k)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001308 dispatch[OBJ[0]] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001309
Guido van Rossum3a41c612003-01-28 15:10:22 +00001310 def load_newobj(self):
1311 args = self.stack.pop()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001312 cls = self.stack.pop()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001313 obj = cls.__new__(cls, *args)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001314 self.append(obj)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001315 dispatch[NEWOBJ[0]] = load_newobj
Guido van Rossum3a41c612003-01-28 15:10:22 +00001316
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001317 def load_newobj_ex(self):
1318 kwargs = self.stack.pop()
1319 args = self.stack.pop()
1320 cls = self.stack.pop()
1321 obj = cls.__new__(cls, *args, **kwargs)
1322 self.append(obj)
1323 dispatch[NEWOBJ_EX[0]] = load_newobj_ex
1324
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001325 def load_global(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001326 module = self.readline()[:-1].decode("utf-8")
1327 name = self.readline()[:-1].decode("utf-8")
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001328 klass = self.find_class(module, name)
1329 self.append(klass)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001330 dispatch[GLOBAL[0]] = load_global
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001331
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001332 def load_stack_global(self):
1333 name = self.stack.pop()
1334 module = self.stack.pop()
1335 if type(name) is not str or type(module) is not str:
1336 raise UnpicklingError("STACK_GLOBAL requires str")
1337 self.append(self.find_class(module, name))
1338 dispatch[STACK_GLOBAL[0]] = load_stack_global
1339
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001340 def load_ext1(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001341 code = self.read(1)[0]
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001342 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001343 dispatch[EXT1[0]] = load_ext1
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001344
1345 def load_ext2(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001346 code, = unpack('<H', self.read(2))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001347 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001348 dispatch[EXT2[0]] = load_ext2
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001349
1350 def load_ext4(self):
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001351 code, = unpack('<i', self.read(4))
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001352 self.get_extension(code)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001353 dispatch[EXT4[0]] = load_ext4
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001354
1355 def get_extension(self, code):
1356 nil = []
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001357 obj = _extension_cache.get(code, nil)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001358 if obj is not nil:
1359 self.append(obj)
1360 return
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001361 key = _inverted_registry.get(code)
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001362 if not key:
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001363 if code <= 0: # note that 0 is forbidden
1364 # Corrupt or hostile pickle.
Alexandre Vassalotticc757172013-04-14 02:25:10 -07001365 raise UnpicklingError("EXT specifies code <= 0")
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001366 raise ValueError("unregistered extension code %d" % code)
1367 obj = self.find_class(*key)
Guido van Rossumd4b920c2003-02-04 01:54:49 +00001368 _extension_cache[code] = obj
Guido van Rossum255f3ee2003-01-29 06:14:11 +00001369 self.append(obj)
1370
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001371 def find_class(self, module, name):
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001372 # Subclasses may override this.
1373 if self.proto < 3 and self.fix_imports:
1374 if (module, name) in _compat_pickle.NAME_MAPPING:
1375 module, name = _compat_pickle.NAME_MAPPING[(module, name)]
1376 if module in _compat_pickle.IMPORT_MAPPING:
1377 module = _compat_pickle.IMPORT_MAPPING[module]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001378 __import__(module, level=0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001379 return _getattribute(sys.modules[module], name,
1380 allow_qualname=self.proto >= 4)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001381
1382 def load_reduce(self):
1383 stack = self.stack
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001384 args = stack.pop()
1385 func = stack[-1]
Guido van Rossum99603b02007-07-20 00:22:32 +00001386 try:
1387 value = func(*args)
1388 except:
1389 print(sys.exc_info())
1390 print(func, args)
1391 raise
Guido van Rossumb26a97a2003-01-28 22:29:13 +00001392 stack[-1] = value
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001393 dispatch[REDUCE[0]] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001394
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001395 def load_pop(self):
1396 del self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001397 dispatch[POP[0]] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001398
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001399 def load_pop_mark(self):
1400 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001401 del self.stack[k:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001402 dispatch[POP_MARK[0]] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001403
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001404 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001405 self.append(self.stack[-1])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001406 dispatch[DUP[0]] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001407
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001408 def load_get(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001409 i = int(self.readline()[:-1])
1410 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001411 dispatch[GET[0]] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001412
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001413 def load_binget(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001414 i = self.read(1)[0]
1415 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001416 dispatch[BINGET[0]] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001417
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001418 def load_long_binget(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001419 i, = unpack('<I', self.read(4))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001420 self.append(self.memo[i])
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001421 dispatch[LONG_BINGET[0]] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001422
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001423 def load_put(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001424 i = int(self.readline()[:-1])
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001425 if i < 0:
1426 raise ValueError("negative PUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001427 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001428 dispatch[PUT[0]] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001429
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001430 def load_binput(self):
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001431 i = self.read(1)[0]
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001432 if i < 0:
1433 raise ValueError("negative BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001434 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001435 dispatch[BINPUT[0]] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001436
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001437 def load_long_binput(self):
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001438 i, = unpack('<I', self.read(4))
1439 if i > maxsize:
Antoine Pitrou55549ec2011-08-30 00:27:10 +02001440 raise ValueError("negative LONG_BINPUT argument")
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441 self.memo[i] = self.stack[-1]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001442 dispatch[LONG_BINPUT[0]] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001443
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001444 def load_memoize(self):
1445 memo = self.memo
1446 memo[len(memo)] = self.stack[-1]
1447 dispatch[MEMOIZE[0]] = load_memoize
1448
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001449 def load_append(self):
1450 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001451 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001452 list = stack[-1]
1453 list.append(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001454 dispatch[APPEND[0]] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001455
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001456 def load_appends(self):
1457 stack = self.stack
1458 mark = self.marker()
Alexandre Vassalotti1f7492c2013-04-20 13:19:46 -07001459 list_obj = stack[mark - 1]
1460 items = stack[mark + 1:]
1461 if isinstance(list_obj, list):
1462 list_obj.extend(items)
1463 else:
1464 append = list_obj.append
1465 for item in items:
1466 append(item)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001467 del stack[mark:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001468 dispatch[APPENDS[0]] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001469
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001470 def load_setitem(self):
1471 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001472 value = stack.pop()
1473 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001474 dict = stack[-1]
1475 dict[key] = value
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001476 dispatch[SETITEM[0]] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001477
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001478 def load_setitems(self):
1479 stack = self.stack
1480 mark = self.marker()
1481 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001482 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001483 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001484
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001485 del stack[mark:]
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001486 dispatch[SETITEMS[0]] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001487
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001488 def load_additems(self):
1489 stack = self.stack
1490 mark = self.marker()
1491 set_obj = stack[mark - 1]
1492 items = stack[mark + 1:]
1493 if isinstance(set_obj, set):
1494 set_obj.update(items)
1495 else:
1496 add = set_obj.add
1497 for item in items:
1498 add(item)
1499 del stack[mark:]
1500 dispatch[ADDITEMS[0]] = load_additems
1501
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001502 def load_build(self):
1503 stack = self.stack
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001504 state = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001505 inst = stack[-1]
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001506 setstate = getattr(inst, "__setstate__", None)
Serhiy Storchakaa3e32c92013-04-14 13:37:02 +03001507 if setstate is not None:
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001508 setstate(state)
1509 return
1510 slotstate = None
1511 if isinstance(state, tuple) and len(state) == 2:
1512 state, slotstate = state
1513 if state:
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001514 inst_dict = inst.__dict__
Antoine Pitroua9f48a02009-05-02 21:41:14 +00001515 intern = sys.intern
Alexandre Vassalottiebfecfd2009-05-25 18:50:33 +00001516 for k, v in state.items():
1517 if type(k) is str:
1518 inst_dict[intern(k)] = v
1519 else:
1520 inst_dict[k] = v
Guido van Rossumac5b5d22003-01-28 22:01:16 +00001521 if slotstate:
1522 for k, v in slotstate.items():
1523 setattr(inst, k, v)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001524 dispatch[BUILD[0]] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001525
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001526 def load_mark(self):
1527 self.append(self.mark)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001528 dispatch[MARK[0]] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001529
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001530 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001531 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001532 raise _Stop(value)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +00001533 dispatch[STOP[0]] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001534
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001535
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001536# Shorthands
1537
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538def _dump(obj, file, protocol=None, *, fix_imports=True):
1539 _Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001540
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541def _dumps(obj, protocol=None, *, fix_imports=True):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001542 f = io.BytesIO()
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 _Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001544 res = f.getvalue()
Guido van Rossum98297ee2007-11-06 21:34:58 +00001545 assert isinstance(res, bytes_types)
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001546 return res
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001547
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001548def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"):
1549 return _Unpickler(file, fix_imports=fix_imports,
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001550 encoding=encoding, errors=errors).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001551
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001552def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"):
Guido van Rossumcfe5f202007-05-08 21:26:54 +00001553 if isinstance(s, str):
1554 raise TypeError("Can't load pickle from unicode string")
1555 file = io.BytesIO(s)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001556 return _Unpickler(file, fix_imports=fix_imports,
1557 encoding=encoding, errors=errors).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001559# Use the faster _pickle if possible
1560try:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001561 from _pickle import (
1562 PickleError,
1563 PicklingError,
1564 UnpicklingError,
1565 Pickler,
1566 Unpickler,
1567 dump,
1568 dumps,
1569 load,
1570 loads
1571 )
Brett Cannoncd171c82013-07-04 17:43:24 -04001572except ImportError:
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001573 Pickler, Unpickler = _Pickler, _Unpickler
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574 dump, dumps, load, loads = _dump, _dumps, _load, _loads
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001575
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001576# Doctest
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001577def _test():
1578 import doctest
1579 return doctest.testmod()
1580
1581if __name__ == "__main__":
Florent Xicluna54540ec2011-11-04 08:29:17 +01001582 import argparse
Alexander Belopolsky455f7bd2010-07-27 23:02:38 +00001583 parser = argparse.ArgumentParser(
1584 description='display contents of the pickle files')
1585 parser.add_argument(
1586 'pickle_file', type=argparse.FileType('br'),
1587 nargs='*', help='the pickle file')
1588 parser.add_argument(
1589 '-t', '--test', action='store_true',
1590 help='run self-test suite')
1591 parser.add_argument(
1592 '-v', action='store_true',
1593 help='run verbosely; only affects self-test run')
1594 args = parser.parse_args()
1595 if args.test:
1596 _test()
1597 else:
1598 if not args.pickle_file:
1599 parser.print_help()
1600 else:
1601 import pprint
1602 for f in args.pickle_file:
1603 obj = load(f)
1604 pprint.pprint(obj)