blob: 4e754169d1d87b95a5a45e0749f212e5966a2a99 [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 d = id(obj)
219 memo_len = len(self.memo)
220 self.write(self.put(memo_len))
221 self.memo[d] = memo_len, obj
222
Tim Petersbb38e302003-01-27 21:25:41 +0000223 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000224 def put(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000225 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000226 s = mdumps(i)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000227 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000228 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000229
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000230 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000231
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000232 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000233
Tim Petersbb38e302003-01-27 21:25:41 +0000234 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000235 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000236 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000237 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000238
Tim Petersc32d8242001-04-10 02:48:53 +0000239 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000240 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000241
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000242 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000243
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000244 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000245
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000246 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000247 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000248
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000249 pid = self.persistent_id(object)
250 if pid is not None:
251 self.save_pers(pid)
252 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000253
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000254 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000255
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000256 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000257
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:
Guido van Rossum85ee4912002-03-26 00:51:56 +0000272 try:
273 issc = issubclass(t, TypeType)
274 except TypeError: # t is not a class
275 issc = 0
276 if issc:
Guido van Rossumf048a8f2001-12-19 16:55:02 +0000277 self.save_global(object)
278 return
279
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000280 try:
281 reduce = dispatch_table[t]
282 except KeyError:
283 try:
284 reduce = object.__reduce__
285 except AttributeError:
286 raise PicklingError, \
Guido van Rossum08a92cb1999-10-10 21:14:25 +0000287 "can't pickle %s object: %s" % (`t.__name__`,
288 `object`)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000289 else:
290 tup = reduce()
291 else:
292 tup = reduce(object)
293
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000294 if type(tup) is StringType:
295 self.save_global(object, tup)
296 return
Guido van Rossumd1f49841997-12-10 23:40:18 +0000297
Tim Petersc32d8242001-04-10 02:48:53 +0000298 if type(tup) is not TupleType:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000299 raise PicklingError, "Value returned by %s must be a " \
300 "tuple" % reduce
301
302 l = len(tup)
Tim Peters2344fae2001-01-15 00:50:52 +0000303
Tim Petersc32d8242001-04-10 02:48:53 +0000304 if (l != 2) and (l != 3):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000305 raise PicklingError, "tuple returned by %s must contain " \
306 "only two or three elements" % reduce
307
308 callable = tup[0]
309 arg_tup = tup[1]
Tim Peters2344fae2001-01-15 00:50:52 +0000310
Tim Petersc32d8242001-04-10 02:48:53 +0000311 if l > 2:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000312 state = tup[2]
313 else:
314 state = None
315
Guido van Rossumd1f49841997-12-10 23:40:18 +0000316 if type(arg_tup) is not TupleType and arg_tup is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000317 raise PicklingError, "Second element of tuple returned " \
318 "by %s must be a tuple" % reduce
319
Tim Peters2344fae2001-01-15 00:50:52 +0000320 self.save_reduce(callable, arg_tup, state)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000321 memo_len = len(memo)
322 self.write(self.put(memo_len))
323 memo[d] = (memo_len, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000324 return
325
326 f(self, object)
327
328 def persistent_id(self, object):
329 return None
330
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000331 def save_pers(self, pid):
Tim Petersc32d8242001-04-10 02:48:53 +0000332 if not self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000333 self.write(PERSID + str(pid) + '\n')
334 else:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000335 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000336 self.write(BINPERSID)
337
Jeremy Hylton3422c992003-01-24 19:29:52 +0000338 def save_reduce(self, acallable, arg_tup, state = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000339 write = self.write
340 save = self.save
341
Jeremy Hylton3422c992003-01-24 19:29:52 +0000342 if not callable(acallable):
343 raise PicklingError("__reduce__() must return callable as "
344 "first argument, not %s" % `acallable`)
Tim Peters22a449a2003-01-27 20:16:36 +0000345
Jeremy Hylton3422c992003-01-24 19:29:52 +0000346 save(acallable)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000347 save(arg_tup)
348 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000349
Tim Petersc32d8242001-04-10 02:48:53 +0000350 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000351 save(state)
352 write(BUILD)
353
354 dispatch = {}
355
356 def save_none(self, object):
357 self.write(NONE)
358 dispatch[NoneType] = save_none
359
Guido van Rossum77f6a652002-04-03 22:41:51 +0000360 def save_bool(self, object):
361 if object:
362 self.write(TRUE)
363 else:
364 self.write(FALSE)
365 dispatch[bool] = save_bool
366
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000367 def save_int(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000368 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000369 # If the int is small enough to fit in a signed 4-byte 2's-comp
370 # format, we can store it more efficiently than the general
371 # case.
372 high_bits = object >> 31 # note that Python shift sign-extends
373 if high_bits == 0 or high_bits == -1:
374 # All high bits are copies of bit 2**31, so the value
375 # fits in a 4-byte signed int.
376 i = mdumps(object)[1:]
377 assert len(i) == 4
378 if i[-2:] == '\000\000': # fits in 2-byte unsigned int
379 if i[-3] == '\000': # fits in 1-byte unsigned int
380 self.write(BININT1 + i[0])
381 else:
382 self.write(BININT2 + i[:2])
383 else:
384 self.write(BININT + i)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000385 return
Tim Peters44714002001-04-10 05:02:52 +0000386 # Text pickle, or int too big to fit in signed 4-byte format.
387 self.write(INT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000388 dispatch[IntType] = save_int
389
390 def save_long(self, object):
391 self.write(LONG + `object` + '\n')
392 dispatch[LongType] = save_long
393
Guido van Rossumd3703791998-10-22 20:15:36 +0000394 def save_float(self, object, pack=struct.pack):
395 if self.bin:
396 self.write(BINFLOAT + pack('>d', object))
397 else:
398 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000399 dispatch[FloatType] = save_float
400
401 def save_string(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000402 if self.bin:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000403 n = len(object)
404 if n < 256:
405 self.write(SHORT_BINSTRING + chr(n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 else:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000407 self.write(BINSTRING + mdumps(n)[1:] + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000408 else:
409 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000410 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000411 dispatch[StringType] = save_string
412
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000413 def save_unicode(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000414 if self.bin:
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000415 encoding = object.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000416 n = len(encoding)
417 s = mdumps(n)[1:]
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000418 self.write(BINUNICODE + s + encoding)
419 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000420 object = object.replace("\\", "\\u005c")
421 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000422 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000423 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000424 dispatch[UnicodeType] = save_unicode
425
Guido van Rossum31584cb2001-01-22 14:53:29 +0000426 if StringType == UnicodeType:
427 # This is true for Jython
428 def save_string(self, object):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000429 unicode = object.isunicode()
430
Tim Petersc32d8242001-04-10 02:48:53 +0000431 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000432 if unicode:
433 object = object.encode("utf-8")
434 l = len(object)
435 s = mdumps(l)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000436 if l < 256 and not unicode:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000437 self.write(SHORT_BINSTRING + s[0] + object)
438 else:
439 if unicode:
440 self.write(BINUNICODE + s + object)
441 else:
442 self.write(BINSTRING + s + object)
443 else:
Tim Peters658cba62001-02-09 20:06:00 +0000444 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000445 object = object.replace("\\", "\\u005c")
446 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000447 object = object.encode('raw-unicode-escape')
448 self.write(UNICODE + object + '\n')
449 else:
450 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000451 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000452 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000453
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000454 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000455 write = self.write
456 save = self.save
457 memo = self.memo
458
459 d = id(object)
460
461 write(MARK)
462
463 for element in object:
464 save(element)
465
Raymond Hettinger54f02222002-06-01 14:18:47 +0000466 if len(object) and d in memo:
Tim Petersc32d8242001-04-10 02:48:53 +0000467 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000468 write(POP_MARK + self.get(memo[d][0]))
469 return
Tim Peters2344fae2001-01-15 00:50:52 +0000470
Guido van Rossum599174f1998-03-31 16:30:28 +0000471 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000472 return
473
474 memo_len = len(memo)
475 self.write(TUPLE + self.put(memo_len))
476 memo[d] = (memo_len, object)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000477
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000478 dispatch[TupleType] = save_tuple
479
480 def save_empty_tuple(self, object):
481 self.write(EMPTY_TUPLE)
482
483 def save_list(self, object):
484 d = id(object)
485
486 write = self.write
487 save = self.save
488 memo = self.memo
489
Tim Petersc32d8242001-04-10 02:48:53 +0000490 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000491 write(EMPTY_LIST)
492 else:
493 write(MARK + LIST)
494
Jeremy Hylton3422c992003-01-24 19:29:52 +0000495 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000496
497 using_appends = (self.bin and (len(object) > 1))
498
Tim Petersc32d8242001-04-10 02:48:53 +0000499 if using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000500 write(MARK)
501
502 for element in object:
503 save(element)
Tim Peters2344fae2001-01-15 00:50:52 +0000504
Tim Petersc32d8242001-04-10 02:48:53 +0000505 if not using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000506 write(APPEND)
507
Tim Petersc32d8242001-04-10 02:48:53 +0000508 if using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000509 write(APPENDS)
510 dispatch[ListType] = save_list
511
512 def save_dict(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000513 write = self.write
514 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000515
Tim Petersc32d8242001-04-10 02:48:53 +0000516 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000517 write(EMPTY_DICT)
518 else:
519 write(MARK + DICT)
520
Jeremy Hylton3422c992003-01-24 19:29:52 +0000521 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000522
523 using_setitems = (self.bin and (len(object) > 1))
524
Tim Petersc32d8242001-04-10 02:48:53 +0000525 if using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000526 write(MARK)
527
528 items = object.items()
529 for key, value in items:
530 save(key)
531 save(value)
532
Tim Petersc32d8242001-04-10 02:48:53 +0000533 if not using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000534 write(SETITEM)
535
Tim Petersc32d8242001-04-10 02:48:53 +0000536 if using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000537 write(SETITEMS)
538
539 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000540 if not PyStringMap is None:
541 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000542
543 def save_inst(self, object):
544 d = id(object)
545 cls = object.__class__
546
547 memo = self.memo
548 write = self.write
549 save = self.save
550
551 if hasattr(object, '__getinitargs__'):
552 args = object.__getinitargs__()
553 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000554 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000555 else:
556 args = ()
557
558 write(MARK)
559
Tim Petersc32d8242001-04-10 02:48:53 +0000560 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000561 save(cls)
562
563 for arg in args:
564 save(arg)
565
Jeremy Hylton3422c992003-01-24 19:29:52 +0000566 # This method does not use memoize() so that it can handle
567 # the special case for non-binary mode.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000568 memo_len = len(memo)
Tim Petersc32d8242001-04-10 02:48:53 +0000569 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570 write(OBJ + self.put(memo_len))
571 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000572 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000573 self.put(memo_len))
574
575 memo[d] = (memo_len, object)
576
577 try:
578 getstate = object.__getstate__
579 except AttributeError:
580 stuff = object.__dict__
581 else:
582 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000583 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000584 save(stuff)
585 write(BUILD)
586 dispatch[InstanceType] = save_inst
587
588 def save_global(self, object, name = None):
589 write = self.write
590 memo = self.memo
591
Tim Petersc32d8242001-04-10 02:48:53 +0000592 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000593 name = object.__name__
594
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000595 try:
596 module = object.__module__
597 except AttributeError:
598 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000599
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000600 try:
601 __import__(module)
602 mod = sys.modules[module]
603 klass = getattr(mod, name)
604 except (ImportError, KeyError, AttributeError):
605 raise PicklingError(
606 "Can't pickle %r: it's not found as %s.%s" %
607 (object, module, name))
608 else:
609 if klass is not object:
610 raise PicklingError(
611 "Can't pickle %r: it's not the same object as %s.%s" %
612 (object, module, name))
613
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000614 memo_len = len(memo)
615 write(GLOBAL + module + '\n' + name + '\n' +
616 self.put(memo_len))
617 memo[id(object)] = (memo_len, object)
618 dispatch[ClassType] = save_global
619 dispatch[FunctionType] = save_global
620 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000621 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000622
Guido van Rossuma48061a1995-01-10 00:31:14 +0000623
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000624def _keep_alive(x, memo):
625 """Keeps a reference to the object x in the memo.
626
627 Because we remember objects by their id, we have
628 to assure that possibly temporary objects are kept
629 alive by referencing them.
630 We store a reference at the id of the memo, which should
631 normally not be used unless someone tries to deepcopy
632 the memo itself...
633 """
634 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000635 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000636 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000637 # aha, this is the first one :-)
638 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000639
640
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000641classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000642
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000643def whichmodule(func, funcname):
644 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000645
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000646 Search sys.modules for the module.
647 Cache in classmap.
648 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000649 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000650 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000651 if func in classmap:
652 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000653
654 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000655 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000656 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000657 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000658 hasattr(module, funcname) and \
659 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000660 break
661 else:
662 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000663 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000664 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000665
666
667class Unpickler:
668
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000669 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000670 """This takes a file-like object for reading a pickle data stream.
671
672 This class automatically determines whether the data stream was
673 written in binary mode or not, so it does not need a flag as in
674 the Pickler class factory.
675
676 The file-like object must have two methods, a read() method that
677 takes an integer argument, and a readline() method that requires no
678 arguments. Both methods should return a string. Thus file-like
679 object can be a file object opened for reading, a StringIO object,
680 or any other custom object that meets this interface.
681
682 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000683 self.readline = file.readline
684 self.read = file.read
685 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000686
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000687 def load(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000688 """Read a pickled object representation from the open file object.
689
690 Return the reconstituted object hierarchy specified in the file
691 object.
692
693 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000694 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000695 self.stack = []
696 self.append = self.stack.append
697 read = self.read
698 dispatch = self.dispatch
699 try:
700 while 1:
701 key = read(1)
702 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000703 except _Stop, stopinst:
704 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000705
706 def marker(self):
707 stack = self.stack
708 mark = self.mark
709 k = len(stack)-1
710 while stack[k] is not mark: k = k-1
711 return k
712
713 dispatch = {}
714
715 def load_eof(self):
716 raise EOFError
717 dispatch[''] = load_eof
718
719 def load_persid(self):
720 pid = self.readline()[:-1]
721 self.append(self.persistent_load(pid))
722 dispatch[PERSID] = load_persid
723
724 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000725 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000726 self.append(self.persistent_load(pid))
727 dispatch[BINPERSID] = load_binpersid
728
729 def load_none(self):
730 self.append(None)
731 dispatch[NONE] = load_none
732
733 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000734 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000735 if data == FALSE[1:]:
736 val = False
737 elif data == TRUE[1:]:
738 val = True
739 else:
740 try:
741 val = int(data)
742 except ValueError:
743 val = long(data)
744 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000745 dispatch[INT] = load_int
746
747 def load_binint(self):
748 self.append(mloads('i' + self.read(4)))
749 dispatch[BININT] = load_binint
750
751 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000752 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000753 dispatch[BININT1] = load_binint1
754
755 def load_binint2(self):
756 self.append(mloads('i' + self.read(2) + '\000\000'))
757 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000758
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000759 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000760 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000761 dispatch[LONG] = load_long
762
763 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000764 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 dispatch[FLOAT] = load_float
766
Guido van Rossumd3703791998-10-22 20:15:36 +0000767 def load_binfloat(self, unpack=struct.unpack):
768 self.append(unpack('>d', self.read(8))[0])
769 dispatch[BINFLOAT] = load_binfloat
770
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000771 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000772 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000773 for q in _quotes:
774 if rep.startswith(q):
775 if not rep.endswith(q):
776 raise ValueError, "insecure string pickle"
777 rep = rep[len(q):-len(q)]
778 break
779 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000780 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000781 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 dispatch[STRING] = load_string
783
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000784 def _is_string_secure(self, s):
785 """Return true if s contains a string that is safe to eval
786
787 The definition of secure string is based on the implementation
788 in cPickle. s is secure as long as it only contains a quoted
789 string and optional trailing whitespace.
790 """
791 q = s[0]
792 if q not in ("'", '"'):
793 return 0
794 # find the closing quote
795 offset = 1
796 i = None
797 while 1:
798 try:
799 i = s.index(q, offset)
800 except ValueError:
801 # if there is an error the first time, there is no
802 # close quote
803 if offset == 1:
804 return 0
805 if s[i-1] != '\\':
806 break
807 # check to see if this one is escaped
808 nslash = 0
809 j = i - 1
810 while j >= offset and s[j] == '\\':
811 j = j - 1
812 nslash = nslash + 1
813 if nslash % 2 == 0:
814 break
815 offset = i + 1
816 for c in s[i+1:]:
817 if ord(c) > 32:
818 return 0
819 return 1
820
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000821 def load_binstring(self):
822 len = mloads('i' + self.read(4))
823 self.append(self.read(len))
824 dispatch[BINSTRING] = load_binstring
825
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000826 def load_unicode(self):
827 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
828 dispatch[UNICODE] = load_unicode
829
830 def load_binunicode(self):
831 len = mloads('i' + self.read(4))
832 self.append(unicode(self.read(len),'utf-8'))
833 dispatch[BINUNICODE] = load_binunicode
834
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000835 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000836 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000837 self.append(self.read(len))
838 dispatch[SHORT_BINSTRING] = load_short_binstring
839
840 def load_tuple(self):
841 k = self.marker()
842 self.stack[k:] = [tuple(self.stack[k+1:])]
843 dispatch[TUPLE] = load_tuple
844
845 def load_empty_tuple(self):
846 self.stack.append(())
847 dispatch[EMPTY_TUPLE] = load_empty_tuple
848
849 def load_empty_list(self):
850 self.stack.append([])
851 dispatch[EMPTY_LIST] = load_empty_list
852
853 def load_empty_dictionary(self):
854 self.stack.append({})
855 dispatch[EMPTY_DICT] = load_empty_dictionary
856
857 def load_list(self):
858 k = self.marker()
859 self.stack[k:] = [self.stack[k+1:]]
860 dispatch[LIST] = load_list
861
862 def load_dict(self):
863 k = self.marker()
864 d = {}
865 items = self.stack[k+1:]
866 for i in range(0, len(items), 2):
867 key = items[i]
868 value = items[i+1]
869 d[key] = value
870 self.stack[k:] = [d]
871 dispatch[DICT] = load_dict
872
873 def load_inst(self):
874 k = self.marker()
875 args = tuple(self.stack[k+1:])
876 del self.stack[k:]
877 module = self.readline()[:-1]
878 name = self.readline()[:-1]
879 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000880 instantiated = 0
881 if (not args and type(klass) is ClassType and
882 not hasattr(klass, "__getinitargs__")):
883 try:
884 value = _EmptyClass()
885 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000886 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000887 except RuntimeError:
888 # In restricted execution, assignment to inst.__class__ is
889 # prohibited
890 pass
891 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000892 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000893 if not hasattr(klass, '__safe_for_unpickling__'):
894 raise UnpicklingError('%s is not safe for unpickling' %
895 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000896 value = apply(klass, args)
897 except TypeError, err:
898 raise TypeError, "in constructor for %s: %s" % (
899 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000900 self.append(value)
901 dispatch[INST] = load_inst
902
903 def load_obj(self):
904 stack = self.stack
905 k = self.marker()
906 klass = stack[k + 1]
907 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000908 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000909 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000910 instantiated = 0
911 if (not args and type(klass) is ClassType and
912 not hasattr(klass, "__getinitargs__")):
913 try:
914 value = _EmptyClass()
915 value.__class__ = klass
916 instantiated = 1
917 except RuntimeError:
918 # In restricted execution, assignment to inst.__class__ is
919 # prohibited
920 pass
921 if not instantiated:
922 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000923 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000924 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000925
926 def load_global(self):
927 module = self.readline()[:-1]
928 name = self.readline()[:-1]
929 klass = self.find_class(module, name)
930 self.append(klass)
931 dispatch[GLOBAL] = load_global
932
933 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +0000934 __import__(module)
935 mod = sys.modules[module]
936 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000937 return klass
938
939 def load_reduce(self):
940 stack = self.stack
941
942 callable = stack[-2]
943 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000944 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000945
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000946 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000947 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000948 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000949 safe = callable.__safe_for_unpickling__
950 except AttributeError:
951 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000952
Tim Petersc32d8242001-04-10 02:48:53 +0000953 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +0000954 raise UnpicklingError, "%s is not safe for " \
955 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000956
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000957 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +0000958 import warnings
959 warnings.warn("The None return argument form of __reduce__ is "
960 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +0000961 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000962 value = callable.__basicnew__()
963 else:
964 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000965 self.append(value)
966 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000967
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000968 def load_pop(self):
969 del self.stack[-1]
970 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000971
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000972 def load_pop_mark(self):
973 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000974 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000975 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000976
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000977 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000978 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000979 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000980
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000981 def load_get(self):
982 self.append(self.memo[self.readline()[:-1]])
983 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000984
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000985 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000986 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000987 self.append(self.memo[`i`])
988 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000989
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000990 def load_long_binget(self):
991 i = mloads('i' + self.read(4))
992 self.append(self.memo[`i`])
993 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000994
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000995 def load_put(self):
996 self.memo[self.readline()[:-1]] = self.stack[-1]
997 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000998
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000999 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001000 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001001 self.memo[`i`] = self.stack[-1]
1002 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001003
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001004 def load_long_binput(self):
1005 i = mloads('i' + self.read(4))
1006 self.memo[`i`] = self.stack[-1]
1007 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001008
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001009 def load_append(self):
1010 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001011 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001012 list = stack[-1]
1013 list.append(value)
1014 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001015
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001016 def load_appends(self):
1017 stack = self.stack
1018 mark = self.marker()
1019 list = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001020 for i in range(mark + 1, len(stack)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001021 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001022
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001023 del stack[mark:]
1024 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001025
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001026 def load_setitem(self):
1027 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001028 value = stack.pop()
1029 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001030 dict = stack[-1]
1031 dict[key] = value
1032 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001033
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001034 def load_setitems(self):
1035 stack = self.stack
1036 mark = self.marker()
1037 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001038 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001039 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001040
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001041 del stack[mark:]
1042 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001043
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001044 def load_build(self):
1045 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001046 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001047 inst = stack[-1]
1048 try:
1049 setstate = inst.__setstate__
1050 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001051 try:
1052 inst.__dict__.update(value)
1053 except RuntimeError:
1054 # XXX In restricted execution, the instance's __dict__ is not
1055 # accessible. Use the old way of unpickling the instance
1056 # variables. This is a semantic different when unpickling in
1057 # restricted vs. unrestricted modes.
1058 for k, v in value.items():
1059 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001060 else:
1061 setstate(value)
1062 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001063
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001064 def load_mark(self):
1065 self.append(self.mark)
1066 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001067
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001068 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001069 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001070 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001071 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001072
Guido van Rossume467be61997-12-05 19:42:42 +00001073# Helper class for load_inst/load_obj
1074
1075class _EmptyClass:
1076 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001077
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001078# Shorthands
1079
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001080try:
1081 from cStringIO import StringIO
1082except ImportError:
1083 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001084
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001085def dump(object, file, proto=1):
1086 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001087
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001088def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001089 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001090 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001091 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001092
1093def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001094 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001095
1096def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001097 file = StringIO(str)
1098 return Unpickler(file).load()