blob: ca98ae3ea70db92b64d51b4a867aa79b28b5a39e [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 Rossumd6c9e632003-01-28 03:49:52 +0000179 if not 0 <= proto <= 2:
180 raise ValueError, "pickle protocol must be 0, 1 or 2"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000181 self.write = file.write
182 self.memo = {}
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000183 self.proto = proto
184 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000185
Fred Drake7f781c92002-05-01 20:33:53 +0000186 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000187 """Clears the pickler's "memo".
188
189 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000190 pickler has already seen, so that shared or recursive objects are
191 pickled by reference and not by value. This method is useful when
192 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000193
194 """
Fred Drake7f781c92002-05-01 20:33:53 +0000195 self.memo.clear()
196
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000197 def dump(self, object):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000198 """Write a pickled representation of object to the open file object.
199
200 Either the binary or ASCII format will be used, depending on the
201 value of the bin flag passed to the constructor.
202
203 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000204 if self.proto >= 2:
205 self.write(PROTO + chr(self.proto))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000206 self.save(object)
207 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000208
Jeremy Hylton3422c992003-01-24 19:29:52 +0000209 def memoize(self, obj):
210 """Store an object in the memo."""
211
Tim Peterse46b73f2003-01-27 21:22:10 +0000212 # The Pickler memo is a dictionary mapping object ids to 2-tuples
213 # that contain the Unpickler memo key and the object being memoized.
214 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000215 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000216 # Pickler memo so that transient objects are kept alive during
217 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000218
Tim Peterse46b73f2003-01-27 21:22:10 +0000219 # The use of the Unpickler memo length as the memo key is just a
220 # convention. The only requirement is that the memo values be unique.
221 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000222 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000223 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000224 memo_len = len(self.memo)
225 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000226 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000227
Tim Petersbb38e302003-01-27 21:25:41 +0000228 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000229 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000230 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000231 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000232 return BINPUT + chr(i)
233 else:
234 return LONG_BINPUT + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000235
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000236 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Tim Petersbb38e302003-01-27 21:25:41 +0000238 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000239 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000240 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000241 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000242 return BINGET + chr(i)
243 else:
244 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000245
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000246 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000247
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000248 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000249 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000250
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000251 pid = self.persistent_id(object)
252 if pid is not None:
253 self.save_pers(pid)
254 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000255
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000256 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000257
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000258 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000259
Tim Petersc9d7c4a2003-01-28 00:43:26 +0000260 # XXX Why are tuples a special case here?
Tim Petersc32d8242001-04-10 02:48:53 +0000261 if (t is TupleType) and (len(object) == 0):
262 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000263 self.save_empty_tuple(object)
264 else:
265 self.save_tuple(object)
266 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000267
Raymond Hettinger54f02222002-06-01 14:18:47 +0000268 if d in memo:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000269 self.write(self.get(memo[d][0]))
270 return
271
272 try:
273 f = self.dispatch[t]
274 except KeyError:
Tim Petersb32a8312003-01-28 00:48:09 +0000275 pass
276 else:
277 f(self, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000278 return
279
Tim Petersb32a8312003-01-28 00:48:09 +0000280 # The dispatch table doesn't know about type t.
281 try:
282 issc = issubclass(t, TypeType)
283 except TypeError: # t is not a class
284 issc = 0
285 if issc:
286 self.save_global(object)
287 return
288
289 try:
290 reduce = dispatch_table[t]
291 except KeyError:
292 try:
293 reduce = object.__reduce__
294 except AttributeError:
295 raise PicklingError, \
296 "can't pickle %s object: %s" % (`t.__name__`,
297 `object`)
298 else:
299 tup = reduce()
300 else:
301 tup = reduce(object)
302
303 if type(tup) is StringType:
304 self.save_global(object, tup)
305 return
306
307 if type(tup) is not TupleType:
308 raise PicklingError, "Value returned by %s must be a " \
309 "tuple" % reduce
310
311 l = len(tup)
312
313 if (l != 2) and (l != 3):
314 raise PicklingError, "tuple returned by %s must contain " \
315 "only two or three elements" % reduce
316
317 callable = tup[0]
318 arg_tup = tup[1]
319
320 if l > 2:
321 state = tup[2]
322 else:
323 state = None
324
325 if type(arg_tup) is not TupleType and arg_tup is not None:
326 raise PicklingError, "Second element of tuple returned " \
327 "by %s must be a tuple" % reduce
328
329 self.save_reduce(callable, arg_tup, state)
Tim Peters518df0d2003-01-28 01:00:38 +0000330 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000331
332 def persistent_id(self, object):
333 return None
334
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335 def save_pers(self, pid):
Tim Petersbd1cdb92003-01-28 01:03:10 +0000336 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000337 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000338 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000339 else:
340 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000341
Jeremy Hylton3422c992003-01-24 19:29:52 +0000342 def save_reduce(self, acallable, arg_tup, state = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000343 write = self.write
344 save = self.save
345
Jeremy Hylton3422c992003-01-24 19:29:52 +0000346 if not callable(acallable):
347 raise PicklingError("__reduce__() must return callable as "
348 "first argument, not %s" % `acallable`)
Tim Peters22a449a2003-01-27 20:16:36 +0000349
Jeremy Hylton3422c992003-01-24 19:29:52 +0000350 save(acallable)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000351 save(arg_tup)
352 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000353
Tim Petersc32d8242001-04-10 02:48:53 +0000354 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000355 save(state)
356 write(BUILD)
357
358 dispatch = {}
359
360 def save_none(self, object):
361 self.write(NONE)
362 dispatch[NoneType] = save_none
363
Guido van Rossum77f6a652002-04-03 22:41:51 +0000364 def save_bool(self, object):
Tim Peters22987e32003-01-28 00:26:14 +0000365 self.write(object and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000366 dispatch[bool] = save_bool
367
Guido van Rossum5c938d02003-01-28 03:03:08 +0000368 def save_int(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000369 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000370 # If the int is small enough to fit in a signed 4-byte 2's-comp
371 # format, we can store it more efficiently than the general
372 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000373 # First one- and two-byte unsigned ints:
374 if object >= 0:
Tim Peters8fda7bc2003-01-28 03:40:52 +0000375 if object <= 0xff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000376 self.write(BININT1 + chr(object))
377 return
Tim Peters8fda7bc2003-01-28 03:40:52 +0000378 if object <= 0xffff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000379 self.write(BININT2 + chr(object&0xff) + chr(object>>8))
380 return
381 # Next check for 4-byte signed ints:
Tim Peters44714002001-04-10 05:02:52 +0000382 high_bits = object >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000383 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000384 # All high bits are copies of bit 2**31, so the value
385 # fits in a 4-byte signed int.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000386 self.write(BININT + pack("<i", object))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000387 return
Tim Peters44714002001-04-10 05:02:52 +0000388 # Text pickle, or int too big to fit in signed 4-byte format.
389 self.write(INT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000390 dispatch[IntType] = save_int
391
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000392 def save_long(self, object, pack=struct.pack):
393 if self.proto >= 2:
394 bytes = encode_long(object)
395 n = len(bytes)
396 if n < 256:
397 self.write(LONG1 + chr(n) + bytes)
398 else:
399 self.write(LONG4 + pack("<i", n) + bytes)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000400 self.write(LONG + `object` + '\n')
401 dispatch[LongType] = save_long
402
Guido van Rossumd3703791998-10-22 20:15:36 +0000403 def save_float(self, object, pack=struct.pack):
404 if self.bin:
405 self.write(BINFLOAT + pack('>d', object))
406 else:
407 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000408 dispatch[FloatType] = save_float
409
Guido van Rossum5c938d02003-01-28 03:03:08 +0000410 def save_string(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000411 if self.bin:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000412 n = len(object)
413 if n < 256:
414 self.write(SHORT_BINSTRING + chr(n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000415 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000416 self.write(BINSTRING + pack("<i", n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000417 else:
418 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000419 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000420 dispatch[StringType] = save_string
421
Guido van Rossum5c938d02003-01-28 03:03:08 +0000422 def save_unicode(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000423 if self.bin:
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000424 encoding = object.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000425 n = len(encoding)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000426 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000427 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000428 object = object.replace("\\", "\\u005c")
429 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000430 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000431 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000432 dispatch[UnicodeType] = save_unicode
433
Guido van Rossum31584cb2001-01-22 14:53:29 +0000434 if StringType == UnicodeType:
435 # This is true for Jython
Guido van Rossum5c938d02003-01-28 03:03:08 +0000436 def save_string(self, object, pack=struct.pack):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000437 unicode = object.isunicode()
438
Tim Petersc32d8242001-04-10 02:48:53 +0000439 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000440 if unicode:
441 object = object.encode("utf-8")
442 l = len(object)
Tim Petersc32d8242001-04-10 02:48:53 +0000443 if l < 256 and not unicode:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000444 self.write(SHORT_BINSTRING + chr(l) + object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000445 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000446 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000447 if unicode:
448 self.write(BINUNICODE + s + object)
449 else:
450 self.write(BINSTRING + s + object)
451 else:
Tim Peters658cba62001-02-09 20:06:00 +0000452 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000453 object = object.replace("\\", "\\u005c")
454 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000455 object = object.encode('raw-unicode-escape')
456 self.write(UNICODE + object + '\n')
457 else:
458 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000459 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000460 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000461
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000462 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000463 write = self.write
464 save = self.save
465 memo = self.memo
466
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000467 write(MARK)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000468 for element in object:
469 save(element)
470
Tim Petersf558da02003-01-28 02:09:55 +0000471 if object and id(object) in memo:
472 # Subtle. d was not in memo when we entered save_tuple(), so
473 # the process of saving the tuple's elements must have saved
474 # the tuple itself: the tuple is recursive. The proper action
475 # now is to throw away everything we put on the stack, and
476 # simply GET the tuple (it's already constructed). This check
477 # could have been done in the "for element" loop instead, but
478 # recursive tuples are a rare thing.
479 get = self.get(memo[id(object)][0])
Tim Petersc32d8242001-04-10 02:48:53 +0000480 if self.bin:
Tim Petersf558da02003-01-28 02:09:55 +0000481 write(POP_MARK + get)
482 else: # proto 0 -- POP_MARK not available
483 write(POP * (len(object) + 1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000484 return
485
Tim Petersf558da02003-01-28 02:09:55 +0000486 # No recursion (including the empty-tuple case).
Tim Peters518df0d2003-01-28 01:00:38 +0000487 self.write(TUPLE)
488 self.memoize(object)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000489
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000490 dispatch[TupleType] = save_tuple
491
492 def save_empty_tuple(self, object):
493 self.write(EMPTY_TUPLE)
494
495 def save_list(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000496 write = self.write
497 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000498
Tim Petersc32d8242001-04-10 02:48:53 +0000499 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000500 write(EMPTY_LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000501 self.memoize(object)
502 n = len(object)
503 if n > 1:
504 write(MARK)
505 for element in object:
506 save(element)
507 write(APPENDS)
508 elif n:
509 assert n == 1
510 save(object[0])
511 write(APPEND)
512 # else the list is empty, and we're already done
513
514 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000515 write(MARK + LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000516 self.memoize(object)
517 for element in object:
518 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000519 write(APPEND)
520
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000521 dispatch[ListType] = save_list
522
523 def save_dict(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000524 write = self.write
525 save = self.save
Tim Peters064567e2003-01-28 01:34:43 +0000526 items = object.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000527
Tim Petersc32d8242001-04-10 02:48:53 +0000528 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000529 write(EMPTY_DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000530 self.memoize(object)
531 if len(object) > 1:
532 write(MARK)
533 for key, value in items:
534 save(key)
535 save(value)
536 write(SETITEMS)
537 return
538
539 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000540 write(MARK + DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000541 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000542
Tim Peters064567e2003-01-28 01:34:43 +0000543 # proto 0 or len(object) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000544 for key, value in items:
545 save(key)
546 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000547 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000548
549 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000550 if not PyStringMap is None:
551 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000552
553 def save_inst(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000554 cls = object.__class__
555
556 memo = self.memo
557 write = self.write
558 save = self.save
559
560 if hasattr(object, '__getinitargs__'):
561 args = object.__getinitargs__()
562 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000563 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000564 else:
565 args = ()
566
567 write(MARK)
568
Tim Petersc32d8242001-04-10 02:48:53 +0000569 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570 save(cls)
Tim Peters3b769832003-01-28 03:51:36 +0000571 for arg in args:
572 save(arg)
573 write(OBJ)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000574 else:
Tim Peters3b769832003-01-28 03:51:36 +0000575 for arg in args:
576 save(arg)
577 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000578
Tim Peters3b769832003-01-28 03:51:36 +0000579 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000580
581 try:
582 getstate = object.__getstate__
583 except AttributeError:
584 stuff = object.__dict__
585 else:
586 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000587 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000588 save(stuff)
589 write(BUILD)
Tim Peters3b769832003-01-28 03:51:36 +0000590
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000591 dispatch[InstanceType] = save_inst
592
593 def save_global(self, object, name = None):
594 write = self.write
595 memo = self.memo
596
Tim Petersc32d8242001-04-10 02:48:53 +0000597 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000598 name = object.__name__
599
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000600 try:
601 module = object.__module__
602 except AttributeError:
603 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000604
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000605 try:
606 __import__(module)
607 mod = sys.modules[module]
608 klass = getattr(mod, name)
609 except (ImportError, KeyError, AttributeError):
610 raise PicklingError(
611 "Can't pickle %r: it's not found as %s.%s" %
612 (object, module, name))
613 else:
614 if klass is not object:
615 raise PicklingError(
616 "Can't pickle %r: it's not the same object as %s.%s" %
617 (object, module, name))
618
Tim Peters518df0d2003-01-28 01:00:38 +0000619 write(GLOBAL + module + '\n' + name + '\n')
620 self.memoize(object)
Tim Peters3b769832003-01-28 03:51:36 +0000621
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000622 dispatch[ClassType] = save_global
623 dispatch[FunctionType] = save_global
624 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000625 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000626
Guido van Rossuma48061a1995-01-10 00:31:14 +0000627
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000628def _keep_alive(x, memo):
629 """Keeps a reference to the object x in the memo.
630
631 Because we remember objects by their id, we have
632 to assure that possibly temporary objects are kept
633 alive by referencing them.
634 We store a reference at the id of the memo, which should
635 normally not be used unless someone tries to deepcopy
636 the memo itself...
637 """
638 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000639 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000640 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000641 # aha, this is the first one :-)
642 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000643
644
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000645classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000646
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000647def whichmodule(func, funcname):
648 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000649
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000650 Search sys.modules for the module.
651 Cache in classmap.
652 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000653 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000654 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000655 if func in classmap:
656 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000657
658 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000659 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000660 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000661 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000662 hasattr(module, funcname) and \
663 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000664 break
665 else:
666 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000667 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000668 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000669
670
671class Unpickler:
672
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000673 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000674 """This takes a file-like object for reading a pickle data stream.
675
676 This class automatically determines whether the data stream was
677 written in binary mode or not, so it does not need a flag as in
678 the Pickler class factory.
679
680 The file-like object must have two methods, a read() method that
681 takes an integer argument, and a readline() method that requires no
682 arguments. Both methods should return a string. Thus file-like
683 object can be a file object opened for reading, a StringIO object,
684 or any other custom object that meets this interface.
685
686 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000687 self.readline = file.readline
688 self.read = file.read
689 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000690
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 def load(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000692 """Read a pickled object representation from the open file object.
693
694 Return the reconstituted object hierarchy specified in the file
695 object.
696
697 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000698 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000699 self.stack = []
700 self.append = self.stack.append
701 read = self.read
702 dispatch = self.dispatch
703 try:
704 while 1:
705 key = read(1)
706 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000707 except _Stop, stopinst:
708 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000709
Tim Petersc23d18a2003-01-28 01:41:51 +0000710 # Return largest index k such that self.stack[k] is self.mark.
711 # If the stack doesn't contain a mark, eventually raises IndexError.
712 # This could be sped by maintaining another stack, of indices at which
713 # the mark appears. For that matter, the latter stack would suffice,
714 # and we wouldn't need to push mark objects on self.stack at all.
715 # Doing so is probably a good thing, though, since if the pickle is
716 # corrupt (or hostile) we may get a clue from finding self.mark embedded
717 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000718 def marker(self):
719 stack = self.stack
720 mark = self.mark
721 k = len(stack)-1
722 while stack[k] is not mark: k = k-1
723 return k
724
725 dispatch = {}
726
727 def load_eof(self):
728 raise EOFError
729 dispatch[''] = load_eof
730
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000731 def load_proto(self):
732 proto = ord(self.read(1))
733 if not 0 <= proto <= 2:
734 raise ValueError, "unsupported pickle protocol: %d" % proto
735 dispatch[PROTO] = load_proto
736
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000737 def load_persid(self):
738 pid = self.readline()[:-1]
739 self.append(self.persistent_load(pid))
740 dispatch[PERSID] = load_persid
741
742 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000743 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000744 self.append(self.persistent_load(pid))
745 dispatch[BINPERSID] = load_binpersid
746
747 def load_none(self):
748 self.append(None)
749 dispatch[NONE] = load_none
750
751 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000752 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000753 if data == FALSE[1:]:
754 val = False
755 elif data == TRUE[1:]:
756 val = True
757 else:
758 try:
759 val = int(data)
760 except ValueError:
761 val = long(data)
762 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000763 dispatch[INT] = load_int
764
765 def load_binint(self):
766 self.append(mloads('i' + self.read(4)))
767 dispatch[BININT] = load_binint
768
769 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000770 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000771 dispatch[BININT1] = load_binint1
772
773 def load_binint2(self):
774 self.append(mloads('i' + self.read(2) + '\000\000'))
775 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000776
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000777 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000778 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000779 dispatch[LONG] = load_long
780
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000781 def load_long1(self):
782 n = ord(self.read(1))
783 bytes = self.read(n)
784 return decode_long(bytes)
785 dispatch[LONG1] = load_long1
786
787 def load_long4(self):
788 n = mloads('i' + self.read(4))
789 bytes = self.read(n)
790 return decode_long(bytes)
791 dispatch[LONG4] = load_long4
792
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000793 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000794 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000795 dispatch[FLOAT] = load_float
796
Guido van Rossumd3703791998-10-22 20:15:36 +0000797 def load_binfloat(self, unpack=struct.unpack):
798 self.append(unpack('>d', self.read(8))[0])
799 dispatch[BINFLOAT] = load_binfloat
800
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000801 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000802 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000803 for q in _quotes:
804 if rep.startswith(q):
805 if not rep.endswith(q):
806 raise ValueError, "insecure string pickle"
807 rep = rep[len(q):-len(q)]
808 break
809 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000810 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000811 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000812 dispatch[STRING] = load_string
813
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000814 def _is_string_secure(self, s):
815 """Return true if s contains a string that is safe to eval
816
817 The definition of secure string is based on the implementation
818 in cPickle. s is secure as long as it only contains a quoted
819 string and optional trailing whitespace.
820 """
821 q = s[0]
822 if q not in ("'", '"'):
823 return 0
824 # find the closing quote
825 offset = 1
826 i = None
827 while 1:
828 try:
829 i = s.index(q, offset)
830 except ValueError:
831 # if there is an error the first time, there is no
832 # close quote
833 if offset == 1:
834 return 0
835 if s[i-1] != '\\':
836 break
837 # check to see if this one is escaped
838 nslash = 0
839 j = i - 1
840 while j >= offset and s[j] == '\\':
841 j = j - 1
842 nslash = nslash + 1
843 if nslash % 2 == 0:
844 break
845 offset = i + 1
846 for c in s[i+1:]:
847 if ord(c) > 32:
848 return 0
849 return 1
850
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000851 def load_binstring(self):
852 len = mloads('i' + self.read(4))
853 self.append(self.read(len))
854 dispatch[BINSTRING] = load_binstring
855
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000856 def load_unicode(self):
857 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
858 dispatch[UNICODE] = load_unicode
859
860 def load_binunicode(self):
861 len = mloads('i' + self.read(4))
862 self.append(unicode(self.read(len),'utf-8'))
863 dispatch[BINUNICODE] = load_binunicode
864
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000865 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000866 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000867 self.append(self.read(len))
868 dispatch[SHORT_BINSTRING] = load_short_binstring
869
870 def load_tuple(self):
871 k = self.marker()
872 self.stack[k:] = [tuple(self.stack[k+1:])]
873 dispatch[TUPLE] = load_tuple
874
875 def load_empty_tuple(self):
876 self.stack.append(())
877 dispatch[EMPTY_TUPLE] = load_empty_tuple
878
879 def load_empty_list(self):
880 self.stack.append([])
881 dispatch[EMPTY_LIST] = load_empty_list
882
883 def load_empty_dictionary(self):
884 self.stack.append({})
885 dispatch[EMPTY_DICT] = load_empty_dictionary
886
887 def load_list(self):
888 k = self.marker()
889 self.stack[k:] = [self.stack[k+1:]]
890 dispatch[LIST] = load_list
891
892 def load_dict(self):
893 k = self.marker()
894 d = {}
895 items = self.stack[k+1:]
896 for i in range(0, len(items), 2):
897 key = items[i]
898 value = items[i+1]
899 d[key] = value
900 self.stack[k:] = [d]
901 dispatch[DICT] = load_dict
902
903 def load_inst(self):
904 k = self.marker()
905 args = tuple(self.stack[k+1:])
906 del self.stack[k:]
907 module = self.readline()[:-1]
908 name = self.readline()[:-1]
909 klass = self.find_class(module, name)
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
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000916 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000917 except RuntimeError:
918 # In restricted execution, assignment to inst.__class__ is
919 # prohibited
920 pass
921 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000922 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000923 if not hasattr(klass, '__safe_for_unpickling__'):
924 raise UnpicklingError('%s is not safe for unpickling' %
925 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000926 value = apply(klass, args)
927 except TypeError, err:
928 raise TypeError, "in constructor for %s: %s" % (
929 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000930 self.append(value)
931 dispatch[INST] = load_inst
932
933 def load_obj(self):
934 stack = self.stack
935 k = self.marker()
936 klass = stack[k + 1]
937 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000938 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000939 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000940 instantiated = 0
941 if (not args and type(klass) is ClassType and
942 not hasattr(klass, "__getinitargs__")):
943 try:
944 value = _EmptyClass()
945 value.__class__ = klass
946 instantiated = 1
947 except RuntimeError:
948 # In restricted execution, assignment to inst.__class__ is
949 # prohibited
950 pass
951 if not instantiated:
952 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000953 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000954 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000955
956 def load_global(self):
957 module = self.readline()[:-1]
958 name = self.readline()[:-1]
959 klass = self.find_class(module, name)
960 self.append(klass)
961 dispatch[GLOBAL] = load_global
962
963 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +0000964 __import__(module)
965 mod = sys.modules[module]
966 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000967 return klass
968
969 def load_reduce(self):
970 stack = self.stack
971
972 callable = stack[-2]
973 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000974 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000975
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000976 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000977 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000978 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000979 safe = callable.__safe_for_unpickling__
980 except AttributeError:
981 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000982
Tim Petersc32d8242001-04-10 02:48:53 +0000983 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +0000984 raise UnpicklingError, "%s is not safe for " \
985 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000986
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000987 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +0000988 import warnings
989 warnings.warn("The None return argument form of __reduce__ is "
990 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +0000991 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000992 value = callable.__basicnew__()
993 else:
994 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000995 self.append(value)
996 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000997
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000998 def load_pop(self):
999 del self.stack[-1]
1000 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001001
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001002 def load_pop_mark(self):
1003 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001004 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001005 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001006
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001007 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001008 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001009 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001010
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001011 def load_get(self):
1012 self.append(self.memo[self.readline()[:-1]])
1013 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001014
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001015 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001016 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001017 self.append(self.memo[`i`])
1018 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001019
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001020 def load_long_binget(self):
1021 i = mloads('i' + self.read(4))
1022 self.append(self.memo[`i`])
1023 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001024
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001025 def load_put(self):
1026 self.memo[self.readline()[:-1]] = self.stack[-1]
1027 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001028
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001029 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001030 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001031 self.memo[`i`] = self.stack[-1]
1032 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001033
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001034 def load_long_binput(self):
1035 i = mloads('i' + self.read(4))
1036 self.memo[`i`] = self.stack[-1]
1037 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001038
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001039 def load_append(self):
1040 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001041 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001042 list = stack[-1]
1043 list.append(value)
1044 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001045
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001046 def load_appends(self):
1047 stack = self.stack
1048 mark = self.marker()
1049 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001050 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001051 del stack[mark:]
1052 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001053
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001054 def load_setitem(self):
1055 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001056 value = stack.pop()
1057 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001058 dict = stack[-1]
1059 dict[key] = value
1060 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001061
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001062 def load_setitems(self):
1063 stack = self.stack
1064 mark = self.marker()
1065 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001066 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001067 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001068
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001069 del stack[mark:]
1070 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001071
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001072 def load_build(self):
1073 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001074 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001075 inst = stack[-1]
1076 try:
1077 setstate = inst.__setstate__
1078 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001079 try:
1080 inst.__dict__.update(value)
1081 except RuntimeError:
1082 # XXX In restricted execution, the instance's __dict__ is not
1083 # accessible. Use the old way of unpickling the instance
1084 # variables. This is a semantic different when unpickling in
1085 # restricted vs. unrestricted modes.
1086 for k, v in value.items():
1087 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001088 else:
1089 setstate(value)
1090 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001091
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001092 def load_mark(self):
1093 self.append(self.mark)
1094 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001095
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001096 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001097 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001098 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001099 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001100
Guido van Rossume467be61997-12-05 19:42:42 +00001101# Helper class for load_inst/load_obj
1102
1103class _EmptyClass:
1104 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001105
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001106# Encode/decode longs.
1107
1108def encode_long(x):
1109 r"""Encode a long to a two's complement little-ending binary string.
1110 >>> encode_long(255L)
1111 '\xff\x00'
1112 >>> encode_long(32767L)
1113 '\xff\x7f'
1114 >>> encode_long(-256L)
1115 '\x00\xff'
1116 >>> encode_long(-32768L)
1117 '\x00\x80'
1118 >>> encode_long(-128L)
1119 '\x80'
1120 >>> encode_long(127L)
1121 '\x7f'
1122 >>>
1123 """
1124 digits = []
1125 while not -128 <= x < 128:
1126 digits.append(x & 0xff)
1127 x >>= 8
1128 digits.append(x & 0xff)
1129 return "".join(map(chr, digits))
1130
1131def decode_long(data):
1132 r"""Decode a long from a two's complement little-endian binary string.
1133 >>> decode_long("\xff\x00")
1134 255L
1135 >>> decode_long("\xff\x7f")
1136 32767L
1137 >>> decode_long("\x00\xff")
1138 -256L
1139 >>> decode_long("\x00\x80")
1140 -32768L
1141 >>> decode_long("\x80")
1142 -128L
1143 >>> decode_long("\x7f")
1144 127L
1145 """
1146 x = 0L
1147 i = 0L
1148 for c in data:
1149 x |= long(ord(c)) << i
1150 i += 8L
1151 if data and ord(c) >= 0x80:
1152 x -= 1L << i
1153 return x
1154
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001155# Shorthands
1156
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001157try:
1158 from cStringIO import StringIO
1159except ImportError:
1160 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001161
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001162def dump(object, file, proto=1):
1163 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001164
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001165def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001166 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001167 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001168 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001169
1170def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001171 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001172
1173def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001174 file = StringIO(str)
1175 return Unpickler(file).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001176
1177# Doctest
1178
1179def _test():
1180 import doctest
1181 return doctest.testmod()
1182
1183if __name__ == "__main__":
1184 _test()