blob: 62f7a5849a725614a90c8e73888e8bd276f4336e [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 Rossuma48061a1995-01-10 00:31:14 +000035
Skip Montanaro352674d2001-02-07 23:14:30 +000036__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
37 "Unpickler", "dump", "dumps", "load", "loads"]
38
Guido van Rossumf29d3d62003-01-27 22:47:53 +000039# These are purely informational; no code usues these
40format_version = "2.0" # File format version we write
41compatible_formats = ["1.0", # Original protocol 0
42 "1.1", # Protocol 0 with class supprt added
43 "1.2", # Original protocol 1
44 "1.3", # Protocol 1 with BINFLOAT added
45 "2.0", # Protocol 2
46 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000047
48mdumps = marshal.dumps
49mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000050
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000051class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000052 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000053 pass
54
55class PicklingError(PickleError):
56 """This exception is raised when an unpicklable object is passed to the
57 dump() method.
58
59 """
60 pass
61
62class UnpicklingError(PickleError):
63 """This exception is raised when there is a problem unpickling an object,
64 such as a security violation.
65
66 Note that other exceptions may also be raised during unpickling, including
67 (but not necessarily limited to) AttributeError, EOFError, ImportError,
68 and IndexError.
69
70 """
71 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000072
Guido van Rossumff871742000-12-13 18:11:56 +000073class _Stop(Exception):
74 def __init__(self, value):
75 self.value = value
76
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000077try:
78 from org.python.core import PyStringMap
79except ImportError:
80 PyStringMap = None
81
Guido van Rossumdbb718f2001-09-21 19:22:34 +000082try:
83 UnicodeType
84except NameError:
85 UnicodeType = None
86
Tim Peters22a449a2003-01-27 20:16:36 +000087# Pickle opcodes. See pickletools.py for extensive docs. The listing
88# here is in kind-of alphabetical order of 1-character pickle code.
89# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000090
Tim Peters22a449a2003-01-27 20:16:36 +000091MARK = '(' # push special markobject on stack
92STOP = '.' # every pickle ends with STOP
93POP = '0' # discard topmost stack item
94POP_MARK = '1' # discard stack top through topmost markobject
95DUP = '2' # duplicate top stack item
96FLOAT = 'F' # push float object; decimal string argument
97INT = 'I' # push integer or bool; decimal string argument
98BININT = 'J' # push four-byte signed int
99BININT1 = 'K' # push 1-byte unsigned int
100LONG = 'L' # push long; decimal string argument
101BININT2 = 'M' # push 2-byte unsigned int
102NONE = 'N' # push None
103PERSID = 'P' # push persistent object; id is taken from string arg
104BINPERSID = 'Q' # " " " ; " " " " stack
105REDUCE = 'R' # apply callable to argtuple, both on stack
106STRING = 'S' # push string; NL-terminated string argument
107BINSTRING = 'T' # push string; counted binary string argument
108SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
109UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
110BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
111APPEND = 'a' # append stack top to list below it
112BUILD = 'b' # call __setstate__ or __dict__.update()
113GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
114DICT = 'd' # build a dict from stack items
115EMPTY_DICT = '}' # push empty dict
116APPENDS = 'e' # extend list on stack by topmost stack slice
117GET = 'g' # push item from memo on stack; index is string arg
118BINGET = 'h' # " " " " " " ; " " 1-byte arg
119INST = 'i' # build & push class instance
120LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
121LIST = 'l' # build list from topmost stack items
122EMPTY_LIST = ']' # push empty list
123OBJ = 'o' # build & push class instance
124PUT = 'p' # store stack top in memo; index is string arg
125BINPUT = 'q' # " " " " " ; " " 1-byte arg
126LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
127SETITEM = 's' # add key+value pair to dict
128TUPLE = 't' # build tuple from topmost stack items
129EMPTY_TUPLE = ')' # push empty tuple
130SETITEMS = 'u' # modify dict by adding topmost key+value pairs
131BINFLOAT = 'G' # push float; arg is 8-byte float encoding
132
133TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
134FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000135
Tim Peterse1054782003-01-28 00:22:12 +0000136# Protocol 2 (not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000137
Tim Peterse1054782003-01-28 00:22:12 +0000138PROTO = '\x80' # identify pickle protocol
139NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
140EXT1 = '\x82' # push object from extension registry; 1-byte index
141EXT2 = '\x83' # ditto, but 2-byte index
142EXT4 = '\x84' # ditto, but 4-byte index
143TUPLE1 = '\x85' # build 1-tuple from stack top
144TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
145TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
146NEWTRUE = '\x88' # push True
147NEWFALSE = '\x89' # push False
148LONG1 = '\x8a' # push long from < 256 bytes
149LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000150
Guido van Rossuma48061a1995-01-10 00:31:14 +0000151
Skip Montanaro23bafc62001-02-18 03:10:09 +0000152__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000153del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000154
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000155_quotes = ["'", '"']
156
Guido van Rossuma48061a1995-01-10 00:31:14 +0000157class Pickler:
158
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000159 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000160 """This takes a file-like object for writing a pickle data stream.
161
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000162 The optional proto argument tells the pickler to use the given
163 protocol; supported protocols are 0, 1, 2. The default
164 protocol is 1 (in previous Python versions the default was 0).
165
166 Protocol 1 is more efficient than protocol 0; protocol 2 is
167 more efficient than protocol 1. Protocol 2 is not the default
168 because it is not supported by older Python versions.
169
170 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000171
172 The file parameter must have a write() method that accepts a single
173 string argument. It can thus be an open file object, a StringIO
174 object, or any other custom object that meets this interface.
175
176 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000177 self.write = file.write
178 self.memo = {}
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000179 self.proto = proto
180 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000181
Fred Drake7f781c92002-05-01 20:33:53 +0000182 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000183 """Clears the pickler's "memo".
184
185 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000186 pickler has already seen, so that shared or recursive objects are
187 pickled by reference and not by value. This method is useful when
188 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000189
190 """
Fred Drake7f781c92002-05-01 20:33:53 +0000191 self.memo.clear()
192
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000193 def dump(self, object):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000194 """Write a pickled representation of object to the open file object.
195
196 Either the binary or ASCII format will be used, depending on the
197 value of the bin flag passed to the constructor.
198
199 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000200 self.save(object)
201 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000202
Jeremy Hylton3422c992003-01-24 19:29:52 +0000203 def memoize(self, obj):
204 """Store an object in the memo."""
205
Tim Peterse46b73f2003-01-27 21:22:10 +0000206 # The Pickler memo is a dictionary mapping object ids to 2-tuples
207 # that contain the Unpickler memo key and the object being memoized.
208 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000209 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000210 # Pickler memo so that transient objects are kept alive during
211 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000212
Tim Peterse46b73f2003-01-27 21:22:10 +0000213 # The use of the Unpickler memo length as the memo key is just a
214 # convention. The only requirement is that the memo values be unique.
215 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000216 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000217 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000218 memo_len = len(self.memo)
219 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000220 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000221
Tim Petersbb38e302003-01-27 21:25:41 +0000222 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000223 def put(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000224 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000225 s = mdumps(i)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000226 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000227 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000228
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000229 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000230
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000231 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000232
Tim Petersbb38e302003-01-27 21:25:41 +0000233 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000234 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000235 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000236 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Tim Petersc32d8242001-04-10 02:48:53 +0000238 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000239 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000240
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000241 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000242
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000243 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000244
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000245 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000246 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000247
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000248 pid = self.persistent_id(object)
249 if pid is not None:
250 self.save_pers(pid)
251 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000252
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000253 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000254
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000255 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000256
Tim Petersc9d7c4a2003-01-28 00:43:26 +0000257 # XXX Why are tuples a special case here?
Tim Petersc32d8242001-04-10 02:48:53 +0000258 if (t is TupleType) and (len(object) == 0):
259 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000260 self.save_empty_tuple(object)
261 else:
262 self.save_tuple(object)
263 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000264
Raymond Hettinger54f02222002-06-01 14:18:47 +0000265 if d in memo:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000266 self.write(self.get(memo[d][0]))
267 return
268
269 try:
270 f = self.dispatch[t]
271 except KeyError:
Tim Petersb32a8312003-01-28 00:48:09 +0000272 pass
273 else:
274 f(self, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000275 return
276
Tim Petersb32a8312003-01-28 00:48:09 +0000277 # The dispatch table doesn't know about type t.
278 try:
279 issc = issubclass(t, TypeType)
280 except TypeError: # t is not a class
281 issc = 0
282 if issc:
283 self.save_global(object)
284 return
285
286 try:
287 reduce = dispatch_table[t]
288 except KeyError:
289 try:
290 reduce = object.__reduce__
291 except AttributeError:
292 raise PicklingError, \
293 "can't pickle %s object: %s" % (`t.__name__`,
294 `object`)
295 else:
296 tup = reduce()
297 else:
298 tup = reduce(object)
299
300 if type(tup) is StringType:
301 self.save_global(object, tup)
302 return
303
304 if type(tup) is not TupleType:
305 raise PicklingError, "Value returned by %s must be a " \
306 "tuple" % reduce
307
308 l = len(tup)
309
310 if (l != 2) and (l != 3):
311 raise PicklingError, "tuple returned by %s must contain " \
312 "only two or three elements" % reduce
313
314 callable = tup[0]
315 arg_tup = tup[1]
316
317 if l > 2:
318 state = tup[2]
319 else:
320 state = None
321
322 if type(arg_tup) is not TupleType and arg_tup is not None:
323 raise PicklingError, "Second element of tuple returned " \
324 "by %s must be a tuple" % reduce
325
326 self.save_reduce(callable, arg_tup, state)
Tim Peters518df0d2003-01-28 01:00:38 +0000327 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000328
329 def persistent_id(self, object):
330 return None
331
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000332 def save_pers(self, pid):
Tim Petersbd1cdb92003-01-28 01:03:10 +0000333 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000334 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000336 else:
337 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000338
Jeremy Hylton3422c992003-01-24 19:29:52 +0000339 def save_reduce(self, acallable, arg_tup, state = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000340 write = self.write
341 save = self.save
342
Jeremy Hylton3422c992003-01-24 19:29:52 +0000343 if not callable(acallable):
344 raise PicklingError("__reduce__() must return callable as "
345 "first argument, not %s" % `acallable`)
Tim Peters22a449a2003-01-27 20:16:36 +0000346
Jeremy Hylton3422c992003-01-24 19:29:52 +0000347 save(acallable)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000348 save(arg_tup)
349 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000350
Tim Petersc32d8242001-04-10 02:48:53 +0000351 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000352 save(state)
353 write(BUILD)
354
355 dispatch = {}
356
357 def save_none(self, object):
358 self.write(NONE)
359 dispatch[NoneType] = save_none
360
Guido van Rossum77f6a652002-04-03 22:41:51 +0000361 def save_bool(self, object):
Tim Peters22987e32003-01-28 00:26:14 +0000362 self.write(object and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000363 dispatch[bool] = save_bool
364
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000365 def save_int(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000366 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000367 # If the int is small enough to fit in a signed 4-byte 2's-comp
368 # format, we can store it more efficiently than the general
369 # case.
370 high_bits = object >> 31 # note that Python shift sign-extends
371 if high_bits == 0 or high_bits == -1:
372 # All high bits are copies of bit 2**31, so the value
373 # fits in a 4-byte signed int.
374 i = mdumps(object)[1:]
375 assert len(i) == 4
376 if i[-2:] == '\000\000': # fits in 2-byte unsigned int
377 if i[-3] == '\000': # fits in 1-byte unsigned int
378 self.write(BININT1 + i[0])
379 else:
380 self.write(BININT2 + i[:2])
381 else:
382 self.write(BININT + i)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000383 return
Tim Peters44714002001-04-10 05:02:52 +0000384 # Text pickle, or int too big to fit in signed 4-byte format.
385 self.write(INT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000386 dispatch[IntType] = save_int
387
388 def save_long(self, object):
389 self.write(LONG + `object` + '\n')
390 dispatch[LongType] = save_long
391
Guido van Rossumd3703791998-10-22 20:15:36 +0000392 def save_float(self, object, pack=struct.pack):
393 if self.bin:
394 self.write(BINFLOAT + pack('>d', object))
395 else:
396 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000397 dispatch[FloatType] = save_float
398
399 def save_string(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000400 if self.bin:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000401 n = len(object)
402 if n < 256:
403 self.write(SHORT_BINSTRING + chr(n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000404 else:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000405 self.write(BINSTRING + mdumps(n)[1:] + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 else:
407 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000408 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000409 dispatch[StringType] = save_string
410
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000411 def save_unicode(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000412 if self.bin:
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000413 encoding = object.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000414 n = len(encoding)
415 s = mdumps(n)[1:]
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000416 self.write(BINUNICODE + s + encoding)
417 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000418 object = object.replace("\\", "\\u005c")
419 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000420 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000421 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000422 dispatch[UnicodeType] = save_unicode
423
Guido van Rossum31584cb2001-01-22 14:53:29 +0000424 if StringType == UnicodeType:
425 # This is true for Jython
426 def save_string(self, object):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000427 unicode = object.isunicode()
428
Tim Petersc32d8242001-04-10 02:48:53 +0000429 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000430 if unicode:
431 object = object.encode("utf-8")
432 l = len(object)
433 s = mdumps(l)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000434 if l < 256 and not unicode:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000435 self.write(SHORT_BINSTRING + s[0] + object)
436 else:
437 if unicode:
438 self.write(BINUNICODE + s + object)
439 else:
440 self.write(BINSTRING + s + object)
441 else:
Tim Peters658cba62001-02-09 20:06:00 +0000442 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000443 object = object.replace("\\", "\\u005c")
444 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000445 object = object.encode('raw-unicode-escape')
446 self.write(UNICODE + object + '\n')
447 else:
448 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000449 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000450 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000451
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000452 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000453 write = self.write
454 save = self.save
455 memo = self.memo
456
457 d = id(object)
458
459 write(MARK)
460
461 for element in object:
462 save(element)
463
Raymond Hettinger54f02222002-06-01 14:18:47 +0000464 if len(object) and d in memo:
Tim Petersc32d8242001-04-10 02:48:53 +0000465 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000466 write(POP_MARK + self.get(memo[d][0]))
467 return
Tim Peters2344fae2001-01-15 00:50:52 +0000468
Guido van Rossum599174f1998-03-31 16:30:28 +0000469 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000470 return
471
Tim Peters518df0d2003-01-28 01:00:38 +0000472 self.write(TUPLE)
473 self.memoize(object)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000474
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000475 dispatch[TupleType] = save_tuple
476
477 def save_empty_tuple(self, object):
478 self.write(EMPTY_TUPLE)
479
480 def save_list(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000481 write = self.write
482 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000483
Tim Petersc32d8242001-04-10 02:48:53 +0000484 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000485 write(EMPTY_LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000486 self.memoize(object)
487 n = len(object)
488 if n > 1:
489 write(MARK)
490 for element in object:
491 save(element)
492 write(APPENDS)
493 elif n:
494 assert n == 1
495 save(object[0])
496 write(APPEND)
497 # else the list is empty, and we're already done
498
499 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000500 write(MARK + LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000501 self.memoize(object)
502 for element in object:
503 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000504 write(APPEND)
505
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000506 dispatch[ListType] = save_list
507
508 def save_dict(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000509 write = self.write
510 save = self.save
Tim Peters064567e2003-01-28 01:34:43 +0000511 items = object.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000512
Tim Petersc32d8242001-04-10 02:48:53 +0000513 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000514 write(EMPTY_DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000515 self.memoize(object)
516 if len(object) > 1:
517 write(MARK)
518 for key, value in items:
519 save(key)
520 save(value)
521 write(SETITEMS)
522 return
523
524 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000525 write(MARK + DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000526 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000527
Tim Peters064567e2003-01-28 01:34:43 +0000528 # proto 0 or len(object) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000529 for key, value in items:
530 save(key)
531 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000532 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000533
534 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000535 if not PyStringMap is None:
536 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000537
538 def save_inst(self, object):
539 d = id(object)
540 cls = object.__class__
541
542 memo = self.memo
543 write = self.write
544 save = self.save
545
546 if hasattr(object, '__getinitargs__'):
547 args = object.__getinitargs__()
548 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000549 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000550 else:
551 args = ()
552
553 write(MARK)
554
Tim Petersc32d8242001-04-10 02:48:53 +0000555 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000556 save(cls)
557
558 for arg in args:
559 save(arg)
560
Jeremy Hylton3422c992003-01-24 19:29:52 +0000561 # This method does not use memoize() so that it can handle
562 # the special case for non-binary mode.
Tim Peters518df0d2003-01-28 01:00:38 +0000563 # XXX What did that comment mean? That is, what "special case for
Tim Petersc23d18a2003-01-28 01:41:51 +0000564 # XXX non-binary mode"? It sure *looks* like nothing special is
Tim Peters518df0d2003-01-28 01:00:38 +0000565 # XXX happening in the INST case.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000566 memo_len = len(memo)
Tim Petersc32d8242001-04-10 02:48:53 +0000567 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000568 write(OBJ + self.put(memo_len))
569 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000570 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000571 self.put(memo_len))
572
573 memo[d] = (memo_len, object)
574
575 try:
576 getstate = object.__getstate__
577 except AttributeError:
578 stuff = object.__dict__
579 else:
580 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000581 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000582 save(stuff)
583 write(BUILD)
584 dispatch[InstanceType] = save_inst
585
586 def save_global(self, object, name = None):
587 write = self.write
588 memo = self.memo
589
Tim Petersc32d8242001-04-10 02:48:53 +0000590 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000591 name = object.__name__
592
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000593 try:
594 module = object.__module__
595 except AttributeError:
596 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000597
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000598 try:
599 __import__(module)
600 mod = sys.modules[module]
601 klass = getattr(mod, name)
602 except (ImportError, KeyError, AttributeError):
603 raise PicklingError(
604 "Can't pickle %r: it's not found as %s.%s" %
605 (object, module, name))
606 else:
607 if klass is not object:
608 raise PicklingError(
609 "Can't pickle %r: it's not the same object as %s.%s" %
610 (object, module, name))
611
Tim Peters518df0d2003-01-28 01:00:38 +0000612 write(GLOBAL + module + '\n' + name + '\n')
613 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000614 dispatch[ClassType] = save_global
615 dispatch[FunctionType] = save_global
616 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000617 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000618
Guido van Rossuma48061a1995-01-10 00:31:14 +0000619
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000620def _keep_alive(x, memo):
621 """Keeps a reference to the object x in the memo.
622
623 Because we remember objects by their id, we have
624 to assure that possibly temporary objects are kept
625 alive by referencing them.
626 We store a reference at the id of the memo, which should
627 normally not be used unless someone tries to deepcopy
628 the memo itself...
629 """
630 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000631 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000632 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000633 # aha, this is the first one :-)
634 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000635
636
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000637classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000638
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000639def whichmodule(func, funcname):
640 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000641
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000642 Search sys.modules for the module.
643 Cache in classmap.
644 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000645 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000646 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000647 if func in classmap:
648 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000649
650 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000651 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000652 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000653 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000654 hasattr(module, funcname) and \
655 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000656 break
657 else:
658 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000659 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000660 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000661
662
663class Unpickler:
664
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000665 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000666 """This takes a file-like object for reading a pickle data stream.
667
668 This class automatically determines whether the data stream was
669 written in binary mode or not, so it does not need a flag as in
670 the Pickler class factory.
671
672 The file-like object must have two methods, a read() method that
673 takes an integer argument, and a readline() method that requires no
674 arguments. Both methods should return a string. Thus file-like
675 object can be a file object opened for reading, a StringIO object,
676 or any other custom object that meets this interface.
677
678 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000679 self.readline = file.readline
680 self.read = file.read
681 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000682
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000683 def load(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000684 """Read a pickled object representation from the open file object.
685
686 Return the reconstituted object hierarchy specified in the file
687 object.
688
689 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000690 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 self.stack = []
692 self.append = self.stack.append
693 read = self.read
694 dispatch = self.dispatch
695 try:
696 while 1:
697 key = read(1)
698 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000699 except _Stop, stopinst:
700 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000701
Tim Petersc23d18a2003-01-28 01:41:51 +0000702 # Return largest index k such that self.stack[k] is self.mark.
703 # If the stack doesn't contain a mark, eventually raises IndexError.
704 # This could be sped by maintaining another stack, of indices at which
705 # the mark appears. For that matter, the latter stack would suffice,
706 # and we wouldn't need to push mark objects on self.stack at all.
707 # Doing so is probably a good thing, though, since if the pickle is
708 # corrupt (or hostile) we may get a clue from finding self.mark embedded
709 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000710 def marker(self):
711 stack = self.stack
712 mark = self.mark
713 k = len(stack)-1
714 while stack[k] is not mark: k = k-1
715 return k
716
717 dispatch = {}
718
719 def load_eof(self):
720 raise EOFError
721 dispatch[''] = load_eof
722
723 def load_persid(self):
724 pid = self.readline()[:-1]
725 self.append(self.persistent_load(pid))
726 dispatch[PERSID] = load_persid
727
728 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000729 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000730 self.append(self.persistent_load(pid))
731 dispatch[BINPERSID] = load_binpersid
732
733 def load_none(self):
734 self.append(None)
735 dispatch[NONE] = load_none
736
737 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000738 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000739 if data == FALSE[1:]:
740 val = False
741 elif data == TRUE[1:]:
742 val = True
743 else:
744 try:
745 val = int(data)
746 except ValueError:
747 val = long(data)
748 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000749 dispatch[INT] = load_int
750
751 def load_binint(self):
752 self.append(mloads('i' + self.read(4)))
753 dispatch[BININT] = load_binint
754
755 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000756 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000757 dispatch[BININT1] = load_binint1
758
759 def load_binint2(self):
760 self.append(mloads('i' + self.read(2) + '\000\000'))
761 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000762
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000763 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000764 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 dispatch[LONG] = load_long
766
767 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000768 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769 dispatch[FLOAT] = load_float
770
Guido van Rossumd3703791998-10-22 20:15:36 +0000771 def load_binfloat(self, unpack=struct.unpack):
772 self.append(unpack('>d', self.read(8))[0])
773 dispatch[BINFLOAT] = load_binfloat
774
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000775 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000776 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000777 for q in _quotes:
778 if rep.startswith(q):
779 if not rep.endswith(q):
780 raise ValueError, "insecure string pickle"
781 rep = rep[len(q):-len(q)]
782 break
783 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000784 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000785 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000786 dispatch[STRING] = load_string
787
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000788 def _is_string_secure(self, s):
789 """Return true if s contains a string that is safe to eval
790
791 The definition of secure string is based on the implementation
792 in cPickle. s is secure as long as it only contains a quoted
793 string and optional trailing whitespace.
794 """
795 q = s[0]
796 if q not in ("'", '"'):
797 return 0
798 # find the closing quote
799 offset = 1
800 i = None
801 while 1:
802 try:
803 i = s.index(q, offset)
804 except ValueError:
805 # if there is an error the first time, there is no
806 # close quote
807 if offset == 1:
808 return 0
809 if s[i-1] != '\\':
810 break
811 # check to see if this one is escaped
812 nslash = 0
813 j = i - 1
814 while j >= offset and s[j] == '\\':
815 j = j - 1
816 nslash = nslash + 1
817 if nslash % 2 == 0:
818 break
819 offset = i + 1
820 for c in s[i+1:]:
821 if ord(c) > 32:
822 return 0
823 return 1
824
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000825 def load_binstring(self):
826 len = mloads('i' + self.read(4))
827 self.append(self.read(len))
828 dispatch[BINSTRING] = load_binstring
829
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000830 def load_unicode(self):
831 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
832 dispatch[UNICODE] = load_unicode
833
834 def load_binunicode(self):
835 len = mloads('i' + self.read(4))
836 self.append(unicode(self.read(len),'utf-8'))
837 dispatch[BINUNICODE] = load_binunicode
838
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000839 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000840 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000841 self.append(self.read(len))
842 dispatch[SHORT_BINSTRING] = load_short_binstring
843
844 def load_tuple(self):
845 k = self.marker()
846 self.stack[k:] = [tuple(self.stack[k+1:])]
847 dispatch[TUPLE] = load_tuple
848
849 def load_empty_tuple(self):
850 self.stack.append(())
851 dispatch[EMPTY_TUPLE] = load_empty_tuple
852
853 def load_empty_list(self):
854 self.stack.append([])
855 dispatch[EMPTY_LIST] = load_empty_list
856
857 def load_empty_dictionary(self):
858 self.stack.append({})
859 dispatch[EMPTY_DICT] = load_empty_dictionary
860
861 def load_list(self):
862 k = self.marker()
863 self.stack[k:] = [self.stack[k+1:]]
864 dispatch[LIST] = load_list
865
866 def load_dict(self):
867 k = self.marker()
868 d = {}
869 items = self.stack[k+1:]
870 for i in range(0, len(items), 2):
871 key = items[i]
872 value = items[i+1]
873 d[key] = value
874 self.stack[k:] = [d]
875 dispatch[DICT] = load_dict
876
877 def load_inst(self):
878 k = self.marker()
879 args = tuple(self.stack[k+1:])
880 del self.stack[k:]
881 module = self.readline()[:-1]
882 name = self.readline()[:-1]
883 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000884 instantiated = 0
885 if (not args and type(klass) is ClassType and
886 not hasattr(klass, "__getinitargs__")):
887 try:
888 value = _EmptyClass()
889 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000890 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000891 except RuntimeError:
892 # In restricted execution, assignment to inst.__class__ is
893 # prohibited
894 pass
895 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000896 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000897 if not hasattr(klass, '__safe_for_unpickling__'):
898 raise UnpicklingError('%s is not safe for unpickling' %
899 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000900 value = apply(klass, args)
901 except TypeError, err:
902 raise TypeError, "in constructor for %s: %s" % (
903 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000904 self.append(value)
905 dispatch[INST] = load_inst
906
907 def load_obj(self):
908 stack = self.stack
909 k = self.marker()
910 klass = stack[k + 1]
911 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000912 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000913 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000914 instantiated = 0
915 if (not args and type(klass) is ClassType and
916 not hasattr(klass, "__getinitargs__")):
917 try:
918 value = _EmptyClass()
919 value.__class__ = klass
920 instantiated = 1
921 except RuntimeError:
922 # In restricted execution, assignment to inst.__class__ is
923 # prohibited
924 pass
925 if not instantiated:
926 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000927 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000928 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000929
930 def load_global(self):
931 module = self.readline()[:-1]
932 name = self.readline()[:-1]
933 klass = self.find_class(module, name)
934 self.append(klass)
935 dispatch[GLOBAL] = load_global
936
937 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +0000938 __import__(module)
939 mod = sys.modules[module]
940 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000941 return klass
942
943 def load_reduce(self):
944 stack = self.stack
945
946 callable = stack[-2]
947 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000948 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000949
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000950 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000951 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000952 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000953 safe = callable.__safe_for_unpickling__
954 except AttributeError:
955 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000956
Tim Petersc32d8242001-04-10 02:48:53 +0000957 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +0000958 raise UnpicklingError, "%s is not safe for " \
959 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000960
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000961 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +0000962 import warnings
963 warnings.warn("The None return argument form of __reduce__ is "
964 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +0000965 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000966 value = callable.__basicnew__()
967 else:
968 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000969 self.append(value)
970 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000971
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000972 def load_pop(self):
973 del self.stack[-1]
974 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000975
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000976 def load_pop_mark(self):
977 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000978 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000979 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000980
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000981 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000982 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000983 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000984
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000985 def load_get(self):
986 self.append(self.memo[self.readline()[:-1]])
987 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000988
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000989 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000990 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000991 self.append(self.memo[`i`])
992 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000993
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000994 def load_long_binget(self):
995 i = mloads('i' + self.read(4))
996 self.append(self.memo[`i`])
997 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000998
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000999 def load_put(self):
1000 self.memo[self.readline()[:-1]] = self.stack[-1]
1001 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001002
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001003 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001004 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001005 self.memo[`i`] = self.stack[-1]
1006 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001007
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001008 def load_long_binput(self):
1009 i = mloads('i' + self.read(4))
1010 self.memo[`i`] = self.stack[-1]
1011 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001012
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001013 def load_append(self):
1014 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001015 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001016 list = stack[-1]
1017 list.append(value)
1018 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001019
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001020 def load_appends(self):
1021 stack = self.stack
1022 mark = self.marker()
1023 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001024 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001025 del stack[mark:]
1026 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001027
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001028 def load_setitem(self):
1029 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001030 value = stack.pop()
1031 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001032 dict = stack[-1]
1033 dict[key] = value
1034 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001035
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001036 def load_setitems(self):
1037 stack = self.stack
1038 mark = self.marker()
1039 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001040 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001041 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001042
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001043 del stack[mark:]
1044 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001045
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001046 def load_build(self):
1047 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001048 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001049 inst = stack[-1]
1050 try:
1051 setstate = inst.__setstate__
1052 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001053 try:
1054 inst.__dict__.update(value)
1055 except RuntimeError:
1056 # XXX In restricted execution, the instance's __dict__ is not
1057 # accessible. Use the old way of unpickling the instance
1058 # variables. This is a semantic different when unpickling in
1059 # restricted vs. unrestricted modes.
1060 for k, v in value.items():
1061 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001062 else:
1063 setstate(value)
1064 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001065
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001066 def load_mark(self):
1067 self.append(self.mark)
1068 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001069
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001070 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001071 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001072 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001073 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001074
Guido van Rossume467be61997-12-05 19:42:42 +00001075# Helper class for load_inst/load_obj
1076
1077class _EmptyClass:
1078 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001079
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001080# Shorthands
1081
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001082try:
1083 from cStringIO import StringIO
1084except ImportError:
1085 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001086
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001087def dump(object, file, proto=1):
1088 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001089
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001090def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001091 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001092 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001093 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001094
1095def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001096 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001097
1098def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001099 file = StringIO(str)
1100 return Unpickler(file).load()