blob: bc01c905a4cc2315d580323c7db7741eacf5658a [file] [log] [blame]
Guido van Rossuma48061a1995-01-10 00:31:14 +00001"""\
2Pickling Algorithm
3------------------
4
5This module implements a basic but powerful algorithm for "pickling" (a.k.a.
6serializing, marshalling or flattening) nearly arbitrary Python objects.
7This is a more primitive notion than persistency -- although pickle
8reads and writes file objects, it does not handle the issue of naming
9persistent objects, nor the (even more complicated) area of concurrent
10access to persistent objects. The pickle module can transform a complex
11object into a byte stream and it can transform the byte stream into
12an object with the same internal structure. The most obvious thing to
13do with these byte streams is to write them onto a file, but it is also
14conceivable to send them across a network or store them in a database.
15
16Unlike the built-in marshal module, pickle handles the following correctly:
17
18- recursive objects
19- pointer sharing
Guido van Rossum0c891ce1995-03-14 15:09:05 +000020- classes and class instances
Guido van Rossuma48061a1995-01-10 00:31:14 +000021
22Pickle is Python-specific. This has the advantage that there are no
23restrictions imposed by external standards such as CORBA (which probably
24can't represent pointer sharing or recursive objects); however it means
25that non-Python programs may not be able to reconstruct pickled Python
26objects.
27
28Pickle uses a printable ASCII representation. This is slightly more
29voluminous than a binary representation. However, small integers actually
30take *less* space when represented as minimal-size decimal strings than
31when represented as 32-bit binary numbers, and strings are only much longer
32if they contain control characters or 8-bit characters. The big advantage
33of using printable ASCII (and of some other characteristics of pickle's
34representation) is that for debugging or recovery purposes it is possible
35for a human to read the pickled file with a standard text editor. (I could
36have gone a step further and used a notation like S-expressions, but the
37parser would have been considerably more complicated and slower, and the
38files would probably have become much larger.)
39
40Pickle doesn't handle code objects, which marshal does.
41I suppose pickle could, and maybe it should, but there's probably no
42great need for it right now (as long as marshal continues to be used
43for reading and writing code objects), and at least this avoids
44the possibility of smuggling Trojan horses into a program.
45
46For the benefit of persistency modules written using pickle, it supports
47the notion of a reference to an object outside the pickled data stream.
48Such objects are referenced by a name, which is an arbitrary string of
49printable ASCII characters. The resolution of such names is not defined
50by the pickle module -- the persistent object module will have to implement
51a method "persistent_load". To write references to persistent objects,
52the persistent module must define a method "persistent_id" which returns
53either None or the persistent ID of the object.
54
55There are some restrictions on the pickling of class instances.
56
57First of all, the class must be defined at the top level in a module.
58
Guido van Rossum37a6f161996-08-08 18:35:22 +000059Next, it must normally be possible to create class instances by
60calling the class without arguments. Usually, this is best
61accomplished by providing default values for all arguments to its
62__init__ method (if it has one). If this is undesirable, the
63class can define a method __getinitargs__, which should return a
64*tuple* containing the arguments to be passed to the class
Guido van Rossuma48061a1995-01-10 00:31:14 +000065constructor.
66
Guido van Rossum0c891ce1995-03-14 15:09:05 +000067Classes can influence how their instances are pickled -- if the class defines
Guido van Rossuma48061a1995-01-10 00:31:14 +000068the method __getstate__, it is called and the return state is pickled
69as the contents for the instance, and if the class defines the
70method __setstate__, it is called with the unpickled state. (Note
71that these methods can also be used to implement copying class instances.)
72If there is no __getstate__ method, the instance's __dict__
73is pickled. If there is no __setstate__ method, the pickled object
74must be a dictionary and its items are assigned to the new instance's
75dictionary. (If a class defines both __getstate__ and __setstate__,
76the state object needn't be a dictionary -- these methods can do what they
77want.)
78
79Note that when class instances are pickled, their class's code and data
80is not pickled along with them. Only the instance data is pickled.
81This is done on purpose, so you can fix bugs in a class or add methods and
82still load objects that were created with an earlier version of the
83class. If you plan to have long-lived objects that will see many versions
84of a class, it may be worth to put a version number in the objects so
85that suitable conversions can be made by the class's __setstate__ method.
86
87The interface is as follows:
88
Guido van Rossum256cbd71995-02-16 16:30:50 +000089To pickle an object x onto a file f, open for writing:
Guido van Rossuma48061a1995-01-10 00:31:14 +000090
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000091 p = pickle.Pickler(f)
92 p.dump(x)
Guido van Rossuma48061a1995-01-10 00:31:14 +000093
94To unpickle an object x from a file f, open for reading:
95
Guido van Rossumb72cf2d1997-04-09 17:32:51 +000096 u = pickle.Unpickler(f)
97 x = u.load()
Guido van Rossuma48061a1995-01-10 00:31:14 +000098
99The Pickler class only calls the method f.write with a string argument
100(XXX possibly the interface should pass f.write instead of f).
101The Unpickler calls the methods f.read(with an integer argument)
102and f.readline(without argument), both returning a string.
103It is explicitly allowed to pass non-file objects here, as long as they
104have the right methods.
105
106The following types can be pickled:
107
108- None
109- integers, long integers, floating point numbers
110- strings
Guido van Rossum256cbd71995-02-16 16:30:50 +0000111- tuples, lists and dictionaries containing only picklable objects
Guido van Rossuma48061a1995-01-10 00:31:14 +0000112- class instances whose __dict__ or __setstate__() is picklable
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000113- classes
Guido van Rossuma48061a1995-01-10 00:31:14 +0000114
115Attempts to pickle unpicklable objects will raise an exception
116after having written an unspecified number of bytes to the file argument.
117
118It is possible to make multiple calls to Pickler.dump() or to
119Unpickler.load(), as long as there is a one-to-one correspondence
Guido van Rossum256cbd71995-02-16 16:30:50 +0000120between pickler and Unpickler objects and between dump and load calls
Guido van Rossuma48061a1995-01-10 00:31:14 +0000121for any pair of corresponding Pickler and Unpicklers. WARNING: this
122is intended for pickleing multiple objects without intervening modifications
123to the objects or their parts. If you modify an object and then pickle
124it again using the same Pickler instance, the object is not pickled
125again -- a reference to it is pickled and the Unpickler will return
126the old value, not the modified one. (XXX There are two problems here:
127(a) detecting changes, and (b) marshalling a minimal set of changes.
128I have no answers. Garbage Collection may also become a problem here.)
129"""
130
Guido van Rossum8be9a111997-04-25 19:52:29 +0000131__version__ = "1.8" # Code version
Guido van Rossuma48061a1995-01-10 00:31:14 +0000132
133from types import *
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000134from copy_reg import *
135import string, marshal
Guido van Rossuma48061a1995-01-10 00:31:14 +0000136
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000137format_version = "1.2" # File format version we write
138compatible_formats = ["1.0", "1.1"] # Old format versions we can read
139
140mdumps = marshal.dumps
141mloads = marshal.loads
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000142
Guido van Rossum7849da81995-03-09 14:08:35 +0000143PicklingError = "pickle.PicklingError"
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000144UnpicklingError = "pickle.UnpicklingError"
Guido van Rossum7849da81995-03-09 14:08:35 +0000145
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000146MARK = '('
147STOP = '.'
148POP = '0'
149POP_MARK = '1'
150DUP = '2'
151FLOAT = 'F'
152INT = 'I'
153BININT = 'J'
154BININT1 = 'K'
155LONG = 'L'
156BININT2 = 'M'
157NONE = 'N'
158PERSID = 'P'
159BINPERSID = 'Q'
160REDUCE = 'R'
161STRING = 'S'
162BINSTRING = 'T'
163SHORT_BINSTRING = 'U'
164APPEND = 'a'
165BUILD = 'b'
166GLOBAL = 'c'
167DICT = 'd'
168EMPTY_DICT = '}'
169APPENDS = 'e'
170GET = 'g'
171BINGET = 'h'
172INST = 'i'
173LONG_BINGET = 'j'
174LIST = 'l'
175EMPTY_LIST = ']'
176OBJ = 'o'
177PUT = 'p'
178BINPUT = 'q'
179LONG_BINPUT = 'r'
180SETITEM = 's'
181TUPLE = 't'
182EMPTY_TUPLE = ')'
183SETITEMS = 'u'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000184
185class Pickler:
186
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000187 def __init__(self, file, bin = 0):
188 self.write = file.write
189 self.memo = {}
190 self.bin = bin
Guido van Rossuma48061a1995-01-10 00:31:14 +0000191
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000192 def dump(self, object):
193 self.save(object)
194 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000195
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000196 def dump_special(self, callable, args, state = None):
197 if (type(args) is not TupleType):
198 raise PicklingError, "Second argument to dump_special " \
199 "must be a tuple"
Guido van Rossuma48061a1995-01-10 00:31:14 +0000200
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000201 self.save_reduce(callable, args, state)
202 self.write(STOP)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000203
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000204 def put(self, i):
205 if (self.bin):
206 s = mdumps(i)[1:]
207 if (i < 256):
208 return BINPUT + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000209
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000210 return LONG_BINPUT + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000211
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000212 return PUT + `i` + '\n'
Guido van Rossuma48061a1995-01-10 00:31:14 +0000213
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000214 def get(self, i):
215 if (self.bin):
216 s = mdumps(i)[1:]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000217
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000218 if (i < 256):
219 return BINGET + s[0]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000220
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000221 return LONG_BINGET + s
Guido van Rossuma48061a1995-01-10 00:31:14 +0000222
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000223 return GET + `i` + '\n'
224
225 def save(self, object, pers_save = 0):
226 memo = self.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000227
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000228 if (not pers_save):
229 pid = self.persistent_id(object)
230 if (pid is not None):
231 self.save_pers(pid)
232 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000233
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000234 d = id(object)
235
236 t = type(object)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000237
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000238 if ((t is TupleType) and (len(object) == 0)):
239 if (self.bin):
240 self.save_empty_tuple(object)
241 else:
242 self.save_tuple(object)
243 return
Guido van Rossuma48061a1995-01-10 00:31:14 +0000244
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000245 if memo.has_key(d):
246 self.write(self.get(memo[d][0]))
247 return
248
249 try:
250 f = self.dispatch[t]
251 except KeyError:
252 pid = self.inst_persistent_id(object)
253 if pid is not None:
254 self.save_pers(pid)
255 return
256
257 try:
258 reduce = dispatch_table[t]
259 except KeyError:
260 try:
261 reduce = object.__reduce__
262 except AttributeError:
263 raise PicklingError, \
264 "can't pickle %s objects" % `t.__name__`
265 else:
266 tup = reduce()
267 else:
268 tup = reduce(object)
269
270 if (type(tup) is not TupleType):
271 raise PicklingError, "Value returned by %s must be a " \
272 "tuple" % reduce
273
274 l = len(tup)
275
276 if ((l != 2) and (l != 3)):
277 raise PicklingError, "tuple returned by %s must contain " \
278 "only two or three elements" % reduce
279
280 callable = tup[0]
281 arg_tup = tup[1]
282
283 if (l > 2):
284 state = tup[2]
285 else:
286 state = None
287
288 if (type(arg_tup) is not TupleType):
289 raise PicklingError, "Second element of tuple returned " \
290 "by %s must be a tuple" % reduce
291
292 self.save_reduce(callable, arg_tup, state)
293 return
294
295 f(self, object)
296
297 def persistent_id(self, object):
298 return None
299
300 def inst_persistent_id(self, object):
301 return None
302
303 def save_pers(self, pid):
304 if (not self.bin):
305 self.write(PERSID + str(pid) + '\n')
306 else:
307 self.save(pid, 1)
308 self.write(BINPERSID)
309
310 def save_reduce(self, callable, arg_tup, state = None):
311 write = self.write
312 save = self.save
313
314 save(callable)
315 save(arg_tup)
316 write(REDUCE)
317
318 if (state is not None):
319 save(state)
320 write(BUILD)
321
322 dispatch = {}
323
324 def save_none(self, object):
325 self.write(NONE)
326 dispatch[NoneType] = save_none
327
328 def save_int(self, object):
329 if (self.bin):
330 i = mdumps(object)[1:]
331 if (i[-2:] == '\000\000'):
332 if (i[-3] == '\000'):
333 self.write(BININT1 + i[:-3])
334 return
335
336 self.write(BININT2 + i[:-2])
337 return
338
339 self.write(BININT + i)
340 else:
341 self.write(INT + `object` + '\n')
342 dispatch[IntType] = save_int
343
344 def save_long(self, object):
345 self.write(LONG + `object` + '\n')
346 dispatch[LongType] = save_long
347
348 def save_float(self, object):
349 self.write(FLOAT + `object` + '\n')
350 dispatch[FloatType] = save_float
351
352 def save_string(self, object):
353 d = id(object)
354 memo = self.memo
355
356 if (self.bin):
357 l = len(object)
358 s = mdumps(l)[1:]
359 if (l < 256):
360 self.write(SHORT_BINSTRING + s[0] + object)
361 else:
362 self.write(BINSTRING + s + object)
363 else:
364 self.write(STRING + `object` + '\n')
365
366 memo_len = len(memo)
367 self.write(self.put(memo_len))
368 memo[d] = (memo_len, object)
369 dispatch[StringType] = save_string
370
371 def save_tuple(self, object):
372
373 write = self.write
374 save = self.save
375 memo = self.memo
376
377 d = id(object)
378
379 write(MARK)
380
381 for element in object:
382 save(element)
383
384 if (len(object) and memo.has_key(d)):
385 if (self.bin):
386 write(POP_MARK + self.get(memo[d][0]))
387 return
388
389 write(POP * (len(object) + 1) + self.get(mem[d][0]))
390 return
391
392 memo_len = len(memo)
393 self.write(TUPLE + self.put(memo_len))
394 memo[d] = (memo_len, object)
395 dispatch[TupleType] = save_tuple
396
397 def save_empty_tuple(self, object):
398 self.write(EMPTY_TUPLE)
399
400 def save_list(self, object):
401 d = id(object)
402
403 write = self.write
404 save = self.save
405 memo = self.memo
406
407 if (self.bin):
408 write(EMPTY_LIST)
409 else:
410 write(MARK + LIST)
411
412 memo_len = len(memo)
413 write(self.put(memo_len))
414 memo[d] = (memo_len, object)
415
416 using_appends = (self.bin and (len(object) > 1))
417
418 if (using_appends):
419 write(MARK)
420
421 for element in object:
422 save(element)
423
424 if (not using_appends):
425 write(APPEND)
426
427 if (using_appends):
428 write(APPENDS)
429 dispatch[ListType] = save_list
430
431 def save_dict(self, object):
432 d = id(object)
433
434 write = self.write
435 save = self.save
436 memo = self.memo
437
438 if (self.bin):
439 write(EMPTY_DICT)
440 else:
441 write(MARK + DICT)
442
443 memo_len = len(memo)
444 self.write(self.put(memo_len))
445 memo[d] = (memo_len, object)
446
447 using_setitems = (self.bin and (len(object) > 1))
448
449 if (using_setitems):
450 write(MARK)
451
452 items = object.items()
453 for key, value in items:
454 save(key)
455 save(value)
456
457 if (not using_setitems):
458 write(SETITEM)
459
460 if (using_setitems):
461 write(SETITEMS)
462
463 dispatch[DictionaryType] = save_dict
464
465 def save_inst(self, object):
466 d = id(object)
467 cls = object.__class__
468
469 memo = self.memo
470 write = self.write
471 save = self.save
472
473 if hasattr(object, '__getinitargs__'):
474 args = object.__getinitargs__()
475 len(args) # XXX Assert it's a sequence
476 else:
477 args = ()
478
479 write(MARK)
480
481 if (self.bin):
482 save(cls)
483
484 for arg in args:
485 save(arg)
486
487 memo_len = len(memo)
488 if (self.bin):
489 write(OBJ + self.put(memo_len))
490 else:
491 module = whichmodule(cls, cls.__name__)
492 name = cls.__name__
493 write(INST + module + '\n' + name + '\n' +
494 self.put(memo_len))
495
496 memo[d] = (memo_len, object)
497
498 try:
499 getstate = object.__getstate__
500 except AttributeError:
501 stuff = object.__dict__
502 else:
503 stuff = getstate()
504 save(stuff)
505 write(BUILD)
506 dispatch[InstanceType] = save_inst
507
508 def save_global(self, object, name = None):
509 write = self.write
510 memo = self.memo
511
512 if (name is None):
513 name = object.__name__
514
515 module = whichmodule(object, name)
516
517 memo_len = len(memo)
518 write(GLOBAL + module + '\n' + name + '\n' +
519 self.put(memo_len))
520 memo[id(object)] = (memo_len, object)
521 dispatch[ClassType] = save_global
522 dispatch[FunctionType] = save_global
523 dispatch[BuiltinFunctionType] = save_global
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000524
Guido van Rossuma48061a1995-01-10 00:31:14 +0000525
526classmap = {}
527
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000528def whichmodule(cls, clsname):
529 """Figure out the module in which a class occurs.
Guido van Rossuma48061a1995-01-10 00:31:14 +0000530
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000531 Search sys.modules for the module.
532 Cache in classmap.
533 Return a module name.
534 If the class cannot be found, return __main__.
535 """
536 if classmap.has_key(cls):
537 return classmap[cls]
538 import sys
539
540 for name, module in sys.modules.items():
Guido van Rossum8be9a111997-04-25 19:52:29 +0000541 if name != '__main__' and \
542 hasattr(module, clsname) and \
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000543 getattr(module, clsname) is cls:
544 break
545 else:
546 name = '__main__'
547 classmap[cls] = name
548 return name
Guido van Rossuma48061a1995-01-10 00:31:14 +0000549
550
551class Unpickler:
552
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000553 def __init__(self, file):
554 self.readline = file.readline
555 self.read = file.read
556 self.memo = {}
Guido van Rossuma48061a1995-01-10 00:31:14 +0000557
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000558 def load(self):
559 self.mark = ['spam'] # Any new unique object
560 self.stack = []
561 self.append = self.stack.append
562 read = self.read
563 dispatch = self.dispatch
564 try:
565 while 1:
566 key = read(1)
567 dispatch[key](self)
568 except STOP, value:
569 return value
570
571 def marker(self):
572 stack = self.stack
573 mark = self.mark
574 k = len(stack)-1
575 while stack[k] is not mark: k = k-1
576 return k
577
578 dispatch = {}
579
580 def load_eof(self):
581 raise EOFError
582 dispatch[''] = load_eof
583
584 def load_persid(self):
585 pid = self.readline()[:-1]
586 self.append(self.persistent_load(pid))
587 dispatch[PERSID] = load_persid
588
589 def load_binpersid(self):
590 stack = self.stack
591
592 pid = stack[-1]
593 del stack[-1]
594
595 self.append(self.persistent_load(pid))
596 dispatch[BINPERSID] = load_binpersid
597
598 def load_none(self):
599 self.append(None)
600 dispatch[NONE] = load_none
601
602 def load_int(self):
603 self.append(string.atoi(self.readline()[:-1], 0))
604 dispatch[INT] = load_int
605
606 def load_binint(self):
607 self.append(mloads('i' + self.read(4)))
608 dispatch[BININT] = load_binint
609
610 def load_binint1(self):
611 self.append(mloads('i' + self.read(1) + '\000\000\000'))
612 dispatch[BININT1] = load_binint1
613
614 def load_binint2(self):
615 self.append(mloads('i' + self.read(2) + '\000\000'))
616 dispatch[BININT2] = load_binint2
617
618 def load_long(self):
619 self.append(string.atol(self.readline()[:-1], 0))
620 dispatch[LONG] = load_long
621
622 def load_float(self):
623 self.append(string.atof(self.readline()[:-1]))
624 dispatch[FLOAT] = load_float
625
626 def load_string(self):
627 self.append(eval(self.readline()[:-1],
628 {'__builtins__': {}})) # Let's be careful
629 dispatch[STRING] = load_string
630
631 def load_binstring(self):
632 len = mloads('i' + self.read(4))
633 self.append(self.read(len))
634 dispatch[BINSTRING] = load_binstring
635
636 def load_short_binstring(self):
637 len = mloads('i' + self.read(1) + '\000\000\000')
638 self.append(self.read(len))
639 dispatch[SHORT_BINSTRING] = load_short_binstring
640
641 def load_tuple(self):
642 k = self.marker()
643 self.stack[k:] = [tuple(self.stack[k+1:])]
644 dispatch[TUPLE] = load_tuple
645
646 def load_empty_tuple(self):
647 self.stack.append(())
648 dispatch[EMPTY_TUPLE] = load_empty_tuple
649
650 def load_empty_list(self):
651 self.stack.append([])
652 dispatch[EMPTY_LIST] = load_empty_list
653
654 def load_empty_dictionary(self):
655 self.stack.append({})
656 dispatch[EMPTY_DICT] = load_empty_dictionary
657
658 def load_list(self):
659 k = self.marker()
660 self.stack[k:] = [self.stack[k+1:]]
661 dispatch[LIST] = load_list
662
663 def load_dict(self):
664 k = self.marker()
665 d = {}
666 items = self.stack[k+1:]
667 for i in range(0, len(items), 2):
668 key = items[i]
669 value = items[i+1]
670 d[key] = value
671 self.stack[k:] = [d]
672 dispatch[DICT] = load_dict
673
674 def load_inst(self):
675 k = self.marker()
676 args = tuple(self.stack[k+1:])
677 del self.stack[k:]
678 module = self.readline()[:-1]
679 name = self.readline()[:-1]
680 klass = self.find_class(module, name)
Guido van Rossum8be9a111997-04-25 19:52:29 +0000681## if (type(klass) is not ClassType):
682## raise SystemError, "Imported object %s from module %s is " \
683## "not a class" % (name, module)
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000684
685 value = apply(klass, args)
686 self.append(value)
687 dispatch[INST] = load_inst
688
689 def load_obj(self):
690 stack = self.stack
691 k = self.marker()
692 klass = stack[k + 1]
693 del stack[k + 1]
694 args = tuple(stack[k + 1:])
695 del stack[k:]
696 value = apply(klass, args)
697 self.append(value)
698 dispatch[OBJ] = load_obj
699
700 def load_global(self):
701 module = self.readline()[:-1]
702 name = self.readline()[:-1]
703 klass = self.find_class(module, name)
704 self.append(klass)
705 dispatch[GLOBAL] = load_global
706
707 def find_class(self, module, name):
708 env = {}
709
710 try:
711 exec 'from %s import %s' % (module, name) in env
712 except ImportError:
713 raise SystemError, \
714 "Failed to import class %s from module %s" % \
715 (name, module)
716 klass = env[name]
717 return klass
718
719 def load_reduce(self):
720 stack = self.stack
721
722 callable = stack[-2]
723 arg_tup = stack[-1]
724 del stack[-2:]
725
726 if (type(callable) is not ClassType):
727 if (not safe_constructors.has_key(callable)):
Guido van Rossuma48061a1995-01-10 00:31:14 +0000728 try:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000729 safe = callable.__safe_for_unpickling__
730 except AttributeError:
731 safe = None
Guido van Rossuma48061a1995-01-10 00:31:14 +0000732
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000733 if (not safe):
734 raise UnpicklingError, "%s is not safe for " \
735 "unpickling" % callable
Guido van Rossuma48061a1995-01-10 00:31:14 +0000736
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000737 value = apply(callable, arg_tup)
738 self.append(value)
739 dispatch[REDUCE] = load_reduce
Guido van Rossuma48061a1995-01-10 00:31:14 +0000740
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000741 def load_pop(self):
742 del self.stack[-1]
743 dispatch[POP] = load_pop
Guido van Rossum7b5430f1995-03-04 22:25:21 +0000744
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000745 def load_pop_mark(self):
746 k = self.marker()
747 del self.stack[k:]
748 dispatch[POP_MARK] = load_pop_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000749
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000750 def load_dup(self):
751 self.append(stack[-1])
752 dispatch[DUP] = load_dup
Guido van Rossuma48061a1995-01-10 00:31:14 +0000753
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000754 def load_get(self):
755 self.append(self.memo[self.readline()[:-1]])
756 dispatch[GET] = load_get
Guido van Rossum78536471996-04-12 13:36:27 +0000757
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000758 def load_binget(self):
759 i = mloads('i' + self.read(1) + '\000\000\000')
760 self.append(self.memo[`i`])
761 dispatch[BINGET] = load_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000762
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000763 def load_long_binget(self):
764 i = mloads('i' + self.read(4))
765 self.append(self.memo[`i`])
766 dispatch[LONG_BINGET] = load_long_binget
Guido van Rossum78536471996-04-12 13:36:27 +0000767
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000768 def load_put(self):
769 self.memo[self.readline()[:-1]] = self.stack[-1]
770 dispatch[PUT] = load_put
Guido van Rossuma48061a1995-01-10 00:31:14 +0000771
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000772 def load_binput(self):
773 i = mloads('i' + self.read(1) + '\000\000\000')
774 self.memo[`i`] = self.stack[-1]
775 dispatch[BINPUT] = load_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000776
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000777 def load_long_binput(self):
778 i = mloads('i' + self.read(4))
779 self.memo[`i`] = self.stack[-1]
780 dispatch[LONG_BINPUT] = load_long_binput
Guido van Rossuma48061a1995-01-10 00:31:14 +0000781
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000782 def load_append(self):
783 stack = self.stack
784 value = stack[-1]
785 del stack[-1]
786 list = stack[-1]
787 list.append(value)
788 dispatch[APPEND] = load_append
Guido van Rossuma48061a1995-01-10 00:31:14 +0000789
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000790 def load_appends(self):
791 stack = self.stack
792 mark = self.marker()
793 list = stack[mark - 1]
794 for i in range(mark + 1, len(stack)):
795 list.append(stack[i])
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000796
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000797 del stack[mark:]
798 dispatch[APPENDS] = load_appends
799
800 def load_setitem(self):
801 stack = self.stack
802 value = stack[-1]
803 key = stack[-2]
804 del stack[-2:]
805 dict = stack[-1]
806 dict[key] = value
807 dispatch[SETITEM] = load_setitem
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000808
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000809 def load_setitems(self):
810 stack = self.stack
811 mark = self.marker()
812 dict = stack[mark - 1]
813 for i in range(mark + 1, len(stack), 2):
814 dict[stack[i]] = stack[i + 1]
Guido van Rossuma48061a1995-01-10 00:31:14 +0000815
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000816 del stack[mark:]
817 dispatch[SETITEMS] = load_setitems
Guido van Rossuma48061a1995-01-10 00:31:14 +0000818
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000819 def load_build(self):
820 stack = self.stack
821 value = stack[-1]
822 del stack[-1]
823 inst = stack[-1]
824 try:
825 setstate = inst.__setstate__
826 except AttributeError:
827 for key in value.keys():
828 setattr(inst, key, value[key])
829 else:
830 setstate(value)
831 dispatch[BUILD] = load_build
Guido van Rossuma48061a1995-01-10 00:31:14 +0000832
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000833 def load_mark(self):
834 self.append(self.mark)
835 dispatch[MARK] = load_mark
Guido van Rossuma48061a1995-01-10 00:31:14 +0000836
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000837 def load_stop(self):
838 value = self.stack[-1]
839 del self.stack[-1]
840 raise STOP, value
841 dispatch[STOP] = load_stop
Guido van Rossuma48061a1995-01-10 00:31:14 +0000842
843
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000844# Shorthands
845
Guido van Rossumc7c5e691996-07-22 22:26:07 +0000846from StringIO import StringIO
847
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000848def dump(object, file, bin = 0):
849 Pickler(file, bin).dump(object)
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000850
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000851def dumps(object, bin = 0):
852 file = StringIO()
853 Pickler(file, bin).dump(object)
854 return file.getvalue()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000855
856def load(file):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000857 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000858
859def loads(str):
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000860 file = StringIO(str)
861 return Unpickler(file).load()
Guido van Rossum0c891ce1995-03-14 15:09:05 +0000862
863
864# The rest is used for testing only
865
Guido van Rossuma48061a1995-01-10 00:31:14 +0000866class C:
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000867 def __cmp__(self, other):
868 return cmp(self.__dict__, other.__dict__)
Guido van Rossuma48061a1995-01-10 00:31:14 +0000869
870def test():
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000871 fn = 'out'
872 c = C()
873 c.foo = 1
874 c.bar = 2
875 x = [0, 1, 2, 3]
876 y = ('abc', 'abc', c, c)
877 x.append(y)
878 x.append(y)
879 x.append(5)
880 f = open(fn, 'w')
881 F = Pickler(f)
882 F.dump(x)
883 f.close()
884 f = open(fn, 'r')
885 U = Unpickler(f)
886 x2 = U.load()
887 print x
888 print x2
889 print x == x2
890 print map(id, x)
891 print map(id, x2)
892 print F.memo
893 print U.memo
Guido van Rossuma48061a1995-01-10 00:31:14 +0000894
895if __name__ == '__main__':
Guido van Rossumb72cf2d1997-04-09 17:32:51 +0000896 test()