blob: 5a8bfea218facac10bb6ca3246e6d0c45d26534b [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
Guido van Rossume467be61997-12-05 19:42:42 +00003See module cPickle for a (much) faster implementation.
4See module copy_reg for a mechanism for registering custom picklers.
Tim Peters22a449a2003-01-27 20:16:36 +00005See module pickletools source for extensive comments.
Guido van Rossuma48061a1995-01-10 00:31:14 +00006
Guido van Rossume467be61997-12-05 19:42:42 +00007Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00008
Guido van Rossume467be61997-12-05 19:42:42 +00009 Pickler
10 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000011
Guido van Rossume467be61997-12-05 19:42:42 +000012Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000013
Guido van Rossume467be61997-12-05 19:42:42 +000014 dump(object, file)
15 dumps(object) -> string
16 load(file) -> object
17 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000018
Guido van Rossume467be61997-12-05 19:42:42 +000019Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000020
Fred Drakefe82acc1998-02-13 03:24:48 +000021 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000022 format_version
23 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000024
Guido van Rossuma48061a1995-01-10 00:31:14 +000025"""
26
Guido van Rossum743d17e1998-09-15 20:25:57 +000027__version__ = "$Revision$" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +000028
29from types import *
Guido van Rossum4fb5b281997-09-12 20:07:24 +000030from copy_reg import dispatch_table, safe_constructors
Guido van Rossumd3703791998-10-22 20:15:36 +000031import marshal
32import sys
33import struct
Skip Montanaro23bafc62001-02-18 03:10:09 +000034import re
Guido van Rossumbc64e222003-01-28 16:34:19 +000035import warnings
Guido van Rossuma48061a1995-01-10 00:31:14 +000036
Skip Montanaro352674d2001-02-07 23:14:30 +000037__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
38 "Unpickler", "dump", "dumps", "load", "loads"]
39
Guido van Rossumf29d3d62003-01-27 22:47:53 +000040# These are purely informational; no code usues these
41format_version = "2.0" # File format version we write
42compatible_formats = ["1.0", # Original protocol 0
Guido van Rossumbc64e222003-01-28 16:34:19 +000043 "1.1", # Protocol 0 with INST added
Guido van Rossumf29d3d62003-01-27 22:47:53 +000044 "1.2", # Original protocol 1
45 "1.3", # Protocol 1 with BINFLOAT added
46 "2.0", # Protocol 2
47 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000048
Guido van Rossume0b90422003-01-28 03:17:21 +000049# Why use struct.pack() for pickling but marshal.loads() for
50# unpickling? struct.pack() is 40% faster than marshal.loads(), but
51# marshal.loads() is twice as fast as struct.unpack()!
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000052mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000053
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000054class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000055 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000056 pass
57
58class PicklingError(PickleError):
59 """This exception is raised when an unpicklable object is passed to the
60 dump() method.
61
62 """
63 pass
64
65class UnpicklingError(PickleError):
66 """This exception is raised when there is a problem unpickling an object,
67 such as a security violation.
68
69 Note that other exceptions may also be raised during unpickling, including
70 (but not necessarily limited to) AttributeError, EOFError, ImportError,
71 and IndexError.
72
73 """
74 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000075
Guido van Rossumff871742000-12-13 18:11:56 +000076class _Stop(Exception):
77 def __init__(self, value):
78 self.value = value
79
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000080try:
81 from org.python.core import PyStringMap
82except ImportError:
83 PyStringMap = None
84
Guido van Rossumdbb718f2001-09-21 19:22:34 +000085try:
86 UnicodeType
87except NameError:
88 UnicodeType = None
89
Tim Peters22a449a2003-01-27 20:16:36 +000090# Pickle opcodes. See pickletools.py for extensive docs. The listing
91# here is in kind-of alphabetical order of 1-character pickle code.
92# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000093
Tim Peters22a449a2003-01-27 20:16:36 +000094MARK = '(' # push special markobject on stack
95STOP = '.' # every pickle ends with STOP
96POP = '0' # discard topmost stack item
97POP_MARK = '1' # discard stack top through topmost markobject
98DUP = '2' # duplicate top stack item
99FLOAT = 'F' # push float object; decimal string argument
100INT = 'I' # push integer or bool; decimal string argument
101BININT = 'J' # push four-byte signed int
102BININT1 = 'K' # push 1-byte unsigned int
103LONG = 'L' # push long; decimal string argument
104BININT2 = 'M' # push 2-byte unsigned int
105NONE = 'N' # push None
106PERSID = 'P' # push persistent object; id is taken from string arg
107BINPERSID = 'Q' # " " " ; " " " " stack
108REDUCE = 'R' # apply callable to argtuple, both on stack
109STRING = 'S' # push string; NL-terminated string argument
110BINSTRING = 'T' # push string; counted binary string argument
111SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
112UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
113BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
114APPEND = 'a' # append stack top to list below it
115BUILD = 'b' # call __setstate__ or __dict__.update()
116GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
117DICT = 'd' # build a dict from stack items
118EMPTY_DICT = '}' # push empty dict
119APPENDS = 'e' # extend list on stack by topmost stack slice
120GET = 'g' # push item from memo on stack; index is string arg
121BINGET = 'h' # " " " " " " ; " " 1-byte arg
122INST = 'i' # build & push class instance
123LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
124LIST = 'l' # build list from topmost stack items
125EMPTY_LIST = ']' # push empty list
126OBJ = 'o' # build & push class instance
127PUT = 'p' # store stack top in memo; index is string arg
128BINPUT = 'q' # " " " " " ; " " 1-byte arg
129LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
130SETITEM = 's' # add key+value pair to dict
131TUPLE = 't' # build tuple from topmost stack items
132EMPTY_TUPLE = ')' # push empty tuple
133SETITEMS = 'u' # modify dict by adding topmost key+value pairs
134BINFLOAT = 'G' # push float; arg is 8-byte float encoding
135
136TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
137FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000138
Tim Peterse1054782003-01-28 00:22:12 +0000139# Protocol 2 (not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000140
Tim Peterse1054782003-01-28 00:22:12 +0000141PROTO = '\x80' # identify pickle protocol
142NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
143EXT1 = '\x82' # push object from extension registry; 1-byte index
144EXT2 = '\x83' # ditto, but 2-byte index
145EXT4 = '\x84' # ditto, but 4-byte index
146TUPLE1 = '\x85' # build 1-tuple from stack top
147TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
148TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
149NEWTRUE = '\x88' # push True
150NEWFALSE = '\x89' # push False
151LONG1 = '\x8a' # push long from < 256 bytes
152LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000153
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000154_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
155
Guido van Rossuma48061a1995-01-10 00:31:14 +0000156
Skip Montanaro23bafc62001-02-18 03:10:09 +0000157__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000158del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000159
Guido van Rossum1be31752003-01-28 15:19:53 +0000160
161# Pickling machinery
162
Guido van Rossuma48061a1995-01-10 00:31:14 +0000163class Pickler:
164
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000165 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000166 """This takes a file-like object for writing a pickle data stream.
167
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000168 The optional proto argument tells the pickler to use the given
169 protocol; supported protocols are 0, 1, 2. The default
170 protocol is 1 (in previous Python versions the default was 0).
171
172 Protocol 1 is more efficient than protocol 0; protocol 2 is
173 more efficient than protocol 1. Protocol 2 is not the default
174 because it is not supported by older Python versions.
175
176 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000177
178 The file parameter must have a write() method that accepts a single
179 string argument. It can thus be an open file object, a StringIO
180 object, or any other custom object that meets this interface.
181
182 """
Guido van Rossum1be31752003-01-28 15:19:53 +0000183 if proto not in (0, 1, 2):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000184 raise ValueError, "pickle protocol must be 0, 1 or 2"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000185 self.write = file.write
186 self.memo = {}
Guido van Rossum1be31752003-01-28 15:19:53 +0000187 self.proto = int(proto)
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000188 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000189
Fred Drake7f781c92002-05-01 20:33:53 +0000190 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000191 """Clears the pickler's "memo".
192
193 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000194 pickler has already seen, so that shared or recursive objects are
195 pickled by reference and not by value. This method is useful when
196 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000197
198 """
Fred Drake7f781c92002-05-01 20:33:53 +0000199 self.memo.clear()
200
Guido van Rossum3a41c612003-01-28 15:10:22 +0000201 def dump(self, obj):
202 """Write a pickled representation of obj to the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000203
204 Either the binary or ASCII format will be used, depending on the
205 value of the bin flag passed to the constructor.
206
207 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000208 if self.proto >= 2:
209 self.write(PROTO + chr(self.proto))
Guido van Rossum3a41c612003-01-28 15:10:22 +0000210 self.save(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000211 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000212
Jeremy Hylton3422c992003-01-24 19:29:52 +0000213 def memoize(self, obj):
214 """Store an object in the memo."""
215
Tim Peterse46b73f2003-01-27 21:22:10 +0000216 # The Pickler memo is a dictionary mapping object ids to 2-tuples
217 # that contain the Unpickler memo key and the object being memoized.
218 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000219 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000220 # Pickler memo so that transient objects are kept alive during
221 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000222
Tim Peterse46b73f2003-01-27 21:22:10 +0000223 # The use of the Unpickler memo length as the memo key is just a
224 # convention. The only requirement is that the memo values be unique.
225 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000226 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000227 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000228 memo_len = len(self.memo)
229 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000230 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000231
Tim Petersbb38e302003-01-27 21:25:41 +0000232 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000233 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000234 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000235 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000236 return BINPUT + chr(i)
237 else:
238 return LONG_BINPUT + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000239
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000240 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000241
Tim Petersbb38e302003-01-27 21:25:41 +0000242 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000243 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000244 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000245 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000246 return BINGET + chr(i)
247 else:
248 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000249
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000250 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000251
Guido van Rossum3a41c612003-01-28 15:10:22 +0000252 def save(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000253 # Check for persistent id (defined by a subclass)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000254 pid = self.persistent_id(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000255 if pid:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000256 self.save_pers(pid)
257 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000258
Guido van Rossumbc64e222003-01-28 16:34:19 +0000259 # Check the memo
260 x = self.memo.get(id(obj))
261 if x:
262 self.write(self.get(x[0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000263 return
264
Guido van Rossumbc64e222003-01-28 16:34:19 +0000265 # Check the type dispatch table
Guido van Rossum3a41c612003-01-28 15:10:22 +0000266 t = type(obj)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000267 f = self.dispatch.get(t)
268 if f:
269 f(self, obj) # Call unbound method with explicit self
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000270 return
271
Guido van Rossumbc64e222003-01-28 16:34:19 +0000272 # Check for a class with a custom metaclass; treat as regular class
Tim Petersb32a8312003-01-28 00:48:09 +0000273 try:
274 issc = issubclass(t, TypeType)
Guido van Rossumbc64e222003-01-28 16:34:19 +0000275 except TypeError: # t is not a class (old Boost; see SF #502085)
Tim Petersb32a8312003-01-28 00:48:09 +0000276 issc = 0
277 if issc:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000278 self.save_global(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000279 return
280
Guido van Rossumbc64e222003-01-28 16:34:19 +0000281 # Check copy_reg.dispatch_table
282 reduce = dispatch_table.get(t)
283 if reduce:
284 rv = reduce(obj)
Tim Petersb32a8312003-01-28 00:48:09 +0000285 else:
Guido van Rossumbc64e222003-01-28 16:34:19 +0000286 # Check for __reduce__ method
287 reduce = getattr(obj, "__reduce__", None)
288 if not reduce:
289 raise PicklingError("Can't pickle %r object: %r" %
290 (t.__name__, obj))
291 rv = reduce()
Tim Petersb32a8312003-01-28 00:48:09 +0000292
Guido van Rossumbc64e222003-01-28 16:34:19 +0000293 # Check for string returned by reduce(), meaning "save as global"
294 if type(rv) is StringType:
295 self.save_global(obj, rv)
Tim Petersb32a8312003-01-28 00:48:09 +0000296 return
297
Guido van Rossumbc64e222003-01-28 16:34:19 +0000298 # Assert that reduce() returned a tuple
299 if type(rv) is not TupleType:
300 raise PicklingError("%s must return string or tuple" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000301
Guido van Rossumbc64e222003-01-28 16:34:19 +0000302 # Assert that it returned a 2-tuple or 3-tuple, and unpack it
303 l = len(rv)
304 if l == 2:
305 func, args = rv
Tim Petersb32a8312003-01-28 00:48:09 +0000306 state = None
Guido van Rossumbc64e222003-01-28 16:34:19 +0000307 elif l == 3:
308 func, args, state = rv
309 else:
310 raise PicklingError("Tuple returned by %s must have "
311 "exactly two or three elements" % reduce)
Tim Petersb32a8312003-01-28 00:48:09 +0000312
Guido van Rossumbc64e222003-01-28 16:34:19 +0000313 # Save the reduce() output and finally memoize the object
314 self.save_reduce(func, args, state)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000315 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000316
Guido van Rossum3a41c612003-01-28 15:10:22 +0000317 def persistent_id(self, obj):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000318 # This exists so a subclass can override it
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000319 return None
320
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000321 def save_pers(self, pid):
Guido van Rossumbc64e222003-01-28 16:34:19 +0000322 # Save a persistent id reference
Tim Petersbd1cdb92003-01-28 01:03:10 +0000323 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000324 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000325 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000326 else:
327 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000328
Guido van Rossumbc64e222003-01-28 16:34:19 +0000329 def save_reduce(self, func, args, state=None):
330 # This API is be called by some subclasses
331
332 # Assert that args is a tuple or None
333 if not isinstance(args, TupleType):
334 if args is None:
335 # A hack for Jim Fulton's ExtensionClass, now deprecated.
336 # See load_reduce()
337 warnings.warn("__basicnew__ special case is deprecated",
338 DeprecationWarning)
339 else:
340 raise PicklingError(
341 "args from reduce() should be a tuple")
342
343 # Assert that func is callable
344 if not callable(func):
345 raise PicklingError("func from reduce should be callable")
346
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000347 save = self.save
Guido van Rossumbc64e222003-01-28 16:34:19 +0000348 write = self.write
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000349
Guido van Rossumbc64e222003-01-28 16:34:19 +0000350 save(func)
351 save(args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000352 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000353
Tim Petersc32d8242001-04-10 02:48:53 +0000354 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000355 save(state)
356 write(BUILD)
357
Guido van Rossumbc64e222003-01-28 16:34:19 +0000358 # Methods below this point are dispatched through the dispatch table
359
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000360 dispatch = {}
361
Guido van Rossum3a41c612003-01-28 15:10:22 +0000362 def save_none(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000363 self.write(NONE)
364 dispatch[NoneType] = save_none
365
Guido van Rossum3a41c612003-01-28 15:10:22 +0000366 def save_bool(self, obj):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000367 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000368 self.write(obj and NEWTRUE or NEWFALSE)
Guido van Rossum7d97d312003-01-28 04:25:27 +0000369 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000370 self.write(obj and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000371 dispatch[bool] = save_bool
372
Guido van Rossum3a41c612003-01-28 15:10:22 +0000373 def save_int(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000374 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000375 # If the int is small enough to fit in a signed 4-byte 2's-comp
376 # format, we can store it more efficiently than the general
377 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000378 # First one- and two-byte unsigned ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000379 if obj >= 0:
380 if obj <= 0xff:
381 self.write(BININT1 + chr(obj))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000382 return
Guido van Rossum3a41c612003-01-28 15:10:22 +0000383 if obj <= 0xffff:
384 self.write(BININT2 + chr(obj&0xff) + chr(obj>>8))
Guido van Rossum5c938d02003-01-28 03:03:08 +0000385 return
386 # Next check for 4-byte signed ints:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000387 high_bits = obj >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000388 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000389 # All high bits are copies of bit 2**31, so the value
390 # fits in a 4-byte signed int.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000391 self.write(BININT + pack("<i", obj))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000392 return
Tim Peters44714002001-04-10 05:02:52 +0000393 # Text pickle, or int too big to fit in signed 4-byte format.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000394 self.write(INT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000395 dispatch[IntType] = save_int
396
Guido van Rossum3a41c612003-01-28 15:10:22 +0000397 def save_long(self, obj, pack=struct.pack):
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000398 if self.proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000399 bytes = encode_long(obj)
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000400 n = len(bytes)
401 if n < 256:
402 self.write(LONG1 + chr(n) + bytes)
403 else:
404 self.write(LONG4 + pack("<i", n) + bytes)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000405 self.write(LONG + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 dispatch[LongType] = save_long
407
Guido van Rossum3a41c612003-01-28 15:10:22 +0000408 def save_float(self, obj, pack=struct.pack):
Guido van Rossumd3703791998-10-22 20:15:36 +0000409 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000410 self.write(BINFLOAT + pack('>d', obj))
Guido van Rossumd3703791998-10-22 20:15:36 +0000411 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000412 self.write(FLOAT + `obj` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000413 dispatch[FloatType] = save_float
414
Guido van Rossum3a41c612003-01-28 15:10:22 +0000415 def save_string(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000416 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000417 n = len(obj)
Tim Petersbbf63cd2003-01-27 21:15:36 +0000418 if n < 256:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000419 self.write(SHORT_BINSTRING + chr(n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000420 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000421 self.write(BINSTRING + pack("<i", n) + obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000422 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000423 self.write(STRING + `obj` + '\n')
424 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000425 dispatch[StringType] = save_string
426
Guido van Rossum3a41c612003-01-28 15:10:22 +0000427 def save_unicode(self, obj, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000428 if self.bin:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000429 encoding = obj.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000430 n = len(encoding)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000431 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000432 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000433 obj = obj.replace("\\", "\\u005c")
434 obj = obj.replace("\n", "\\u000a")
435 self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
436 self.memoize(obj)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000437 dispatch[UnicodeType] = save_unicode
438
Guido van Rossum31584cb2001-01-22 14:53:29 +0000439 if StringType == UnicodeType:
440 # This is true for Jython
Guido van Rossum3a41c612003-01-28 15:10:22 +0000441 def save_string(self, obj, pack=struct.pack):
442 unicode = obj.isunicode()
Guido van Rossum31584cb2001-01-22 14:53:29 +0000443
Tim Petersc32d8242001-04-10 02:48:53 +0000444 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000445 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000446 obj = obj.encode("utf-8")
447 l = len(obj)
Tim Petersc32d8242001-04-10 02:48:53 +0000448 if l < 256 and not unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000449 self.write(SHORT_BINSTRING + chr(l) + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000450 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000451 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000452 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000453 self.write(BINUNICODE + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000454 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000455 self.write(BINSTRING + s + obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000456 else:
Tim Peters658cba62001-02-09 20:06:00 +0000457 if unicode:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000458 obj = obj.replace("\\", "\\u005c")
459 obj = obj.replace("\n", "\\u000a")
460 obj = obj.encode('raw-unicode-escape')
461 self.write(UNICODE + obj + '\n')
Guido van Rossum31584cb2001-01-22 14:53:29 +0000462 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000463 self.write(STRING + `obj` + '\n')
464 self.memoize(obj)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000465 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000466
Guido van Rossum3a41c612003-01-28 15:10:22 +0000467 def save_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000468 write = self.write
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000469 proto = self.proto
470
Guido van Rossum3a41c612003-01-28 15:10:22 +0000471 n = len(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000472 if n == 0 and proto:
473 write(EMPTY_TUPLE)
474 return
475
476 save = self.save
477 memo = self.memo
478 if n <= 3 and proto >= 2:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000479 for element in obj:
Tim Petersd97da802003-01-28 05:48:29 +0000480 save(element)
481 # Subtle. Same as in the big comment below.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000482 if id(obj) in memo:
483 get = self.get(memo[id(obj)][0])
Tim Petersd97da802003-01-28 05:48:29 +0000484 write(POP * n + get)
485 else:
486 write(_tuplesize2code[n])
Guido van Rossum3a41c612003-01-28 15:10:22 +0000487 self.memoize(obj)
Tim Petersd97da802003-01-28 05:48:29 +0000488 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000489
Tim Petersff57bff2003-01-28 05:34:53 +0000490 # proto 0, or proto 1 and tuple isn't empty, or proto > 1 and tuple
491 # has more than 3 elements.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000492 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000493 for element in obj:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000494 save(element)
495
Guido van Rossum3a41c612003-01-28 15:10:22 +0000496 if n and id(obj) in memo:
Tim Petersf558da02003-01-28 02:09:55 +0000497 # Subtle. d was not in memo when we entered save_tuple(), so
498 # the process of saving the tuple's elements must have saved
499 # the tuple itself: the tuple is recursive. The proper action
500 # now is to throw away everything we put on the stack, and
501 # simply GET the tuple (it's already constructed). This check
502 # could have been done in the "for element" loop instead, but
503 # recursive tuples are a rare thing.
Guido van Rossum3a41c612003-01-28 15:10:22 +0000504 get = self.get(memo[id(obj)][0])
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000505 if proto:
Tim Petersf558da02003-01-28 02:09:55 +0000506 write(POP_MARK + get)
507 else: # proto 0 -- POP_MARK not available
Tim Petersd97da802003-01-28 05:48:29 +0000508 write(POP * (n+1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000509 return
510
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000511 # No recursion (including the empty-tuple case for protocol 0).
Tim Peters518df0d2003-01-28 01:00:38 +0000512 self.write(TUPLE)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000513 if obj: # No need to memoize empty tuple
514 self.memoize(obj)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000515
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000516 dispatch[TupleType] = save_tuple
517
Guido van Rossum3a41c612003-01-28 15:10:22 +0000518 def save_empty_tuple(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000519 self.write(EMPTY_TUPLE)
520
Guido van Rossum3a41c612003-01-28 15:10:22 +0000521 def save_list(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000522 write = self.write
523 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000524
Tim Petersc32d8242001-04-10 02:48:53 +0000525 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000526 write(EMPTY_LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000527 self.memoize(obj)
528 n = len(obj)
Tim Peters21c18f02003-01-28 01:15:46 +0000529 if n > 1:
530 write(MARK)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000531 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000532 save(element)
533 write(APPENDS)
534 elif n:
535 assert n == 1
Guido van Rossum3a41c612003-01-28 15:10:22 +0000536 save(obj[0])
Tim Peters21c18f02003-01-28 01:15:46 +0000537 write(APPEND)
538 # else the list is empty, and we're already done
539
540 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000541 write(MARK + LIST)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000542 self.memoize(obj)
543 for element in obj:
Tim Peters21c18f02003-01-28 01:15:46 +0000544 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000545 write(APPEND)
546
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000547 dispatch[ListType] = save_list
548
Guido van Rossum3a41c612003-01-28 15:10:22 +0000549 def save_dict(self, obj):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000550 write = self.write
551 save = self.save
Guido van Rossum3a41c612003-01-28 15:10:22 +0000552 items = obj.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000553
Tim Petersc32d8242001-04-10 02:48:53 +0000554 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000555 write(EMPTY_DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000556 self.memoize(obj)
557 if len(obj) > 1:
Tim Peters064567e2003-01-28 01:34:43 +0000558 write(MARK)
559 for key, value in items:
560 save(key)
561 save(value)
562 write(SETITEMS)
563 return
564
565 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000566 write(MARK + DICT)
Guido van Rossum3a41c612003-01-28 15:10:22 +0000567 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000568
Guido van Rossum3a41c612003-01-28 15:10:22 +0000569 # proto 0 or len(obj) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570 for key, value in items:
571 save(key)
572 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000573 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000574
575 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000576 if not PyStringMap is None:
577 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000578
Guido van Rossum3a41c612003-01-28 15:10:22 +0000579 def save_inst(self, obj):
580 cls = obj.__class__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000581
582 memo = self.memo
583 write = self.write
584 save = self.save
585
Guido van Rossum3a41c612003-01-28 15:10:22 +0000586 if hasattr(obj, '__getinitargs__'):
587 args = obj.__getinitargs__()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000588 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000589 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000590 else:
591 args = ()
592
593 write(MARK)
594
Tim Petersc32d8242001-04-10 02:48:53 +0000595 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000596 save(cls)
Tim Peters3b769832003-01-28 03:51:36 +0000597 for arg in args:
598 save(arg)
599 write(OBJ)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000600 else:
Tim Peters3b769832003-01-28 03:51:36 +0000601 for arg in args:
602 save(arg)
603 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000604
Guido van Rossum3a41c612003-01-28 15:10:22 +0000605 self.memoize(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000606
607 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000608 getstate = obj.__getstate__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000609 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000610 stuff = obj.__dict__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000611 else:
612 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000613 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000614 save(stuff)
615 write(BUILD)
Tim Peters3b769832003-01-28 03:51:36 +0000616
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000617 dispatch[InstanceType] = save_inst
618
Guido van Rossum3a41c612003-01-28 15:10:22 +0000619 def save_global(self, obj, name = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000620 write = self.write
621 memo = self.memo
622
Tim Petersc32d8242001-04-10 02:48:53 +0000623 if name is None:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000624 name = obj.__name__
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000625
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000626 try:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000627 module = obj.__module__
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000628 except AttributeError:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000629 module = whichmodule(obj, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000630
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000631 try:
632 __import__(module)
633 mod = sys.modules[module]
634 klass = getattr(mod, name)
635 except (ImportError, KeyError, AttributeError):
636 raise PicklingError(
637 "Can't pickle %r: it's not found as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000638 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000639 else:
Guido van Rossum3a41c612003-01-28 15:10:22 +0000640 if klass is not obj:
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000641 raise PicklingError(
642 "Can't pickle %r: it's not the same object as %s.%s" %
Guido van Rossum3a41c612003-01-28 15:10:22 +0000643 (obj, module, name))
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000644
Tim Peters518df0d2003-01-28 01:00:38 +0000645 write(GLOBAL + module + '\n' + name + '\n')
Guido van Rossum3a41c612003-01-28 15:10:22 +0000646 self.memoize(obj)
Tim Peters3b769832003-01-28 03:51:36 +0000647
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000648 dispatch[ClassType] = save_global
649 dispatch[FunctionType] = save_global
650 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000652
Guido van Rossum1be31752003-01-28 15:19:53 +0000653# Pickling helpers
Guido van Rossuma48061a1995-01-10 00:31:14 +0000654
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000655def _keep_alive(x, memo):
656 """Keeps a reference to the object x in the memo.
657
658 Because we remember objects by their id, we have
659 to assure that possibly temporary objects are kept
660 alive by referencing them.
661 We store a reference at the id of the memo, which should
662 normally not be used unless someone tries to deepcopy
663 the memo itself...
664 """
665 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000666 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000667 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000668 # aha, this is the first one :-)
669 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000670
671
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000672classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000673
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000674def whichmodule(func, funcname):
675 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000676
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000677 Search sys.modules for the module.
678 Cache in classmap.
679 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000680 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000681 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000682 if func in classmap:
683 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000684
685 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000686 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000687 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000688 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000689 hasattr(module, funcname) and \
690 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 break
692 else:
693 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000694 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000695 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000696
697
Guido van Rossum1be31752003-01-28 15:19:53 +0000698# Unpickling machinery
699
Guido van Rossuma48061a1995-01-10 00:31:14 +0000700class Unpickler:
701
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000702 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000703 """This takes a file-like object for reading a pickle data stream.
704
705 This class automatically determines whether the data stream was
706 written in binary mode or not, so it does not need a flag as in
707 the Pickler class factory.
708
709 The file-like object must have two methods, a read() method that
710 takes an integer argument, and a readline() method that requires no
711 arguments. Both methods should return a string. Thus file-like
712 object can be a file object opened for reading, a StringIO object,
713 or any other custom object that meets this interface.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000714 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000715 self.readline = file.readline
716 self.read = file.read
717 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000718
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000719 def load(self):
Guido van Rossum3a41c612003-01-28 15:10:22 +0000720 """Read a pickled object representation from the open file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000721
Guido van Rossum3a41c612003-01-28 15:10:22 +0000722 Return the reconstituted object hierarchy specified in the file.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000723 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000724 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000725 self.stack = []
726 self.append = self.stack.append
727 read = self.read
728 dispatch = self.dispatch
729 try:
730 while 1:
731 key = read(1)
732 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000733 except _Stop, stopinst:
734 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000735
Tim Petersc23d18a2003-01-28 01:41:51 +0000736 # Return largest index k such that self.stack[k] is self.mark.
737 # If the stack doesn't contain a mark, eventually raises IndexError.
738 # This could be sped by maintaining another stack, of indices at which
739 # the mark appears. For that matter, the latter stack would suffice,
740 # and we wouldn't need to push mark objects on self.stack at all.
741 # Doing so is probably a good thing, though, since if the pickle is
742 # corrupt (or hostile) we may get a clue from finding self.mark embedded
743 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000744 def marker(self):
745 stack = self.stack
746 mark = self.mark
747 k = len(stack)-1
748 while stack[k] is not mark: k = k-1
749 return k
750
751 dispatch = {}
752
753 def load_eof(self):
754 raise EOFError
755 dispatch[''] = load_eof
756
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000757 def load_proto(self):
758 proto = ord(self.read(1))
759 if not 0 <= proto <= 2:
760 raise ValueError, "unsupported pickle protocol: %d" % proto
761 dispatch[PROTO] = load_proto
762
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000763 def load_persid(self):
764 pid = self.readline()[:-1]
765 self.append(self.persistent_load(pid))
766 dispatch[PERSID] = load_persid
767
768 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000769 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000770 self.append(self.persistent_load(pid))
771 dispatch[BINPERSID] = load_binpersid
772
773 def load_none(self):
774 self.append(None)
775 dispatch[NONE] = load_none
776
Guido van Rossum7d97d312003-01-28 04:25:27 +0000777 def load_false(self):
778 self.append(False)
779 dispatch[NEWFALSE] = load_false
780
781 def load_true(self):
782 self.append(True)
783 dispatch[NEWTRUE] = load_true
784
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000785 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000786 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000787 if data == FALSE[1:]:
788 val = False
789 elif data == TRUE[1:]:
790 val = True
791 else:
792 try:
793 val = int(data)
794 except ValueError:
795 val = long(data)
796 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000797 dispatch[INT] = load_int
798
799 def load_binint(self):
800 self.append(mloads('i' + self.read(4)))
801 dispatch[BININT] = load_binint
802
803 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000804 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000805 dispatch[BININT1] = load_binint1
806
807 def load_binint2(self):
808 self.append(mloads('i' + self.read(2) + '\000\000'))
809 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000810
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000811 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000812 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000813 dispatch[LONG] = load_long
814
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000815 def load_long1(self):
816 n = ord(self.read(1))
817 bytes = self.read(n)
818 return decode_long(bytes)
819 dispatch[LONG1] = load_long1
820
821 def load_long4(self):
822 n = mloads('i' + self.read(4))
823 bytes = self.read(n)
824 return decode_long(bytes)
825 dispatch[LONG4] = load_long4
826
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000827 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000828 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000829 dispatch[FLOAT] = load_float
830
Guido van Rossumd3703791998-10-22 20:15:36 +0000831 def load_binfloat(self, unpack=struct.unpack):
832 self.append(unpack('>d', self.read(8))[0])
833 dispatch[BINFLOAT] = load_binfloat
834
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000835 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000836 rep = self.readline()[:-1]
Tim Petersad5a7712003-01-28 16:23:33 +0000837 for q in "\"'": # double or single quote
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000838 if rep.startswith(q):
839 if not rep.endswith(q):
840 raise ValueError, "insecure string pickle"
841 rep = rep[len(q):-len(q)]
842 break
843 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000844 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000845 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000846 dispatch[STRING] = load_string
847
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000848 def _is_string_secure(self, s):
849 """Return true if s contains a string that is safe to eval
850
851 The definition of secure string is based on the implementation
852 in cPickle. s is secure as long as it only contains a quoted
853 string and optional trailing whitespace.
854 """
855 q = s[0]
856 if q not in ("'", '"'):
857 return 0
858 # find the closing quote
859 offset = 1
860 i = None
861 while 1:
862 try:
863 i = s.index(q, offset)
864 except ValueError:
865 # if there is an error the first time, there is no
866 # close quote
867 if offset == 1:
868 return 0
869 if s[i-1] != '\\':
870 break
871 # check to see if this one is escaped
872 nslash = 0
873 j = i - 1
874 while j >= offset and s[j] == '\\':
875 j = j - 1
876 nslash = nslash + 1
877 if nslash % 2 == 0:
878 break
879 offset = i + 1
880 for c in s[i+1:]:
881 if ord(c) > 32:
882 return 0
883 return 1
884
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000885 def load_binstring(self):
886 len = mloads('i' + self.read(4))
887 self.append(self.read(len))
888 dispatch[BINSTRING] = load_binstring
889
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000890 def load_unicode(self):
891 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
892 dispatch[UNICODE] = load_unicode
893
894 def load_binunicode(self):
895 len = mloads('i' + self.read(4))
896 self.append(unicode(self.read(len),'utf-8'))
897 dispatch[BINUNICODE] = load_binunicode
898
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000899 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000900 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000901 self.append(self.read(len))
902 dispatch[SHORT_BINSTRING] = load_short_binstring
903
904 def load_tuple(self):
905 k = self.marker()
906 self.stack[k:] = [tuple(self.stack[k+1:])]
907 dispatch[TUPLE] = load_tuple
908
909 def load_empty_tuple(self):
910 self.stack.append(())
911 dispatch[EMPTY_TUPLE] = load_empty_tuple
912
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000913 def load_tuple1(self):
914 self.stack[-1] = (self.stack[-1],)
915 dispatch[TUPLE1] = load_tuple1
916
917 def load_tuple2(self):
918 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
919 dispatch[TUPLE2] = load_tuple2
920
921 def load_tuple3(self):
922 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
923 dispatch[TUPLE3] = load_tuple3
924
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000925 def load_empty_list(self):
926 self.stack.append([])
927 dispatch[EMPTY_LIST] = load_empty_list
928
929 def load_empty_dictionary(self):
930 self.stack.append({})
931 dispatch[EMPTY_DICT] = load_empty_dictionary
932
933 def load_list(self):
934 k = self.marker()
935 self.stack[k:] = [self.stack[k+1:]]
936 dispatch[LIST] = load_list
937
938 def load_dict(self):
939 k = self.marker()
940 d = {}
941 items = self.stack[k+1:]
942 for i in range(0, len(items), 2):
943 key = items[i]
944 value = items[i+1]
945 d[key] = value
946 self.stack[k:] = [d]
947 dispatch[DICT] = load_dict
948
949 def load_inst(self):
950 k = self.marker()
951 args = tuple(self.stack[k+1:])
952 del self.stack[k:]
953 module = self.readline()[:-1]
954 name = self.readline()[:-1]
955 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000956 instantiated = 0
957 if (not args and type(klass) is ClassType and
958 not hasattr(klass, "__getinitargs__")):
959 try:
960 value = _EmptyClass()
961 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000962 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000963 except RuntimeError:
964 # In restricted execution, assignment to inst.__class__ is
965 # prohibited
966 pass
967 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000968 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000969 if not hasattr(klass, '__safe_for_unpickling__'):
970 raise UnpicklingError('%s is not safe for unpickling' %
971 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000972 value = apply(klass, args)
973 except TypeError, err:
974 raise TypeError, "in constructor for %s: %s" % (
975 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000976 self.append(value)
977 dispatch[INST] = load_inst
978
979 def load_obj(self):
980 stack = self.stack
981 k = self.marker()
982 klass = stack[k + 1]
983 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000984 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000985 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000986 instantiated = 0
987 if (not args and type(klass) is ClassType and
988 not hasattr(klass, "__getinitargs__")):
989 try:
990 value = _EmptyClass()
991 value.__class__ = klass
992 instantiated = 1
993 except RuntimeError:
994 # In restricted execution, assignment to inst.__class__ is
995 # prohibited
996 pass
997 if not instantiated:
998 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000999 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +00001000 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001001
Guido van Rossum3a41c612003-01-28 15:10:22 +00001002 def load_newobj(self):
1003 args = self.stack.pop()
1004 cls = self.stack[-1]
1005 obj = cls.__new__(cls, *args)
1006 self.stack[-1:] = obj
1007 dispatch[NEWOBJ] = load_newobj
1008
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001009 def load_global(self):
1010 module = self.readline()[:-1]
1011 name = self.readline()[:-1]
1012 klass = self.find_class(module, name)
1013 self.append(klass)
1014 dispatch[GLOBAL] = load_global
1015
1016 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +00001017 __import__(module)
1018 mod = sys.modules[module]
1019 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001020 return klass
1021
1022 def load_reduce(self):
1023 stack = self.stack
1024
1025 callable = stack[-2]
1026 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001027 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001028
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001029 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +00001030 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001031 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001032 safe = callable.__safe_for_unpickling__
1033 except AttributeError:
1034 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +00001035
Tim Petersc32d8242001-04-10 02:48:53 +00001036 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +00001037 raise UnpicklingError, "%s is not safe for " \
1038 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +00001039
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001040 if arg_tup is None:
Guido van Rossumbc64e222003-01-28 16:34:19 +00001041 # A hack for Jim Fulton's ExtensionClass, now deprecated
1042 warnings.warn("__basicnew__ special case is deprecated",
Tim Peters8ac14952002-05-23 15:15:30 +00001043 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001044 value = callable.__basicnew__()
1045 else:
1046 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001047 self.append(value)
1048 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001049
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001050 def load_pop(self):
1051 del self.stack[-1]
1052 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001053
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001054 def load_pop_mark(self):
1055 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001056 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001057 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001058
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001059 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001060 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001061 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001062
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001063 def load_get(self):
1064 self.append(self.memo[self.readline()[:-1]])
1065 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001066
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001067 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001068 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001069 self.append(self.memo[`i`])
1070 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001071
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001072 def load_long_binget(self):
1073 i = mloads('i' + self.read(4))
1074 self.append(self.memo[`i`])
1075 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001076
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001077 def load_put(self):
1078 self.memo[self.readline()[:-1]] = self.stack[-1]
1079 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001080
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001081 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001082 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001083 self.memo[`i`] = self.stack[-1]
1084 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001085
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001086 def load_long_binput(self):
1087 i = mloads('i' + self.read(4))
1088 self.memo[`i`] = self.stack[-1]
1089 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001090
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001091 def load_append(self):
1092 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001093 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001094 list = stack[-1]
1095 list.append(value)
1096 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001097
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001098 def load_appends(self):
1099 stack = self.stack
1100 mark = self.marker()
1101 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001102 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001103 del stack[mark:]
1104 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001105
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001106 def load_setitem(self):
1107 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001108 value = stack.pop()
1109 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001110 dict = stack[-1]
1111 dict[key] = value
1112 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001113
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001114 def load_setitems(self):
1115 stack = self.stack
1116 mark = self.marker()
1117 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001118 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001119 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001120
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001121 del stack[mark:]
1122 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001123
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001124 def load_build(self):
1125 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001126 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001127 inst = stack[-1]
1128 try:
1129 setstate = inst.__setstate__
1130 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001131 try:
1132 inst.__dict__.update(value)
1133 except RuntimeError:
1134 # XXX In restricted execution, the instance's __dict__ is not
1135 # accessible. Use the old way of unpickling the instance
1136 # variables. This is a semantic different when unpickling in
1137 # restricted vs. unrestricted modes.
1138 for k, v in value.items():
1139 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001140 else:
1141 setstate(value)
1142 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001143
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001144 def load_mark(self):
1145 self.append(self.mark)
1146 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001147
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001148 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001149 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001150 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001151 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001152
Guido van Rossume467be61997-12-05 19:42:42 +00001153# Helper class for load_inst/load_obj
1154
1155class _EmptyClass:
1156 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001157
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001158# Encode/decode longs.
1159
1160def encode_long(x):
1161 r"""Encode a long to a two's complement little-ending binary string.
1162 >>> encode_long(255L)
1163 '\xff\x00'
1164 >>> encode_long(32767L)
1165 '\xff\x7f'
1166 >>> encode_long(-256L)
1167 '\x00\xff'
1168 >>> encode_long(-32768L)
1169 '\x00\x80'
1170 >>> encode_long(-128L)
1171 '\x80'
1172 >>> encode_long(127L)
1173 '\x7f'
1174 >>>
1175 """
1176 digits = []
1177 while not -128 <= x < 128:
1178 digits.append(x & 0xff)
1179 x >>= 8
1180 digits.append(x & 0xff)
1181 return "".join(map(chr, digits))
1182
1183def decode_long(data):
1184 r"""Decode a long from a two's complement little-endian binary string.
1185 >>> decode_long("\xff\x00")
1186 255L
1187 >>> decode_long("\xff\x7f")
1188 32767L
1189 >>> decode_long("\x00\xff")
1190 -256L
1191 >>> decode_long("\x00\x80")
1192 -32768L
1193 >>> decode_long("\x80")
1194 -128L
1195 >>> decode_long("\x7f")
1196 127L
1197 """
1198 x = 0L
1199 i = 0L
1200 for c in data:
1201 x |= long(ord(c)) << i
1202 i += 8L
1203 if data and ord(c) >= 0x80:
1204 x -= 1L << i
1205 return x
1206
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001207# Shorthands
1208
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001209try:
1210 from cStringIO import StringIO
1211except ImportError:
1212 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001213
Guido van Rossum3a41c612003-01-28 15:10:22 +00001214def dump(obj, file, proto=1):
1215 Pickler(file, proto).dump(obj)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001216
Guido van Rossum3a41c612003-01-28 15:10:22 +00001217def dumps(obj, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001218 file = StringIO()
Guido van Rossum3a41c612003-01-28 15:10:22 +00001219 Pickler(file, proto).dump(obj)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001220 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001221
1222def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001223 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001224
1225def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001226 file = StringIO(str)
1227 return Unpickler(file).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001228
1229# Doctest
1230
1231def _test():
1232 import doctest
1233 return doctest.testmod()
1234
1235if __name__ == "__main__":
1236 _test()