blob: 80eaf0f1c3d9a67accb4ecf3c3ff94a25fc1ec44 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Create portable serialized representations of Python objects.
Guido van Rossuma48061a1995-01-10 00:31:14 +00002
Guido van Rossume467be61997-12-05 19:42:42 +00003See module cPickle for a (much) faster implementation.
4See module copy_reg for a mechanism for registering custom picklers.
Tim Peters22a449a2003-01-27 20:16:36 +00005See module pickletools source for extensive comments.
Guido van Rossuma48061a1995-01-10 00:31:14 +00006
Guido van Rossume467be61997-12-05 19:42:42 +00007Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00008
Guido van Rossume467be61997-12-05 19:42:42 +00009 Pickler
10 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000011
Guido van Rossume467be61997-12-05 19:42:42 +000012Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000013
Guido van Rossume467be61997-12-05 19:42:42 +000014 dump(object, file)
15 dumps(object) -> string
16 load(file) -> object
17 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000018
Guido van Rossume467be61997-12-05 19:42:42 +000019Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000020
Fred Drakefe82acc1998-02-13 03:24:48 +000021 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000022 format_version
23 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000024
Guido van Rossuma48061a1995-01-10 00:31:14 +000025"""
26
Guido van Rossum743d17e1998-09-15 20:25:57 +000027__version__ = "$Revision$" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +000028
29from types import *
Guido van Rossum4fb5b281997-09-12 20:07:24 +000030from copy_reg import dispatch_table, safe_constructors
Guido van Rossumd3703791998-10-22 20:15:36 +000031import marshal
32import sys
33import struct
Skip Montanaro23bafc62001-02-18 03:10:09 +000034import re
Guido van Rossuma48061a1995-01-10 00:31:14 +000035
Skip Montanaro352674d2001-02-07 23:14:30 +000036__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
37 "Unpickler", "dump", "dumps", "load", "loads"]
38
Guido van Rossumf29d3d62003-01-27 22:47:53 +000039# These are purely informational; no code usues these
40format_version = "2.0" # File format version we write
41compatible_formats = ["1.0", # Original protocol 0
42 "1.1", # Protocol 0 with class supprt added
43 "1.2", # Original protocol 1
44 "1.3", # Protocol 1 with BINFLOAT added
45 "2.0", # Protocol 2
46 ] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000047
48mdumps = marshal.dumps
49mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000050
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000051class PickleError(Exception):
Neal Norwitzefbb67b2002-05-30 12:12:04 +000052 """A common base class for the other pickling exceptions."""
Raymond Hettingeraef22fb2002-05-29 16:18:42 +000053 pass
54
55class PicklingError(PickleError):
56 """This exception is raised when an unpicklable object is passed to the
57 dump() method.
58
59 """
60 pass
61
62class UnpicklingError(PickleError):
63 """This exception is raised when there is a problem unpickling an object,
64 such as a security violation.
65
66 Note that other exceptions may also be raised during unpickling, including
67 (but not necessarily limited to) AttributeError, EOFError, ImportError,
68 and IndexError.
69
70 """
71 pass
Guido van Rossum7849da81995-03-09 14:08:35 +000072
Guido van Rossumff871742000-12-13 18:11:56 +000073class _Stop(Exception):
74 def __init__(self, value):
75 self.value = value
76
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000077try:
78 from org.python.core import PyStringMap
79except ImportError:
80 PyStringMap = None
81
Guido van Rossumdbb718f2001-09-21 19:22:34 +000082try:
83 UnicodeType
84except NameError:
85 UnicodeType = None
86
Tim Peters22a449a2003-01-27 20:16:36 +000087# Pickle opcodes. See pickletools.py for extensive docs. The listing
88# here is in kind-of alphabetical order of 1-character pickle code.
89# pickletools groups them by purpose.
Guido van Rossumdbb718f2001-09-21 19:22:34 +000090
Tim Peters22a449a2003-01-27 20:16:36 +000091MARK = '(' # push special markobject on stack
92STOP = '.' # every pickle ends with STOP
93POP = '0' # discard topmost stack item
94POP_MARK = '1' # discard stack top through topmost markobject
95DUP = '2' # duplicate top stack item
96FLOAT = 'F' # push float object; decimal string argument
97INT = 'I' # push integer or bool; decimal string argument
98BININT = 'J' # push four-byte signed int
99BININT1 = 'K' # push 1-byte unsigned int
100LONG = 'L' # push long; decimal string argument
101BININT2 = 'M' # push 2-byte unsigned int
102NONE = 'N' # push None
103PERSID = 'P' # push persistent object; id is taken from string arg
104BINPERSID = 'Q' # " " " ; " " " " stack
105REDUCE = 'R' # apply callable to argtuple, both on stack
106STRING = 'S' # push string; NL-terminated string argument
107BINSTRING = 'T' # push string; counted binary string argument
108SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes
109UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument
110BINUNICODE = 'X' # " " " ; counted UTF-8 string argument
111APPEND = 'a' # append stack top to list below it
112BUILD = 'b' # call __setstate__ or __dict__.update()
113GLOBAL = 'c' # push self.find_class(modname, name); 2 string args
114DICT = 'd' # build a dict from stack items
115EMPTY_DICT = '}' # push empty dict
116APPENDS = 'e' # extend list on stack by topmost stack slice
117GET = 'g' # push item from memo on stack; index is string arg
118BINGET = 'h' # " " " " " " ; " " 1-byte arg
119INST = 'i' # build & push class instance
120LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg
121LIST = 'l' # build list from topmost stack items
122EMPTY_LIST = ']' # push empty list
123OBJ = 'o' # build & push class instance
124PUT = 'p' # store stack top in memo; index is string arg
125BINPUT = 'q' # " " " " " ; " " 1-byte arg
126LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg
127SETITEM = 's' # add key+value pair to dict
128TUPLE = 't' # build tuple from topmost stack items
129EMPTY_TUPLE = ')' # push empty tuple
130SETITEMS = 'u' # modify dict by adding topmost key+value pairs
131BINFLOAT = 'G' # push float; arg is 8-byte float encoding
132
133TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py
134FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py
Guido van Rossum77f6a652002-04-03 22:41:51 +0000135
Tim Peterse1054782003-01-28 00:22:12 +0000136# Protocol 2 (not yet implemented).
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000137
Tim Peterse1054782003-01-28 00:22:12 +0000138PROTO = '\x80' # identify pickle protocol
139NEWOBJ = '\x81' # build object by applying cls.__new__ to argtuple
140EXT1 = '\x82' # push object from extension registry; 1-byte index
141EXT2 = '\x83' # ditto, but 2-byte index
142EXT4 = '\x84' # ditto, but 4-byte index
143TUPLE1 = '\x85' # build 1-tuple from stack top
144TUPLE2 = '\x86' # build 2-tuple from two topmost stack items
145TUPLE3 = '\x87' # build 3-tuple from three topmost stack items
146NEWTRUE = '\x88' # push True
147NEWFALSE = '\x89' # push False
148LONG1 = '\x8a' # push long from < 256 bytes
149LONG4 = '\x8b' # push really big long
Guido van Rossum5a2d8f52003-01-27 21:44:25 +0000150
Guido van Rossuma48061a1995-01-10 00:31:14 +0000151
Skip Montanaro23bafc62001-02-18 03:10:09 +0000152__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
Neal Norwitzd5ba4ae2002-02-11 18:12:06 +0000153del x
Skip Montanaro23bafc62001-02-18 03:10:09 +0000154
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000155_quotes = ["'", '"']
156
Guido van Rossuma48061a1995-01-10 00:31:14 +0000157class Pickler:
158
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000159 def __init__(self, file, proto=1):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000160 """This takes a file-like object for writing a pickle data stream.
161
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000162 The optional proto argument tells the pickler to use the given
163 protocol; supported protocols are 0, 1, 2. The default
164 protocol is 1 (in previous Python versions the default was 0).
165
166 Protocol 1 is more efficient than protocol 0; protocol 2 is
167 more efficient than protocol 1. Protocol 2 is not the default
168 because it is not supported by older Python versions.
169
170 XXX Protocol 2 is not yet implemented.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000171
172 The file parameter must have a write() method that accepts a single
173 string argument. It can thus be an open file object, a StringIO
174 object, or any other custom object that meets this interface.
175
176 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000177 self.write = file.write
178 self.memo = {}
Guido van Rossumf29d3d62003-01-27 22:47:53 +0000179 self.proto = proto
180 self.bin = proto >= 1
Guido van Rossuma48061a1995-01-10 00:31:14 +0000181
Fred Drake7f781c92002-05-01 20:33:53 +0000182 def clear_memo(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000183 """Clears the pickler's "memo".
184
185 The memo is the data structure that remembers which objects the
Tim Petersb377f8a2003-01-28 00:23:36 +0000186 pickler has already seen, so that shared or recursive objects are
187 pickled by reference and not by value. This method is useful when
188 re-using picklers.
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000189
190 """
Fred Drake7f781c92002-05-01 20:33:53 +0000191 self.memo.clear()
192
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000193 def dump(self, object):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000194 """Write a pickled representation of object to the open file object.
195
196 Either the binary or ASCII format will be used, depending on the
197 value of the bin flag passed to the constructor.
198
199 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000200 self.save(object)
201 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000202
Jeremy Hylton3422c992003-01-24 19:29:52 +0000203 def memoize(self, obj):
204 """Store an object in the memo."""
205
Tim Peterse46b73f2003-01-27 21:22:10 +0000206 # The Pickler memo is a dictionary mapping object ids to 2-tuples
207 # that contain the Unpickler memo key and the object being memoized.
208 # The memo key is written to the pickle and will become
Jeremy Hylton3422c992003-01-24 19:29:52 +0000209 # the key in the Unpickler's memo. The object is stored in the
Tim Peterse46b73f2003-01-27 21:22:10 +0000210 # Pickler memo so that transient objects are kept alive during
211 # pickling.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000212
Tim Peterse46b73f2003-01-27 21:22:10 +0000213 # The use of the Unpickler memo length as the memo key is just a
214 # convention. The only requirement is that the memo values be unique.
215 # But there appears no advantage to any other scheme, and this
Tim Peterscbd0a322003-01-28 00:24:43 +0000216 # scheme allows the Unpickler memo to be implemented as a plain (but
Tim Peterse46b73f2003-01-27 21:22:10 +0000217 # growable) array, indexed by memo key.
Jeremy Hylton3422c992003-01-24 19:29:52 +0000218 memo_len = len(self.memo)
219 self.write(self.put(memo_len))
Tim Peters518df0d2003-01-28 01:00:38 +0000220 self.memo[id(obj)] = memo_len, obj
Jeremy Hylton3422c992003-01-24 19:29:52 +0000221
Tim Petersbb38e302003-01-27 21:25:41 +0000222 # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000223 def put(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000224 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000225 s = mdumps(i)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000226 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000227 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000228
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000229 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000230
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000231 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000232
Tim Petersbb38e302003-01-27 21:25:41 +0000233 # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000234 def get(self, i):
Tim Petersc32d8242001-04-10 02:48:53 +0000235 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000236 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Tim Petersc32d8242001-04-10 02:48:53 +0000238 if i < 256:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000239 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000240
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000241 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000242
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000243 return GET + `i` + '\n'
Tim Peters2344fae2001-01-15 00:50:52 +0000244
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000245 def save(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000246 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000247
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000248 pid = self.persistent_id(object)
249 if pid is not None:
250 self.save_pers(pid)
251 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000252
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000253 d = id(object)
Tim Peters2344fae2001-01-15 00:50:52 +0000254
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000255 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000256
Tim Petersc9d7c4a2003-01-28 00:43:26 +0000257 # XXX Why are tuples a special case here?
Tim Petersc32d8242001-04-10 02:48:53 +0000258 if (t is TupleType) and (len(object) == 0):
259 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000260 self.save_empty_tuple(object)
261 else:
262 self.save_tuple(object)
263 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000264
Raymond Hettinger54f02222002-06-01 14:18:47 +0000265 if d in memo:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000266 self.write(self.get(memo[d][0]))
267 return
268
269 try:
270 f = self.dispatch[t]
271 except KeyError:
Tim Petersb32a8312003-01-28 00:48:09 +0000272 pass
273 else:
274 f(self, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000275 return
276
Tim Petersb32a8312003-01-28 00:48:09 +0000277 # The dispatch table doesn't know about type t.
278 try:
279 issc = issubclass(t, TypeType)
280 except TypeError: # t is not a class
281 issc = 0
282 if issc:
283 self.save_global(object)
284 return
285
286 try:
287 reduce = dispatch_table[t]
288 except KeyError:
289 try:
290 reduce = object.__reduce__
291 except AttributeError:
292 raise PicklingError, \
293 "can't pickle %s object: %s" % (`t.__name__`,
294 `object`)
295 else:
296 tup = reduce()
297 else:
298 tup = reduce(object)
299
300 if type(tup) is StringType:
301 self.save_global(object, tup)
302 return
303
304 if type(tup) is not TupleType:
305 raise PicklingError, "Value returned by %s must be a " \
306 "tuple" % reduce
307
308 l = len(tup)
309
310 if (l != 2) and (l != 3):
311 raise PicklingError, "tuple returned by %s must contain " \
312 "only two or three elements" % reduce
313
314 callable = tup[0]
315 arg_tup = tup[1]
316
317 if l > 2:
318 state = tup[2]
319 else:
320 state = None
321
322 if type(arg_tup) is not TupleType and arg_tup is not None:
323 raise PicklingError, "Second element of tuple returned " \
324 "by %s must be a tuple" % reduce
325
326 self.save_reduce(callable, arg_tup, state)
Tim Peters518df0d2003-01-28 01:00:38 +0000327 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000328
329 def persistent_id(self, object):
330 return None
331
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000332 def save_pers(self, pid):
Tim Petersbd1cdb92003-01-28 01:03:10 +0000333 if self.bin:
Jeremy Hylton5e0f4e72002-11-13 22:01:27 +0000334 self.save(pid)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335 self.write(BINPERSID)
Tim Petersbd1cdb92003-01-28 01:03:10 +0000336 else:
337 self.write(PERSID + str(pid) + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000338
Jeremy Hylton3422c992003-01-24 19:29:52 +0000339 def save_reduce(self, acallable, arg_tup, state = None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000340 write = self.write
341 save = self.save
342
Jeremy Hylton3422c992003-01-24 19:29:52 +0000343 if not callable(acallable):
344 raise PicklingError("__reduce__() must return callable as "
345 "first argument, not %s" % `acallable`)
Tim Peters22a449a2003-01-27 20:16:36 +0000346
Jeremy Hylton3422c992003-01-24 19:29:52 +0000347 save(acallable)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000348 save(arg_tup)
349 write(REDUCE)
Tim Peters2344fae2001-01-15 00:50:52 +0000350
Tim Petersc32d8242001-04-10 02:48:53 +0000351 if state is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000352 save(state)
353 write(BUILD)
354
355 dispatch = {}
356
357 def save_none(self, object):
358 self.write(NONE)
359 dispatch[NoneType] = save_none
360
Guido van Rossum77f6a652002-04-03 22:41:51 +0000361 def save_bool(self, object):
Tim Peters22987e32003-01-28 00:26:14 +0000362 self.write(object and TRUE or FALSE)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000363 dispatch[bool] = save_bool
364
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000365 def save_int(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000366 if self.bin:
Tim Peters44714002001-04-10 05:02:52 +0000367 # If the int is small enough to fit in a signed 4-byte 2's-comp
368 # format, we can store it more efficiently than the general
369 # case.
370 high_bits = object >> 31 # note that Python shift sign-extends
371 if high_bits == 0 or high_bits == -1:
372 # All high bits are copies of bit 2**31, so the value
373 # fits in a 4-byte signed int.
374 i = mdumps(object)[1:]
375 assert len(i) == 4
376 if i[-2:] == '\000\000': # fits in 2-byte unsigned int
377 if i[-3] == '\000': # fits in 1-byte unsigned int
378 self.write(BININT1 + i[0])
379 else:
380 self.write(BININT2 + i[:2])
381 else:
382 self.write(BININT + i)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000383 return
Tim Peters44714002001-04-10 05:02:52 +0000384 # Text pickle, or int too big to fit in signed 4-byte format.
385 self.write(INT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000386 dispatch[IntType] = save_int
387
388 def save_long(self, object):
389 self.write(LONG + `object` + '\n')
390 dispatch[LongType] = save_long
391
Guido van Rossumd3703791998-10-22 20:15:36 +0000392 def save_float(self, object, pack=struct.pack):
393 if self.bin:
394 self.write(BINFLOAT + pack('>d', object))
395 else:
396 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000397 dispatch[FloatType] = save_float
398
399 def save_string(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000400 if self.bin:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000401 n = len(object)
402 if n < 256:
403 self.write(SHORT_BINSTRING + chr(n) + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000404 else:
Tim Petersbbf63cd2003-01-27 21:15:36 +0000405 self.write(BINSTRING + mdumps(n)[1:] + object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 else:
407 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000408 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000409 dispatch[StringType] = save_string
410
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000411 def save_unicode(self, object):
Tim Petersc32d8242001-04-10 02:48:53 +0000412 if self.bin:
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000413 encoding = object.encode('utf-8')
Tim Petersbbf63cd2003-01-27 21:15:36 +0000414 n = len(encoding)
415 s = mdumps(n)[1:]
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000416 self.write(BINUNICODE + s + encoding)
417 else:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000418 object = object.replace("\\", "\\u005c")
419 object = object.replace("\n", "\\u000a")
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000420 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000421 self.memoize(object)
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000422 dispatch[UnicodeType] = save_unicode
423
Guido van Rossum31584cb2001-01-22 14:53:29 +0000424 if StringType == UnicodeType:
425 # This is true for Jython
426 def save_string(self, object):
Guido van Rossum31584cb2001-01-22 14:53:29 +0000427 unicode = object.isunicode()
428
Tim Petersc32d8242001-04-10 02:48:53 +0000429 if self.bin:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000430 if unicode:
431 object = object.encode("utf-8")
432 l = len(object)
433 s = mdumps(l)[1:]
Tim Petersc32d8242001-04-10 02:48:53 +0000434 if l < 256 and not unicode:
Guido van Rossum31584cb2001-01-22 14:53:29 +0000435 self.write(SHORT_BINSTRING + s[0] + object)
436 else:
437 if unicode:
438 self.write(BINUNICODE + s + object)
439 else:
440 self.write(BINSTRING + s + object)
441 else:
Tim Peters658cba62001-02-09 20:06:00 +0000442 if unicode:
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000443 object = object.replace("\\", "\\u005c")
444 object = object.replace("\n", "\\u000a")
Guido van Rossum31584cb2001-01-22 14:53:29 +0000445 object = object.encode('raw-unicode-escape')
446 self.write(UNICODE + object + '\n')
447 else:
448 self.write(STRING + `object` + '\n')
Jeremy Hylton3422c992003-01-24 19:29:52 +0000449 self.memoize(object)
Guido van Rossum31584cb2001-01-22 14:53:29 +0000450 dispatch[StringType] = save_string
Tim Peters658cba62001-02-09 20:06:00 +0000451
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000452 def save_tuple(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000453 write = self.write
454 save = self.save
455 memo = self.memo
456
457 d = id(object)
458
459 write(MARK)
460
461 for element in object:
462 save(element)
463
Raymond Hettinger54f02222002-06-01 14:18:47 +0000464 if len(object) and d in memo:
Tim Petersc32d8242001-04-10 02:48:53 +0000465 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000466 write(POP_MARK + self.get(memo[d][0]))
467 return
Tim Peters2344fae2001-01-15 00:50:52 +0000468
Guido van Rossum599174f1998-03-31 16:30:28 +0000469 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000470 return
471
Tim Peters518df0d2003-01-28 01:00:38 +0000472 self.write(TUPLE)
473 self.memoize(object)
Jeremy Hylton3422c992003-01-24 19:29:52 +0000474
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000475 dispatch[TupleType] = save_tuple
476
477 def save_empty_tuple(self, object):
478 self.write(EMPTY_TUPLE)
479
480 def save_list(self, object):
481 d = id(object)
482
483 write = self.write
484 save = self.save
485 memo = self.memo
486
Tim Petersc32d8242001-04-10 02:48:53 +0000487 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000488 write(EMPTY_LIST)
489 else:
490 write(MARK + LIST)
491
Jeremy Hylton3422c992003-01-24 19:29:52 +0000492 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000493
494 using_appends = (self.bin and (len(object) > 1))
495
Tim Petersc32d8242001-04-10 02:48:53 +0000496 if using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000497 write(MARK)
498
499 for element in object:
500 save(element)
Tim Peters2344fae2001-01-15 00:50:52 +0000501
Tim Petersc32d8242001-04-10 02:48:53 +0000502 if not using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000503 write(APPEND)
504
Tim Petersc32d8242001-04-10 02:48:53 +0000505 if using_appends:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000506 write(APPENDS)
507 dispatch[ListType] = save_list
508
509 def save_dict(self, object):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000510 write = self.write
511 save = self.save
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000512
Tim Petersc32d8242001-04-10 02:48:53 +0000513 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000514 write(EMPTY_DICT)
515 else:
516 write(MARK + DICT)
517
Jeremy Hylton3422c992003-01-24 19:29:52 +0000518 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000519
520 using_setitems = (self.bin and (len(object) > 1))
521
Tim Petersc32d8242001-04-10 02:48:53 +0000522 if using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000523 write(MARK)
524
525 items = object.items()
526 for key, value in items:
527 save(key)
528 save(value)
529
Tim Petersc32d8242001-04-10 02:48:53 +0000530 if not using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000531 write(SETITEM)
532
Tim Petersc32d8242001-04-10 02:48:53 +0000533 if using_setitems:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000534 write(SETITEMS)
535
536 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000537 if not PyStringMap is None:
538 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000539
540 def save_inst(self, object):
541 d = id(object)
542 cls = object.__class__
543
544 memo = self.memo
545 write = self.write
546 save = self.save
547
548 if hasattr(object, '__getinitargs__'):
549 args = object.__getinitargs__()
550 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000551 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000552 else:
553 args = ()
554
555 write(MARK)
556
Tim Petersc32d8242001-04-10 02:48:53 +0000557 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000558 save(cls)
559
560 for arg in args:
561 save(arg)
562
Jeremy Hylton3422c992003-01-24 19:29:52 +0000563 # This method does not use memoize() so that it can handle
564 # the special case for non-binary mode.
Tim Peters518df0d2003-01-28 01:00:38 +0000565 # XXX What did that comment mean? That is, what "special case for
566 # XXX non-binary mode? It sure *looks* like nothing special is
567 # XXX happening in the INST case.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000568 memo_len = len(memo)
Tim Petersc32d8242001-04-10 02:48:53 +0000569 if self.bin:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000570 write(OBJ + self.put(memo_len))
571 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000572 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000573 self.put(memo_len))
574
575 memo[d] = (memo_len, object)
576
577 try:
578 getstate = object.__getstate__
579 except AttributeError:
580 stuff = object.__dict__
581 else:
582 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000583 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000584 save(stuff)
585 write(BUILD)
586 dispatch[InstanceType] = save_inst
587
588 def save_global(self, object, name = None):
589 write = self.write
590 memo = self.memo
591
Tim Petersc32d8242001-04-10 02:48:53 +0000592 if name is None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000593 name = object.__name__
594
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000595 try:
596 module = object.__module__
597 except AttributeError:
598 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000599
Guido van Rossumb0a98e92001-08-17 18:49:52 +0000600 try:
601 __import__(module)
602 mod = sys.modules[module]
603 klass = getattr(mod, name)
604 except (ImportError, KeyError, AttributeError):
605 raise PicklingError(
606 "Can't pickle %r: it's not found as %s.%s" %
607 (object, module, name))
608 else:
609 if klass is not object:
610 raise PicklingError(
611 "Can't pickle %r: it's not the same object as %s.%s" %
612 (object, module, name))
613
Tim Peters518df0d2003-01-28 01:00:38 +0000614 write(GLOBAL + module + '\n' + name + '\n')
615 self.memoize(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000616 dispatch[ClassType] = save_global
617 dispatch[FunctionType] = save_global
618 dispatch[BuiltinFunctionType] = save_global
Tim Peters6d6c1a32001-08-02 04:15:00 +0000619 dispatch[TypeType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000620
Guido van Rossuma48061a1995-01-10 00:31:14 +0000621
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000622def _keep_alive(x, memo):
623 """Keeps a reference to the object x in the memo.
624
625 Because we remember objects by their id, we have
626 to assure that possibly temporary objects are kept
627 alive by referencing them.
628 We store a reference at the id of the memo, which should
629 normally not be used unless someone tries to deepcopy
630 the memo itself...
631 """
632 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000633 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000634 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000635 # aha, this is the first one :-)
636 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000637
638
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000639classmap = {} # called classmap for backwards compatibility
Guido van Rossuma48061a1995-01-10 00:31:14 +0000640
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000641def whichmodule(func, funcname):
642 """Figure out the module in which a function occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000643
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000644 Search sys.modules for the module.
645 Cache in classmap.
646 Return a module name.
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000647 If the function cannot be found, return __main__.
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000648 """
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000649 if func in classmap:
650 return classmap[func]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000651
652 for name, module in sys.modules.items():
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000653 if module is None:
Jeremy Hylton065a5ab2002-09-19 22:57:26 +0000654 continue # skip dummy package entries
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000655 if name != '__main__' and \
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000656 hasattr(module, funcname) and \
657 getattr(module, funcname) is func:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000658 break
659 else:
660 name = '__main__'
Jeremy Hyltonf0cfdf72002-09-19 23:00:12 +0000661 classmap[func] = name
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000662 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000663
664
665class Unpickler:
666
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000667 def __init__(self, file):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000668 """This takes a file-like object for reading a pickle data stream.
669
670 This class automatically determines whether the data stream was
671 written in binary mode or not, so it does not need a flag as in
672 the Pickler class factory.
673
674 The file-like object must have two methods, a read() method that
675 takes an integer argument, and a readline() method that requires no
676 arguments. Both methods should return a string. Thus file-like
677 object can be a file object opened for reading, a StringIO object,
678 or any other custom object that meets this interface.
679
680 """
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000681 self.readline = file.readline
682 self.read = file.read
683 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000684
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000685 def load(self):
Raymond Hettingeraef22fb2002-05-29 16:18:42 +0000686 """Read a pickled object representation from the open file object.
687
688 Return the reconstituted object hierarchy specified in the file
689 object.
690
691 """
Jeremy Hylton20747fa2001-11-09 16:15:04 +0000692 self.mark = object() # any new unique object
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000693 self.stack = []
694 self.append = self.stack.append
695 read = self.read
696 dispatch = self.dispatch
697 try:
698 while 1:
699 key = read(1)
700 dispatch[key](self)
Guido van Rossumff871742000-12-13 18:11:56 +0000701 except _Stop, stopinst:
702 return stopinst.value
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000703
704 def marker(self):
705 stack = self.stack
706 mark = self.mark
707 k = len(stack)-1
708 while stack[k] is not mark: k = k-1
709 return k
710
711 dispatch = {}
712
713 def load_eof(self):
714 raise EOFError
715 dispatch[''] = load_eof
716
717 def load_persid(self):
718 pid = self.readline()[:-1]
719 self.append(self.persistent_load(pid))
720 dispatch[PERSID] = load_persid
721
722 def load_binpersid(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000723 pid = self.stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000724 self.append(self.persistent_load(pid))
725 dispatch[BINPERSID] = load_binpersid
726
727 def load_none(self):
728 self.append(None)
729 dispatch[NONE] = load_none
730
731 def load_int(self):
Tim Peters19ef62d2001-08-28 22:21:18 +0000732 data = self.readline()
Guido van Rossume2763392002-04-05 19:30:08 +0000733 if data == FALSE[1:]:
734 val = False
735 elif data == TRUE[1:]:
736 val = True
737 else:
738 try:
739 val = int(data)
740 except ValueError:
741 val = long(data)
742 self.append(val)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000743 dispatch[INT] = load_int
744
745 def load_binint(self):
746 self.append(mloads('i' + self.read(4)))
747 dispatch[BININT] = load_binint
748
749 def load_binint1(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000750 self.append(ord(self.read(1)))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000751 dispatch[BININT1] = load_binint1
752
753 def load_binint2(self):
754 self.append(mloads('i' + self.read(2) + '\000\000'))
755 dispatch[BININT2] = load_binint2
Tim Peters2344fae2001-01-15 00:50:52 +0000756
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000757 def load_long(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000758 self.append(long(self.readline()[:-1], 0))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000759 dispatch[LONG] = load_long
760
761 def load_float(self):
Guido van Rossumff871742000-12-13 18:11:56 +0000762 self.append(float(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000763 dispatch[FLOAT] = load_float
764
Guido van Rossumd3703791998-10-22 20:15:36 +0000765 def load_binfloat(self, unpack=struct.unpack):
766 self.append(unpack('>d', self.read(8))[0])
767 dispatch[BINFLOAT] = load_binfloat
768
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000769 def load_string(self):
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000770 rep = self.readline()[:-1]
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000771 for q in _quotes:
772 if rep.startswith(q):
773 if not rep.endswith(q):
774 raise ValueError, "insecure string pickle"
775 rep = rep[len(q):-len(q)]
776 break
777 else:
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000778 raise ValueError, "insecure string pickle"
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000779 self.append(rep.decode("string-escape"))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000780 dispatch[STRING] = load_string
781
Jeremy Hyltonbe467e52000-09-15 15:14:51 +0000782 def _is_string_secure(self, s):
783 """Return true if s contains a string that is safe to eval
784
785 The definition of secure string is based on the implementation
786 in cPickle. s is secure as long as it only contains a quoted
787 string and optional trailing whitespace.
788 """
789 q = s[0]
790 if q not in ("'", '"'):
791 return 0
792 # find the closing quote
793 offset = 1
794 i = None
795 while 1:
796 try:
797 i = s.index(q, offset)
798 except ValueError:
799 # if there is an error the first time, there is no
800 # close quote
801 if offset == 1:
802 return 0
803 if s[i-1] != '\\':
804 break
805 # check to see if this one is escaped
806 nslash = 0
807 j = i - 1
808 while j >= offset and s[j] == '\\':
809 j = j - 1
810 nslash = nslash + 1
811 if nslash % 2 == 0:
812 break
813 offset = i + 1
814 for c in s[i+1:]:
815 if ord(c) > 32:
816 return 0
817 return 1
818
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000819 def load_binstring(self):
820 len = mloads('i' + self.read(4))
821 self.append(self.read(len))
822 dispatch[BINSTRING] = load_binstring
823
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000824 def load_unicode(self):
825 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
826 dispatch[UNICODE] = load_unicode
827
828 def load_binunicode(self):
829 len = mloads('i' + self.read(4))
830 self.append(unicode(self.read(len),'utf-8'))
831 dispatch[BINUNICODE] = load_binunicode
832
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000833 def load_short_binstring(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000834 len = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000835 self.append(self.read(len))
836 dispatch[SHORT_BINSTRING] = load_short_binstring
837
838 def load_tuple(self):
839 k = self.marker()
840 self.stack[k:] = [tuple(self.stack[k+1:])]
841 dispatch[TUPLE] = load_tuple
842
843 def load_empty_tuple(self):
844 self.stack.append(())
845 dispatch[EMPTY_TUPLE] = load_empty_tuple
846
847 def load_empty_list(self):
848 self.stack.append([])
849 dispatch[EMPTY_LIST] = load_empty_list
850
851 def load_empty_dictionary(self):
852 self.stack.append({})
853 dispatch[EMPTY_DICT] = load_empty_dictionary
854
855 def load_list(self):
856 k = self.marker()
857 self.stack[k:] = [self.stack[k+1:]]
858 dispatch[LIST] = load_list
859
860 def load_dict(self):
861 k = self.marker()
862 d = {}
863 items = self.stack[k+1:]
864 for i in range(0, len(items), 2):
865 key = items[i]
866 value = items[i+1]
867 d[key] = value
868 self.stack[k:] = [d]
869 dispatch[DICT] = load_dict
870
871 def load_inst(self):
872 k = self.marker()
873 args = tuple(self.stack[k+1:])
874 del self.stack[k:]
875 module = self.readline()[:-1]
876 name = self.readline()[:-1]
877 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000878 instantiated = 0
879 if (not args and type(klass) is ClassType and
880 not hasattr(klass, "__getinitargs__")):
881 try:
882 value = _EmptyClass()
883 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000884 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000885 except RuntimeError:
886 # In restricted execution, assignment to inst.__class__ is
887 # prohibited
888 pass
889 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000890 try:
Barry Warsawbf4d9592001-11-15 23:42:58 +0000891 if not hasattr(klass, '__safe_for_unpickling__'):
892 raise UnpicklingError('%s is not safe for unpickling' %
893 klass)
Guido van Rossum743d17e1998-09-15 20:25:57 +0000894 value = apply(klass, args)
895 except TypeError, err:
896 raise TypeError, "in constructor for %s: %s" % (
897 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000898 self.append(value)
899 dispatch[INST] = load_inst
900
901 def load_obj(self):
902 stack = self.stack
903 k = self.marker()
904 klass = stack[k + 1]
905 del stack[k + 1]
Tim Peters2344fae2001-01-15 00:50:52 +0000906 args = tuple(stack[k + 1:])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000907 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000908 instantiated = 0
909 if (not args and type(klass) is ClassType and
910 not hasattr(klass, "__getinitargs__")):
911 try:
912 value = _EmptyClass()
913 value.__class__ = klass
914 instantiated = 1
915 except RuntimeError:
916 # In restricted execution, assignment to inst.__class__ is
917 # prohibited
918 pass
919 if not instantiated:
920 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000921 self.append(value)
Tim Peters2344fae2001-01-15 00:50:52 +0000922 dispatch[OBJ] = load_obj
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000923
924 def load_global(self):
925 module = self.readline()[:-1]
926 name = self.readline()[:-1]
927 klass = self.find_class(module, name)
928 self.append(klass)
929 dispatch[GLOBAL] = load_global
930
931 def find_class(self, module, name):
Barry Warsawbf4d9592001-11-15 23:42:58 +0000932 __import__(module)
933 mod = sys.modules[module]
934 klass = getattr(mod, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000935 return klass
936
937 def load_reduce(self):
938 stack = self.stack
939
940 callable = stack[-2]
941 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000942 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000943
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000944 if type(callable) is not ClassType:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000945 if not callable in safe_constructors:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000946 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000947 safe = callable.__safe_for_unpickling__
948 except AttributeError:
949 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000950
Tim Petersc32d8242001-04-10 02:48:53 +0000951 if not safe:
Tim Peters2344fae2001-01-15 00:50:52 +0000952 raise UnpicklingError, "%s is not safe for " \
953 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000954
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000955 if arg_tup is None:
Raymond Hettinger97394bc2002-05-21 17:22:02 +0000956 import warnings
957 warnings.warn("The None return argument form of __reduce__ is "
958 "deprecated. Return a tuple of arguments instead.",
Tim Peters8ac14952002-05-23 15:15:30 +0000959 DeprecationWarning)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000960 value = callable.__basicnew__()
961 else:
962 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000963 self.append(value)
964 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000965
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000966 def load_pop(self):
967 del self.stack[-1]
968 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000969
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000970 def load_pop_mark(self):
971 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000972 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000973 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000974
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000975 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000976 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000977 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000978
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000979 def load_get(self):
980 self.append(self.memo[self.readline()[:-1]])
981 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000982
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000983 def load_binget(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000984 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000985 self.append(self.memo[`i`])
986 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000987
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000988 def load_long_binget(self):
989 i = mloads('i' + self.read(4))
990 self.append(self.memo[`i`])
991 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000992
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000993 def load_put(self):
994 self.memo[self.readline()[:-1]] = self.stack[-1]
995 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000996
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000997 def load_binput(self):
Tim Petersbbf63cd2003-01-27 21:15:36 +0000998 i = ord(self.read(1))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000999 self.memo[`i`] = self.stack[-1]
1000 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001001
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001002 def load_long_binput(self):
1003 i = mloads('i' + self.read(4))
1004 self.memo[`i`] = self.stack[-1]
1005 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +00001006
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001007 def load_append(self):
1008 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001009 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001010 list = stack[-1]
1011 list.append(value)
1012 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +00001013
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001014 def load_appends(self):
1015 stack = self.stack
1016 mark = self.marker()
1017 list = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001018 for i in range(mark + 1, len(stack)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001019 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001020
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001021 del stack[mark:]
1022 dispatch[APPENDS] = load_appends
Tim Peters2344fae2001-01-15 00:50:52 +00001023
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001024 def load_setitem(self):
1025 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001026 value = stack.pop()
1027 key = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001028 dict = stack[-1]
1029 dict[key] = value
1030 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001031
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001032 def load_setitems(self):
1033 stack = self.stack
1034 mark = self.marker()
1035 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001036 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001037 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001038
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001039 del stack[mark:]
1040 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +00001041
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001042 def load_build(self):
1043 stack = self.stack
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001044 value = stack.pop()
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001045 inst = stack[-1]
1046 try:
1047 setstate = inst.__setstate__
1048 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00001049 try:
1050 inst.__dict__.update(value)
1051 except RuntimeError:
1052 # XXX In restricted execution, the instance's __dict__ is not
1053 # accessible. Use the old way of unpickling the instance
1054 # variables. This is a semantic different when unpickling in
1055 # restricted vs. unrestricted modes.
1056 for k, v in value.items():
1057 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001058 else:
1059 setstate(value)
1060 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +00001061
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001062 def load_mark(self):
1063 self.append(self.mark)
1064 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +00001065
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001066 def load_stop(self):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +00001067 value = self.stack.pop()
Guido van Rossumff871742000-12-13 18:11:56 +00001068 raise _Stop(value)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001069 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +00001070
Guido van Rossume467be61997-12-05 19:42:42 +00001071# Helper class for load_inst/load_obj
1072
1073class _EmptyClass:
1074 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +00001075
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001076# Shorthands
1077
Jeremy Hyltonabe2c622001-10-15 21:29:28 +00001078try:
1079 from cStringIO import StringIO
1080except ImportError:
1081 from StringIO import StringIO
Guido van Rossumc7c5e691996-07-22 22:26:07 +00001082
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001083def dump(object, file, proto=1):
1084 Pickler(file, proto).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001085
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001086def dumps(object, proto=1):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001087 file = StringIO()
Guido van Rossumf29d3d62003-01-27 22:47:53 +00001088 Pickler(file, proto).dump(object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001089 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001090
1091def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001092 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +00001093
1094def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +00001095 file = StringIO(str)
1096 return Unpickler(file).load()