blob: cfad6ed9c2b3812bccb08bee6a5b0d932014c3bc [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
Guido van Rossume0b90422003-01-28 03:17:21 +000048# Why use struct.pack() for pickling but marshal.loads() for
49# unpickling? struct.pack() is 40% faster than marshal.loads(), but
50# marshal.loads() is twice as fast as struct.unpack()!
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000051mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000052
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000053class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000054 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000055 pass
56
57class PicklingError(PickleError):
58 """This exception is raised when an unpicklable object is passed to the
59 dump() method.
60
61 """
62 pass
63
64class UnpicklingError(PickleError):
65 """This exception is raised when there is a problem unpickling an object,
66 such as a security violation.
67
68 Note that other exceptions may also be raised during unpickling, including
69 (but not necessarily limited to) AttributeError, EOFError, ImportError,
70 and IndexError.
71
72 """
73 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000074
Guido van Rossumff871742000-12-13 18:11:56 +000075class _Stop(Exception):
76 def __init__(self, value):
77 self.value = value
78
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000079try:
80 from org.python.core import PyStringMap
81except ImportError:
82 PyStringMap = None
83
Guido van Rossumdbb718f2001-09-21 19:22:34 +000084try:
85 UnicodeType
86except NameError:
87 UnicodeType = None
88
Tim Peters22a449a2003-01-27 20:16:36 +000089# Pickle opcodes. See pickletools.py for extensive docs. The listing
90# here is in kind-of alphabetical order of 1-character pickle code.
91# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000092
Tim Peters22a449a2003-01-27 20:16:36 +000093MARK = '(' # push special markobject on stack
94STOP = '.' # every pickle ends with STOP
95POP = '0' # discard topmost stack item
96POP_MARK = '1' # discard stack top through topmost markobject
97DUP = '2' # duplicate top stack item
98FLOAT = 'F' # push float object; decimal string argument
99INT = 'I' # push integer or bool; decimal string argument
100BININT = 'J' # push four-byte signed int
101BININT1 = 'K' # push 1-byte unsigned int
102LONG = 'L' # push long; decimal string argument
103BININT2 = 'M' # push 2-byte unsigned int
104NONE = 'N' # push None
105PERSID = 'P' # push persistent object; id is taken from string arg
106BINPERSID = 'Q' # " " " ; " " " " stack
107REDUCE = 'R' # apply callable to argtuple, both on stack
108STRING = 'S' # push string; NL-terminated string argument
109BINSTRING = 'T' # push string; counted binary string argument
110SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
111UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
112BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
113APPEND = 'a' # append stack top to list below it
114BUILD = 'b' # call __setstate__ or __dict__.update()
115GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
116DICT = 'd' # build a dict from stack items
117EMPTY_DICT = '}' # push empty dict
118APPENDS = 'e' # extend list on stack by topmost stack slice
119GET = 'g' # push item from memo on stack; index is string arg
120BINGET = 'h' # " " " " " " ; " " 1-byte arg
121INST = 'i' # build & push class instance
122LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
123LIST = 'l' # build list from topmost stack items
124EMPTY_LIST = ']' # push empty list
125OBJ = 'o' # build & push class instance
126PUT = 'p' # store stack top in memo; index is string arg
127BINPUT = 'q' # " " " " " ; " " 1-byte arg
128LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
129SETITEM = 's' # add key+value pair to dict
130TUPLE = 't' # build tuple from topmost stack items
131EMPTY_TUPLE = ')' # push empty tuple
132SETITEMS = 'u' # modify dict by adding topmost key+value pairs
133BINFLOAT = 'G' # push float; arg is 8-byte float encoding
134
135TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
136FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000137
Tim Peterse1054782003-01-28 00:22:12 +0000138# Protocol 2 (not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000139
Tim Peterse1054782003-01-28 00:22:12 +0000140PROTO = '\x80' # identify pickle protocol
141NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
142EXT1 = '\x82' # push object from extension registry; 1-byte index
143EXT2 = '\x83' # ditto, but 2-byte index
144EXT4 = '\x84' # ditto, but 4-byte index
145TUPLE1 = '\x85' # build 1-tuple from stack top
146TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
147TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
148NEWTRUE = '\x88' # push True
149NEWFALSE = '\x89' # push False
150LONG1 = '\x8a' # push long from < 256 bytes
151LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000152
Guido van Rossuma48061a1995-01-10 00:31:14 +0000153
Skip Montanaro23bafc62001-02-18 03:10:09 +0000154__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000155del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000156
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000157_quotes = ["'", '"']
158
Guido van Rossuma48061a1995-01-10 00:31:14 +0000159class Pickler:
160
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000161 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000162 """This takes a file-like object for writing a pickle data stream.
163
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000164 The optional proto argument tells the pickler to use the given
165 protocol; supported protocols are 0, 1, 2. The default
166 protocol is 1 (in previous Python versions the default was 0).
167
168 Protocol 1 is more efficient than protocol 0; protocol 2 is
169 more efficient than protocol 1. Protocol 2 is not the default
170 because it is not supported by older Python versions.
171
172 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000173
174 The file parameter must have a write() method that accepts a single
175 string argument. It can thus be an open file object, a StringIO
176 object, or any other custom object that meets this interface.
177
178 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000179 self.write = file.write
180 self.memo = {}
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000181 self.proto = proto
182 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000183
Fred Drake7f781c92002-05-01 20:33:53 +0000184 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000185 """Clears the pickler's "memo".
186
187 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000188 pickler has already seen, so that shared or recursive objects are
189 pickled by reference and not by value. This method is useful when
190 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000191
192 """
Fred Drake7f781c92002-05-01 20:33:53 +0000193 self.memo.clear()
194
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000195 def dump(self, object):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000196 """Write a pickled representation of object to the open file object.
197
198 Either the binary or ASCII format will be used, depending on the
199 value of the bin flag passed to the constructor.
200
201 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000202 self.save(object)
203 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000204
Jeremy Hylton3422c992003-01-24 19:29:52 +0000205 def memoize(self, obj):
206 """Store an object in the memo."""
207
Tim Peterse46b73f2003-01-27 21:22:10 +0000208 # The Pickler memo is a dictionary mapping object ids to 2-tuples
209 # that contain the Unpickler memo key and the object being memoized.
210 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000211 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000212 # Pickler memo so that transient objects are kept alive during
213 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000214
Tim Peterse46b73f2003-01-27 21:22:10 +0000215 # The use of the Unpickler memo length as the memo key is just a
216 # convention. The only requirement is that the memo values be unique.
217 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000218 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000219 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000220 memo_len = len(self.memo)
221 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000222 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000223
Tim Petersbb38e302003-01-27 21:25:41 +0000224 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000225 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000226 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000227 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000228 return BINPUT + chr(i)
229 else:
230 return LONG_BINPUT + pack("<i", i)
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 Rossum5c938d02003-01-28 03:03:08 +0000235 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000236 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000237 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000238 return BINGET + chr(i)
239 else:
240 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000241
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000242 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000243
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000244 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000245 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000246
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000247 pid = self.persistent_id(object)
248 if pid is not None:
249 self.save_pers(pid)
250 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000251
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000252 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000253
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000254 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000255
Tim Petersc9d7c4a2003-01-28 00:43:26 +0000256 # XXX Why are tuples a special case here?
Tim Petersc32d8242001-04-10 02:48:53 +0000257 if (t is TupleType) and (len(object) == 0):
258 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000259 self.save_empty_tuple(object)
260 else:
261 self.save_tuple(object)
262 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000263
Raymond Hettinger54f02222002-06-01 14:18:47 +0000264 if d in memo:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000265 self.write(self.get(memo[d][0]))
266 return
267
268 try:
269 f = self.dispatch[t]
270 except KeyError:
Tim Petersb32a8312003-01-28 00:48:09 +0000271 pass
272 else:
273 f(self, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000274 return
275
Tim Petersb32a8312003-01-28 00:48:09 +0000276 # The dispatch table doesn't know about type t.
277 try:
278 issc = issubclass(t, TypeType)
279 except TypeError: # t is not a class
280 issc = 0
281 if issc:
282 self.save_global(object)
283 return
284
285 try:
286 reduce = dispatch_table[t]
287 except KeyError:
288 try:
289 reduce = object.__reduce__
290 except AttributeError:
291 raise PicklingError, \
292 "can't pickle %s object: %s" % (`t.__name__`,
293 `object`)
294 else:
295 tup = reduce()
296 else:
297 tup = reduce(object)
298
299 if type(tup) is StringType:
300 self.save_global(object, tup)
301 return
302
303 if type(tup) is not TupleType:
304 raise PicklingError, "Value returned by %s must be a " \
305 "tuple" % reduce
306
307 l = len(tup)
308
309 if (l != 2) and (l != 3):
310 raise PicklingError, "tuple returned by %s must contain " \
311 "only two or three elements" % reduce
312
313 callable = tup[0]
314 arg_tup = tup[1]
315
316 if l > 2:
317 state = tup[2]
318 else:
319 state = None
320
321 if type(arg_tup) is not TupleType and arg_tup is not None:
322 raise PicklingError, "Second element of tuple returned " \
323 "by %s must be a tuple" % reduce
324
325 self.save_reduce(callable, arg_tup, state)
Tim Peters518df0d2003-01-28 01:00:38 +0000326 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000327
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 Petersbd1cdb92003-01-28 01:03:10 +0000332 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000333 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000334 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000335 else:
336 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000337
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):
Tim Peters22987e32003-01-28 00:26:14 +0000361 self.write(object and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000362 dispatch[bool] = save_bool
363
Guido van Rossum5c938d02003-01-28 03:03:08 +0000364 def save_int(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000365 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000366 # If the int is small enough to fit in a signed 4-byte 2's-comp
367 # format, we can store it more efficiently than the general
368 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000369 # First one- and two-byte unsigned ints:
370 if object >= 0:
Tim Peters8fda7bc2003-01-28 03:40:52 +0000371 if object <= 0xff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000372 self.write(BININT1 + chr(object))
373 return
Tim Peters8fda7bc2003-01-28 03:40:52 +0000374 if object <= 0xffff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000375 self.write(BININT2 + chr(object&0xff) + chr(object>>8))
376 return
377 # Next check for 4-byte signed ints:
Tim Peters44714002001-04-10 05:02:52 +0000378 high_bits = object >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000379 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000380 # All high bits are copies of bit 2**31, so the value
381 # fits in a 4-byte signed int.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000382 self.write(BININT + pack("<i", object))
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
Guido van Rossum5c938d02003-01-28 03:03:08 +0000399 def save_string(self, object, pack=struct.pack):
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:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000405 self.write(BINSTRING + pack("<i", n) + 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 Rossum5c938d02003-01-28 03:03:08 +0000411 def save_unicode(self, object, pack=struct.pack):
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)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000415 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000416 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000417 object = object.replace("\\", "\\u005c")
418 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000419 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000420 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000421 dispatch[UnicodeType] = save_unicode
422
Guido van Rossum31584cb2001-01-22 14:53:29 +0000423 if StringType == UnicodeType:
424 # This is true for Jython
Guido van Rossum5c938d02003-01-28 03:03:08 +0000425 def save_string(self, object, pack=struct.pack):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000426 unicode = object.isunicode()
427
Tim Petersc32d8242001-04-10 02:48:53 +0000428 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000429 if unicode:
430 object = object.encode("utf-8")
431 l = len(object)
Tim Petersc32d8242001-04-10 02:48:53 +0000432 if l < 256 and not unicode:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000433 self.write(SHORT_BINSTRING + chr(l) + object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000434 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000435 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000436 if unicode:
437 self.write(BINUNICODE + s + object)
438 else:
439 self.write(BINSTRING + s + object)
440 else:
Tim Peters658cba62001-02-09 20:06:00 +0000441 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000442 object = object.replace("\\", "\\u005c")
443 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000444 object = object.encode('raw-unicode-escape')
445 self.write(UNICODE + object + '\n')
446 else:
447 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000448 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000449 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000450
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000451 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000452 write = self.write
453 save = self.save
454 memo = self.memo
455
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000456 write(MARK)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000457 for element in object:
458 save(element)
459
Tim Petersf558da02003-01-28 02:09:55 +0000460 if object and id(object) in memo:
461 # Subtle. d was not in memo when we entered save_tuple(), so
462 # the process of saving the tuple's elements must have saved
463 # the tuple itself: the tuple is recursive. The proper action
464 # now is to throw away everything we put on the stack, and
465 # simply GET the tuple (it's already constructed). This check
466 # could have been done in the "for element" loop instead, but
467 # recursive tuples are a rare thing.
468 get = self.get(memo[id(object)][0])
Tim Petersc32d8242001-04-10 02:48:53 +0000469 if self.bin:
Tim Petersf558da02003-01-28 02:09:55 +0000470 write(POP_MARK + get)
471 else: # proto 0 -- POP_MARK not available
472 write(POP * (len(object) + 1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000473 return
474
Tim Petersf558da02003-01-28 02:09:55 +0000475 # No recursion (including the empty-tuple case).
Tim Peters518df0d2003-01-28 01:00:38 +0000476 self.write(TUPLE)
477 self.memoize(object)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000478
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000479 dispatch[TupleType] = save_tuple
480
481 def save_empty_tuple(self, object):
482 self.write(EMPTY_TUPLE)
483
484 def save_list(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000485 write = self.write
486 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000487
Tim Petersc32d8242001-04-10 02:48:53 +0000488 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000489 write(EMPTY_LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000490 self.memoize(object)
491 n = len(object)
492 if n > 1:
493 write(MARK)
494 for element in object:
495 save(element)
496 write(APPENDS)
497 elif n:
498 assert n == 1
499 save(object[0])
500 write(APPEND)
501 # else the list is empty, and we're already done
502
503 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000504 write(MARK + LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000505 self.memoize(object)
506 for element in object:
507 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000508 write(APPEND)
509
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000510 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
Tim Peters064567e2003-01-28 01:34:43 +0000515 items = object.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000516
Tim Petersc32d8242001-04-10 02:48:53 +0000517 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000518 write(EMPTY_DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000519 self.memoize(object)
520 if len(object) > 1:
521 write(MARK)
522 for key, value in items:
523 save(key)
524 save(value)
525 write(SETITEMS)
526 return
527
528 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000529 write(MARK + DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000530 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000531
Tim Peters064567e2003-01-28 01:34:43 +0000532 # proto 0 or len(object) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000533 for key, value in items:
534 save(key)
535 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000536 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000537
538 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000539 if not PyStringMap is None:
540 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000541
542 def save_inst(self, object):
543 d = id(object)
544 cls = object.__class__
545
546 memo = self.memo
547 write = self.write
548 save = self.save
549
550 if hasattr(object, '__getinitargs__'):
551 args = object.__getinitargs__()
552 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000553 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000554 else:
555 args = ()
556
557 write(MARK)
558
Tim Petersc32d8242001-04-10 02:48:53 +0000559 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000560 save(cls)
561
562 for arg in args:
563 save(arg)
564
Jeremy Hylton3422c992003-01-24 19:29:52 +0000565 # This method does not use memoize() so that it can handle
566 # the special case for non-binary mode.
Tim Peters518df0d2003-01-28 01:00:38 +0000567 # XXX What did that comment mean? That is, what "special case for
Tim Petersc23d18a2003-01-28 01:41:51 +0000568 # XXX non-binary mode"? It sure *looks* like nothing special is
Tim Peters518df0d2003-01-28 01:00:38 +0000569 # XXX happening in the INST case.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570 memo_len = len(memo)
Tim Petersc32d8242001-04-10 02:48:53 +0000571 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000572 write(OBJ + self.put(memo_len))
573 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000574 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000575 self.put(memo_len))
576
577 memo[d] = (memo_len, object)
578
579 try:
580 getstate = object.__getstate__
581 except AttributeError:
582 stuff = object.__dict__
583 else:
584 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000585 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000586 save(stuff)
587 write(BUILD)
588 dispatch[InstanceType] = save_inst
589
590 def save_global(self, object, name = None):
591 write = self.write
592 memo = self.memo
593
Tim Petersc32d8242001-04-10 02:48:53 +0000594 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000595 name = object.__name__
596
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000597 try:
598 module = object.__module__
599 except AttributeError:
600 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000601
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000602 try:
603 __import__(module)
604 mod = sys.modules[module]
605 klass = getattr(mod, name)
606 except (ImportError, KeyError, AttributeError):
607 raise PicklingError(
608 "Can't pickle %r: it's not found as %s.%s" %
609 (object, module, name))
610 else:
611 if klass is not object:
612 raise PicklingError(
613 "Can't pickle %r: it's not the same object as %s.%s" %
614 (object, module, name))
615
Tim Peters518df0d2003-01-28 01:00:38 +0000616 write(GLOBAL + module + '\n' + name + '\n')
617 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000618 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
Tim Petersc23d18a2003-01-28 01:41:51 +0000706 # Return largest index k such that self.stack[k] is self.mark.
707 # If the stack doesn't contain a mark, eventually raises IndexError.
708 # This could be sped by maintaining another stack, of indices at which
709 # the mark appears. For that matter, the latter stack would suffice,
710 # and we wouldn't need to push mark objects on self.stack at all.
711 # Doing so is probably a good thing, though, since if the pickle is
712 # corrupt (or hostile) we may get a clue from finding self.mark embedded
713 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000714 def marker(self):
715 stack = self.stack
716 mark = self.mark
717 k = len(stack)-1
718 while stack[k] is not mark: k = k-1
719 return k
720
721 dispatch = {}
722
723 def load_eof(self):
724 raise EOFError
725 dispatch[''] = load_eof
726
727 def load_persid(self):
728 pid = self.readline()[:-1]
729 self.append(self.persistent_load(pid))
730 dispatch[PERSID] = load_persid
731
732 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000733 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000734 self.append(self.persistent_load(pid))
735 dispatch[BINPERSID] = load_binpersid
736
737 def load_none(self):
738 self.append(None)
739 dispatch[NONE] = load_none
740
741 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000742 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000743 if data == FALSE[1:]:
744 val = False
745 elif data == TRUE[1:]:
746 val = True
747 else:
748 try:
749 val = int(data)
750 except ValueError:
751 val = long(data)
752 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000753 dispatch[INT] = load_int
754
755 def load_binint(self):
756 self.append(mloads('i' + self.read(4)))
757 dispatch[BININT] = load_binint
758
759 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000760 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000761 dispatch[BININT1] = load_binint1
762
763 def load_binint2(self):
764 self.append(mloads('i' + self.read(2) + '\000\000'))
765 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000766
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000767 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000768 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769 dispatch[LONG] = load_long
770
771 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000772 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000773 dispatch[FLOAT] = load_float
774
Guido van Rossumd3703791998-10-22 20:15:36 +0000775 def load_binfloat(self, unpack=struct.unpack):
776 self.append(unpack('>d', self.read(8))[0])
777 dispatch[BINFLOAT] = load_binfloat
778
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000779 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000780 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000781 for q in _quotes:
782 if rep.startswith(q):
783 if not rep.endswith(q):
784 raise ValueError, "insecure string pickle"
785 rep = rep[len(q):-len(q)]
786 break
787 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000788 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000789 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000790 dispatch[STRING] = load_string
791
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000792 def _is_string_secure(self, s):
793 """Return true if s contains a string that is safe to eval
794
795 The definition of secure string is based on the implementation
796 in cPickle. s is secure as long as it only contains a quoted
797 string and optional trailing whitespace.
798 """
799 q = s[0]
800 if q not in ("'", '"'):
801 return 0
802 # find the closing quote
803 offset = 1
804 i = None
805 while 1:
806 try:
807 i = s.index(q, offset)
808 except ValueError:
809 # if there is an error the first time, there is no
810 # close quote
811 if offset == 1:
812 return 0
813 if s[i-1] != '\\':
814 break
815 # check to see if this one is escaped
816 nslash = 0
817 j = i - 1
818 while j >= offset and s[j] == '\\':
819 j = j - 1
820 nslash = nslash + 1
821 if nslash % 2 == 0:
822 break
823 offset = i + 1
824 for c in s[i+1:]:
825 if ord(c) > 32:
826 return 0
827 return 1
828
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000829 def load_binstring(self):
830 len = mloads('i' + self.read(4))
831 self.append(self.read(len))
832 dispatch[BINSTRING] = load_binstring
833
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000834 def load_unicode(self):
835 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
836 dispatch[UNICODE] = load_unicode
837
838 def load_binunicode(self):
839 len = mloads('i' + self.read(4))
840 self.append(unicode(self.read(len),'utf-8'))
841 dispatch[BINUNICODE] = load_binunicode
842
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000843 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000844 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000845 self.append(self.read(len))
846 dispatch[SHORT_BINSTRING] = load_short_binstring
847
848 def load_tuple(self):
849 k = self.marker()
850 self.stack[k:] = [tuple(self.stack[k+1:])]
851 dispatch[TUPLE] = load_tuple
852
853 def load_empty_tuple(self):
854 self.stack.append(())
855 dispatch[EMPTY_TUPLE] = load_empty_tuple
856
857 def load_empty_list(self):
858 self.stack.append([])
859 dispatch[EMPTY_LIST] = load_empty_list
860
861 def load_empty_dictionary(self):
862 self.stack.append({})
863 dispatch[EMPTY_DICT] = load_empty_dictionary
864
865 def load_list(self):
866 k = self.marker()
867 self.stack[k:] = [self.stack[k+1:]]
868 dispatch[LIST] = load_list
869
870 def load_dict(self):
871 k = self.marker()
872 d = {}
873 items = self.stack[k+1:]
874 for i in range(0, len(items), 2):
875 key = items[i]
876 value = items[i+1]
877 d[key] = value
878 self.stack[k:] = [d]
879 dispatch[DICT] = load_dict
880
881 def load_inst(self):
882 k = self.marker()
883 args = tuple(self.stack[k+1:])
884 del self.stack[k:]
885 module = self.readline()[:-1]
886 name = self.readline()[:-1]
887 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000888 instantiated = 0
889 if (not args and type(klass) is ClassType and
890 not hasattr(klass, "__getinitargs__")):
891 try:
892 value = _EmptyClass()
893 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000894 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000895 except RuntimeError:
896 # In restricted execution, assignment to inst.__class__ is
897 # prohibited
898 pass
899 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000900 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000901 if not hasattr(klass, '__safe_for_unpickling__'):
902 raise UnpicklingError('%s is not safe for unpickling' %
903 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000904 value = apply(klass, args)
905 except TypeError, err:
906 raise TypeError, "in constructor for %s: %s" % (
907 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000908 self.append(value)
909 dispatch[INST] = load_inst
910
911 def load_obj(self):
912 stack = self.stack
913 k = self.marker()
914 klass = stack[k + 1]
915 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000916 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000917 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000918 instantiated = 0
919 if (not args and type(klass) is ClassType and
920 not hasattr(klass, "__getinitargs__")):
921 try:
922 value = _EmptyClass()
923 value.__class__ = klass
924 instantiated = 1
925 except RuntimeError:
926 # In restricted execution, assignment to inst.__class__ is
927 # prohibited
928 pass
929 if not instantiated:
930 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000931 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000932 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000933
934 def load_global(self):
935 module = self.readline()[:-1]
936 name = self.readline()[:-1]
937 klass = self.find_class(module, name)
938 self.append(klass)
939 dispatch[GLOBAL] = load_global
940
941 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +0000942 __import__(module)
943 mod = sys.modules[module]
944 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000945 return klass
946
947 def load_reduce(self):
948 stack = self.stack
949
950 callable = stack[-2]
951 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000952 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000953
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000954 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000955 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000956 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000957 safe = callable.__safe_for_unpickling__
958 except AttributeError:
959 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000960
Tim Petersc32d8242001-04-10 02:48:53 +0000961 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +0000962 raise UnpicklingError, "%s is not safe for " \
963 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000964
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000965 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +0000966 import warnings
967 warnings.warn("The None return argument form of __reduce__ is "
968 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +0000969 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000970 value = callable.__basicnew__()
971 else:
972 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000973 self.append(value)
974 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000975
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000976 def load_pop(self):
977 del self.stack[-1]
978 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000979
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000980 def load_pop_mark(self):
981 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000982 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000983 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000984
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000985 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000986 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000987 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000988
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000989 def load_get(self):
990 self.append(self.memo[self.readline()[:-1]])
991 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000992
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000993 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000994 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000995 self.append(self.memo[`i`])
996 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000997
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000998 def load_long_binget(self):
999 i = mloads('i' + self.read(4))
1000 self.append(self.memo[`i`])
1001 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001002
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001003 def load_put(self):
1004 self.memo[self.readline()[:-1]] = self.stack[-1]
1005 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001006
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001007 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001008 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001009 self.memo[`i`] = self.stack[-1]
1010 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001011
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001012 def load_long_binput(self):
1013 i = mloads('i' + self.read(4))
1014 self.memo[`i`] = self.stack[-1]
1015 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001016
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001017 def load_append(self):
1018 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001019 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001020 list = stack[-1]
1021 list.append(value)
1022 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001023
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001024 def load_appends(self):
1025 stack = self.stack
1026 mark = self.marker()
1027 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001028 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001029 del stack[mark:]
1030 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001031
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001032 def load_setitem(self):
1033 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001034 value = stack.pop()
1035 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001036 dict = stack[-1]
1037 dict[key] = value
1038 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001039
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001040 def load_setitems(self):
1041 stack = self.stack
1042 mark = self.marker()
1043 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001044 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001045 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001046
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001047 del stack[mark:]
1048 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001049
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001050 def load_build(self):
1051 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001052 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001053 inst = stack[-1]
1054 try:
1055 setstate = inst.__setstate__
1056 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001057 try:
1058 inst.__dict__.update(value)
1059 except RuntimeError:
1060 # XXX In restricted execution, the instance's __dict__ is not
1061 # accessible. Use the old way of unpickling the instance
1062 # variables. This is a semantic different when unpickling in
1063 # restricted vs. unrestricted modes.
1064 for k, v in value.items():
1065 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001066 else:
1067 setstate(value)
1068 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001069
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001070 def load_mark(self):
1071 self.append(self.mark)
1072 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001073
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001074 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001075 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001076 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001077 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001078
Guido van Rossume467be61997-12-05 19:42:42 +00001079# Helper class for load_inst/load_obj
1080
1081class _EmptyClass:
1082 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001083
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001084# Shorthands
1085
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001086try:
1087 from cStringIO import StringIO
1088except ImportError:
1089 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001090
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001091def dump(object, file, proto=1):
1092 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001093
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001094def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001095 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001096 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001097 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001098
1099def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001100 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001101
1102def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001103 file = StringIO(str)
1104 return Unpickler(file).load()