1 """Create portable serialized representations of Python objects.
2
3 See module cPickle for a (much) faster implementation.
4 See module copy_reg for a mechanism for registering custom picklers.
5 See module pickletools source for extensive comments.
6
7 Classes:
8
9 Pickler
10 Unpickler
11
12 Functions:
13
14 dump(object, file)
15 dumps(object) -> string
16 load(file) -> object
17 loads(string) -> object
18
19 Misc variables:
20
21 __version__
22 format_version
23 compatible_formats
24
25 """
26
27
28
29
30 __version__ = "$Revision: 1.156 $"
31
32 from types import *
33 from copy_reg import dispatch_table
34 from copy_reg import _extension_registry, _inverted_registry, _extension_cache
35 import marshal
36 import sys
37 import struct
38 import re
39 import warnings
40
41 __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
42 "Unpickler", "dump", "dumps", "load", "loads"]
43
44
45 format_version = "2.0"
46 compatible_formats = ["1.0",
47 "1.1",
48 "1.2",
49 "1.3",
50 "2.0",
51 ]
52
53
54
55 HIGHEST_PROTOCOL = 2
56
57
58
59
60 mloads = marshal.loads
61
63 """A common base class for the other pickling exceptions."""
64 pass
65
67 """This exception is raised when an unpicklable object is passed to the
68 dump() method.
69
70 """
71 pass
72
74 """This exception is raised when there is a problem unpickling an object,
75 such as a security violation.
76
77 Note that other exceptions may also be raised during unpickling, including
78 (but not necessarily limited to) AttributeError, EOFError, ImportError,
79 and IndexError.
80
81 """
82 pass
83
84
85
89
90
91 try:
92 from org.python.core import PyStringMap
93 except ImportError:
94 PyStringMap = None
95
96
97 try:
98 UnicodeType
99 except NameError:
100 UnicodeType = None
101
102
103
104
105
106 MARK = '('
107 STOP = '.'
108 POP = '0'
109 POP_MARK = '1'
110 DUP = '2'
111 FLOAT = 'F'
112 INT = 'I'
113 BININT = 'J'
114 BININT1 = 'K'
115 LONG = 'L'
116 BININT2 = 'M'
117 NONE = 'N'
118 PERSID = 'P'
119 BINPERSID = 'Q'
120 REDUCE = 'R'
121 STRING = 'S'
122 BINSTRING = 'T'
123 SHORT_BINSTRING = 'U'
124 UNICODE = 'V'
125 BINUNICODE = 'X'
126 APPEND = 'a'
127 BUILD = 'b'
128 GLOBAL = 'c'
129 DICT = 'd'
130 EMPTY_DICT = '}'
131 APPENDS = 'e'
132 GET = 'g'
133 BINGET = 'h'
134 INST = 'i'
135 LONG_BINGET = 'j'
136 LIST = 'l'
137 EMPTY_LIST = ']'
138 OBJ = 'o'
139 PUT = 'p'
140 BINPUT = 'q'
141 LONG_BINPUT = 'r'
142 SETITEM = 's'
143 TUPLE = 't'
144 EMPTY_TUPLE = ')'
145 SETITEMS = 'u'
146 BINFLOAT = 'G'
147
148 TRUE = 'I01\n'
149 FALSE = 'I00\n'
150
151
152
153 PROTO = '\x80'
154 NEWOBJ = '\x81'
155 EXT1 = '\x82'
156 EXT2 = '\x83'
157 EXT4 = '\x84'
158 TUPLE1 = '\x85'
159 TUPLE2 = '\x86'
160 TUPLE3 = '\x87'
161 NEWTRUE = '\x88'
162 NEWFALSE = '\x89'
163 LONG1 = '\x8a'
164 LONG4 = '\x8b'
165
166 _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
167
168
169 __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
170 del x
171
172
173
174
176
177 - def __init__(self, file, protocol=None, bin=None):
178 """This takes a file-like object for writing a pickle data stream.
179
180 The optional protocol argument tells the pickler to use the
181 given protocol; supported protocols are 0, 1, 2. The default
182 protocol is 0, to be backwards compatible. (Protocol 0 is the
183 only protocol that can be written to a file opened in text
184 mode and read back successfully. When using a protocol higher
185 than 0, make sure the file is opened in binary mode, both when
186 pickling and unpickling.)
187
188 Protocol 1 is more efficient than protocol 0; protocol 2 is
189 more efficient than protocol 1.
190
191 Specifying a negative protocol version selects the highest
192 protocol version supported. The higher the protocol used, the
193 more recent the version of Python needed to read the pickle
194 produced.
195
196 The file parameter must have a write() method that accepts a single
197 string argument. It can thus be an open file object, a StringIO
198 object, or any other custom object that meets this interface.
199
200 """
201 if protocol is not None and bin is not None:
202 raise ValueError, "can't specify both 'protocol' and 'bin'"
203 if bin is not None:
204 warnings.warn("The 'bin' argument to Pickler() is deprecated",
205 PendingDeprecationWarning)
206 protocol = bin
207 if protocol is None:
208 protocol = 0
209 if protocol < 0:
210 protocol = HIGHEST_PROTOCOL
211 elif not 0 <= protocol <= HIGHEST_PROTOCOL:
212 raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
213 self.write = file.write
214 self.memo = {}
215 self.proto = int(protocol)
216 self.bin = protocol >= 1
217 self.fast = 0
218
220 """Clears the pickler's "memo".
221
222 The memo is the data structure that remembers which objects the
223 pickler has already seen, so that shared or recursive objects are
224 pickled by reference and not by value. This method is useful when
225 re-using picklers.
226
227 """
228 self.memo.clear()
229
230 - def dump(self, obj):
231 """Write a pickled representation of obj to the open file."""
232 if self.proto >= 2:
233 self.write(PROTO + chr(self.proto))
234 self.save(obj)
235 self.write(STOP)
236
238 """Store an object in the memo."""
239
240
241
242
243
244
245
246
247
248
249
250
251
252 if self.fast:
253 return
254 assert id(obj) not in self.memo
255 memo_len = len(self.memo)
256 self.write(self.put(memo_len))
257 self.memo[id(obj)] = memo_len, obj
258
259
260 - def put(self, i, pack=struct.pack):
261 if self.bin:
262 if i < 256:
263 return BINPUT + chr(i)
264 else:
265 return LONG_BINPUT + pack("<i", i)
266
267 return PUT + `i` + '\n'
268
269
270 - def get(self, i, pack=struct.pack):
271 if self.bin:
272 if i < 256:
273 return BINGET + chr(i)
274 else:
275 return LONG_BINGET + pack("<i", i)
276
277 return GET + `i` + '\n'
278
279 - def save(self, obj):
280
281 pid = self.persistent_id(obj)
282 if pid:
283 self.save_pers(pid)
284 return
285
286
287 x = self.memo.get(id(obj))
288 if x:
289 self.write(self.get(x[0]))
290 return
291
292
293 t = type(obj)
294 f = self.dispatch.get(t)
295 if f:
296 f(self, obj)
297 return
298
299
300 try:
301 issc = issubclass(t, TypeType)
302 except TypeError:
303 issc = 0
304 if issc:
305 self.save_global(obj)
306 return
307
308
309 reduce = dispatch_table.get(t)
310 if reduce:
311 rv = reduce(obj)
312 else:
313
314 reduce = getattr(obj, "__reduce_ex__", None)
315 if reduce:
316
317 try:
318 rv = reduce(self.proto)
319 except:
320 print "Problem with ", obj
321 raise
322 else:
323 reduce = getattr(obj, "__reduce__", None)
324 if reduce:
325 rv = reduce()
326 else:
327 raise PicklingError("Can't pickle %r object: %r" %
328 (t.__name__, obj))
329
330
331 if type(rv) is StringType:
332 self.save_global(obj, rv)
333 return
334
335
336 if type(rv) is not TupleType:
337 raise PicklingError("%s must return string or tuple" % reduce)
338
339
340 l = len(rv)
341 if not (2 <= l <= 5):
342 raise PicklingError("Tuple returned by %s must have "
343 "two to five elements" % reduce)
344
345
346 self.save_reduce(obj=obj, *rv)
347
351
353
354 if self.bin:
355 self.save(pid)
356 self.write(BINPERSID)
357 else:
358 self.write(PERSID + str(pid) + '\n')
359
360 - def save_reduce(self, func, args, state=None,
361 listitems=None, dictitems=None, obj=None):
362
363
364
365 if not isinstance(args, TupleType):
366 if args is None:
367
368
369 warnings.warn("__basicnew__ special case is deprecated",
370 DeprecationWarning)
371 else:
372 raise PicklingError(
373 "args from reduce() should be a tuple")
374
375
376 if not callable(func):
377 raise PicklingError("func from reduce should be callable")
378
379 save = self.save
380 write = self.write
381
382
383 if self.proto >= 2 and getattr(func, "__name__", "") == "__newobj__":
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 cls = args[0]
411 if not hasattr(cls, "__new__"):
412 raise PicklingError(
413 "args[0] from __newobj__ args has no __new__")
414 if obj is not None and cls is not obj.__class__:
415 raise PicklingError(
416 "args[0] from __newobj__ args has the wrong class")
417 args = args[1:]
418 save(cls)
419 save(args)
420 write(NEWOBJ)
421 else:
422 save(func)
423 save(args)
424 write(REDUCE)
425
426 if obj is not None:
427 self.memoize(obj)
428
429
430
431
432
433
434 if listitems is not None:
435 self._batch_appends(listitems)
436
437 if dictitems is not None:
438 self._batch_setitems(dictitems)
439
440 if state is not None:
441 save(state)
442 write(BUILD)
443
444
445
446 dispatch = {}
447
450 dispatch[NoneType] = save_none
451
457 dispatch[bool] = save_bool
458
459 - def save_int(self, obj, pack=struct.pack):
460 if self.bin:
461
462
463
464
465 if obj >= 0:
466 if obj <= 0xff:
467 self.write(BININT1 + chr(obj))
468 return
469 if obj <= 0xffff:
470 self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
471 return
472
473 high_bits = obj >> 31
474 if high_bits == 0 or high_bits == -1:
475
476
477 self.write(BININT + pack("<i", obj))
478 return
479
480 self.write(INT + `obj` + '\n')
481 dispatch[IntType] = save_int
482
484 if self.proto >= 2:
485 bytes = encode_long(obj)
486 n = len(bytes)
487 if n < 256:
488 self.write(LONG1 + chr(n) + bytes)
489 else:
490 self.write(LONG4 + pack("<i", n) + bytes)
491 return
492 self.write(LONG + `obj` + '\n')
493 dispatch[LongType] = save_long
494
496 if self.bin:
497 self.write(BINFLOAT + pack('>d', obj))
498 else:
499 self.write(FLOAT + `obj` + '\n')
500 dispatch[FloatType] = save_float
501
503 if self.bin:
504 n = len(obj)
505 if n < 256:
506 self.write(SHORT_BINSTRING + chr(n) + obj)
507 else:
508 self.write(BINSTRING + pack("<i", n) + obj)
509 else:
510 self.write(STRING + `obj` + '\n')
511 self.memoize(obj)
512 dispatch[StringType] = save_string
513
515 if self.bin:
516 encoding = obj.encode('utf-8')
517 n = len(encoding)
518 self.write(BINUNICODE + pack("<i", n) + encoding)
519 else:
520 obj = obj.replace("\\", "\\u005c")
521 obj = obj.replace("\n", "\\u000a")
522 self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
523 self.memoize(obj)
524 dispatch[UnicodeType] = save_unicode
525
526 if StringType == UnicodeType:
527
529 unicode = obj.isunicode()
530
531 if self.bin:
532 if unicode:
533 obj = obj.encode("utf-8")
534 l = len(obj)
535 if l < 256 and not unicode:
536 self.write(SHORT_BINSTRING + chr(l) + obj)
537 else:
538 s = pack("<i", l)
539 if unicode:
540 self.write(BINUNICODE + s + obj)
541 else:
542 self.write(BINSTRING + s + obj)
543 else:
544 if unicode:
545 obj = obj.replace("\\", "\\u005c")
546 obj = obj.replace("\n", "\\u000a")
547 obj = obj.encode('raw-unicode-escape')
548 self.write(UNICODE + obj + '\n')
549 else:
550 self.write(STRING + `obj` + '\n')
551 self.memoize(obj)
552 dispatch[StringType] = save_string
553
555 write = self.write
556 proto = self.proto
557
558 n = len(obj)
559 if n == 0:
560 if proto:
561 write(EMPTY_TUPLE)
562 else:
563 write(MARK + TUPLE)
564 return
565
566 save = self.save
567 memo = self.memo
568 if n <= 3 and proto >= 2:
569 for element in obj:
570 save(element)
571
572 if id(obj) in memo:
573 get = self.get(memo[id(obj)][0])
574 write(POP * n + get)
575 else:
576 write(_tuplesize2code[n])
577 self.memoize(obj)
578 return
579
580
581
582 write(MARK)
583 for element in obj:
584 save(element)
585
586 if id(obj) in memo:
587
588
589
590
591
592
593
594 get = self.get(memo[id(obj)][0])
595 if proto:
596 write(POP_MARK + get)
597 else:
598 write(POP * (n+1) + get)
599 return
600
601
602 self.write(TUPLE)
603 self.memoize(obj)
604
605 dispatch[TupleType] = save_tuple
606
607
608
609
612
623
624 dispatch[ListType] = save_list
625
626
627
628 _BATCHSIZE = 1000
629
631
632 save = self.save
633 write = self.write
634
635 if not self.bin:
636 for x in items:
637 save(x)
638 write(APPEND)
639 return
640
641 r = xrange(self._BATCHSIZE)
642 while items is not None:
643 tmp = []
644 for i in r:
645 try:
646 x = items.next()
647 tmp.append(x)
648 except StopIteration:
649 items = None
650 break
651 n = len(tmp)
652 if n > 1:
653 write(MARK)
654 for x in tmp:
655 save(x)
656 write(APPENDS)
657 elif n:
658 save(tmp[0])
659 write(APPEND)
660
661
672
673 dispatch[DictionaryType] = save_dict
674 if not PyStringMap is None:
675 dispatch[PyStringMap] = save_dict
676
678
679 save = self.save
680 write = self.write
681
682 if not self.bin:
683 for k, v in items:
684 save(k)
685 save(v)
686 write(SETITEM)
687 return
688
689 r = xrange(self._BATCHSIZE)
690 while items is not None:
691 tmp = []
692 for i in r:
693 try:
694 tmp.append(items.next())
695 except StopIteration:
696 items = None
697 break
698 n = len(tmp)
699 if n > 1:
700 write(MARK)
701 for k, v in tmp:
702 save(k)
703 save(v)
704 write(SETITEMS)
705 elif n:
706 k, v = tmp[0]
707 save(k)
708 save(v)
709 write(SETITEM)
710
711
713 cls = obj.__class__
714
715 memo = self.memo
716 write = self.write
717 save = self.save
718
719 if hasattr(obj, '__getinitargs__'):
720 args = obj.__getinitargs__()
721 len(args)
722 _keep_alive(args, memo)
723 else:
724 args = ()
725
726 write(MARK)
727
728 if self.bin:
729 save(cls)
730 for arg in args:
731 save(arg)
732 write(OBJ)
733 else:
734 for arg in args:
735 save(arg)
736 write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
737
738 self.memoize(obj)
739
740 try:
741 getstate = obj.__getstate__
742 except AttributeError:
743 stuff = obj.__dict__
744 else:
745 stuff = getstate()
746 _keep_alive(stuff, memo)
747 save(stuff)
748 write(BUILD)
749
750 dispatch[InstanceType] = save_inst
751
752 - def save_global(self, obj, name=None, pack=struct.pack):
753 write = self.write
754 memo = self.memo
755
756 if name is None:
757 name = obj.__name__
758
759 module = getattr(obj, "__module__", None)
760 if module is None:
761 module = whichmodule(obj, name)
762
763 try:
764 __import__(module)
765 mod = sys.modules[module]
766 klass = getattr(mod, name)
767 except (ImportError, KeyError, AttributeError):
768 raise PicklingError(
769 "Can't pickle %r: it's not found as %s.%s" %
770 (obj, module, name))
771 else:
772 if klass is not obj:
773 raise PicklingError(
774 "Can't pickle %r: it's not the same object as %s.%s" %
775 (obj, module, name))
776
777 if self.proto >= 2:
778 code = _extension_registry.get((module, name))
779 if code:
780 assert code > 0
781 if code <= 0xff:
782 write(EXT1 + chr(code))
783 elif code <= 0xffff:
784 write("%c%c%c" % (EXT2, code&0xff, code>>8))
785 else:
786 write(EXT4 + pack("<i", code))
787 return
788
789 write(GLOBAL + module + '\n' + name + '\n')
790 self.memoize(obj)
791
792 dispatch[ClassType] = save_global
793 dispatch[FunctionType] = save_global
794 dispatch[BuiltinFunctionType] = save_global
795 dispatch[TypeType] = save_global
796
797
798
800 """Keeps a reference to the object x in the memo.
801
802 Because we remember objects by their id, we have
803 to assure that possibly temporary objects are kept
804 alive by referencing them.
805 We store a reference at the id of the memo, which should
806 normally not be used unless someone tries to deepcopy
807 the memo itself...
808 """
809 try:
810 memo[id(memo)].append(x)
811 except KeyError:
812
813 memo[id(memo)]=[x]
814
815
816
817
818
819 classmap = {}
820
822 """Figure out the module in which a function occurs.
823
824 Search sys.modules for the module.
825 Cache in classmap.
826 Return a module name.
827 If the function cannot be found, return "__main__".
828 """
829
830 mod = getattr(func, "__module__", None)
831 if mod is not None:
832 return mod
833 if func in classmap:
834 return classmap[func]
835
836 for name, module in sys.modules.items():
837 if module is None:
838 continue
839 if name != '__main__' and getattr(module, funcname, None) is func:
840 break
841 else:
842 name = '__main__'
843 classmap[func] = name
844 return name
845
846
847
848
850
852 """This takes a file-like object for reading a pickle data stream.
853
854 The protocol version of the pickle is detected automatically, so no
855 proto argument is needed.
856
857 The file-like object must have two methods, a read() method that
858 takes an integer argument, and a readline() method that requires no
859 arguments. Both methods should return a string. Thus file-like
860 object can be a file object opened for reading, a StringIO object,
861 or any other custom object that meets this interface.
862 """
863 self.readline = file.readline
864 self.read = file.read
865 self.memo = {}
866
868 """Read a pickled object representation from the open file.
869
870 Return the reconstituted object hierarchy specified in the file.
871 """
872 self.mark = object()
873 self.stack = []
874 self.append = self.stack.append
875 read = self.read
876 dispatch = self.dispatch
877 try:
878 while 1:
879 key = read(1)
880 dispatch[key](self)
881 except _Stop, stopinst:
882 return stopinst.value
883
884
885
886
887
888
889
890
891
893 stack = self.stack
894 mark = self.mark
895 k = len(stack)-1
896 while stack[k] is not mark: k = k-1
897 return k
898
899 dispatch = {}
900
903 dispatch[''] = load_eof
904
906 proto = ord(self.read(1))
907 if not 0 <= proto <= 2:
908 raise ValueError, "unsupported pickle protocol: %d" % proto
909 dispatch[PROTO] = load_proto
910
912 pid = self.readline()[:-1]
913 self.append(self.persistent_load(pid))
914 dispatch[PERSID] = load_persid
915
917 pid = self.stack.pop()
918 self.append(self.persistent_load(pid))
919 dispatch[BINPERSID] = load_binpersid
920
923 dispatch[NONE] = load_none
924
927 dispatch[NEWFALSE] = load_false
928
931 dispatch[NEWTRUE] = load_true
932
934 data = self.readline()
935 if data == FALSE[1:]:
936 val = False
937 elif data == TRUE[1:]:
938 val = True
939 else:
940 try:
941 val = int(data)
942 except ValueError:
943 val = long(data)
944 self.append(val)
945 dispatch[INT] = load_int
946
948 self.append(mloads('i' + self.read(4)))
949 dispatch[BININT] = load_binint
950
952 self.append(ord(self.read(1)))
953 dispatch[BININT1] = load_binint1
954
956 self.append(mloads('i' + self.read(2) + '\000\000'))
957 dispatch[BININT2] = load_binint2
958
960 self.append(long(self.readline()[:-1], 0))
961 dispatch[LONG] = load_long
962
967 dispatch[LONG1] = load_long1
968
970 n = mloads('i' + self.read(4))
971 bytes = self.read(n)
972 self.append(decode_long(bytes))
973 dispatch[LONG4] = load_long4
974
976 s = self.readline()[:-1]
977 try:
978 f = float(s)
979 except ValueError:
980 s = s.upper()
981 if s in ["1.#INF", "INF"]:
982 f = 1e300*1e300
983 elif s in ["-1.#INF", "-INF"]:
984 f = -1e300*1e300
985 elif s in ["NAN","1.#QNAN","QNAN","1.#IND","IND","-1.#IND"]:
986 f = -((1e300*1e300)/(1e300*1e300))
987 else:
988 raise ValueError, "Don't know what to do with "+`s`
989 self.append(f)
990 dispatch[FLOAT] = load_float
991
993 self.append(unpack('>d', self.read(8))[0])
994 dispatch[BINFLOAT] = load_binfloat
995
997 rep = self.readline()[:-1]
998 for q in "\"'":
999 if rep.startswith(q):
1000 if not rep.endswith(q):
1001 raise ValueError, "insecure string pickle"
1002 rep = rep[len(q):-len(q)]
1003 break
1004 else:
1005 raise ValueError, "insecure string pickle"
1006 self.append(rep.decode("string-escape"))
1007 dispatch[STRING] = load_string
1008
1010 len = mloads('i' + self.read(4))
1011 self.append(self.read(len))
1012 dispatch[BINSTRING] = load_binstring
1013
1015 self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
1016 dispatch[UNICODE] = load_unicode
1017
1019 len = mloads('i' + self.read(4))
1020 self.append(unicode(self.read(len),'utf-8'))
1021 dispatch[BINUNICODE] = load_binunicode
1022
1024 len = ord(self.read(1))
1025 self.append(self.read(len))
1026 dispatch[SHORT_BINSTRING] = load_short_binstring
1027
1029 k = self.marker()
1030 self.stack[k:] = [tuple(self.stack[k+1:])]
1031 dispatch[TUPLE] = load_tuple
1032
1035 dispatch[EMPTY_TUPLE] = load_empty_tuple
1036
1038 self.stack[-1] = (self.stack[-1],)
1039 dispatch[TUPLE1] = load_tuple1
1040
1042 self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
1043 dispatch[TUPLE2] = load_tuple2
1044
1046 self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
1047 dispatch[TUPLE3] = load_tuple3
1048
1051 dispatch[EMPTY_LIST] = load_empty_list
1052
1055 dispatch[EMPTY_DICT] = load_empty_dictionary
1056
1058 k = self.marker()
1059 self.stack[k:] = [self.stack[k+1:]]
1060 dispatch[LIST] = load_list
1061
1063 k = self.marker()
1064 d = {}
1065 items = self.stack[k+1:]
1066 for i in range(0, len(items), 2):
1067 key = items[i]
1068 value = items[i+1]
1069 d[key] = value
1070 self.stack[k:] = [d]
1071 dispatch[DICT] = load_dict
1072
1073
1074
1075
1076
1077
1079 args = tuple(self.stack[k+1:])
1080 del self.stack[k:]
1081 instantiated = 0
1082 if (not args and
1083 type(klass) is ClassType and
1084 not hasattr(klass, "__getinitargs__")):
1085 try:
1086 value = _EmptyClass()
1087 value.__class__ = klass
1088 instantiated = 1
1089 except RuntimeError:
1090
1091
1092 pass
1093 if not instantiated:
1094 try:
1095 value = klass(*args)
1096 except TypeError, err:
1097 raise TypeError, "in constructor for %s: %s" % (
1098 klass.__name__, str(err)), sys.exc_info()[2]
1099 self.append(value)
1100
1106 dispatch[INST] = load_inst
1107
1113 dispatch[OBJ] = load_obj
1114
1116 args = self.stack.pop()
1117 cls = self.stack[-1]
1118 obj = cls.__new__(cls, *args)
1119 self.stack[-1] = obj
1120 dispatch[NEWOBJ] = load_newobj
1121
1123 module = self.readline()[:-1]
1124 name = self.readline()[:-1]
1125 klass = self.find_class(module, name)
1126 self.append(klass)
1127 dispatch[GLOBAL] = load_global
1128
1132 dispatch[EXT1] = load_ext1
1133
1135 code = mloads('i' + self.read(2) + '\000\000')
1136 self.get_extension(code)
1137 dispatch[EXT2] = load_ext2
1138
1140 code = mloads('i' + self.read(4))
1141 self.get_extension(code)
1142 dispatch[EXT4] = load_ext4
1143
1145 nil = []
1146 obj = _extension_cache.get(code, nil)
1147 if obj is not nil:
1148 self.append(obj)
1149 return
1150 key = _inverted_registry.get(code)
1151 if not key:
1152 raise ValueError("unregistered extension code %d" % code)
1153 obj = self.find_class(*key)
1154 _extension_cache[code] = obj
1155 self.append(obj)
1156
1158
1159 __import__(module)
1160 mod = sys.modules[module]
1161 klass = getattr(mod, name)
1162 return klass
1163
1165 stack = self.stack
1166 args = stack.pop()
1167 func = stack[-1]
1168 if args is None:
1169
1170 warnings.warn("__basicnew__ special case is deprecated",
1171 DeprecationWarning)
1172 value = func.__basicnew__()
1173 else:
1174 value = func(*args)
1175 stack[-1] = value
1176 dispatch[REDUCE] = load_reduce
1177
1180 dispatch[POP] = load_pop
1181
1183 k = self.marker()
1184 del self.stack[k:]
1185 dispatch[POP_MARK] = load_pop_mark
1186
1188 self.append(self.stack[-1])
1189 dispatch[DUP] = load_dup
1190
1192 self.append(self.memo[self.readline()[:-1]])
1193 dispatch[GET] = load_get
1194
1196 i = ord(self.read(1))
1197 self.append(self.memo[`i`])
1198 dispatch[BINGET] = load_binget
1199
1201 i = mloads('i' + self.read(4))
1202 self.append(self.memo[`i`])
1203 dispatch[LONG_BINGET] = load_long_binget
1204
1206 self.memo[self.readline()[:-1]] = self.stack[-1]
1207 dispatch[PUT] = load_put
1208
1212 dispatch[BINPUT] = load_binput
1213
1217 dispatch[LONG_BINPUT] = load_long_binput
1218
1220 stack = self.stack
1221 value = stack.pop()
1222 list = stack[-1]
1223 list.append(value)
1224 dispatch[APPEND] = load_append
1225
1227 stack = self.stack
1228 mark = self.marker()
1229 list = stack[mark - 1]
1230 list.extend(stack[mark + 1:])
1231 del stack[mark:]
1232 dispatch[APPENDS] = load_appends
1233
1235 stack = self.stack
1236 value = stack.pop()
1237 key = stack.pop()
1238 dict = stack[-1]
1239 dict[key] = value
1240 dispatch[SETITEM] = load_setitem
1241
1243 stack = self.stack
1244 mark = self.marker()
1245 dict = stack[mark - 1]
1246 for i in range(mark + 1, len(stack), 2):
1247 dict[stack[i]] = stack[i + 1]
1248
1249 del stack[mark:]
1250 dispatch[SETITEMS] = load_setitems
1251
1253 stack = self.stack
1254 state = stack.pop()
1255 inst = stack[-1]
1256 setstate = getattr(inst, "__setstate__", None)
1257 if setstate:
1258 setstate(state)
1259 return
1260 slotstate = None
1261 if isinstance(state, tuple) and len(state) == 2:
1262 state, slotstate = state
1263 if state:
1264 try:
1265 inst.__dict__.update(state)
1266 except RuntimeError:
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276 for k, v in state.items():
1277 setattr(inst, k, v)
1278 if slotstate:
1279 for k, v in slotstate.items():
1280 setattr(inst, k, v)
1281 dispatch[BUILD] = load_build
1282
1285 dispatch[MARK] = load_mark
1286
1288 value = self.stack.pop()
1289 raise _Stop(value)
1290 dispatch[STOP] = load_stop
1291
1292
1293
1296
1297
1298
1299 import binascii as _binascii
1300
1302 r"""Encode a long to a two's complement little-endian binary string.
1303 Note that 0L is a special case, returning an empty string, to save a
1304 byte in the LONG1 pickling context.
1305
1306 >>> encode_long(0L)
1307 ''
1308 >>> encode_long(255L)
1309 '\xff\x00'
1310 >>> encode_long(32767L)
1311 '\xff\x7f'
1312 >>> encode_long(-256L)
1313 '\x00\xff'
1314 >>> encode_long(-32768L)
1315 '\x00\x80'
1316 >>> encode_long(-128L)
1317 '\x80'
1318 >>> encode_long(127L)
1319 '\x7f'
1320 >>>
1321 """
1322
1323 if x == 0:
1324 return ''
1325 if x > 0:
1326 ashex = hex(x)
1327 assert ashex.startswith("0x")
1328 njunkchars = 2 + ashex.endswith('L')
1329 nibbles = len(ashex) - njunkchars
1330 if nibbles & 1:
1331
1332 ashex = "0x0" + ashex[2:]
1333 elif int(ashex[2], 16) >= 8:
1334
1335 ashex = "0x00" + ashex[2:]
1336 else:
1337
1338
1339
1340 ashex = hex(-x)
1341 assert ashex.startswith("0x")
1342 njunkchars = 2 + ashex.endswith('L')
1343 nibbles = len(ashex) - njunkchars
1344 if nibbles & 1:
1345
1346 nibbles += 1
1347 nbits = nibbles * 4
1348 x += 1L << nbits
1349 assert x > 0
1350 ashex = hex(x)
1351 njunkchars = 2 + ashex.endswith('L')
1352 newnibbles = len(ashex) - njunkchars
1353 if newnibbles < nibbles:
1354 ashex = "0x" + "0" * (nibbles - newnibbles) + ashex[2:]
1355 if int(ashex[2], 16) < 8:
1356
1357 ashex = "0xff" + ashex[2:]
1358
1359 if ashex.endswith('L'):
1360 ashex = ashex[2:-1]
1361 else:
1362 ashex = ashex[2:]
1363 assert len(ashex) & 1 == 0, (x, ashex)
1364 binary = _binascii.unhexlify(ashex)
1365 return binary[::-1]
1366
1368 r"""Decode a long from a two's complement little-endian binary string.
1369
1370 >>> decode_long('')
1371 0L
1372 >>> decode_long("\xff\x00")
1373 255L
1374 >>> decode_long("\xff\x7f")
1375 32767L
1376 >>> decode_long("\x00\xff")
1377 -256L
1378 >>> decode_long("\x00\x80")
1379 -32768L
1380 >>> decode_long("\x80")
1381 -128L
1382 >>> decode_long("\x7f")
1383 127L
1384 """
1385
1386 nbytes = len(data)
1387 if nbytes == 0:
1388 return 0L
1389 ashex = _binascii.hexlify(data[::-1])
1390 n = long(ashex, 16)
1391 if data[-1] >= '\x80':
1392 n -= 1L << (nbytes * 8)
1393 return n
1394
1395
1396
1397 try:
1398 from cStringIO import StringIO
1399 except ImportError:
1400 from StringIO import StringIO
1401
1402 -def dump(obj, file, protocol=None, bin=None):
1404
1405 -def dumps(obj, protocol=None, bin=None):
1406 file = StringIO()
1407 Pickler(file, protocol, bin).dump(obj)
1408 return file.getvalue()
1409
1412
1416
1417
1418
1420 import doctest
1421 return doctest.testmod()
1422
1423 if __name__ == "__main__":
1424 _test()
1425