blob: bf735994fe9d14ac18e24df3625e7ea8bca10b82 [file] [log] [blame]
Guido van Rossume467be61997-12-05 19:42:42 +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 Rossume467be61997-12-05 19:42:42 +000026__version__ = "1.9" # 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 Rossumb72cf2d1997-04-09 17:32:51 +000030import string, marshal
Guido van Rossuma48061a1995-01-10 00:31:14 +000031
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000032format_version = "1.2" # File format version we write
33compatible_formats = ["1.0", "1.1"] # Old format versions we can read
34
35mdumps = marshal.dumps
36mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +000037
Guido van Rossum7849da81995-03-09 14:08:35 +000038PicklingError = "pickle.PicklingError"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000039UnpicklingError = "pickle.UnpicklingError"
Guido van Rossum7849da81995-03-09 14:08:35 +000040
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000041MARK = '('
42STOP = '.'
43POP = '0'
44POP_MARK = '1'
45DUP = '2'
46FLOAT = 'F'
47INT = 'I'
48BININT = 'J'
49BININT1 = 'K'
50LONG = 'L'
51BININT2 = 'M'
52NONE = 'N'
53PERSID = 'P'
54BINPERSID = 'Q'
55REDUCE = 'R'
56STRING = 'S'
57BINSTRING = 'T'
58SHORT_BINSTRING = 'U'
59APPEND = 'a'
60BUILD = 'b'
61GLOBAL = 'c'
62DICT = 'd'
63EMPTY_DICT = '}'
64APPENDS = 'e'
65GET = 'g'
66BINGET = 'h'
67INST = 'i'
68LONG_BINGET = 'j'
69LIST = 'l'
70EMPTY_LIST = ']'
71OBJ = 'o'
72PUT = 'p'
73BINPUT = 'q'
74LONG_BINPUT = 'r'
75SETITEM = 's'
76TUPLE = 't'
77EMPTY_TUPLE = ')'
78SETITEMS = 'u'
Guido van Rossuma48061a1995-01-10 00:31:14 +000079
80class Pickler:
81
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000082 def __init__(self, file, bin = 0):
83 self.write = file.write
84 self.memo = {}
85 self.bin = bin
Guido van Rossuma48061a1995-01-10 00:31:14 +000086
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000087 def dump(self, object):
88 self.save(object)
89 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +000090
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000091 def dump_special(self, callable, args, state = None):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000092 if type(args) is not TupleType and args is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000093 raise PicklingError, "Second argument to dump_special " \
94 "must be a tuple"
Guido van Rossuma48061a1995-01-10 00:31:14 +000095
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000096 self.save_reduce(callable, args, state)
97 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +000098
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000099 def put(self, i):
100 if (self.bin):
101 s = mdumps(i)[1:]
102 if (i < 256):
103 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000104
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000105 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000106
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000107 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000108
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000109 def get(self, i):
110 if (self.bin):
111 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000113 if (i < 256):
114 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000115
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000116 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000117
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000118 return GET + `i` + '\n'
119
120 def save(self, object, pers_save = 0):
121 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000122
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000123 if (not pers_save):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000124 pid = self.persistent_id(object)
125 if (pid is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000126 self.save_pers(pid)
127 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000128
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000129 d = id(object)
130
131 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 if ((t is TupleType) and (len(object) == 0)):
134 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000135 self.save_empty_tuple(object)
136 else:
137 self.save_tuple(object)
138 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000139
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000140 if memo.has_key(d):
141 self.write(self.get(memo[d][0]))
142 return
143
144 try:
145 f = self.dispatch[t]
146 except KeyError:
147 pid = self.inst_persistent_id(object)
148 if pid is not None:
149 self.save_pers(pid)
150 return
151
152 try:
153 reduce = dispatch_table[t]
154 except KeyError:
155 try:
156 reduce = object.__reduce__
157 except AttributeError:
158 raise PicklingError, \
159 "can't pickle %s objects" % `t.__name__`
160 else:
161 tup = reduce()
162 else:
163 tup = reduce(object)
164
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 if type(tup) is StringType:
166 self.save_global(object, tup)
167 return
Guido van Rossumd1f49841997-12-10 23:40:18 +0000168
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000169 if (type(tup) is not TupleType):
170 raise PicklingError, "Value returned by %s must be a " \
171 "tuple" % reduce
172
173 l = len(tup)
174
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000175 if ((l != 2) and (l != 3)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000176 raise PicklingError, "tuple returned by %s must contain " \
177 "only two or three elements" % reduce
178
179 callable = tup[0]
180 arg_tup = tup[1]
181
182 if (l > 2):
183 state = tup[2]
184 else:
185 state = None
186
Guido van Rossumd1f49841997-12-10 23:40:18 +0000187 if type(arg_tup) is not TupleType and arg_tup is not None:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000188 raise PicklingError, "Second element of tuple returned " \
189 "by %s must be a tuple" % reduce
190
191 self.save_reduce(callable, arg_tup, state)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000192 memo_len = len(memo)
193 self.write(self.put(memo_len))
194 memo[d] = (memo_len, object)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000195 return
196
197 f(self, object)
198
199 def persistent_id(self, object):
200 return None
201
202 def inst_persistent_id(self, object):
203 return None
204
205 def save_pers(self, pid):
206 if (not self.bin):
207 self.write(PERSID + str(pid) + '\n')
208 else:
209 self.save(pid, 1)
210 self.write(BINPERSID)
211
212 def save_reduce(self, callable, arg_tup, state = None):
213 write = self.write
214 save = self.save
215
216 save(callable)
217 save(arg_tup)
218 write(REDUCE)
219
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000220 if (state is not None):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000221 save(state)
222 write(BUILD)
223
224 dispatch = {}
225
226 def save_none(self, object):
227 self.write(NONE)
228 dispatch[NoneType] = save_none
229
230 def save_int(self, object):
231 if (self.bin):
232 i = mdumps(object)[1:]
233 if (i[-2:] == '\000\000'):
234 if (i[-3] == '\000'):
235 self.write(BININT1 + i[:-3])
236 return
237
238 self.write(BININT2 + i[:-2])
239 return
240
241 self.write(BININT + i)
242 else:
243 self.write(INT + `object` + '\n')
244 dispatch[IntType] = save_int
245
246 def save_long(self, object):
247 self.write(LONG + `object` + '\n')
248 dispatch[LongType] = save_long
249
250 def save_float(self, object):
251 self.write(FLOAT + `object` + '\n')
252 dispatch[FloatType] = save_float
253
254 def save_string(self, object):
255 d = id(object)
256 memo = self.memo
257
258 if (self.bin):
259 l = len(object)
260 s = mdumps(l)[1:]
261 if (l < 256):
262 self.write(SHORT_BINSTRING + s[0] + object)
263 else:
264 self.write(BINSTRING + s + object)
265 else:
266 self.write(STRING + `object` + '\n')
267
268 memo_len = len(memo)
269 self.write(self.put(memo_len))
270 memo[d] = (memo_len, object)
271 dispatch[StringType] = save_string
272
273 def save_tuple(self, object):
274
275 write = self.write
276 save = self.save
277 memo = self.memo
278
279 d = id(object)
280
281 write(MARK)
282
283 for element in object:
284 save(element)
285
286 if (len(object) and memo.has_key(d)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000287 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000288 write(POP_MARK + self.get(memo[d][0]))
289 return
290
Guido van Rossum599174f1998-03-31 16:30:28 +0000291 write(POP * (len(object) + 1) + self.get(memo[d][0]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000292 return
293
294 memo_len = len(memo)
295 self.write(TUPLE + self.put(memo_len))
296 memo[d] = (memo_len, object)
297 dispatch[TupleType] = save_tuple
298
299 def save_empty_tuple(self, object):
300 self.write(EMPTY_TUPLE)
301
302 def save_list(self, object):
303 d = id(object)
304
305 write = self.write
306 save = self.save
307 memo = self.memo
308
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000309 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000310 write(EMPTY_LIST)
311 else:
312 write(MARK + LIST)
313
314 memo_len = len(memo)
315 write(self.put(memo_len))
316 memo[d] = (memo_len, object)
317
318 using_appends = (self.bin and (len(object) > 1))
319
320 if (using_appends):
321 write(MARK)
322
323 for element in object:
324 save(element)
325
326 if (not using_appends):
327 write(APPEND)
328
329 if (using_appends):
330 write(APPENDS)
331 dispatch[ListType] = save_list
332
333 def save_dict(self, object):
334 d = id(object)
335
336 write = self.write
337 save = self.save
338 memo = self.memo
339
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000340 if (self.bin):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000341 write(EMPTY_DICT)
342 else:
343 write(MARK + DICT)
344
345 memo_len = len(memo)
346 self.write(self.put(memo_len))
347 memo[d] = (memo_len, object)
348
349 using_setitems = (self.bin and (len(object) > 1))
350
351 if (using_setitems):
352 write(MARK)
353
354 items = object.items()
355 for key, value in items:
356 save(key)
357 save(value)
358
359 if (not using_setitems):
360 write(SETITEM)
361
362 if (using_setitems):
363 write(SETITEMS)
364
365 dispatch[DictionaryType] = save_dict
366
367 def save_inst(self, object):
368 d = id(object)
369 cls = object.__class__
370
371 memo = self.memo
372 write = self.write
373 save = self.save
374
375 if hasattr(object, '__getinitargs__'):
376 args = object.__getinitargs__()
377 len(args) # XXX Assert it's a sequence
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000378 _keep_alive(args, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000379 else:
380 args = ()
381
382 write(MARK)
383
384 if (self.bin):
385 save(cls)
386
387 for arg in args:
388 save(arg)
389
390 memo_len = len(memo)
391 if (self.bin):
392 write(OBJ + self.put(memo_len))
393 else:
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000394 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000395 self.put(memo_len))
396
397 memo[d] = (memo_len, object)
398
399 try:
400 getstate = object.__getstate__
401 except AttributeError:
402 stuff = object.__dict__
403 else:
404 stuff = getstate()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000405 _keep_alive(stuff, memo)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000406 save(stuff)
407 write(BUILD)
408 dispatch[InstanceType] = save_inst
409
410 def save_global(self, object, name = None):
411 write = self.write
412 memo = self.memo
413
414 if (name is None):
415 name = object.__name__
416
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000417 try:
418 module = object.__module__
419 except AttributeError:
420 module = whichmodule(object, name)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000421
422 memo_len = len(memo)
423 write(GLOBAL + module + '\n' + name + '\n' +
424 self.put(memo_len))
425 memo[id(object)] = (memo_len, object)
426 dispatch[ClassType] = save_global
427 dispatch[FunctionType] = save_global
428 dispatch[BuiltinFunctionType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000429
Guido van Rossuma48061a1995-01-10 00:31:14 +0000430
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000431def _keep_alive(x, memo):
432 """Keeps a reference to the object x in the memo.
433
434 Because we remember objects by their id, we have
435 to assure that possibly temporary objects are kept
436 alive by referencing them.
437 We store a reference at the id of the memo, which should
438 normally not be used unless someone tries to deepcopy
439 the memo itself...
440 """
441 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000442 memo[id(memo)].append(x)
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000443 except KeyError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000444 # aha, this is the first one :-)
445 memo[id(memo)]=[x]
Guido van Rossum5ed5c4c1997-09-03 00:23:54 +0000446
447
Guido van Rossuma48061a1995-01-10 00:31:14 +0000448classmap = {}
449
Guido van Rossum4fb5b281997-09-12 20:07:24 +0000450# This is no longer used to find classes, but still for functions
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000451def whichmodule(cls, clsname):
452 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000453
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000454 Search sys.modules for the module.
455 Cache in classmap.
456 Return a module name.
457 If the class cannot be found, return __main__.
458 """
459 if classmap.has_key(cls):
460 return classmap[cls]
461 import sys
462
463 for name, module in sys.modules.items():
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000464 if name != '__main__' and \
465 hasattr(module, clsname) and \
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000466 getattr(module, clsname) is cls:
467 break
468 else:
469 name = '__main__'
470 classmap[cls] = name
471 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000472
473
474class Unpickler:
475
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000476 def __init__(self, file):
477 self.readline = file.readline
478 self.read = file.read
479 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000480
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000481 def load(self):
482 self.mark = ['spam'] # Any new unique object
483 self.stack = []
484 self.append = self.stack.append
485 read = self.read
486 dispatch = self.dispatch
487 try:
488 while 1:
489 key = read(1)
490 dispatch[key](self)
491 except STOP, value:
492 return value
493
494 def marker(self):
495 stack = self.stack
496 mark = self.mark
497 k = len(stack)-1
498 while stack[k] is not mark: k = k-1
499 return k
500
501 dispatch = {}
502
503 def load_eof(self):
504 raise EOFError
505 dispatch[''] = load_eof
506
507 def load_persid(self):
508 pid = self.readline()[:-1]
509 self.append(self.persistent_load(pid))
510 dispatch[PERSID] = load_persid
511
512 def load_binpersid(self):
513 stack = self.stack
514
515 pid = stack[-1]
516 del stack[-1]
517
518 self.append(self.persistent_load(pid))
519 dispatch[BINPERSID] = load_binpersid
520
521 def load_none(self):
522 self.append(None)
523 dispatch[NONE] = load_none
524
525 def load_int(self):
Guido van Rossume6805461997-12-10 19:36:41 +0000526 self.append(string.atoi(self.readline()[:-1]))
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000527 dispatch[INT] = load_int
528
529 def load_binint(self):
530 self.append(mloads('i' + self.read(4)))
531 dispatch[BININT] = load_binint
532
533 def load_binint1(self):
534 self.append(mloads('i' + self.read(1) + '\000\000\000'))
535 dispatch[BININT1] = load_binint1
536
537 def load_binint2(self):
538 self.append(mloads('i' + self.read(2) + '\000\000'))
539 dispatch[BININT2] = load_binint2
540
541 def load_long(self):
542 self.append(string.atol(self.readline()[:-1], 0))
543 dispatch[LONG] = load_long
544
545 def load_float(self):
546 self.append(string.atof(self.readline()[:-1]))
547 dispatch[FLOAT] = load_float
548
549 def load_string(self):
550 self.append(eval(self.readline()[:-1],
551 {'__builtins__': {}})) # Let's be careful
552 dispatch[STRING] = load_string
553
554 def load_binstring(self):
555 len = mloads('i' + self.read(4))
556 self.append(self.read(len))
557 dispatch[BINSTRING] = load_binstring
558
559 def load_short_binstring(self):
560 len = mloads('i' + self.read(1) + '\000\000\000')
561 self.append(self.read(len))
562 dispatch[SHORT_BINSTRING] = load_short_binstring
563
564 def load_tuple(self):
565 k = self.marker()
566 self.stack[k:] = [tuple(self.stack[k+1:])]
567 dispatch[TUPLE] = load_tuple
568
569 def load_empty_tuple(self):
570 self.stack.append(())
571 dispatch[EMPTY_TUPLE] = load_empty_tuple
572
573 def load_empty_list(self):
574 self.stack.append([])
575 dispatch[EMPTY_LIST] = load_empty_list
576
577 def load_empty_dictionary(self):
578 self.stack.append({})
579 dispatch[EMPTY_DICT] = load_empty_dictionary
580
581 def load_list(self):
582 k = self.marker()
583 self.stack[k:] = [self.stack[k+1:]]
584 dispatch[LIST] = load_list
585
586 def load_dict(self):
587 k = self.marker()
588 d = {}
589 items = self.stack[k+1:]
590 for i in range(0, len(items), 2):
591 key = items[i]
592 value = items[i+1]
593 d[key] = value
594 self.stack[k:] = [d]
595 dispatch[DICT] = load_dict
596
597 def load_inst(self):
598 k = self.marker()
599 args = tuple(self.stack[k+1:])
600 del self.stack[k:]
601 module = self.readline()[:-1]
602 name = self.readline()[:-1]
603 klass = self.find_class(module, name)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000604 instantiated = 0
605 if (not args and type(klass) is ClassType and
606 not hasattr(klass, "__getinitargs__")):
607 try:
608 value = _EmptyClass()
609 value.__class__ = klass
Guido van Rossumb19e2a31998-04-13 18:08:45 +0000610 instantiated = 1
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000611 except RuntimeError:
612 # In restricted execution, assignment to inst.__class__ is
613 # prohibited
614 pass
615 if not instantiated:
616 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000617 self.append(value)
618 dispatch[INST] = load_inst
619
620 def load_obj(self):
621 stack = self.stack
622 k = self.marker()
623 klass = stack[k + 1]
624 del stack[k + 1]
625 args = tuple(stack[k + 1:])
626 del stack[k:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000627 instantiated = 0
628 if (not args and type(klass) is ClassType and
629 not hasattr(klass, "__getinitargs__")):
630 try:
631 value = _EmptyClass()
632 value.__class__ = klass
633 instantiated = 1
634 except RuntimeError:
635 # In restricted execution, assignment to inst.__class__ is
636 # prohibited
637 pass
638 if not instantiated:
639 value = apply(klass, args)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000640 self.append(value)
641 dispatch[OBJ] = load_obj
642
643 def load_global(self):
644 module = self.readline()[:-1]
645 name = self.readline()[:-1]
646 klass = self.find_class(module, name)
647 self.append(klass)
648 dispatch[GLOBAL] = load_global
649
650 def find_class(self, module, name):
651 env = {}
652
653 try:
654 exec 'from %s import %s' % (module, name) in env
655 except ImportError:
656 raise SystemError, \
657 "Failed to import class %s from module %s" % \
658 (name, module)
659 klass = env[name]
660 return klass
661
662 def load_reduce(self):
663 stack = self.stack
664
665 callable = stack[-2]
666 arg_tup = stack[-1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000667 del stack[-2:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000668
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000669 if type(callable) is not ClassType:
670 if not safe_constructors.has_key(callable):
671 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000672 safe = callable.__safe_for_unpickling__
673 except AttributeError:
674 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000675
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000676 if (not safe):
677 raise UnpicklingError, "%s is not safe for " \
678 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000679
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000680 if arg_tup is None:
681 value = callable.__basicnew__()
682 else:
683 value = apply(callable, arg_tup)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000684 self.append(value)
685 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000686
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000687 def load_pop(self):
688 del self.stack[-1]
689 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000690
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000691 def load_pop_mark(self):
692 k = self.marker()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000693 del self.stack[k:]
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000694 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000695
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000696 def load_dup(self):
Guido van Rossumb1062fc1998-03-31 17:00:46 +0000697 self.append(self.stack[-1])
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000698 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000699
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000700 def load_get(self):
701 self.append(self.memo[self.readline()[:-1]])
702 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000703
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000704 def load_binget(self):
705 i = mloads('i' + self.read(1) + '\000\000\000')
706 self.append(self.memo[`i`])
707 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000708
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000709 def load_long_binget(self):
710 i = mloads('i' + self.read(4))
711 self.append(self.memo[`i`])
712 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000713
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000714 def load_put(self):
715 self.memo[self.readline()[:-1]] = self.stack[-1]
716 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000717
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000718 def load_binput(self):
719 i = mloads('i' + self.read(1) + '\000\000\000')
720 self.memo[`i`] = self.stack[-1]
721 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000722
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000723 def load_long_binput(self):
724 i = mloads('i' + self.read(4))
725 self.memo[`i`] = self.stack[-1]
726 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000727
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000728 def load_append(self):
729 stack = self.stack
730 value = stack[-1]
731 del stack[-1]
732 list = stack[-1]
733 list.append(value)
734 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000735
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000736 def load_appends(self):
737 stack = self.stack
738 mark = self.marker()
739 list = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000740 for i in range(mark + 1, len(stack)):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000741 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000742
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000743 del stack[mark:]
744 dispatch[APPENDS] = load_appends
745
746 def load_setitem(self):
747 stack = self.stack
748 value = stack[-1]
749 key = stack[-2]
750 del stack[-2:]
751 dict = stack[-1]
752 dict[key] = value
753 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000754
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000755 def load_setitems(self):
756 stack = self.stack
757 mark = self.marker()
758 dict = stack[mark - 1]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000759 for i in range(mark + 1, len(stack), 2):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000760 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000761
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000762 del stack[mark:]
763 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000764
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000765 def load_build(self):
766 stack = self.stack
767 value = stack[-1]
768 del stack[-1]
769 inst = stack[-1]
770 try:
771 setstate = inst.__setstate__
772 except AttributeError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000773 try:
774 inst.__dict__.update(value)
775 except RuntimeError:
776 # XXX In restricted execution, the instance's __dict__ is not
777 # accessible. Use the old way of unpickling the instance
778 # variables. This is a semantic different when unpickling in
779 # restricted vs. unrestricted modes.
780 for k, v in value.items():
781 setattr(inst, k, v)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 else:
783 setstate(value)
784 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000785
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000786 def load_mark(self):
787 self.append(self.mark)
788 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000789
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000790 def load_stop(self):
791 value = self.stack[-1]
792 del self.stack[-1]
793 raise STOP, value
794 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000795
Guido van Rossume467be61997-12-05 19:42:42 +0000796# Helper class for load_inst/load_obj
797
798class _EmptyClass:
799 pass
Guido van Rossuma48061a1995-01-10 00:31:14 +0000800
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000801# Shorthands
802
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000803from StringIO import StringIO
804
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000805def dump(object, file, bin = 0):
806 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000807
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000808def dumps(object, bin = 0):
809 file = StringIO()
810 Pickler(file, bin).dump(object)
811 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000812
813def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000814 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000815
816def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000817 file = StringIO(str)
818 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000819
820
821# The rest is used for testing only
822
Guido van Rossuma48061a1995-01-10 00:31:14 +0000823class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000824 def __cmp__(self, other):
825 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000826
827def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000828 fn = 'out'
829 c = C()
830 c.foo = 1
831 c.bar = 2
832 x = [0, 1, 2, 3]
833 y = ('abc', 'abc', c, c)
834 x.append(y)
835 x.append(y)
836 x.append(5)
837 f = open(fn, 'w')
838 F = Pickler(f)
839 F.dump(x)
840 f.close()
841 f = open(fn, 'r')
842 U = Unpickler(f)
843 x2 = U.load()
844 print x
845 print x2
846 print x == x2
847 print map(id, x)
848 print map(id, x2)
849 print F.memo
850 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000851
852if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000853 test()