blob: 6f4ee5c9ace9d1928d3207ed315e836928aa1e68 [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 Rossum44f0ea52003-01-28 04:14:51 +0000153_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
154
Guido van Rossuma48061a1995-01-10 00:31:14 +0000155
Skip Montanaro23bafc62001-02-18 03:10:09 +0000156__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000157del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000158
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000159_quotes = ["'", '"']
160
Guido van Rossuma48061a1995-01-10 00:31:14 +0000161class Pickler:
162
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000163 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000164 """This takes a file-like object for writing a pickle data stream.
165
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000166 The optional proto argument tells the pickler to use the given
167 protocol; supported protocols are 0, 1, 2. The default
168 protocol is 1 (in previous Python versions the default was 0).
169
170 Protocol 1 is more efficient than protocol 0; protocol 2 is
171 more efficient than protocol 1. Protocol 2 is not the default
172 because it is not supported by older Python versions.
173
174 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000175
176 The file parameter must have a write() method that accepts a single
177 string argument. It can thus be an open file object, a StringIO
178 object, or any other custom object that meets this interface.
179
180 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000181 if not 0 <= proto <= 2:
182 raise ValueError, "pickle protocol must be 0, 1 or 2"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000183 self.write = file.write
184 self.memo = {}
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000185 self.proto = proto
186 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000187
Fred Drake7f781c92002-05-01 20:33:53 +0000188 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000189 """Clears the pickler's "memo".
190
191 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000192 pickler has already seen, so that shared or recursive objects are
193 pickled by reference and not by value. This method is useful when
194 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000195
196 """
Fred Drake7f781c92002-05-01 20:33:53 +0000197 self.memo.clear()
198
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000199 def dump(self, object):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000200 """Write a pickled representation of object to the open file object.
201
202 Either the binary or ASCII format will be used, depending on the
203 value of the bin flag passed to the constructor.
204
205 """
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000206 if self.proto >= 2:
207 self.write(PROTO + chr(self.proto))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000208 self.save(object)
209 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000210
Jeremy Hylton3422c992003-01-24 19:29:52 +0000211 def memoize(self, obj):
212 """Store an object in the memo."""
213
Tim Peterse46b73f2003-01-27 21:22:10 +0000214 # The Pickler memo is a dictionary mapping object ids to 2-tuples
215 # that contain the Unpickler memo key and the object being memoized.
216 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000217 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000218 # Pickler memo so that transient objects are kept alive during
219 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000220
Tim Peterse46b73f2003-01-27 21:22:10 +0000221 # The use of the Unpickler memo length as the memo key is just a
222 # convention. The only requirement is that the memo values be unique.
223 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000224 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000225 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000226 memo_len = len(self.memo)
227 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000228 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000229
Tim Petersbb38e302003-01-27 21:25:41 +0000230 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000231 def put(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000232 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000233 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000234 return BINPUT + chr(i)
235 else:
236 return LONG_BINPUT + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000238 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000239
Tim Petersbb38e302003-01-27 21:25:41 +0000240 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000241 def get(self, i, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000242 if self.bin:
Tim Petersc32d8242001-04-10 02:48:53 +0000243 if i < 256:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000244 return BINGET + chr(i)
245 else:
246 return LONG_BINGET + pack("<i", i)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000247
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000248 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000249
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000250 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000251 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000252
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000253 pid = self.persistent_id(object)
254 if pid is not None:
255 self.save_pers(pid)
256 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000257
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000258 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000259
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000260 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000261
Raymond Hettinger54f02222002-06-01 14:18:47 +0000262 if d in memo:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000263 self.write(self.get(memo[d][0]))
264 return
265
266 try:
267 f = self.dispatch[t]
268 except KeyError:
Tim Petersb32a8312003-01-28 00:48:09 +0000269 pass
270 else:
271 f(self, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000272 return
273
Tim Petersb32a8312003-01-28 00:48:09 +0000274 # The dispatch table doesn't know about type t.
275 try:
276 issc = issubclass(t, TypeType)
277 except TypeError: # t is not a class
278 issc = 0
279 if issc:
280 self.save_global(object)
281 return
282
283 try:
284 reduce = dispatch_table[t]
285 except KeyError:
286 try:
287 reduce = object.__reduce__
288 except AttributeError:
289 raise PicklingError, \
290 "can't pickle %s object: %s" % (`t.__name__`,
291 `object`)
292 else:
293 tup = reduce()
294 else:
295 tup = reduce(object)
296
297 if type(tup) is StringType:
298 self.save_global(object, tup)
299 return
300
301 if type(tup) is not TupleType:
302 raise PicklingError, "Value returned by %s must be a " \
303 "tuple" % reduce
304
305 l = len(tup)
306
307 if (l != 2) and (l != 3):
308 raise PicklingError, "tuple returned by %s must contain " \
309 "only two or three elements" % reduce
310
311 callable = tup[0]
312 arg_tup = tup[1]
313
314 if l > 2:
315 state = tup[2]
316 else:
317 state = None
318
319 if type(arg_tup) is not TupleType and arg_tup is not None:
320 raise PicklingError, "Second element of tuple returned " \
321 "by %s must be a tuple" % reduce
322
323 self.save_reduce(callable, arg_tup, state)
Tim Peters518df0d2003-01-28 01:00:38 +0000324 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000325
326 def persistent_id(self, object):
327 return None
328
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000329 def save_pers(self, pid):
Tim Petersbd1cdb92003-01-28 01:03:10 +0000330 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000331 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000332 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000333 else:
334 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335
Jeremy Hylton3422c992003-01-24 19:29:52 +0000336 def save_reduce(self, acallable, arg_tup, state = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000337 write = self.write
338 save = self.save
339
Jeremy Hylton3422c992003-01-24 19:29:52 +0000340 if not callable(acallable):
341 raise PicklingError("__reduce__() must return callable as "
342 "first argument, not %s" % `acallable`)
Tim Peters22a449a2003-01-27 20:16:36 +0000343
Jeremy Hylton3422c992003-01-24 19:29:52 +0000344 save(acallable)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000345 save(arg_tup)
346 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000347
Tim Petersc32d8242001-04-10 02:48:53 +0000348 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000349 save(state)
350 write(BUILD)
351
352 dispatch = {}
353
354 def save_none(self, object):
355 self.write(NONE)
356 dispatch[NoneType] = save_none
357
Guido van Rossum77f6a652002-04-03 22:41:51 +0000358 def save_bool(self, object):
Guido van Rossum7d97d312003-01-28 04:25:27 +0000359 if self.proto >= 2:
360 self.write(object and NEWTRUE or NEWFALSE)
361 else:
362 self.write(object and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000363 dispatch[bool] = save_bool
364
Guido van Rossum5c938d02003-01-28 03:03:08 +0000365 def save_int(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000366 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000367 # If the int is small enough to fit in a signed 4-byte 2's-comp
368 # format, we can store it more efficiently than the general
369 # case.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000370 # First one- and two-byte unsigned ints:
371 if object >= 0:
Tim Peters8fda7bc2003-01-28 03:40:52 +0000372 if object <= 0xff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000373 self.write(BININT1 + chr(object))
374 return
Tim Peters8fda7bc2003-01-28 03:40:52 +0000375 if object <= 0xffff:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000376 self.write(BININT2 + chr(object&0xff) + chr(object>>8))
377 return
378 # Next check for 4-byte signed ints:
Tim Peters44714002001-04-10 05:02:52 +0000379 high_bits = object >> 31 # note that Python shift sign-extends
Tim Petersd95c2df2003-01-28 03:41:54 +0000380 if high_bits == 0 or high_bits == -1:
Tim Peters44714002001-04-10 05:02:52 +0000381 # All high bits are copies of bit 2**31, so the value
382 # fits in a 4-byte signed int.
Guido van Rossum5c938d02003-01-28 03:03:08 +0000383 self.write(BININT + pack("<i", object))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000384 return
Tim Peters44714002001-04-10 05:02:52 +0000385 # Text pickle, or int too big to fit in signed 4-byte format.
386 self.write(INT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000387 dispatch[IntType] = save_int
388
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000389 def save_long(self, object, pack=struct.pack):
390 if self.proto >= 2:
391 bytes = encode_long(object)
392 n = len(bytes)
393 if n < 256:
394 self.write(LONG1 + chr(n) + bytes)
395 else:
396 self.write(LONG4 + pack("<i", n) + bytes)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000397 self.write(LONG + `object` + '\n')
398 dispatch[LongType] = save_long
399
Guido van Rossumd3703791998-10-22 20:15:36 +0000400 def save_float(self, object, pack=struct.pack):
401 if self.bin:
402 self.write(BINFLOAT + pack('>d', object))
403 else:
404 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000405 dispatch[FloatType] = save_float
406
Guido van Rossum5c938d02003-01-28 03:03:08 +0000407 def save_string(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000408 if self.bin:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000409 n = len(object)
410 if n < 256:
411 self.write(SHORT_BINSTRING + chr(n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000412 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000413 self.write(BINSTRING + pack("<i", n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000414 else:
415 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000416 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000417 dispatch[StringType] = save_string
418
Guido van Rossum5c938d02003-01-28 03:03:08 +0000419 def save_unicode(self, object, pack=struct.pack):
Tim Petersc32d8242001-04-10 02:48:53 +0000420 if self.bin:
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000421 encoding = object.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000422 n = len(encoding)
Guido van Rossum5c938d02003-01-28 03:03:08 +0000423 self.write(BINUNICODE + pack("<i", n) + encoding)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000424 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000425 object = object.replace("\\", "\\u005c")
426 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000427 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000428 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000429 dispatch[UnicodeType] = save_unicode
430
Guido van Rossum31584cb2001-01-22 14:53:29 +0000431 if StringType == UnicodeType:
432 # This is true for Jython
Guido van Rossum5c938d02003-01-28 03:03:08 +0000433 def save_string(self, object, pack=struct.pack):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000434 unicode = object.isunicode()
435
Tim Petersc32d8242001-04-10 02:48:53 +0000436 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000437 if unicode:
438 object = object.encode("utf-8")
439 l = len(object)
Tim Petersc32d8242001-04-10 02:48:53 +0000440 if l < 256 and not unicode:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000441 self.write(SHORT_BINSTRING + chr(l) + object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000442 else:
Guido van Rossum5c938d02003-01-28 03:03:08 +0000443 s = pack("<i", l)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000444 if unicode:
445 self.write(BINUNICODE + s + object)
446 else:
447 self.write(BINSTRING + s + object)
448 else:
Tim Peters658cba62001-02-09 20:06:00 +0000449 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000450 object = object.replace("\\", "\\u005c")
451 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000452 object = object.encode('raw-unicode-escape')
453 self.write(UNICODE + object + '\n')
454 else:
455 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000456 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000457 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000458
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000459 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000460 write = self.write
461 save = self.save
462 memo = self.memo
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000463 proto = self.proto
464
465 if proto >= 1:
466 n = len(object)
467 if n <= 3:
468 if not object:
469 write(EMPTY_TUPLE)
470 return
471 if proto >= 2:
472 for element in object:
473 save(element)
Tim Petersff57bff2003-01-28 05:34:53 +0000474 # Subtle. Same as in the big comment below.
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000475 if id(object) in memo:
476 get = self.get(memo[id(object)][0])
Tim Petersff57bff2003-01-28 05:34:53 +0000477 write(POP * n + get)
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000478 else:
479 write(_tuplesize2code[n])
Tim Petersff57bff2003-01-28 05:34:53 +0000480 self.memoize(object)
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000481 return
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000482
Tim Petersff57bff2003-01-28 05:34:53 +0000483 # proto 0, or proto 1 and tuple isn't empty, or proto > 1 and tuple
484 # has more than 3 elements.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000485 write(MARK)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000486 for element in object:
487 save(element)
488
Tim Petersf558da02003-01-28 02:09:55 +0000489 if object and id(object) in memo:
490 # Subtle. d was not in memo when we entered save_tuple(), so
491 # the process of saving the tuple's elements must have saved
492 # the tuple itself: the tuple is recursive. The proper action
493 # now is to throw away everything we put on the stack, and
494 # simply GET the tuple (it's already constructed). This check
495 # could have been done in the "for element" loop instead, but
496 # recursive tuples are a rare thing.
497 get = self.get(memo[id(object)][0])
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000498 if proto:
Tim Petersf558da02003-01-28 02:09:55 +0000499 write(POP_MARK + get)
500 else: # proto 0 -- POP_MARK not available
501 write(POP * (len(object) + 1) + get)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000502 return
503
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000504 # No recursion (including the empty-tuple case for protocol 0).
Tim Peters518df0d2003-01-28 01:00:38 +0000505 self.write(TUPLE)
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000506 self.memoize(object) # XXX shouldn't memoize empty tuple?!
Jeremy Hylton3422c992003-01-24 19:29:52 +0000507
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000508 dispatch[TupleType] = save_tuple
509
510 def save_empty_tuple(self, object):
511 self.write(EMPTY_TUPLE)
512
513 def save_list(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000514 write = self.write
515 save = self.save
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_LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000519 self.memoize(object)
520 n = len(object)
521 if n > 1:
522 write(MARK)
523 for element in object:
524 save(element)
525 write(APPENDS)
526 elif n:
527 assert n == 1
528 save(object[0])
529 write(APPEND)
530 # else the list is empty, and we're already done
531
532 else: # proto 0 -- can't use EMPTY_LIST or APPENDS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000533 write(MARK + LIST)
Tim Peters21c18f02003-01-28 01:15:46 +0000534 self.memoize(object)
535 for element in object:
536 save(element)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000537 write(APPEND)
538
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000539 dispatch[ListType] = save_list
540
541 def save_dict(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000542 write = self.write
543 save = self.save
Tim Peters064567e2003-01-28 01:34:43 +0000544 items = object.iteritems()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000545
Tim Petersc32d8242001-04-10 02:48:53 +0000546 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000547 write(EMPTY_DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000548 self.memoize(object)
549 if len(object) > 1:
550 write(MARK)
551 for key, value in items:
552 save(key)
553 save(value)
554 write(SETITEMS)
555 return
556
557 else: # proto 0 -- can't use EMPTY_DICT or SETITEMS
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000558 write(MARK + DICT)
Tim Peters064567e2003-01-28 01:34:43 +0000559 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000560
Tim Peters064567e2003-01-28 01:34:43 +0000561 # proto 0 or len(object) < 2
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000562 for key, value in items:
563 save(key)
564 save(value)
Tim Peters064567e2003-01-28 01:34:43 +0000565 write(SETITEM)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000566
567 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000568 if not PyStringMap is None:
569 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570
571 def save_inst(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000572 cls = object.__class__
573
574 memo = self.memo
575 write = self.write
576 save = self.save
577
578 if hasattr(object, '__getinitargs__'):
579 args = object.__getinitargs__()
580 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000581 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000582 else:
583 args = ()
584
585 write(MARK)
586
Tim Petersc32d8242001-04-10 02:48:53 +0000587 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000588 save(cls)
Tim Peters3b769832003-01-28 03:51:36 +0000589 for arg in args:
590 save(arg)
591 write(OBJ)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000592 else:
Tim Peters3b769832003-01-28 03:51:36 +0000593 for arg in args:
594 save(arg)
595 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000596
Tim Peters3b769832003-01-28 03:51:36 +0000597 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000598
599 try:
600 getstate = object.__getstate__
601 except AttributeError:
602 stuff = object.__dict__
603 else:
604 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000605 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000606 save(stuff)
607 write(BUILD)
Tim Peters3b769832003-01-28 03:51:36 +0000608
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000609 dispatch[InstanceType] = save_inst
610
611 def save_global(self, object, name = None):
612 write = self.write
613 memo = self.memo
614
Tim Petersc32d8242001-04-10 02:48:53 +0000615 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000616 name = object.__name__
617
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000618 try:
619 module = object.__module__
620 except AttributeError:
621 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000622
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000623 try:
624 __import__(module)
625 mod = sys.modules[module]
626 klass = getattr(mod, name)
627 except (ImportError, KeyError, AttributeError):
628 raise PicklingError(
629 "Can't pickle %r: it's not found as %s.%s" %
630 (object, module, name))
631 else:
632 if klass is not object:
633 raise PicklingError(
634 "Can't pickle %r: it's not the same object as %s.%s" %
635 (object, module, name))
636
Tim Peters518df0d2003-01-28 01:00:38 +0000637 write(GLOBAL + module + '\n' + name + '\n')
638 self.memoize(object)
Tim Peters3b769832003-01-28 03:51:36 +0000639
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000640 dispatch[ClassType] = save_global
641 dispatch[FunctionType] = save_global
642 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000644
Guido van Rossuma48061a1995-01-10 00:31:14 +0000645
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000646def _keep_alive(x, memo):
647 """Keeps a reference to the object x in the memo.
648
649 Because we remember objects by their id, we have
650 to assure that possibly temporary objects are kept
651 alive by referencing them.
652 We store a reference at the id of the memo, which should
653 normally not be used unless someone tries to deepcopy
654 the memo itself...
655 """
656 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000657 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000658 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000659 # aha, this is the first one :-)
660 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000661
662
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000663classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000664
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000665def whichmodule(func, funcname):
666 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000667
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000668 Search sys.modules for the module.
669 Cache in classmap.
670 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000671 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000672 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000673 if func in classmap:
674 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000675
676 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000677 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000678 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000679 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000680 hasattr(module, funcname) and \
681 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000682 break
683 else:
684 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000685 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000686 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000687
688
689class Unpickler:
690
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000692 """This takes a file-like object for reading a pickle data stream.
693
694 This class automatically determines whether the data stream was
695 written in binary mode or not, so it does not need a flag as in
696 the Pickler class factory.
697
698 The file-like object must have two methods, a read() method that
699 takes an integer argument, and a readline() method that requires no
700 arguments. Both methods should return a string. Thus file-like
701 object can be a file object opened for reading, a StringIO object,
702 or any other custom object that meets this interface.
703
704 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000705 self.readline = file.readline
706 self.read = file.read
707 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000708
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000709 def load(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000710 """Read a pickled object representation from the open file object.
711
712 Return the reconstituted object hierarchy specified in the file
713 object.
714
715 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000716 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000717 self.stack = []
718 self.append = self.stack.append
719 read = self.read
720 dispatch = self.dispatch
721 try:
722 while 1:
723 key = read(1)
724 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000725 except _Stop, stopinst:
726 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000727
Tim Petersc23d18a2003-01-28 01:41:51 +0000728 # Return largest index k such that self.stack[k] is self.mark.
729 # If the stack doesn't contain a mark, eventually raises IndexError.
730 # This could be sped by maintaining another stack, of indices at which
731 # the mark appears. For that matter, the latter stack would suffice,
732 # and we wouldn't need to push mark objects on self.stack at all.
733 # Doing so is probably a good thing, though, since if the pickle is
734 # corrupt (or hostile) we may get a clue from finding self.mark embedded
735 # in unpickled objects.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000736 def marker(self):
737 stack = self.stack
738 mark = self.mark
739 k = len(stack)-1
740 while stack[k] is not mark: k = k-1
741 return k
742
743 dispatch = {}
744
745 def load_eof(self):
746 raise EOFError
747 dispatch[''] = load_eof
748
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000749 def load_proto(self):
750 proto = ord(self.read(1))
751 if not 0 <= proto <= 2:
752 raise ValueError, "unsupported pickle protocol: %d" % proto
753 dispatch[PROTO] = load_proto
754
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000755 def load_persid(self):
756 pid = self.readline()[:-1]
757 self.append(self.persistent_load(pid))
758 dispatch[PERSID] = load_persid
759
760 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000761 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000762 self.append(self.persistent_load(pid))
763 dispatch[BINPERSID] = load_binpersid
764
765 def load_none(self):
766 self.append(None)
767 dispatch[NONE] = load_none
768
Guido van Rossum7d97d312003-01-28 04:25:27 +0000769 def load_false(self):
770 self.append(False)
771 dispatch[NEWFALSE] = load_false
772
773 def load_true(self):
774 self.append(True)
775 dispatch[NEWTRUE] = load_true
776
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000777 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000778 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000779 if data == FALSE[1:]:
780 val = False
781 elif data == TRUE[1:]:
782 val = True
783 else:
784 try:
785 val = int(data)
786 except ValueError:
787 val = long(data)
788 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000789 dispatch[INT] = load_int
790
791 def load_binint(self):
792 self.append(mloads('i' + self.read(4)))
793 dispatch[BININT] = load_binint
794
795 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000796 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000797 dispatch[BININT1] = load_binint1
798
799 def load_binint2(self):
800 self.append(mloads('i' + self.read(2) + '\000\000'))
801 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000802
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000803 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000804 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000805 dispatch[LONG] = load_long
806
Guido van Rossumd6c9e632003-01-28 03:49:52 +0000807 def load_long1(self):
808 n = ord(self.read(1))
809 bytes = self.read(n)
810 return decode_long(bytes)
811 dispatch[LONG1] = load_long1
812
813 def load_long4(self):
814 n = mloads('i' + self.read(4))
815 bytes = self.read(n)
816 return decode_long(bytes)
817 dispatch[LONG4] = load_long4
818
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000819 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000820 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000821 dispatch[FLOAT] = load_float
822
Guido van Rossumd3703791998-10-22 20:15:36 +0000823 def load_binfloat(self, unpack=struct.unpack):
824 self.append(unpack('>d', self.read(8))[0])
825 dispatch[BINFLOAT] = load_binfloat
826
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000827 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000828 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000829 for q in _quotes:
830 if rep.startswith(q):
831 if not rep.endswith(q):
832 raise ValueError, "insecure string pickle"
833 rep = rep[len(q):-len(q)]
834 break
835 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000836 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000837 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000838 dispatch[STRING] = load_string
839
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000840 def _is_string_secure(self, s):
841 """Return true if s contains a string that is safe to eval
842
843 The definition of secure string is based on the implementation
844 in cPickle. s is secure as long as it only contains a quoted
845 string and optional trailing whitespace.
846 """
847 q = s[0]
848 if q not in ("'", '"'):
849 return 0
850 # find the closing quote
851 offset = 1
852 i = None
853 while 1:
854 try:
855 i = s.index(q, offset)
856 except ValueError:
857 # if there is an error the first time, there is no
858 # close quote
859 if offset == 1:
860 return 0
861 if s[i-1] != '\\':
862 break
863 # check to see if this one is escaped
864 nslash = 0
865 j = i - 1
866 while j >= offset and s[j] == '\\':
867 j = j - 1
868 nslash = nslash + 1
869 if nslash % 2 == 0:
870 break
871 offset = i + 1
872 for c in s[i+1:]:
873 if ord(c) > 32:
874 return 0
875 return 1
876
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000877 def load_binstring(self):
878 len = mloads('i' + self.read(4))
879 self.append(self.read(len))
880 dispatch[BINSTRING] = load_binstring
881
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000882 def load_unicode(self):
883 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
884 dispatch[UNICODE] = load_unicode
885
886 def load_binunicode(self):
887 len = mloads('i' + self.read(4))
888 self.append(unicode(self.read(len),'utf-8'))
889 dispatch[BINUNICODE] = load_binunicode
890
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000891 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000892 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000893 self.append(self.read(len))
894 dispatch[SHORT_BINSTRING] = load_short_binstring
895
896 def load_tuple(self):
897 k = self.marker()
898 self.stack[k:] = [tuple(self.stack[k+1:])]
899 dispatch[TUPLE] = load_tuple
900
901 def load_empty_tuple(self):
902 self.stack.append(())
903 dispatch[EMPTY_TUPLE] = load_empty_tuple
904
Guido van Rossum44f0ea52003-01-28 04:14:51 +0000905 def load_tuple1(self):
906 self.stack[-1] = (self.stack[-1],)
907 dispatch[TUPLE1] = load_tuple1
908
909 def load_tuple2(self):
910 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
911 dispatch[TUPLE2] = load_tuple2
912
913 def load_tuple3(self):
914 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
915 dispatch[TUPLE3] = load_tuple3
916
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000917 def load_empty_list(self):
918 self.stack.append([])
919 dispatch[EMPTY_LIST] = load_empty_list
920
921 def load_empty_dictionary(self):
922 self.stack.append({})
923 dispatch[EMPTY_DICT] = load_empty_dictionary
924
925 def load_list(self):
926 k = self.marker()
927 self.stack[k:] = [self.stack[k+1:]]
928 dispatch[LIST] = load_list
929
930 def load_dict(self):
931 k = self.marker()
932 d = {}
933 items = self.stack[k+1:]
934 for i in range(0, len(items), 2):
935 key = items[i]
936 value = items[i+1]
937 d[key] = value
938 self.stack[k:] = [d]
939 dispatch[DICT] = load_dict
940
941 def load_inst(self):
942 k = self.marker()
943 args = tuple(self.stack[k+1:])
944 del self.stack[k:]
945 module = self.readline()[:-1]
946 name = self.readline()[:-1]
947 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000948 instantiated = 0
949 if (not args and type(klass) is ClassType and
950 not hasattr(klass, "__getinitargs__")):
951 try:
952 value = _EmptyClass()
953 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000954 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000955 except RuntimeError:
956 # In restricted execution, assignment to inst.__class__ is
957 # prohibited
958 pass
959 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000960 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000961 if not hasattr(klass, '__safe_for_unpickling__'):
962 raise UnpicklingError('%s is not safe for unpickling' %
963 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000964 value = apply(klass, args)
965 except TypeError, err:
966 raise TypeError, "in constructor for %s: %s" % (
967 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000968 self.append(value)
969 dispatch[INST] = load_inst
970
971 def load_obj(self):
972 stack = self.stack
973 k = self.marker()
974 klass = stack[k + 1]
975 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000976 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000977 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000978 instantiated = 0
979 if (not args and type(klass) is ClassType and
980 not hasattr(klass, "__getinitargs__")):
981 try:
982 value = _EmptyClass()
983 value.__class__ = klass
984 instantiated = 1
985 except RuntimeError:
986 # In restricted execution, assignment to inst.__class__ is
987 # prohibited
988 pass
989 if not instantiated:
990 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000991 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000992 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000993
994 def load_global(self):
995 module = self.readline()[:-1]
996 name = self.readline()[:-1]
997 klass = self.find_class(module, name)
998 self.append(klass)
999 dispatch[GLOBAL] = load_global
1000
1001 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +00001002 __import__(module)
1003 mod = sys.modules[module]
1004 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001005 return klass
1006
1007 def load_reduce(self):
1008 stack = self.stack
1009
1010 callable = stack[-2]
1011 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001012 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001013
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001014 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +00001015 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001016 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001017 safe = callable.__safe_for_unpickling__
1018 except AttributeError:
1019 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +00001020
Tim Petersc32d8242001-04-10 02:48:53 +00001021 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +00001022 raise UnpicklingError, "%s is not safe for " \
1023 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +00001024
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001025 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +00001026 import warnings
1027 warnings.warn("The None return argument form of __reduce__ is "
1028 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +00001029 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001030 value = callable.__basicnew__()
1031 else:
1032 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001033 self.append(value)
1034 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +00001035
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001036 def load_pop(self):
1037 del self.stack[-1]
1038 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +00001039
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001040 def load_pop_mark(self):
1041 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001042 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001043 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001044
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001045 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +00001046 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001047 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +00001048
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001049 def load_get(self):
1050 self.append(self.memo[self.readline()[:-1]])
1051 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +00001052
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001053 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001054 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001055 self.append(self.memo[`i`])
1056 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001057
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001058 def load_long_binget(self):
1059 i = mloads('i' + self.read(4))
1060 self.append(self.memo[`i`])
1061 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +00001062
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001063 def load_put(self):
1064 self.memo[self.readline()[:-1]] = self.stack[-1]
1065 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +00001066
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001067 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +00001068 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001069 self.memo[`i`] = self.stack[-1]
1070 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001071
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001072 def load_long_binput(self):
1073 i = mloads('i' + self.read(4))
1074 self.memo[`i`] = self.stack[-1]
1075 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001076
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001077 def load_append(self):
1078 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001079 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001080 list = stack[-1]
1081 list.append(value)
1082 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001083
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001084 def load_appends(self):
1085 stack = self.stack
1086 mark = self.marker()
1087 list = stack[mark - 1]
Tim Peters209ad952003-01-28 01:44:45 +00001088 list.extend(stack[mark + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001089 del stack[mark:]
1090 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001091
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001092 def load_setitem(self):
1093 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001094 value = stack.pop()
1095 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001096 dict = stack[-1]
1097 dict[key] = value
1098 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001099
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001100 def load_setitems(self):
1101 stack = self.stack
1102 mark = self.marker()
1103 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001104 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001105 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001106
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001107 del stack[mark:]
1108 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001109
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001110 def load_build(self):
1111 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001112 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001113 inst = stack[-1]
1114 try:
1115 setstate = inst.__setstate__
1116 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001117 try:
1118 inst.__dict__.update(value)
1119 except RuntimeError:
1120 # XXX In restricted execution, the instance's __dict__ is not
1121 # accessible. Use the old way of unpickling the instance
1122 # variables. This is a semantic different when unpickling in
1123 # restricted vs. unrestricted modes.
1124 for k, v in value.items():
1125 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001126 else:
1127 setstate(value)
1128 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001129
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001130 def load_mark(self):
1131 self.append(self.mark)
1132 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001133
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001134 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001135 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001136 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001137 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001138
Guido van Rossume467be61997-12-05 19:42:42 +00001139# Helper class for load_inst/load_obj
1140
1141class _EmptyClass:
1142 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001143
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001144# Encode/decode longs.
1145
1146def encode_long(x):
1147 r"""Encode a long to a two's complement little-ending binary string.
1148 >>> encode_long(255L)
1149 '\xff\x00'
1150 >>> encode_long(32767L)
1151 '\xff\x7f'
1152 >>> encode_long(-256L)
1153 '\x00\xff'
1154 >>> encode_long(-32768L)
1155 '\x00\x80'
1156 >>> encode_long(-128L)
1157 '\x80'
1158 >>> encode_long(127L)
1159 '\x7f'
1160 >>>
1161 """
1162 digits = []
1163 while not -128 <= x < 128:
1164 digits.append(x & 0xff)
1165 x >>= 8
1166 digits.append(x & 0xff)
1167 return "".join(map(chr, digits))
1168
1169def decode_long(data):
1170 r"""Decode a long from a two's complement little-endian binary string.
1171 >>> decode_long("\xff\x00")
1172 255L
1173 >>> decode_long("\xff\x7f")
1174 32767L
1175 >>> decode_long("\x00\xff")
1176 -256L
1177 >>> decode_long("\x00\x80")
1178 -32768L
1179 >>> decode_long("\x80")
1180 -128L
1181 >>> decode_long("\x7f")
1182 127L
1183 """
1184 x = 0L
1185 i = 0L
1186 for c in data:
1187 x |= long(ord(c)) << i
1188 i += 8L
1189 if data and ord(c) >= 0x80:
1190 x -= 1L << i
1191 return x
1192
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001193# Shorthands
1194
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001195try:
1196 from cStringIO import StringIO
1197except ImportError:
1198 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001199
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001200def dump(object, file, proto=1):
1201 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001202
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001203def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001204 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001205 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001206 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001207
1208def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001209 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001210
1211def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001212 file = StringIO(str)
1213 return Unpickler(file).load()
Guido van Rossumd6c9e632003-01-28 03:49:52 +00001214
1215# Doctest
1216
1217def _test():
1218 import doctest
1219 return doctest.testmod()
1220
1221if __name__ == "__main__":
1222 _test()