Updated for hfsplus module, new gusi libs.
[python/dscho.git] / Lib / pickle.py
blob8a079255f13052ff975e3d1f7a08278c01c6de2e
1 """Create portable serialized representations of Python objects.
3 See module cPickle for a (much) faster implementation.
4 See module copy_reg for a mechanism for registering custom picklers.
6 Classes:
8 Pickler
9 Unpickler
11 Functions:
13 dump(object, file)
14 dumps(object) -> string
15 load(file) -> object
16 loads(string) -> object
18 Misc variables:
20 __version__
21 format_version
22 compatible_formats
24 """
26 __version__ = "$Revision$" # Code version
28 from types import *
29 from copy_reg import dispatch_table, safe_constructors
30 import marshal
31 import sys
32 import struct
33 import re
35 __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
36 "Unpickler", "dump", "dumps", "load", "loads"]
38 format_version = "1.3" # File format version we write
39 compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read
41 mdumps = marshal.dumps
42 mloads = marshal.loads
44 class PickleError(Exception): pass
45 class PicklingError(PickleError): pass
46 class UnpicklingError(PickleError): pass
48 class _Stop(Exception):
49 def __init__(self, value):
50 self.value = value
52 try:
53 from org.python.core import PyStringMap
54 except ImportError:
55 PyStringMap = None
57 try:
58 UnicodeType
59 except NameError:
60 UnicodeType = None
63 MARK = '('
64 STOP = '.'
65 POP = '0'
66 POP_MARK = '1'
67 DUP = '2'
68 FLOAT = 'F'
69 INT = 'I'
70 BININT = 'J'
71 BININT1 = 'K'
72 LONG = 'L'
73 BININT2 = 'M'
74 NONE = 'N'
75 PERSID = 'P'
76 BINPERSID = 'Q'
77 REDUCE = 'R'
78 STRING = 'S'
79 BINSTRING = 'T'
80 SHORT_BINSTRING = 'U'
81 UNICODE = 'V'
82 BINUNICODE = 'X'
83 APPEND = 'a'
84 BUILD = 'b'
85 GLOBAL = 'c'
86 DICT = 'd'
87 EMPTY_DICT = '}'
88 APPENDS = 'e'
89 GET = 'g'
90 BINGET = 'h'
91 INST = 'i'
92 LONG_BINGET = 'j'
93 LIST = 'l'
94 EMPTY_LIST = ']'
95 OBJ = 'o'
96 PUT = 'p'
97 BINPUT = 'q'
98 LONG_BINPUT = 'r'
99 SETITEM = 's'
100 TUPLE = 't'
101 EMPTY_TUPLE = ')'
102 SETITEMS = 'u'
103 BINFLOAT = 'G'
105 __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
107 class Pickler:
109 def __init__(self, file, bin = 0):
110 self.write = file.write
111 self.memo = {}
112 self.bin = bin
114 def dump(self, object):
115 self.save(object)
116 self.write(STOP)
118 def put(self, i):
119 if self.bin:
120 s = mdumps(i)[1:]
121 if i < 256:
122 return BINPUT + s[0]
124 return LONG_BINPUT + s
126 return PUT + `i` + '\n'
128 def get(self, i):
129 if self.bin:
130 s = mdumps(i)[1:]
132 if i < 256:
133 return BINGET + s[0]
135 return LONG_BINGET + s
137 return GET + `i` + '\n'
139 def save(self, object, pers_save = 0):
140 memo = self.memo
142 if not pers_save:
143 pid = self.persistent_id(object)
144 if pid is not None:
145 self.save_pers(pid)
146 return
148 d = id(object)
150 t = type(object)
152 if (t is TupleType) and (len(object) == 0):
153 if self.bin:
154 self.save_empty_tuple(object)
155 else:
156 self.save_tuple(object)
157 return
159 if memo.has_key(d):
160 self.write(self.get(memo[d][0]))
161 return
163 try:
164 f = self.dispatch[t]
165 except KeyError:
166 pid = self.inst_persistent_id(object)
167 if pid is not None:
168 self.save_pers(pid)
169 return
171 try:
172 reduce = dispatch_table[t]
173 except KeyError:
174 try:
175 reduce = object.__reduce__
176 except AttributeError:
177 raise PicklingError, \
178 "can't pickle %s object: %s" % (`t.__name__`,
179 `object`)
180 else:
181 tup = reduce()
182 else:
183 tup = reduce(object)
185 if type(tup) is StringType:
186 self.save_global(object, tup)
187 return
189 if type(tup) is not TupleType:
190 raise PicklingError, "Value returned by %s must be a " \
191 "tuple" % reduce
193 l = len(tup)
195 if (l != 2) and (l != 3):
196 raise PicklingError, "tuple returned by %s must contain " \
197 "only two or three elements" % reduce
199 callable = tup[0]
200 arg_tup = tup[1]
202 if l > 2:
203 state = tup[2]
204 else:
205 state = None
207 if type(arg_tup) is not TupleType and arg_tup is not None:
208 raise PicklingError, "Second element of tuple returned " \
209 "by %s must be a tuple" % reduce
211 self.save_reduce(callable, arg_tup, state)
212 memo_len = len(memo)
213 self.write(self.put(memo_len))
214 memo[d] = (memo_len, object)
215 return
217 f(self, object)
219 def persistent_id(self, object):
220 return None
222 def inst_persistent_id(self, object):
223 return None
225 def save_pers(self, pid):
226 if not self.bin:
227 self.write(PERSID + str(pid) + '\n')
228 else:
229 self.save(pid, 1)
230 self.write(BINPERSID)
232 def save_reduce(self, callable, arg_tup, state = None):
233 write = self.write
234 save = self.save
236 save(callable)
237 save(arg_tup)
238 write(REDUCE)
240 if state is not None:
241 save(state)
242 write(BUILD)
244 dispatch = {}
246 def save_none(self, object):
247 self.write(NONE)
248 dispatch[NoneType] = save_none
250 def save_int(self, object):
251 if self.bin:
252 # If the int is small enough to fit in a signed 4-byte 2's-comp
253 # format, we can store it more efficiently than the general
254 # case.
255 high_bits = object >> 31 # note that Python shift sign-extends
256 if high_bits == 0 or high_bits == -1:
257 # All high bits are copies of bit 2**31, so the value
258 # fits in a 4-byte signed int.
259 i = mdumps(object)[1:]
260 assert len(i) == 4
261 if i[-2:] == '\000\000': # fits in 2-byte unsigned int
262 if i[-3] == '\000': # fits in 1-byte unsigned int
263 self.write(BININT1 + i[0])
264 else:
265 self.write(BININT2 + i[:2])
266 else:
267 self.write(BININT + i)
268 return
269 # Text pickle, or int too big to fit in signed 4-byte format.
270 self.write(INT + `object` + '\n')
271 dispatch[IntType] = save_int
273 def save_long(self, object):
274 self.write(LONG + `object` + '\n')
275 dispatch[LongType] = save_long
277 def save_float(self, object, pack=struct.pack):
278 if self.bin:
279 self.write(BINFLOAT + pack('>d', object))
280 else:
281 self.write(FLOAT + `object` + '\n')
282 dispatch[FloatType] = save_float
284 def save_string(self, object):
285 d = id(object)
286 memo = self.memo
288 if self.bin:
289 l = len(object)
290 s = mdumps(l)[1:]
291 if l < 256:
292 self.write(SHORT_BINSTRING + s[0] + object)
293 else:
294 self.write(BINSTRING + s + object)
295 else:
296 self.write(STRING + `object` + '\n')
298 memo_len = len(memo)
299 self.write(self.put(memo_len))
300 memo[d] = (memo_len, object)
301 dispatch[StringType] = save_string
303 def save_unicode(self, object):
304 d = id(object)
305 memo = self.memo
307 if self.bin:
308 encoding = object.encode('utf-8')
309 l = len(encoding)
310 s = mdumps(l)[1:]
311 self.write(BINUNICODE + s + encoding)
312 else:
313 object = object.replace("\\", "\\u005c")
314 object = object.replace("\n", "\\u000a")
315 self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
317 memo_len = len(memo)
318 self.write(self.put(memo_len))
319 memo[d] = (memo_len, object)
320 dispatch[UnicodeType] = save_unicode
322 if StringType == UnicodeType:
323 # This is true for Jython
324 def save_string(self, object):
325 d = id(object)
326 memo = self.memo
327 unicode = object.isunicode()
329 if self.bin:
330 if unicode:
331 object = object.encode("utf-8")
332 l = len(object)
333 s = mdumps(l)[1:]
334 if l < 256 and not unicode:
335 self.write(SHORT_BINSTRING + s[0] + object)
336 else:
337 if unicode:
338 self.write(BINUNICODE + s + object)
339 else:
340 self.write(BINSTRING + s + object)
341 else:
342 if unicode:
343 object = object.replace("\\", "\\u005c")
344 object = object.replace("\n", "\\u000a")
345 object = object.encode('raw-unicode-escape')
346 self.write(UNICODE + object + '\n')
347 else:
348 self.write(STRING + `object` + '\n')
350 memo_len = len(memo)
351 self.write(self.put(memo_len))
352 memo[d] = (memo_len, object)
353 dispatch[StringType] = save_string
355 def save_tuple(self, object):
357 write = self.write
358 save = self.save
359 memo = self.memo
361 d = id(object)
363 write(MARK)
365 for element in object:
366 save(element)
368 if len(object) and memo.has_key(d):
369 if self.bin:
370 write(POP_MARK + self.get(memo[d][0]))
371 return
373 write(POP * (len(object) + 1) + self.get(memo[d][0]))
374 return
376 memo_len = len(memo)
377 self.write(TUPLE + self.put(memo_len))
378 memo[d] = (memo_len, object)
379 dispatch[TupleType] = save_tuple
381 def save_empty_tuple(self, object):
382 self.write(EMPTY_TUPLE)
384 def save_list(self, object):
385 d = id(object)
387 write = self.write
388 save = self.save
389 memo = self.memo
391 if self.bin:
392 write(EMPTY_LIST)
393 else:
394 write(MARK + LIST)
396 memo_len = len(memo)
397 write(self.put(memo_len))
398 memo[d] = (memo_len, object)
400 using_appends = (self.bin and (len(object) > 1))
402 if using_appends:
403 write(MARK)
405 for element in object:
406 save(element)
408 if not using_appends:
409 write(APPEND)
411 if using_appends:
412 write(APPENDS)
413 dispatch[ListType] = save_list
415 def save_dict(self, object):
416 d = id(object)
418 write = self.write
419 save = self.save
420 memo = self.memo
422 if self.bin:
423 write(EMPTY_DICT)
424 else:
425 write(MARK + DICT)
427 memo_len = len(memo)
428 self.write(self.put(memo_len))
429 memo[d] = (memo_len, object)
431 using_setitems = (self.bin and (len(object) > 1))
433 if using_setitems:
434 write(MARK)
436 items = object.items()
437 for key, value in items:
438 save(key)
439 save(value)
441 if not using_setitems:
442 write(SETITEM)
444 if using_setitems:
445 write(SETITEMS)
447 dispatch[DictionaryType] = save_dict
448 if not PyStringMap is None:
449 dispatch[PyStringMap] = save_dict
451 def save_inst(self, object):
452 d = id(object)
453 cls = object.__class__
455 memo = self.memo
456 write = self.write
457 save = self.save
459 if hasattr(object, '__getinitargs__'):
460 args = object.__getinitargs__()
461 len(args) # XXX Assert it's a sequence
462 _keep_alive(args, memo)
463 else:
464 args = ()
466 write(MARK)
468 if self.bin:
469 save(cls)
471 for arg in args:
472 save(arg)
474 memo_len = len(memo)
475 if self.bin:
476 write(OBJ + self.put(memo_len))
477 else:
478 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' +
479 self.put(memo_len))
481 memo[d] = (memo_len, object)
483 try:
484 getstate = object.__getstate__
485 except AttributeError:
486 stuff = object.__dict__
487 else:
488 stuff = getstate()
489 _keep_alive(stuff, memo)
490 save(stuff)
491 write(BUILD)
492 dispatch[InstanceType] = save_inst
494 def save_global(self, object, name = None):
495 write = self.write
496 memo = self.memo
498 if name is None:
499 name = object.__name__
501 try:
502 module = object.__module__
503 except AttributeError:
504 module = whichmodule(object, name)
506 try:
507 __import__(module)
508 mod = sys.modules[module]
509 klass = getattr(mod, name)
510 except (ImportError, KeyError, AttributeError):
511 raise PicklingError(
512 "Can't pickle %r: it's not found as %s.%s" %
513 (object, module, name))
514 else:
515 if klass is not object:
516 raise PicklingError(
517 "Can't pickle %r: it's not the same object as %s.%s" %
518 (object, module, name))
520 memo_len = len(memo)
521 write(GLOBAL + module + '\n' + name + '\n' +
522 self.put(memo_len))
523 memo[id(object)] = (memo_len, object)
524 dispatch[ClassType] = save_global
525 dispatch[FunctionType] = save_global
526 dispatch[BuiltinFunctionType] = save_global
527 dispatch[TypeType] = save_global
530 def _keep_alive(x, memo):
531 """Keeps a reference to the object x in the memo.
533 Because we remember objects by their id, we have
534 to assure that possibly temporary objects are kept
535 alive by referencing them.
536 We store a reference at the id of the memo, which should
537 normally not be used unless someone tries to deepcopy
538 the memo itself...
540 try:
541 memo[id(memo)].append(x)
542 except KeyError:
543 # aha, this is the first one :-)
544 memo[id(memo)]=[x]
547 classmap = {}
549 # This is no longer used to find classes, but still for functions
550 def whichmodule(cls, clsname):
551 """Figure out the module in which a class occurs.
553 Search sys.modules for the module.
554 Cache in classmap.
555 Return a module name.
556 If the class cannot be found, return __main__.
558 if classmap.has_key(cls):
559 return classmap[cls]
561 for name, module in sys.modules.items():
562 if name != '__main__' and \
563 hasattr(module, clsname) and \
564 getattr(module, clsname) is cls:
565 break
566 else:
567 name = '__main__'
568 classmap[cls] = name
569 return name
572 class Unpickler:
574 def __init__(self, file):
575 self.readline = file.readline
576 self.read = file.read
577 self.memo = {}
579 def load(self):
580 self.mark = object() # any new unique object
581 self.stack = []
582 self.append = self.stack.append
583 read = self.read
584 dispatch = self.dispatch
585 try:
586 while 1:
587 key = read(1)
588 dispatch[key](self)
589 except _Stop, stopinst:
590 return stopinst.value
592 def marker(self):
593 stack = self.stack
594 mark = self.mark
595 k = len(stack)-1
596 while stack[k] is not mark: k = k-1
597 return k
599 dispatch = {}
601 def load_eof(self):
602 raise EOFError
603 dispatch[''] = load_eof
605 def load_persid(self):
606 pid = self.readline()[:-1]
607 self.append(self.persistent_load(pid))
608 dispatch[PERSID] = load_persid
610 def load_binpersid(self):
611 stack = self.stack
613 pid = stack[-1]
614 del stack[-1]
616 self.append(self.persistent_load(pid))
617 dispatch[BINPERSID] = load_binpersid
619 def load_none(self):
620 self.append(None)
621 dispatch[NONE] = load_none
623 def load_int(self):
624 data = self.readline()
625 try:
626 self.append(int(data))
627 except ValueError:
628 self.append(long(data))
629 dispatch[INT] = load_int
631 def load_binint(self):
632 self.append(mloads('i' + self.read(4)))
633 dispatch[BININT] = load_binint
635 def load_binint1(self):
636 self.append(mloads('i' + self.read(1) + '\000\000\000'))
637 dispatch[BININT1] = load_binint1
639 def load_binint2(self):
640 self.append(mloads('i' + self.read(2) + '\000\000'))
641 dispatch[BININT2] = load_binint2
643 def load_long(self):
644 self.append(long(self.readline()[:-1], 0))
645 dispatch[LONG] = load_long
647 def load_float(self):
648 self.append(float(self.readline()[:-1]))
649 dispatch[FLOAT] = load_float
651 def load_binfloat(self, unpack=struct.unpack):
652 self.append(unpack('>d', self.read(8))[0])
653 dispatch[BINFLOAT] = load_binfloat
655 def load_string(self):
656 rep = self.readline()[:-1]
657 if not self._is_string_secure(rep):
658 raise ValueError, "insecure string pickle"
659 self.append(eval(rep,
660 {'__builtins__': {}})) # Let's be careful
661 dispatch[STRING] = load_string
663 def _is_string_secure(self, s):
664 """Return true if s contains a string that is safe to eval
666 The definition of secure string is based on the implementation
667 in cPickle. s is secure as long as it only contains a quoted
668 string and optional trailing whitespace.
670 q = s[0]
671 if q not in ("'", '"'):
672 return 0
673 # find the closing quote
674 offset = 1
675 i = None
676 while 1:
677 try:
678 i = s.index(q, offset)
679 except ValueError:
680 # if there is an error the first time, there is no
681 # close quote
682 if offset == 1:
683 return 0
684 if s[i-1] != '\\':
685 break
686 # check to see if this one is escaped
687 nslash = 0
688 j = i - 1
689 while j >= offset and s[j] == '\\':
690 j = j - 1
691 nslash = nslash + 1
692 if nslash % 2 == 0:
693 break
694 offset = i + 1
695 for c in s[i+1:]:
696 if ord(c) > 32:
697 return 0
698 return 1
700 def load_binstring(self):
701 len = mloads('i' + self.read(4))
702 self.append(self.read(len))
703 dispatch[BINSTRING] = load_binstring
705 def load_unicode(self):
706 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
707 dispatch[UNICODE] = load_unicode
709 def load_binunicode(self):
710 len = mloads('i' + self.read(4))
711 self.append(unicode(self.read(len),'utf-8'))
712 dispatch[BINUNICODE] = load_binunicode
714 def load_short_binstring(self):
715 len = mloads('i' + self.read(1) + '\000\000\000')
716 self.append(self.read(len))
717 dispatch[SHORT_BINSTRING] = load_short_binstring
719 def load_tuple(self):
720 k = self.marker()
721 self.stack[k:] = [tuple(self.stack[k+1:])]
722 dispatch[TUPLE] = load_tuple
724 def load_empty_tuple(self):
725 self.stack.append(())
726 dispatch[EMPTY_TUPLE] = load_empty_tuple
728 def load_empty_list(self):
729 self.stack.append([])
730 dispatch[EMPTY_LIST] = load_empty_list
732 def load_empty_dictionary(self):
733 self.stack.append({})
734 dispatch[EMPTY_DICT] = load_empty_dictionary
736 def load_list(self):
737 k = self.marker()
738 self.stack[k:] = [self.stack[k+1:]]
739 dispatch[LIST] = load_list
741 def load_dict(self):
742 k = self.marker()
743 d = {}
744 items = self.stack[k+1:]
745 for i in range(0, len(items), 2):
746 key = items[i]
747 value = items[i+1]
748 d[key] = value
749 self.stack[k:] = [d]
750 dispatch[DICT] = load_dict
752 def load_inst(self):
753 k = self.marker()
754 args = tuple(self.stack[k+1:])
755 del self.stack[k:]
756 module = self.readline()[:-1]
757 name = self.readline()[:-1]
758 klass = self.find_class(module, name)
759 instantiated = 0
760 if (not args and type(klass) is ClassType and
761 not hasattr(klass, "__getinitargs__")):
762 try:
763 value = _EmptyClass()
764 value.__class__ = klass
765 instantiated = 1
766 except RuntimeError:
767 # In restricted execution, assignment to inst.__class__ is
768 # prohibited
769 pass
770 if not instantiated:
771 try:
772 if not hasattr(klass, '__safe_for_unpickling__'):
773 raise UnpicklingError('%s is not safe for unpickling' %
774 klass)
775 value = apply(klass, args)
776 except TypeError, err:
777 raise TypeError, "in constructor for %s: %s" % (
778 klass.__name__, str(err)), sys.exc_info()[2]
779 self.append(value)
780 dispatch[INST] = load_inst
782 def load_obj(self):
783 stack = self.stack
784 k = self.marker()
785 klass = stack[k + 1]
786 del stack[k + 1]
787 args = tuple(stack[k + 1:])
788 del stack[k:]
789 instantiated = 0
790 if (not args and type(klass) is ClassType and
791 not hasattr(klass, "__getinitargs__")):
792 try:
793 value = _EmptyClass()
794 value.__class__ = klass
795 instantiated = 1
796 except RuntimeError:
797 # In restricted execution, assignment to inst.__class__ is
798 # prohibited
799 pass
800 if not instantiated:
801 value = apply(klass, args)
802 self.append(value)
803 dispatch[OBJ] = load_obj
805 def load_global(self):
806 module = self.readline()[:-1]
807 name = self.readline()[:-1]
808 klass = self.find_class(module, name)
809 self.append(klass)
810 dispatch[GLOBAL] = load_global
812 def find_class(self, module, name):
813 __import__(module)
814 mod = sys.modules[module]
815 klass = getattr(mod, name)
816 return klass
818 def load_reduce(self):
819 stack = self.stack
821 callable = stack[-2]
822 arg_tup = stack[-1]
823 del stack[-2:]
825 if type(callable) is not ClassType:
826 if not safe_constructors.has_key(callable):
827 try:
828 safe = callable.__safe_for_unpickling__
829 except AttributeError:
830 safe = None
832 if not safe:
833 raise UnpicklingError, "%s is not safe for " \
834 "unpickling" % callable
836 if arg_tup is None:
837 value = callable.__basicnew__()
838 else:
839 value = apply(callable, arg_tup)
840 self.append(value)
841 dispatch[REDUCE] = load_reduce
843 def load_pop(self):
844 del self.stack[-1]
845 dispatch[POP] = load_pop
847 def load_pop_mark(self):
848 k = self.marker()
849 del self.stack[k:]
850 dispatch[POP_MARK] = load_pop_mark
852 def load_dup(self):
853 self.append(self.stack[-1])
854 dispatch[DUP] = load_dup
856 def load_get(self):
857 self.append(self.memo[self.readline()[:-1]])
858 dispatch[GET] = load_get
860 def load_binget(self):
861 i = mloads('i' + self.read(1) + '\000\000\000')
862 self.append(self.memo[`i`])
863 dispatch[BINGET] = load_binget
865 def load_long_binget(self):
866 i = mloads('i' + self.read(4))
867 self.append(self.memo[`i`])
868 dispatch[LONG_BINGET] = load_long_binget
870 def load_put(self):
871 self.memo[self.readline()[:-1]] = self.stack[-1]
872 dispatch[PUT] = load_put
874 def load_binput(self):
875 i = mloads('i' + self.read(1) + '\000\000\000')
876 self.memo[`i`] = self.stack[-1]
877 dispatch[BINPUT] = load_binput
879 def load_long_binput(self):
880 i = mloads('i' + self.read(4))
881 self.memo[`i`] = self.stack[-1]
882 dispatch[LONG_BINPUT] = load_long_binput
884 def load_append(self):
885 stack = self.stack
886 value = stack[-1]
887 del stack[-1]
888 list = stack[-1]
889 list.append(value)
890 dispatch[APPEND] = load_append
892 def load_appends(self):
893 stack = self.stack
894 mark = self.marker()
895 list = stack[mark - 1]
896 for i in range(mark + 1, len(stack)):
897 list.append(stack[i])
899 del stack[mark:]
900 dispatch[APPENDS] = load_appends
902 def load_setitem(self):
903 stack = self.stack
904 value = stack[-1]
905 key = stack[-2]
906 del stack[-2:]
907 dict = stack[-1]
908 dict[key] = value
909 dispatch[SETITEM] = load_setitem
911 def load_setitems(self):
912 stack = self.stack
913 mark = self.marker()
914 dict = stack[mark - 1]
915 for i in range(mark + 1, len(stack), 2):
916 dict[stack[i]] = stack[i + 1]
918 del stack[mark:]
919 dispatch[SETITEMS] = load_setitems
921 def load_build(self):
922 stack = self.stack
923 value = stack[-1]
924 del stack[-1]
925 inst = stack[-1]
926 try:
927 setstate = inst.__setstate__
928 except AttributeError:
929 try:
930 inst.__dict__.update(value)
931 except RuntimeError:
932 # XXX In restricted execution, the instance's __dict__ is not
933 # accessible. Use the old way of unpickling the instance
934 # variables. This is a semantic different when unpickling in
935 # restricted vs. unrestricted modes.
936 for k, v in value.items():
937 setattr(inst, k, v)
938 else:
939 setstate(value)
940 dispatch[BUILD] = load_build
942 def load_mark(self):
943 self.append(self.mark)
944 dispatch[MARK] = load_mark
946 def load_stop(self):
947 value = self.stack[-1]
948 del self.stack[-1]
949 raise _Stop(value)
950 dispatch[STOP] = load_stop
952 # Helper class for load_inst/load_obj
954 class _EmptyClass:
955 pass
957 # Shorthands
959 try:
960 from cStringIO import StringIO
961 except ImportError:
962 from StringIO import StringIO
964 def dump(object, file, bin = 0):
965 Pickler(file, bin).dump(object)
967 def dumps(object, bin = 0):
968 file = StringIO()
969 Pickler(file, bin).dump(object)
970 return file.getvalue()
972 def load(file):
973 return Unpickler(file).load()
975 def loads(str):
976 file = StringIO(str)
977 return Unpickler(file).load()