blob: 6c1bcbeae554ebe437d92a7afa742fbf0be5a89b [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.
Guido van Rossuma48061a1995-01-10 00:31:14 +00005
Guido van Rossume467be61997-12-05 19:42:42 +00006Classes:
Guido van Rossuma48061a1995-01-10 00:31:14 +00007
Guido van Rossume467be61997-12-05 19:42:42 +00008 Pickler
9 Unpickler
Guido van Rossuma48061a1995-01-10 00:31:14 +000010
Guido van Rossume467be61997-12-05 19:42:42 +000011Functions:
Guido van Rossuma48061a1995-01-10 00:31:14 +000012
Guido van Rossume467be61997-12-05 19:42:42 +000013 dump(object, file)
14 dumps(object) -> string
15 load(file) -> object
16 loads(string) -> object
Guido van Rossuma48061a1995-01-10 00:31:14 +000017
Guido van Rossume467be61997-12-05 19:42:42 +000018Misc variables:
Guido van Rossuma48061a1995-01-10 00:31:14 +000019
Fred Drakefe82acc1998-02-13 03:24:48 +000020 __version__
Guido van Rossume467be61997-12-05 19:42:42 +000021 format_version
22 compatible_formats
Guido van Rossuma48061a1995-01-10 00:31:14 +000023
Guido van Rossuma48061a1995-01-10 00:31:14 +000024"""
25
Guido van Rossum743d17e1998-09-15 20:25:57 +000026__version__ = "$Revision$" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +000027
28from types import *
Guido van Rossum4fb5b281997-09-12 20:07:24 +000029from copy_reg import dispatch_table, safe_constructors
Guido van Rossumd3703791998-10-22 20:15:36 +000030import string
31import marshal
32import sys
33import struct
Guido van Rossuma48061a1995-01-10 00:31:14 +000034
Guido van Rossumd3703791998-10-22 20:15:36 +000035format_version = "1.3" # File format version we write
36compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000037
38mdumps = marshal.dumps
39mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000040
Guido van Rossum26e89d02000-06-29 16:15:52 +000041class PickleError(Exception): pass
42class PicklingError(PickleError): pass
43class UnpicklingError(PickleError): pass
Guido van Rossum7849da81995-03-09 14:08:35 +000044
Jeremy Hylton2b9d0291998-05-27 22:38:22 +000045try:
46 from org.python.core import PyStringMap
47except ImportError:
48 PyStringMap = None
49
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000050MARK = '('
51STOP = '.'
52POP = '0'
53POP_MARK = '1'
54DUP = '2'
55FLOAT = 'F'
56INT = 'I'
57BININT = 'J'
58BININT1 = 'K'
59LONG = 'L'
60BININT2 = 'M'
61NONE = 'N'
62PERSID = 'P'
63BINPERSID = 'Q'
64REDUCE = 'R'
65STRING = 'S'
66BINSTRING = 'T'
67SHORT_BINSTRING = 'U'
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +000068UNICODE = 'V'
69BINUNICODE = 'X'
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000070APPEND = 'a'
71BUILD = 'b'
72GLOBAL = 'c'
73DICT = 'd'
74EMPTY_DICT = '}'
75APPENDS = 'e'
76GET = 'g'
77BINGET = 'h'
78INST = 'i'
79LONG_BINGET = 'j'
80LIST = 'l'
81EMPTY_LIST = ']'
82OBJ = 'o'
83PUT = 'p'
84BINPUT = 'q'
85LONG_BINPUT = 'r'
86SETITEM = 's'
87TUPLE = 't'
88EMPTY_TUPLE = ')'
89SETITEMS = 'u'
Guido van Rossumd3703791998-10-22 20:15:36 +000090BINFLOAT = 'G'
Guido van Rossuma48061a1995-01-10 00:31:14 +000091
92class Pickler:
93
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000094 def __init__(self, file, bin = 0):
95 self.write = file.write
96 self.memo = {}
97 self.bin = bin
Guido van Rossuma48061a1995-01-10 00:31:14 +000098
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000099 def dump(self, object):
100 self.save(object)
101 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000102
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000103 def put(self, i):
104 if (self.bin):
105 s = mdumps(i)[1:]
106 if (i < 256):
107 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000108
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000109 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000110
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000111 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000113 def get(self, i):
114 if (self.bin):
115 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000116
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000117 if (i < 256):
118 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000119
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000120 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000121
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000122 return GET + `i` + '\n'
123
124 def save(self, object, pers_save = 0):
125 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000126
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000127 if (not pers_save):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000128 pid = self.persistent_id(object)
129 if (pid is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000130 self.save_pers(pid)
131 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000133 d = id(object)
134
135 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000136
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 if ((t is TupleType) and (len(object) == 0)):
138 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000139 self.save_empty_tuple(object)
140 else:
141 self.save_tuple(object)
142 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000143
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000144 if memo.has_key(d):
145 self.write(self.get(memo[d][0]))
146 return
147
148 try:
149 f = self.dispatch[t]
150 except KeyError:
151 pid = self.inst_persistent_id(object)
152 if pid is not None:
153 self.save_pers(pid)
154 return
155
156 try:
157 reduce = dispatch_table[t]
158 except KeyError:
159 try:
160 reduce = object.__reduce__
161 except AttributeError:
162 raise PicklingError, \
Guido van Rossum08a92cb1999-10-10 21:14:25 +0000163 "can't pickle %s object: %s" % (`t.__name__`,
164 `object`)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000165 else:
166 tup = reduce()
167 else:
168 tup = reduce(object)
169
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000170 if type(tup) is StringType:
171 self.save_global(object, tup)
172 return
Guido van Rossumd1f49841997-12-10 23:40:18 +0000173
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000174 if (type(tup) is not TupleType):
175 raise PicklingError, "Value returned by %s must be a " \
176 "tuple" % reduce
177
178 l = len(tup)
179
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000180 if ((l != 2) and (l != 3)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000181 raise PicklingError, "tuple returned by %s must contain " \
182 "only two or three elements" % reduce
183
184 callable = tup[0]
185 arg_tup = tup[1]
186
187 if (l > 2):
188 state = tup[2]
189 else:
190 state = None
191
Guido van Rossumd1f49841997-12-10 23:40:18 +0000192 if type(arg_tup) is not TupleType and arg_tup is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000193 raise PicklingError, "Second element of tuple returned " \
194 "by %s must be a tuple" % reduce
195
196 self.save_reduce(callable, arg_tup, state)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000197 memo_len = len(memo)
198 self.write(self.put(memo_len))
199 memo[d] = (memo_len, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000200 return
201
202 f(self, object)
203
204 def persistent_id(self, object):
205 return None
206
207 def inst_persistent_id(self, object):
208 return None
209
210 def save_pers(self, pid):
211 if (not self.bin):
212 self.write(PERSID + str(pid) + '\n')
213 else:
214 self.save(pid, 1)
215 self.write(BINPERSID)
216
217 def save_reduce(self, callable, arg_tup, state = None):
218 write = self.write
219 save = self.save
220
221 save(callable)
222 save(arg_tup)
223 write(REDUCE)
224
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000225 if (state is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000226 save(state)
227 write(BUILD)
228
229 dispatch = {}
230
231 def save_none(self, object):
232 self.write(NONE)
233 dispatch[NoneType] = save_none
234
235 def save_int(self, object):
236 if (self.bin):
237 i = mdumps(object)[1:]
238 if (i[-2:] == '\000\000'):
239 if (i[-3] == '\000'):
240 self.write(BININT1 + i[:-3])
241 return
242
243 self.write(BININT2 + i[:-2])
244 return
245
246 self.write(BININT + i)
247 else:
248 self.write(INT + `object` + '\n')
249 dispatch[IntType] = save_int
250
251 def save_long(self, object):
252 self.write(LONG + `object` + '\n')
253 dispatch[LongType] = save_long
254
Guido van Rossumd3703791998-10-22 20:15:36 +0000255 def save_float(self, object, pack=struct.pack):
256 if self.bin:
257 self.write(BINFLOAT + pack('>d', object))
258 else:
259 self.write(FLOAT + `object` + '\n')
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000260 dispatch[FloatType] = save_float
261
262 def save_string(self, object):
263 d = id(object)
264 memo = self.memo
265
266 if (self.bin):
267 l = len(object)
268 s = mdumps(l)[1:]
269 if (l < 256):
270 self.write(SHORT_BINSTRING + s[0] + object)
271 else:
272 self.write(BINSTRING + s + object)
273 else:
274 self.write(STRING + `object` + '\n')
275
276 memo_len = len(memo)
277 self.write(self.put(memo_len))
278 memo[d] = (memo_len, object)
279 dispatch[StringType] = save_string
280
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000281 def save_unicode(self, object):
282 d = id(object)
283 memo = self.memo
284
285 if (self.bin):
286 encoding = object.encode('utf-8')
287 l = len(encoding)
288 s = mdumps(l)[1:]
289 self.write(BINUNICODE + s + encoding)
290 else:
291 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
292
293 memo_len = len(memo)
294 self.write(self.put(memo_len))
295 memo[d] = (memo_len, object)
296 dispatch[UnicodeType] = save_unicode
297
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000298 def save_tuple(self, object):
299
300 write = self.write
301 save = self.save
302 memo = self.memo
303
304 d = id(object)
305
306 write(MARK)
307
308 for element in object:
309 save(element)
310
311 if (len(object) and memo.has_key(d)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000312 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000313 write(POP_MARK + self.get(memo[d][0]))
314 return
315
Guido van Rossum599174f1998-03-31 16:30:28 +0000316 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000317 return
318
319 memo_len = len(memo)
320 self.write(TUPLE + self.put(memo_len))
321 memo[d] = (memo_len, object)
322 dispatch[TupleType] = save_tuple
323
324 def save_empty_tuple(self, object):
325 self.write(EMPTY_TUPLE)
326
327 def save_list(self, object):
328 d = id(object)
329
330 write = self.write
331 save = self.save
332 memo = self.memo
333
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000334 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000335 write(EMPTY_LIST)
336 else:
337 write(MARK + LIST)
338
339 memo_len = len(memo)
340 write(self.put(memo_len))
341 memo[d] = (memo_len, object)
342
343 using_appends = (self.bin and (len(object) > 1))
344
345 if (using_appends):
346 write(MARK)
347
348 for element in object:
349 save(element)
350
351 if (not using_appends):
352 write(APPEND)
353
354 if (using_appends):
355 write(APPENDS)
356 dispatch[ListType] = save_list
357
358 def save_dict(self, object):
359 d = id(object)
360
361 write = self.write
362 save = self.save
363 memo = self.memo
364
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000365 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000366 write(EMPTY_DICT)
367 else:
368 write(MARK + DICT)
369
370 memo_len = len(memo)
371 self.write(self.put(memo_len))
372 memo[d] = (memo_len, object)
373
374 using_setitems = (self.bin and (len(object) > 1))
375
376 if (using_setitems):
377 write(MARK)
378
379 items = object.items()
380 for key, value in items:
381 save(key)
382 save(value)
383
384 if (not using_setitems):
385 write(SETITEM)
386
387 if (using_setitems):
388 write(SETITEMS)
389
390 dispatch[DictionaryType] = save_dict
Jeremy Hylton2b9d0291998-05-27 22:38:22 +0000391 if not PyStringMap is None:
392 dispatch[PyStringMap] = save_dict
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000393
394 def save_inst(self, object):
395 d = id(object)
396 cls = object.__class__
397
398 memo = self.memo
399 write = self.write
400 save = self.save
401
402 if hasattr(object, '__getinitargs__'):
403 args = object.__getinitargs__()
404 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000405 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 else:
407 args = ()
408
409 write(MARK)
410
411 if (self.bin):
412 save(cls)
413
414 for arg in args:
415 save(arg)
416
417 memo_len = len(memo)
418 if (self.bin):
419 write(OBJ + self.put(memo_len))
420 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000421 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000422 self.put(memo_len))
423
424 memo[d] = (memo_len, object)
425
426 try:
427 getstate = object.__getstate__
428 except AttributeError:
429 stuff = object.__dict__
430 else:
431 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000432 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000433 save(stuff)
434 write(BUILD)
435 dispatch[InstanceType] = save_inst
436
437 def save_global(self, object, name = None):
438 write = self.write
439 memo = self.memo
440
441 if (name is None):
442 name = object.__name__
443
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000444 try:
445 module = object.__module__
446 except AttributeError:
447 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000448
449 memo_len = len(memo)
450 write(GLOBAL + module + '\n' + name + '\n' +
451 self.put(memo_len))
452 memo[id(object)] = (memo_len, object)
453 dispatch[ClassType] = save_global
454 dispatch[FunctionType] = save_global
455 dispatch[BuiltinFunctionType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000456
Guido van Rossuma48061a1995-01-10 00:31:14 +0000457
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000458def _keep_alive(x, memo):
459 """Keeps a reference to the object x in the memo.
460
461 Because we remember objects by their id, we have
462 to assure that possibly temporary objects are kept
463 alive by referencing them.
464 We store a reference at the id of the memo, which should
465 normally not be used unless someone tries to deepcopy
466 the memo itself...
467 """
468 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000469 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000470 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000471 # aha, this is the first one :-)
472 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000473
474
Guido van Rossuma48061a1995-01-10 00:31:14 +0000475classmap = {}
476
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000477# This is no longer used to find classes, but still for functions
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000478def whichmodule(cls, clsname):
479 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000480
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000481 Search sys.modules for the module.
482 Cache in classmap.
483 Return a module name.
484 If the class cannot be found, return __main__.
485 """
486 if classmap.has_key(cls):
487 return classmap[cls]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000488
489 for name, module in sys.modules.items():
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000490 if name != '__main__' and \
491 hasattr(module, clsname) and \
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000492 getattr(module, clsname) is cls:
493 break
494 else:
495 name = '__main__'
496 classmap[cls] = name
497 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000498
499
500class Unpickler:
501
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000502 def __init__(self, file):
503 self.readline = file.readline
504 self.read = file.read
505 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000506
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000507 def load(self):
508 self.mark = ['spam'] # Any new unique object
509 self.stack = []
510 self.append = self.stack.append
511 read = self.read
512 dispatch = self.dispatch
513 try:
514 while 1:
515 key = read(1)
516 dispatch[key](self)
517 except STOP, value:
518 return value
519
520 def marker(self):
521 stack = self.stack
522 mark = self.mark
523 k = len(stack)-1
524 while stack[k] is not mark: k = k-1
525 return k
526
527 dispatch = {}
528
529 def load_eof(self):
530 raise EOFError
531 dispatch[''] = load_eof
532
533 def load_persid(self):
534 pid = self.readline()[:-1]
535 self.append(self.persistent_load(pid))
536 dispatch[PERSID] = load_persid
537
538 def load_binpersid(self):
539 stack = self.stack
540
541 pid = stack[-1]
542 del stack[-1]
543
544 self.append(self.persistent_load(pid))
545 dispatch[BINPERSID] = load_binpersid
546
547 def load_none(self):
548 self.append(None)
549 dispatch[NONE] = load_none
550
551 def load_int(self):
Guido van Rossume6805461997-12-10 19:36:41 +0000552 self.append(string.atoi(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000553 dispatch[INT] = load_int
554
555 def load_binint(self):
556 self.append(mloads('i' + self.read(4)))
557 dispatch[BININT] = load_binint
558
559 def load_binint1(self):
560 self.append(mloads('i' + self.read(1) + '\000\000\000'))
561 dispatch[BININT1] = load_binint1
562
563 def load_binint2(self):
564 self.append(mloads('i' + self.read(2) + '\000\000'))
565 dispatch[BININT2] = load_binint2
566
567 def load_long(self):
568 self.append(string.atol(self.readline()[:-1], 0))
569 dispatch[LONG] = load_long
570
571 def load_float(self):
572 self.append(string.atof(self.readline()[:-1]))
573 dispatch[FLOAT] = load_float
574
Guido van Rossumd3703791998-10-22 20:15:36 +0000575 def load_binfloat(self, unpack=struct.unpack):
576 self.append(unpack('>d', self.read(8))[0])
577 dispatch[BINFLOAT] = load_binfloat
578
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000579 def load_string(self):
580 self.append(eval(self.readline()[:-1],
581 {'__builtins__': {}})) # Let's be careful
582 dispatch[STRING] = load_string
583
584 def load_binstring(self):
585 len = mloads('i' + self.read(4))
586 self.append(self.read(len))
587 dispatch[BINSTRING] = load_binstring
588
Guido van Rossumb5f2f1b2000-03-10 23:20:09 +0000589 def load_unicode(self):
590 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
591 dispatch[UNICODE] = load_unicode
592
593 def load_binunicode(self):
594 len = mloads('i' + self.read(4))
595 self.append(unicode(self.read(len),'utf-8'))
596 dispatch[BINUNICODE] = load_binunicode
597
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000598 def load_short_binstring(self):
599 len = mloads('i' + self.read(1) + '\000\000\000')
600 self.append(self.read(len))
601 dispatch[SHORT_BINSTRING] = load_short_binstring
602
603 def load_tuple(self):
604 k = self.marker()
605 self.stack[k:] = [tuple(self.stack[k+1:])]
606 dispatch[TUPLE] = load_tuple
607
608 def load_empty_tuple(self):
609 self.stack.append(())
610 dispatch[EMPTY_TUPLE] = load_empty_tuple
611
612 def load_empty_list(self):
613 self.stack.append([])
614 dispatch[EMPTY_LIST] = load_empty_list
615
616 def load_empty_dictionary(self):
617 self.stack.append({})
618 dispatch[EMPTY_DICT] = load_empty_dictionary
619
620 def load_list(self):
621 k = self.marker()
622 self.stack[k:] = [self.stack[k+1:]]
623 dispatch[LIST] = load_list
624
625 def load_dict(self):
626 k = self.marker()
627 d = {}
628 items = self.stack[k+1:]
629 for i in range(0, len(items), 2):
630 key = items[i]
631 value = items[i+1]
632 d[key] = value
633 self.stack[k:] = [d]
634 dispatch[DICT] = load_dict
635
636 def load_inst(self):
637 k = self.marker()
638 args = tuple(self.stack[k+1:])
639 del self.stack[k:]
640 module = self.readline()[:-1]
641 name = self.readline()[:-1]
642 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000643 instantiated = 0
644 if (not args and type(klass) is ClassType and
645 not hasattr(klass, "__getinitargs__")):
646 try:
647 value = _EmptyClass()
648 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000649 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000650 except RuntimeError:
651 # In restricted execution, assignment to inst.__class__ is
652 # prohibited
653 pass
654 if not instantiated:
Guido van Rossum743d17e1998-09-15 20:25:57 +0000655 try:
656 value = apply(klass, args)
657 except TypeError, err:
658 raise TypeError, "in constructor for %s: %s" % (
659 klass.__name__, str(err)), sys.exc_info()[2]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000660 self.append(value)
661 dispatch[INST] = load_inst
662
663 def load_obj(self):
664 stack = self.stack
665 k = self.marker()
666 klass = stack[k + 1]
667 del stack[k + 1]
668 args = tuple(stack[k + 1:])
669 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000670 instantiated = 0
671 if (not args and type(klass) is ClassType and
672 not hasattr(klass, "__getinitargs__")):
673 try:
674 value = _EmptyClass()
675 value.__class__ = klass
676 instantiated = 1
677 except RuntimeError:
678 # In restricted execution, assignment to inst.__class__ is
679 # prohibited
680 pass
681 if not instantiated:
682 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000683 self.append(value)
684 dispatch[OBJ] = load_obj
685
686 def load_global(self):
687 module = self.readline()[:-1]
688 name = self.readline()[:-1]
689 klass = self.find_class(module, name)
690 self.append(klass)
691 dispatch[GLOBAL] = load_global
692
693 def find_class(self, module, name):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000694 try:
Guido van Rossum397032a1999-03-25 21:58:59 +0000695 __import__(module)
696 mod = sys.modules[module]
697 klass = getattr(mod, name)
698 except (ImportError, KeyError, AttributeError):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000699 raise SystemError, \
700 "Failed to import class %s from module %s" % \
701 (name, module)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000702 return klass
703
704 def load_reduce(self):
705 stack = self.stack
706
707 callable = stack[-2]
708 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000709 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000710
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000711 if type(callable) is not ClassType:
712 if not safe_constructors.has_key(callable):
713 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000714 safe = callable.__safe_for_unpickling__
715 except AttributeError:
716 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000717
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000718 if (not safe):
719 raise UnpicklingError, "%s is not safe for " \
720 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000721
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000722 if arg_tup is None:
723 value = callable.__basicnew__()
724 else:
725 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000726 self.append(value)
727 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000728
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000729 def load_pop(self):
730 del self.stack[-1]
731 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000732
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000733 def load_pop_mark(self):
734 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000735 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000736 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000737
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000738 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000739 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000740 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000741
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000742 def load_get(self):
743 self.append(self.memo[self.readline()[:-1]])
744 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000745
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000746 def load_binget(self):
747 i = mloads('i' + self.read(1) + '\000\000\000')
748 self.append(self.memo[`i`])
749 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000750
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000751 def load_long_binget(self):
752 i = mloads('i' + self.read(4))
753 self.append(self.memo[`i`])
754 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000755
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000756 def load_put(self):
757 self.memo[self.readline()[:-1]] = self.stack[-1]
758 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000759
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000760 def load_binput(self):
761 i = mloads('i' + self.read(1) + '\000\000\000')
762 self.memo[`i`] = self.stack[-1]
763 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000764
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 def load_long_binput(self):
766 i = mloads('i' + self.read(4))
767 self.memo[`i`] = self.stack[-1]
768 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000769
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000770 def load_append(self):
771 stack = self.stack
772 value = stack[-1]
773 del stack[-1]
774 list = stack[-1]
775 list.append(value)
776 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000777
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000778 def load_appends(self):
779 stack = self.stack
780 mark = self.marker()
781 list = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000782 for i in range(mark + 1, len(stack)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000783 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000784
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000785 del stack[mark:]
786 dispatch[APPENDS] = load_appends
787
788 def load_setitem(self):
789 stack = self.stack
790 value = stack[-1]
791 key = stack[-2]
792 del stack[-2:]
793 dict = stack[-1]
794 dict[key] = value
795 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000796
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000797 def load_setitems(self):
798 stack = self.stack
799 mark = self.marker()
800 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000801 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000802 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000803
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000804 del stack[mark:]
805 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000806
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000807 def load_build(self):
808 stack = self.stack
809 value = stack[-1]
810 del stack[-1]
811 inst = stack[-1]
812 try:
813 setstate = inst.__setstate__
814 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000815 try:
816 inst.__dict__.update(value)
817 except RuntimeError:
818 # XXX In restricted execution, the instance's __dict__ is not
819 # accessible. Use the old way of unpickling the instance
820 # variables. This is a semantic different when unpickling in
821 # restricted vs. unrestricted modes.
822 for k, v in value.items():
823 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000824 else:
825 setstate(value)
826 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000827
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000828 def load_mark(self):
829 self.append(self.mark)
830 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000831
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000832 def load_stop(self):
833 value = self.stack[-1]
834 del self.stack[-1]
835 raise STOP, value
836 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000837
Guido van Rossume467be61997-12-05 19:42:42 +0000838# Helper class for load_inst/load_obj
839
840class _EmptyClass:
841 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +0000842
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000843# Shorthands
844
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000845from StringIO import StringIO
846
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000847def dump(object, file, bin = 0):
848 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000849
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000850def dumps(object, bin = 0):
851 file = StringIO()
852 Pickler(file, bin).dump(object)
853 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000854
855def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000856 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000857
858def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000859 file = StringIO(str)
860 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000861
862
863# The rest is used for testing only
864
Guido van Rossuma48061a1995-01-10 00:31:14 +0000865class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000866 def __cmp__(self, other):
867 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000868
869def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000870 fn = 'out'
871 c = C()
872 c.foo = 1
873 c.bar = 2
874 x = [0, 1, 2, 3]
875 y = ('abc', 'abc', c, c)
876 x.append(y)
877 x.append(y)
878 x.append(5)
879 f = open(fn, 'w')
880 F = Pickler(f)
881 F.dump(x)
882 f.close()
883 f = open(fn, 'r')
884 U = Unpickler(f)
885 x2 = U.load()
886 print x
887 print x2
888 print x == x2
889 print map(id, x)
890 print map(id, x2)
891 print F.memo
892 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000893
894if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000895 test()