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.
14 dumps(object) -> string
16 loads(string) -> object
26 __version__
= "$Revision$" # Code version
29 from copy_reg
import dispatch_table
, safe_constructors
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
):
53 from org
.python
.core
import PyStringMap
105 __all__
.extend([x
for x
in dir() if re
.match("[A-Z][A-Z0-9_]+$",x
)])
109 def __init__(self
, file, bin
= 0):
110 self
.write
= file.write
114 def dump(self
, object):
124 return LONG_BINPUT
+ s
126 return PUT
+ `i`
+ '\n'
135 return LONG_BINGET
+ s
137 return GET
+ `i`
+ '\n'
139 def save(self
, object, pers_save
= 0):
143 pid
= self
.persistent_id(object)
152 if (t
is TupleType
) and (len(object) == 0):
154 self
.save_empty_tuple(object)
156 self
.save_tuple(object)
160 self
.write(self
.get(memo
[d
][0]))
166 if issubclass(t
, TypeType
):
167 self
.save_global(object)
170 pid
= self
.inst_persistent_id(object)
176 reduce = dispatch_table
[t
]
179 reduce = object.__reduce
__
180 except AttributeError:
181 raise PicklingError
, \
182 "can't pickle %s object: %s" % (`t
.__name
__`
,
189 if type(tup
) is StringType
:
190 self
.save_global(object, tup
)
193 if type(tup
) is not TupleType
:
194 raise PicklingError
, "Value returned by %s must be a " \
199 if (l
!= 2) and (l
!= 3):
200 raise PicklingError
, "tuple returned by %s must contain " \
201 "only two or three elements" % reduce
211 if type(arg_tup
) is not TupleType
and arg_tup
is not None:
212 raise PicklingError
, "Second element of tuple returned " \
213 "by %s must be a tuple" % reduce
215 self
.save_reduce(callable, arg_tup
, state
)
217 self
.write(self
.put(memo_len
))
218 memo
[d
] = (memo_len
, object)
223 def persistent_id(self
, object):
226 def inst_persistent_id(self
, object):
229 def save_pers(self
, pid
):
231 self
.write(PERSID
+ str(pid
) + '\n')
234 self
.write(BINPERSID
)
236 def save_reduce(self
, callable, arg_tup
, state
= None):
244 if state
is not None:
250 def save_none(self
, object):
252 dispatch
[NoneType
] = save_none
254 def save_int(self
, object):
256 # If the int is small enough to fit in a signed 4-byte 2's-comp
257 # format, we can store it more efficiently than the general
259 high_bits
= object >> 31 # note that Python shift sign-extends
260 if high_bits
== 0 or high_bits
== -1:
261 # All high bits are copies of bit 2**31, so the value
262 # fits in a 4-byte signed int.
263 i
= mdumps(object)[1:]
265 if i
[-2:] == '\000\000': # fits in 2-byte unsigned int
266 if i
[-3] == '\000': # fits in 1-byte unsigned int
267 self
.write(BININT1
+ i
[0])
269 self
.write(BININT2
+ i
[:2])
271 self
.write(BININT
+ i
)
273 # Text pickle, or int too big to fit in signed 4-byte format.
274 self
.write(INT
+ `
object`
+ '\n')
275 dispatch
[IntType
] = save_int
277 def save_long(self
, object):
278 self
.write(LONG
+ `
object`
+ '\n')
279 dispatch
[LongType
] = save_long
281 def save_float(self
, object, pack
=struct
.pack
):
283 self
.write(BINFLOAT
+ pack('>d', object))
285 self
.write(FLOAT
+ `
object`
+ '\n')
286 dispatch
[FloatType
] = save_float
288 def save_string(self
, object):
296 self
.write(SHORT_BINSTRING
+ s
[0] + object)
298 self
.write(BINSTRING
+ s
+ object)
300 self
.write(STRING
+ `
object`
+ '\n')
303 self
.write(self
.put(memo_len
))
304 memo
[d
] = (memo_len
, object)
305 dispatch
[StringType
] = save_string
307 def save_unicode(self
, object):
312 encoding
= object.encode('utf-8')
315 self
.write(BINUNICODE
+ s
+ encoding
)
317 object = object.replace("\\", "\\u005c")
318 object = object.replace("\n", "\\u000a")
319 self
.write(UNICODE
+ object.encode('raw-unicode-escape') + '\n')
322 self
.write(self
.put(memo_len
))
323 memo
[d
] = (memo_len
, object)
324 dispatch
[UnicodeType
] = save_unicode
326 if StringType
== UnicodeType
:
327 # This is true for Jython
328 def save_string(self
, object):
331 unicode = object.isunicode()
335 object = object.encode("utf-8")
338 if l
< 256 and not unicode:
339 self
.write(SHORT_BINSTRING
+ s
[0] + object)
342 self
.write(BINUNICODE
+ s
+ object)
344 self
.write(BINSTRING
+ s
+ object)
347 object = object.replace("\\", "\\u005c")
348 object = object.replace("\n", "\\u000a")
349 object = object.encode('raw-unicode-escape')
350 self
.write(UNICODE
+ object + '\n')
352 self
.write(STRING
+ `
object`
+ '\n')
355 self
.write(self
.put(memo_len
))
356 memo
[d
] = (memo_len
, object)
357 dispatch
[StringType
] = save_string
359 def save_tuple(self
, object):
369 for element
in object:
372 if len(object) and memo
.has_key(d
):
374 write(POP_MARK
+ self
.get(memo
[d
][0]))
377 write(POP
* (len(object) + 1) + self
.get(memo
[d
][0]))
381 self
.write(TUPLE
+ self
.put(memo_len
))
382 memo
[d
] = (memo_len
, object)
383 dispatch
[TupleType
] = save_tuple
385 def save_empty_tuple(self
, object):
386 self
.write(EMPTY_TUPLE
)
388 def save_list(self
, object):
401 write(self
.put(memo_len
))
402 memo
[d
] = (memo_len
, object)
404 using_appends
= (self
.bin
and (len(object) > 1))
409 for element
in object:
412 if not using_appends
:
417 dispatch
[ListType
] = save_list
419 def save_dict(self
, object):
432 self
.write(self
.put(memo_len
))
433 memo
[d
] = (memo_len
, object)
435 using_setitems
= (self
.bin
and (len(object) > 1))
440 items
= object.items()
441 for key
, value
in items
:
445 if not using_setitems
:
451 dispatch
[DictionaryType
] = save_dict
452 if not PyStringMap
is None:
453 dispatch
[PyStringMap
] = save_dict
455 def save_inst(self
, object):
457 cls
= object.__class
__
463 if hasattr(object, '__getinitargs__'):
464 args
= object.__getinitargs
__()
465 len(args
) # XXX Assert it's a sequence
466 _keep_alive(args
, memo
)
480 write(OBJ
+ self
.put(memo_len
))
482 write(INST
+ cls
.__module
__ + '\n' + cls
.__name
__ + '\n' +
485 memo
[d
] = (memo_len
, object)
488 getstate
= object.__getstate
__
489 except AttributeError:
490 stuff
= object.__dict
__
493 _keep_alive(stuff
, memo
)
496 dispatch
[InstanceType
] = save_inst
498 def save_global(self
, object, name
= None):
503 name
= object.__name
__
506 module
= object.__module
__
507 except AttributeError:
508 module
= whichmodule(object, name
)
512 mod
= sys
.modules
[module
]
513 klass
= getattr(mod
, name
)
514 except (ImportError, KeyError, AttributeError):
516 "Can't pickle %r: it's not found as %s.%s" %
517 (object, module
, name
))
519 if klass
is not object:
521 "Can't pickle %r: it's not the same object as %s.%s" %
522 (object, module
, name
))
525 write(GLOBAL
+ module
+ '\n' + name
+ '\n' +
527 memo
[id(object)] = (memo_len
, object)
528 dispatch
[ClassType
] = save_global
529 dispatch
[FunctionType
] = save_global
530 dispatch
[BuiltinFunctionType
] = save_global
531 dispatch
[TypeType
] = save_global
534 def _keep_alive(x
, memo
):
535 """Keeps a reference to the object x in the memo.
537 Because we remember objects by their id, we have
538 to assure that possibly temporary objects are kept
539 alive by referencing them.
540 We store a reference at the id of the memo, which should
541 normally not be used unless someone tries to deepcopy
545 memo
[id(memo
)].append(x
)
547 # aha, this is the first one :-)
553 # This is no longer used to find classes, but still for functions
554 def whichmodule(cls
, clsname
):
555 """Figure out the module in which a class occurs.
557 Search sys.modules for the module.
559 Return a module name.
560 If the class cannot be found, return __main__.
562 if classmap
.has_key(cls
):
565 for name
, module
in sys
.modules
.items():
566 if name
!= '__main__' and \
567 hasattr(module
, clsname
) and \
568 getattr(module
, clsname
) is cls
:
578 def __init__(self
, file):
579 self
.readline
= file.readline
580 self
.read
= file.read
584 self
.mark
= object() # any new unique object
586 self
.append
= self
.stack
.append
588 dispatch
= self
.dispatch
593 except _Stop
, stopinst
:
594 return stopinst
.value
600 while stack
[k
] is not mark
: k
= k
-1
607 dispatch
[''] = load_eof
609 def load_persid(self
):
610 pid
= self
.readline()[:-1]
611 self
.append(self
.persistent_load(pid
))
612 dispatch
[PERSID
] = load_persid
614 def load_binpersid(self
):
620 self
.append(self
.persistent_load(pid
))
621 dispatch
[BINPERSID
] = load_binpersid
625 dispatch
[NONE
] = load_none
628 data
= self
.readline()
630 self
.append(int(data
))
632 self
.append(long(data
))
633 dispatch
[INT
] = load_int
635 def load_binint(self
):
636 self
.append(mloads('i' + self
.read(4)))
637 dispatch
[BININT
] = load_binint
639 def load_binint1(self
):
640 self
.append(mloads('i' + self
.read(1) + '\000\000\000'))
641 dispatch
[BININT1
] = load_binint1
643 def load_binint2(self
):
644 self
.append(mloads('i' + self
.read(2) + '\000\000'))
645 dispatch
[BININT2
] = load_binint2
648 self
.append(long(self
.readline()[:-1], 0))
649 dispatch
[LONG
] = load_long
651 def load_float(self
):
652 self
.append(float(self
.readline()[:-1]))
653 dispatch
[FLOAT
] = load_float
655 def load_binfloat(self
, unpack
=struct
.unpack
):
656 self
.append(unpack('>d', self
.read(8))[0])
657 dispatch
[BINFLOAT
] = load_binfloat
659 def load_string(self
):
660 rep
= self
.readline()[:-1]
661 if not self
._is
_string
_secure
(rep
):
662 raise ValueError, "insecure string pickle"
663 self
.append(eval(rep
,
664 {'__builtins__': {}})) # Let's be careful
665 dispatch
[STRING
] = load_string
667 def _is_string_secure(self
, s
):
668 """Return true if s contains a string that is safe to eval
670 The definition of secure string is based on the implementation
671 in cPickle. s is secure as long as it only contains a quoted
672 string and optional trailing whitespace.
675 if q
not in ("'", '"'):
677 # find the closing quote
682 i
= s
.index(q
, offset
)
684 # if there is an error the first time, there is no
690 # check to see if this one is escaped
693 while j
>= offset
and s
[j
] == '\\':
704 def load_binstring(self
):
705 len = mloads('i' + self
.read(4))
706 self
.append(self
.read(len))
707 dispatch
[BINSTRING
] = load_binstring
709 def load_unicode(self
):
710 self
.append(unicode(self
.readline()[:-1],'raw-unicode-escape'))
711 dispatch
[UNICODE
] = load_unicode
713 def load_binunicode(self
):
714 len = mloads('i' + self
.read(4))
715 self
.append(unicode(self
.read(len),'utf-8'))
716 dispatch
[BINUNICODE
] = load_binunicode
718 def load_short_binstring(self
):
719 len = mloads('i' + self
.read(1) + '\000\000\000')
720 self
.append(self
.read(len))
721 dispatch
[SHORT_BINSTRING
] = load_short_binstring
723 def load_tuple(self
):
725 self
.stack
[k
:] = [tuple(self
.stack
[k
+1:])]
726 dispatch
[TUPLE
] = load_tuple
728 def load_empty_tuple(self
):
729 self
.stack
.append(())
730 dispatch
[EMPTY_TUPLE
] = load_empty_tuple
732 def load_empty_list(self
):
733 self
.stack
.append([])
734 dispatch
[EMPTY_LIST
] = load_empty_list
736 def load_empty_dictionary(self
):
737 self
.stack
.append({})
738 dispatch
[EMPTY_DICT
] = load_empty_dictionary
742 self
.stack
[k
:] = [self
.stack
[k
+1:]]
743 dispatch
[LIST
] = load_list
748 items
= self
.stack
[k
+1:]
749 for i
in range(0, len(items
), 2):
754 dispatch
[DICT
] = load_dict
758 args
= tuple(self
.stack
[k
+1:])
760 module
= self
.readline()[:-1]
761 name
= self
.readline()[:-1]
762 klass
= self
.find_class(module
, name
)
764 if (not args
and type(klass
) is ClassType
and
765 not hasattr(klass
, "__getinitargs__")):
767 value
= _EmptyClass()
768 value
.__class
__ = klass
771 # In restricted execution, assignment to inst.__class__ is
776 if not hasattr(klass
, '__safe_for_unpickling__'):
777 raise UnpicklingError('%s is not safe for unpickling' %
779 value
= apply(klass
, args
)
780 except TypeError, err
:
781 raise TypeError, "in constructor for %s: %s" % (
782 klass
.__name
__, str(err
)), sys
.exc_info()[2]
784 dispatch
[INST
] = load_inst
791 args
= tuple(stack
[k
+ 1:])
794 if (not args
and type(klass
) is ClassType
and
795 not hasattr(klass
, "__getinitargs__")):
797 value
= _EmptyClass()
798 value
.__class
__ = klass
801 # In restricted execution, assignment to inst.__class__ is
805 value
= apply(klass
, args
)
807 dispatch
[OBJ
] = load_obj
809 def load_global(self
):
810 module
= self
.readline()[:-1]
811 name
= self
.readline()[:-1]
812 klass
= self
.find_class(module
, name
)
814 dispatch
[GLOBAL
] = load_global
816 def find_class(self
, module
, name
):
818 mod
= sys
.modules
[module
]
819 klass
= getattr(mod
, name
)
822 def load_reduce(self
):
829 if type(callable) is not ClassType
:
830 if not safe_constructors
.has_key(callable):
832 safe
= callable.__safe
_for
_unpickling
__
833 except AttributeError:
837 raise UnpicklingError
, "%s is not safe for " \
838 "unpickling" % callable
841 value
= callable.__basicnew
__()
843 value
= apply(callable, arg_tup
)
845 dispatch
[REDUCE
] = load_reduce
849 dispatch
[POP
] = load_pop
851 def load_pop_mark(self
):
854 dispatch
[POP_MARK
] = load_pop_mark
857 self
.append(self
.stack
[-1])
858 dispatch
[DUP
] = load_dup
861 self
.append(self
.memo
[self
.readline()[:-1]])
862 dispatch
[GET
] = load_get
864 def load_binget(self
):
865 i
= mloads('i' + self
.read(1) + '\000\000\000')
866 self
.append(self
.memo
[`i`
])
867 dispatch
[BINGET
] = load_binget
869 def load_long_binget(self
):
870 i
= mloads('i' + self
.read(4))
871 self
.append(self
.memo
[`i`
])
872 dispatch
[LONG_BINGET
] = load_long_binget
875 self
.memo
[self
.readline()[:-1]] = self
.stack
[-1]
876 dispatch
[PUT
] = load_put
878 def load_binput(self
):
879 i
= mloads('i' + self
.read(1) + '\000\000\000')
880 self
.memo
[`i`
] = self
.stack
[-1]
881 dispatch
[BINPUT
] = load_binput
883 def load_long_binput(self
):
884 i
= mloads('i' + self
.read(4))
885 self
.memo
[`i`
] = self
.stack
[-1]
886 dispatch
[LONG_BINPUT
] = load_long_binput
888 def load_append(self
):
894 dispatch
[APPEND
] = load_append
896 def load_appends(self
):
899 list = stack
[mark
- 1]
900 for i
in range(mark
+ 1, len(stack
)):
901 list.append(stack
[i
])
904 dispatch
[APPENDS
] = load_appends
906 def load_setitem(self
):
913 dispatch
[SETITEM
] = load_setitem
915 def load_setitems(self
):
918 dict = stack
[mark
- 1]
919 for i
in range(mark
+ 1, len(stack
), 2):
920 dict[stack
[i
]] = stack
[i
+ 1]
923 dispatch
[SETITEMS
] = load_setitems
925 def load_build(self
):
931 setstate
= inst
.__setstate
__
932 except AttributeError:
934 inst
.__dict
__.update(value
)
936 # XXX In restricted execution, the instance's __dict__ is not
937 # accessible. Use the old way of unpickling the instance
938 # variables. This is a semantic different when unpickling in
939 # restricted vs. unrestricted modes.
940 for k
, v
in value
.items():
944 dispatch
[BUILD
] = load_build
947 self
.append(self
.mark
)
948 dispatch
[MARK
] = load_mark
951 value
= self
.stack
[-1]
954 dispatch
[STOP
] = load_stop
956 # Helper class for load_inst/load_obj
964 from cStringIO
import StringIO
966 from StringIO
import StringIO
968 def dump(object, file, bin
= 0):
969 Pickler(file, bin
).dump(object)
971 def dumps(object, bin
= 0):
973 Pickler(file, bin
).dump(object)
974 return file.getvalue()
977 return Unpickler(file).load()
981 return Unpickler(file).load()