1"""Create portable serialized representations of Python objects.
2
3See module cPickle for a (much) faster implementation.
4See module copy_reg for a mechanism for registering custom picklers.
5See module pickletools source for extensive comments.
6
7Classes:
8
9    Pickler
10    Unpickler
11
12Functions:
13
14    dump(object, file)
15    dumps(object) -> string
16    load(file) -> object
17    loads(string) -> object
18
19Misc variables:
20
21    __version__
22    format_version
23    compatible_formats
24
25"""
26
27__version__ = "$Revision: 72223 $"       # Code version
28
29from types import *
30from copy_reg import dispatch_table
31from copy_reg import _extension_registry, _inverted_registry, _extension_cache
32import marshal
33import sys
34import struct
35import re
36
37__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
38           "Unpickler", "dump", "dumps", "load", "loads"]
39
40# These are purely informational; no code uses these.
41format_version = "3.0"                  # File format version we write
42compatible_formats = ["1.0",            # Original protocol 0
43                      "1.1",            # Protocol 0 with INST added
44                      "1.2",            # Original protocol 1
45                      "1.3",            # Protocol 1 with BINFLOAT added
46                      "2.0",            # Protocol 2
47                      "3.0",            # Protocol 3
48                      ]                 # Old format versions we can read
49
50# Keep in synch with cPickle.  This is the highest protocol number we
51# know how to read.
52HIGHEST_PROTOCOL = 3
53DEFAULT_PROTOCOL = 3
54
55# Why use struct.pack() for pickling but marshal.loads() for
56# unpickling?  struct.pack() is 40% faster than marshal.dumps(), but
57# marshal.loads() is twice as fast as struct.unpack()!
58mloads = marshal.loads
59
60class PickleError(Exception):
61    """A common base class for the other pickling exceptions."""
62    pass
63
64class PicklingError(PickleError):
65    """This exception is raised when an unpicklable object is passed to the
66    dump() method.
67
68    """
69    pass
70
71class UnpicklingError(PickleError):
72    """This exception is raised when there is a problem unpickling an object,
73    such as a security violation.
74
75    Note that other exceptions may also be raised during unpickling, including
76    (but not necessarily limited to) AttributeError, EOFError, ImportError,
77    and IndexError.
78
79    """
80    pass
81
82# An instance of _Stop is raised by Unpickler.load_stop() in response to
83# the STOP opcode, passing the object that is the result of unpickling.
84class _Stop(Exception):
85    def __init__(self, value):
86        self.value = value
87
88# Jython has PyStringMap; it's a dict subclass with string keys
89try:
90    from org.python.core import PyStringMap
91except ImportError:
92    PyStringMap = None
93
94# UnicodeType may or may not be exported (normally imported from types)
95try:
96    UnicodeType
97except NameError:
98    UnicodeType = None
99
100# Pickle opcodes.  See pickletools.py for extensive docs.  The listing
101# here is in kind-of alphabetical order of 1-character pickle code.
102# pickletools groups them by purpose.
103
104MARK            = '('   # push special markobject on stack
105STOP            = '.'   # every pickle ends with STOP
106POP             = '0'   # discard topmost stack item
107POP_MARK        = '1'   # discard stack top through topmost markobject
108DUP             = '2'   # duplicate top stack item
109FLOAT           = 'F'   # push float object; decimal string argument
110INT             = 'I'   # push integer or bool; decimal string argument
111BININT          = 'J'   # push four-byte signed int
112BININT1         = 'K'   # push 1-byte unsigned int
113LONG            = 'L'   # push long; decimal string argument
114BININT2         = 'M'   # push 2-byte unsigned int
115NONE            = 'N'   # push None
116PERSID          = 'P'   # push persistent object; id is taken from string arg
117BINPERSID       = 'Q'   #  "       "         "  ;  "  "   "     "  stack
118REDUCE          = 'R'   # apply callable to argtuple, both on stack
119STRING          = 'S'   # push string; NL-terminated string argument
120BINSTRING       = 'T'   # push string; counted binary string argument
121SHORT_BINSTRING = 'U'   #  "     "   ;    "      "       "      " < 256 bytes
122UNICODE         = 'V'   # push Unicode string; raw-unicode-escaped'd argument
123BINUNICODE      = 'X'   #   "     "       "  ; counted UTF-8 string argument
124APPEND          = 'a'   # append stack top to list below it
125BUILD           = 'b'   # call __setstate__ or __dict__.update()
126GLOBAL          = 'c'   # push self.find_class(modname, name); 2 string args
127DICT            = 'd'   # build a dict from stack items
128EMPTY_DICT      = '}'   # push empty dict
129APPENDS         = 'e'   # extend list on stack by topmost stack slice
130GET             = 'g'   # push item from memo on stack; index is string arg
131BINGET          = 'h'   #   "    "    "    "   "   "  ;   "    " 1-byte arg
132INST            = 'i'   # build & push class instance
133LONG_BINGET     = 'j'   # push item from memo on stack; index is 4-byte arg
134LIST            = 'l'   # build list from topmost stack items
135EMPTY_LIST      = ']'   # push empty list
136OBJ             = 'o'   # build & push class instance
137PUT             = 'p'   # store stack top in memo; index is string arg
138BINPUT          = 'q'   #   "     "    "   "   " ;   "    " 1-byte arg
139LONG_BINPUT     = 'r'   #   "     "    "   "   " ;   "    " 4-byte arg
140SETITEM         = 's'   # add key+value pair to dict
141TUPLE           = 't'   # build tuple from topmost stack items
142EMPTY_TUPLE     = ')'   # push empty tuple
143SETITEMS        = 'u'   # modify dict by adding topmost key+value pairs
144BINFLOAT        = 'G'   # push float; arg is 8-byte float encoding
145
146TRUE            = 'I01\n'  # not an opcode; see INT docs in pickletools.py
147FALSE           = 'I00\n'  # not an opcode; see INT docs in pickletools.py
148
149# Protocol 2
150
151PROTO           = '\x80'  # identify pickle protocol
152NEWOBJ          = '\x81'  # build object by applying cls.__new__ to argtuple
153EXT1            = '\x82'  # push object from extension registry; 1-byte index
154EXT2            = '\x83'  # ditto, but 2-byte index
155EXT4            = '\x84'  # ditto, but 4-byte index
156TUPLE1          = '\x85'  # build 1-tuple from stack top
157TUPLE2          = '\x86'  # build 2-tuple from two topmost stack items
158TUPLE3          = '\x87'  # build 3-tuple from three topmost stack items
159NEWTRUE         = '\x88'  # push True
160NEWFALSE        = '\x89'  # push False
161LONG1           = '\x8a'  # push long from < 256 bytes
162LONG4           = '\x8b'  # push really big long
163
164# Protocol 3
165
166BINBYTES        = 'B'
167SHORT_BINBYTES  = 'C'
168
169from zodbpickle import binary as BinaryType
170
171_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3]
172
173
174__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
175del x
176
177
178# Pickling machinery
179
180class Pickler:
181
182    def __init__(self, file, protocol=None):
183        """This takes a file-like object for writing a pickle data stream.
184
185        The optional protocol argument tells the pickler to use the
186        given protocol; supported protocols are 0, 1, 2.  The default
187        protocol is 0, to be backwards compatible.  (Protocol 0 is the
188        only protocol that can be written to a file opened in text
189        mode and read back successfully.  When using a protocol higher
190        than 0, make sure the file is opened in binary mode, both when
191        pickling and unpickling.)
192
193        Protocol 1 is more efficient than protocol 0; protocol 2 is
194        more efficient than protocol 1.
195
196        Specifying a negative protocol version selects the highest
197        protocol version supported.  The higher the protocol used, the
198        more recent the version of Python needed to read the pickle
199        produced.
200
201        The file parameter must have a write() method that accepts a single
202        string argument.  It can thus be an open file object, a StringIO
203        object, or any other custom object that meets this interface.
204
205        """
206        if protocol is None:
207            protocol = DEFAULT_PROTOCOL
208        if protocol < 0:
209            protocol = HIGHEST_PROTOCOL
210        elif not 0 <= protocol <= HIGHEST_PROTOCOL:
211            raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
212        self.write = file.write
213        self.memo = {}
214        self.proto = int(protocol)
215        self.bin = protocol >= 1
216        self.fast = 0
217
218    def clear_memo(self):
219        """Clears the pickler's "memo".
220
221        The memo is the data structure that remembers which objects the
222        pickler has already seen, so that shared or recursive objects are
223        pickled by reference and not by value.  This method is useful when
224        re-using picklers.
225
226        """
227        self.memo.clear()
228
229    def dump(self, obj):
230        """Write a pickled representation of obj to the open file."""
231        if self.proto >= 2:
232            self.write(PROTO + chr(self.proto))
233        self.save(obj)
234        self.write(STOP)
235
236    def memoize(self, obj):
237        """Store an object in the memo."""
238
239        # The Pickler memo is a dictionary mapping object ids to 2-tuples
240        # that contain the Unpickler memo key and the object being memoized.
241        # The memo key is written to the pickle and will become
242        # the key in the Unpickler's memo.  The object is stored in the
243        # Pickler memo so that transient objects are kept alive during
244        # pickling.
245
246        # The use of the Unpickler memo length as the memo key is just a
247        # convention.  The only requirement is that the memo values be unique.
248        # But there appears no advantage to any other scheme, and this
249        # scheme allows the Unpickler memo to be implemented as a plain (but
250        # growable) array, indexed by memo key.
251        if self.fast:
252            return
253        assert id(obj) not in self.memo
254        memo_len = len(self.memo)
255        self.write(self.put(memo_len))
256        self.memo[id(obj)] = memo_len, obj
257
258    # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i.
259    def put(self, i, pack=struct.pack):
260        if self.bin:
261            if i < 256:
262                return BINPUT + chr(i)
263            else:
264                return LONG_BINPUT + pack("<i", i)
265
266        return PUT + repr(i) + '\n'
267
268    # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
269    def get(self, i, pack=struct.pack):
270        if self.bin:
271            if i < 256:
272                return BINGET + chr(i)
273            else:
274                return LONG_BINGET + pack("<i", i)
275
276        return GET + repr(i) + '\n'
277
278    def save(self, obj):
279        # Check for persistent id (defined by a subclass)
280        pid = self.persistent_id(obj)
281        if pid:
282            self.save_pers(pid)
283            return
284
285        # Check the memo
286        x = self.memo.get(id(obj))
287        if x:
288            self.write(self.get(x[0]))
289            return
290
291        # Check the type dispatch table
292        t = type(obj)
293        f = self.dispatch.get(t)
294        if f:
295            f(self, obj) # Call unbound method with explicit self
296            return
297
298        # Check copy_reg.dispatch_table
299        reduce = dispatch_table.get(t)
300        if reduce:
301            rv = reduce(obj)
302        else:
303            # Check for a class with a custom metaclass; treat as regular class
304            try:
305                issc = issubclass(t, TypeType)
306            except TypeError: # t is not a class (old Boost; see SF #502085)
307                issc = 0
308            if issc:
309                self.save_global(obj)
310                return
311
312            # Check for a __reduce_ex__ method, fall back to __reduce__
313            reduce = getattr(obj, "__reduce_ex__", None)
314            if reduce:
315                rv = reduce(self.proto)
316            else:
317                reduce = getattr(obj, "__reduce__", None)
318                if reduce:
319                    rv = reduce()
320                else:
321                    raise PicklingError("Can't pickle %r object: %r" %
322                                        (t.__name__, obj))
323
324        # Check for string returned by reduce(), meaning "save as global"
325        if type(rv) is StringType:
326            self.save_global(obj, rv)
327            return
328
329        # Assert that reduce() returned a tuple
330        if type(rv) is not TupleType:
331            raise PicklingError("%s must return string or tuple" % reduce)
332
333        # Assert that it returned an appropriately sized tuple
334        l = len(rv)
335        if not (2 <= l <= 5):
336            raise PicklingError("Tuple returned by %s must have "
337                                "two to five elements" % reduce)
338
339        # Save the reduce() output and finally memoize the object
340        self.save_reduce(obj=obj, *rv)
341
342    def persistent_id(self, obj):
343        # This exists so a subclass can override it
344        return None
345
346    def save_pers(self, pid):
347        # Save a persistent id reference
348        if self.bin:
349            self.save(pid)
350            self.write(BINPERSID)
351        else:
352            self.write(PERSID + str(pid) + '\n')
353
354    def save_reduce(self, func, args, state=None,
355                    listitems=None, dictitems=None, obj=None):
356        # This API is called by some subclasses
357
358        # Assert that args is a tuple or None
359        if not isinstance(args, TupleType):
360            raise PicklingError("args from reduce() should be a tuple")
361
362        # Assert that func is callable
363        if not hasattr(func, '__call__'):
364            raise PicklingError("func from reduce should be callable")
365
366        save = self.save
367        write = self.write
368
369        # Protocol 2 special case: if func's name is __newobj__, use NEWOBJ
370        if self.proto >= 2 and getattr(func, "__name__", "") == "__newobj__":
371            # A __reduce__ implementation can direct protocol 2 to
372            # use the more efficient NEWOBJ opcode, while still
373            # allowing protocol 0 and 1 to work normally.  For this to
374            # work, the function returned by __reduce__ should be
375            # called __newobj__, and its first argument should be a
376            # new-style class.  The implementation for __newobj__
377            # should be as follows, although pickle has no way to
378            # verify this:
379            #
380            # def __newobj__(cls, *args):
381            #     return cls.__new__(cls, *args)
382            #
383            # Protocols 0 and 1 will pickle a reference to __newobj__,
384            # while protocol 2 (and above) will pickle a reference to
385            # cls, the remaining args tuple, and the NEWOBJ code,
386            # which calls cls.__new__(cls, *args) at unpickling time
387            # (see load_newobj below).  If __reduce__ returns a
388            # three-tuple, the state from the third tuple item will be
389            # pickled regardless of the protocol, calling __setstate__
390            # at unpickling time (see load_build below).
391            #
392            # Note that no standard __newobj__ implementation exists;
393            # you have to provide your own.  This is to enforce
394            # compatibility with Python 2.2 (pickles written using
395            # protocol 0 or 1 in Python 2.3 should be unpicklable by
396            # Python 2.2).
397            cls = args[0]
398            if not hasattr(cls, "__new__"):
399                raise PicklingError(
400                    "args[0] from __newobj__ args has no __new__")
401            if obj is not None and cls is not obj.__class__:
402                raise PicklingError(
403                    "args[0] from __newobj__ args has the wrong class")
404            args = args[1:]
405            save(cls)
406            save(args)
407            write(NEWOBJ)
408        else:
409            save(func)
410            save(args)
411            write(REDUCE)
412
413        if obj is not None:
414            self.memoize(obj)
415
416        # More new special cases (that work with older protocols as
417        # well): when __reduce__ returns a tuple with 4 or 5 items,
418        # the 4th and 5th item should be iterators that provide list
419        # items and dict items (as (key, value) tuples), or None.
420
421        if listitems is not None:
422            self._batch_appends(listitems)
423
424        if dictitems is not None:
425            self._batch_setitems(dictitems)
426
427        if state is not None:
428            save(state)
429            write(BUILD)
430
431    # Methods below this point are dispatched through the dispatch table
432
433    dispatch = {}
434
435    def save_none(self, obj):
436        self.write(NONE)
437    dispatch[NoneType] = save_none
438
439    def save_bool(self, obj):
440        if self.proto >= 2:
441            self.write(obj and NEWTRUE or NEWFALSE)
442        else:
443            self.write(obj and TRUE or FALSE)
444    dispatch[bool] = save_bool
445
446    def save_int(self, obj, pack=struct.pack):
447        if self.bin:
448            # If the int is small enough to fit in a signed 4-byte 2's-comp
449            # format, we can store it more efficiently than the general
450            # case.
451            # First one- and two-byte unsigned ints:
452            if obj >= 0:
453                if obj <= 0xff:
454                    self.write(BININT1 + chr(obj))
455                    return
456                if obj <= 0xffff:
457                    self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
458                    return
459            # Next check for 4-byte signed ints:
460            high_bits = obj >> 31  # note that Python shift sign-extends
461            if high_bits == 0 or high_bits == -1:
462                # All high bits are copies of bit 2**31, so the value
463                # fits in a 4-byte signed int.
464                self.write(BININT + pack("<i", obj))
465                return
466        # Text pickle, or int too big to fit in signed 4-byte format.
467        self.write(INT + repr(obj) + '\n')
468    dispatch[IntType] = save_int
469
470    def save_long(self, obj, pack=struct.pack):
471        if self.proto >= 2:
472            bytes = encode_long(obj)
473            n = len(bytes)
474            if n < 256:
475                self.write(LONG1 + chr(n) + bytes)
476            else:
477                self.write(LONG4 + pack("<i", n) + bytes)
478            return
479        self.write(LONG + repr(obj) + '\n')
480    dispatch[LongType] = save_long
481
482    def save_float(self, obj, pack=struct.pack):
483        if self.bin:
484            self.write(BINFLOAT + pack('>d', obj))
485        else:
486            self.write(FLOAT + repr(obj) + '\n')
487    dispatch[FloatType] = save_float
488
489    def save_string(self, obj, pack=struct.pack):
490        if self.bin:
491            n = len(obj)
492            if n < 256:
493                self.write(SHORT_BINSTRING + chr(n) + obj)
494            else:
495                self.write(BINSTRING + pack("<i", n) + obj)
496        else:
497            self.write(STRING + repr(obj) + '\n')
498        self.memoize(obj)
499    dispatch[StringType] = save_string
500
501    def save_unicode(self, obj, pack=struct.pack):
502        if self.bin:
503            encoding = obj.encode('utf-8')
504            n = len(encoding)
505            self.write(BINUNICODE + pack("<i", n) + encoding)
506        else:
507            obj = obj.replace("\\", "\\u005c")
508            obj = obj.replace("\n", "\\u000a")
509            self.write(UNICODE + obj.encode('raw-unicode-escape') + '\n')
510        self.memoize(obj)
511    dispatch[UnicodeType] = save_unicode
512
513    if StringType is UnicodeType:
514        # This is true for Jython
515        def save_string(self, obj, pack=struct.pack):
516            unicode = obj.isunicode()
517
518            if self.bin:
519                if unicode:
520                    obj = obj.encode("utf-8")
521                l = len(obj)
522                if l < 256 and not unicode:
523                    self.write(SHORT_BINSTRING + chr(l) + obj)
524                else:
525                    s = pack("<i", l)
526                    if unicode:
527                        self.write(BINUNICODE + s + obj)
528                    else:
529                        self.write(BINSTRING + s + obj)
530            else:
531                if unicode:
532                    obj = obj.replace("\\", "\\u005c")
533                    obj = obj.replace("\n", "\\u000a")
534                    obj = obj.encode('raw-unicode-escape')
535                    self.write(UNICODE + obj + '\n')
536                else:
537                    self.write(STRING + repr(obj) + '\n')
538            self.memoize(obj)
539        dispatch[StringType] = save_string
540
541    def save_tuple(self, obj):
542        write = self.write
543        proto = self.proto
544
545        n = len(obj)
546        if n == 0:
547            if proto:
548                write(EMPTY_TUPLE)
549            else:
550                write(MARK + TUPLE)
551            return
552
553        save = self.save
554        memo = self.memo
555        if n <= 3 and proto >= 2:
556            for element in obj:
557                save(element)
558            # Subtle.  Same as in the big comment below.
559            if id(obj) in memo:
560                get = self.get(memo[id(obj)][0])
561                write(POP * n + get)
562            else:
563                write(_tuplesize2code[n])
564                self.memoize(obj)
565            return
566
567        # proto 0 or proto 1 and tuple isn't empty, or proto > 1 and tuple
568        # has more than 3 elements.
569        write(MARK)
570        for element in obj:
571            save(element)
572
573        if id(obj) in memo:
574            # Subtle.  d was not in memo when we entered save_tuple(), so
575            # the process of saving the tuple's elements must have saved
576            # the tuple itself:  the tuple is recursive.  The proper action
577            # now is to throw away everything we put on the stack, and
578            # simply GET the tuple (it's already constructed).  This check
579            # could have been done in the "for element" loop instead, but
580            # recursive tuples are a rare thing.
581            get = self.get(memo[id(obj)][0])
582            if proto:
583                write(POP_MARK + get)
584            else:   # proto 0 -- POP_MARK not available
585                write(POP * (n+1) + get)
586            return
587
588        # No recursion.
589        self.write(TUPLE)
590        self.memoize(obj)
591
592    dispatch[TupleType] = save_tuple
593
594    # save_empty_tuple() isn't used by anything in Python 2.3.  However, I
595    # found a Pickler subclass in Zope3 that calls it, so it's not harmless
596    # to remove it.
597    def save_empty_tuple(self, obj):
598        self.write(EMPTY_TUPLE)
599
600    def save_list(self, obj):
601        write = self.write
602
603        if self.bin:
604            write(EMPTY_LIST)
605        else:   # proto 0 -- can't use EMPTY_LIST
606            write(MARK + LIST)
607
608        self.memoize(obj)
609        self._batch_appends(iter(obj))
610
611    dispatch[ListType] = save_list
612
613    # Keep in synch with cPickle's BATCHSIZE.  Nothing will break if it gets
614    # out of synch, though.
615    _BATCHSIZE = 1000
616
617    def _batch_appends(self, items):
618        # Helper to batch up APPENDS sequences
619        save = self.save
620        write = self.write
621
622        if not self.bin:
623            for x in items:
624                save(x)
625                write(APPEND)
626            return
627
628        r = xrange(self._BATCHSIZE)
629        while items is not None:
630            tmp = []
631            for i in r:
632                try:
633                    x = items.next()
634                    tmp.append(x)
635                except StopIteration:
636                    items = None
637                    break
638            n = len(tmp)
639            if n > 1:
640                write(MARK)
641                for x in tmp:
642                    save(x)
643                write(APPENDS)
644            elif n:
645                save(tmp[0])
646                write(APPEND)
647            # else tmp is empty, and we're done
648
649    def save_dict(self, obj):
650        write = self.write
651
652        if self.bin:
653            write(EMPTY_DICT)
654        else:   # proto 0 -- can't use EMPTY_DICT
655            write(MARK + DICT)
656
657        self.memoize(obj)
658        self._batch_setitems(obj.iteritems())
659
660    dispatch[DictionaryType] = save_dict
661    if not PyStringMap is None:
662        dispatch[PyStringMap] = save_dict
663
664    def _batch_setitems(self, items):
665        # Helper to batch up SETITEMS sequences; proto >= 1 only
666        save = self.save
667        write = self.write
668
669        if not self.bin:
670            for k, v in items:
671                save(k)
672                save(v)
673                write(SETITEM)
674            return
675
676        r = xrange(self._BATCHSIZE)
677        while items is not None:
678            tmp = []
679            for i in r:
680                try:
681                    tmp.append(items.next())
682                except StopIteration:
683                    items = None
684                    break
685            n = len(tmp)
686            if n > 1:
687                write(MARK)
688                for k, v in tmp:
689                    save(k)
690                    save(v)
691                write(SETITEMS)
692            elif n:
693                k, v = tmp[0]
694                save(k)
695                save(v)
696                write(SETITEM)
697            # else tmp is empty, and we're done
698
699    def save_binary(self, obj, pack=struct.pack):
700        if self.proto >= 3:
701            n = len(obj)
702            if n < 256:
703                self.write(SHORT_BINBYTES + chr(n) + obj)
704            else:
705                self.write(BINBYTES + pack("<i", n) + obj)
706            self.memoize(obj)
707        else:
708            self.save_string(obj)
709    dispatch[BinaryType] = save_binary
710
711    def save_inst(self, obj):
712        cls = obj.__class__
713        if cls is BinaryType:
714            return self.save_binary(obj)
715
716        memo  = self.memo
717        write = self.write
718        save  = self.save
719
720        if hasattr(obj, '__getinitargs__'):
721            args = obj.__getinitargs__()
722            len(args) # XXX Assert it's a sequence
723            _keep_alive(args, memo)
724        else:
725            args = ()
726
727        write(MARK)
728
729        if self.bin:
730            save(cls)
731            for arg in args:
732                save(arg)
733            write(OBJ)
734        else:
735            for arg in args:
736                save(arg)
737            write(INST + cls.__module__ + '\n' + cls.__name__ + '\n')
738
739        self.memoize(obj)
740
741        try:
742            getstate = obj.__getstate__
743        except AttributeError:
744            stuff = obj.__dict__
745        else:
746            stuff = getstate()
747            _keep_alive(stuff, memo)
748        save(stuff)
749        write(BUILD)
750
751    dispatch[InstanceType] = save_inst
752
753    def save_global(self, obj, name=None, pack=struct.pack):
754        write = self.write
755        memo = self.memo
756
757        if name is None:
758            name = obj.__name__
759
760        module = getattr(obj, "__module__", None)
761        if module is None:
762            module = whichmodule(obj, name)
763
764        try:
765            __import__(module)
766            mod = sys.modules[module]
767            klass = getattr(mod, name)
768        except (ImportError, KeyError, AttributeError):
769            raise PicklingError(
770                "Can't pickle %r: it's not found as %s.%s" %
771                (obj, module, name))
772        else:
773            if klass is not obj:
774                raise PicklingError(
775                    "Can't pickle %r: it's not the same object as %s.%s" %
776                    (obj, module, name))
777
778        if self.proto >= 2:
779            code = _extension_registry.get((module, name))
780            if code:
781                assert code > 0
782                if code <= 0xff:
783                    write(EXT1 + chr(code))
784                elif code <= 0xffff:
785                    write("%c%c%c" % (EXT2, code&0xff, code>>8))
786                else:
787                    write(EXT4 + pack("<i", code))
788                return
789
790        write(GLOBAL + module + '\n' + name + '\n')
791        self.memoize(obj)
792
793    dispatch[ClassType] = save_global
794    dispatch[FunctionType] = save_global
795    dispatch[BuiltinFunctionType] = save_global
796    dispatch[TypeType] = save_global
797
798# Pickling helpers
799
800def _keep_alive(x, memo):
801    """Keeps a reference to the object x in the memo.
802
803    Because we remember objects by their id, we have
804    to assure that possibly temporary objects are kept
805    alive by referencing them.
806    We store a reference at the id of the memo, which should
807    normally not be used unless someone tries to deepcopy
808    the memo itself...
809    """
810    try:
811        memo[id(memo)].append(x)
812    except KeyError:
813        # aha, this is the first one :-)
814        memo[id(memo)]=[x]
815
816
817# A cache for whichmodule(), mapping a function object to the name of
818# the module in which the function was found.
819
820classmap = {} # called classmap for backwards compatibility
821
822def whichmodule(func, funcname):
823    """Figure out the module in which a function occurs.
824
825    Search sys.modules for the module.
826    Cache in classmap.
827    Return a module name.
828    If the function cannot be found, return "__main__".
829    """
830    # Python functions should always get an __module__ from their globals.
831    mod = getattr(func, "__module__", None)
832    if mod is not None:
833        return mod
834    if func in classmap:
835        return classmap[func]
836
837    for name, module in sys.modules.items():
838        if module is None:
839            continue # skip dummy package entries
840        if name != '__main__' and getattr(module, funcname, None) is func:
841            break
842    else:
843        name = '__main__'
844    classmap[func] = name
845    return name
846
847
848# Unpickling machinery
849
850class Unpickler:
851
852    def __init__(self, file):
853        """This takes a file-like object for reading a pickle data stream.
854
855        The protocol version of the pickle is detected automatically, so no
856        proto argument is needed.
857
858        The file-like object must have two methods, a read() method that
859        takes an integer argument, and a readline() method that requires no
860        arguments.  Both methods should return a string.  Thus file-like
861        object can be a file object opened for reading, a StringIO object,
862        or any other custom object that meets this interface.
863        """
864        self.readline = file.readline
865        self.read = file.read
866        self.memo = {}
867
868    def load(self):
869        """Read a pickled object representation from the open file.
870
871        Return the reconstituted object hierarchy specified in the file.
872        """
873        self.mark = object() # any new unique object
874        self.stack = []
875        self.append = self.stack.append
876        read = self.read
877        dispatch = self.dispatch
878        try:
879            while 1:
880                key = read(1)
881                dispatch[key](self)
882        except _Stop, stopinst:
883            return stopinst.value
884
885    def noload(self):
886        """Read a pickled object representation from the open file.
887
888        If the object was an intrinsic type such as a literal list, dict
889        or tuple, return it. Otherwise (if the object was an instance),
890        return nothing useful.
891        """
892        self.mark = object() # any new unique object
893        self.stack = []
894        self.append = self.stack.append
895        read = self.read
896        dispatch = self.nl_dispatch
897        try:
898            while 1:
899                key = read(1)
900                dispatch[key](self)
901        except _Stop, stopinst:
902            return stopinst.value
903
904    # Return largest index k such that self.stack[k] is self.mark.
905    # If the stack doesn't contain a mark, eventually raises IndexError.
906    # This could be sped by maintaining another stack, of indices at which
907    # the mark appears.  For that matter, the latter stack would suffice,
908    # and we wouldn't need to push mark objects on self.stack at all.
909    # Doing so is probably a good thing, though, since if the pickle is
910    # corrupt (or hostile) we may get a clue from finding self.mark embedded
911    # in unpickled objects.
912    def marker(self):
913        stack = self.stack
914        mark = self.mark
915        k = len(stack)-1
916        while stack[k] is not mark: k = k-1
917        return k
918
919    dispatch = {}
920
921    def load_eof(self):
922        raise EOFError
923    dispatch[''] = load_eof
924
925    def load_proto(self):
926        proto = ord(self.read(1))
927        if not 0 <= proto <= HIGHEST_PROTOCOL:
928            raise ValueError, "unsupported pickle protocol: %d" % proto
929    dispatch[PROTO] = load_proto
930
931    def load_persid(self):
932        pid = self.readline()[:-1]
933        self.append(self.persistent_load(pid))
934    dispatch[PERSID] = load_persid
935
936    def load_binpersid(self):
937        pid = self.stack.pop()
938        self.append(self.persistent_load(pid))
939    dispatch[BINPERSID] = load_binpersid
940
941    def load_none(self):
942        self.append(None)
943    dispatch[NONE] = load_none
944
945    def load_false(self):
946        self.append(False)
947    dispatch[NEWFALSE] = load_false
948
949    def load_true(self):
950        self.append(True)
951    dispatch[NEWTRUE] = load_true
952
953    def load_int(self):
954        data = self.readline()
955        if data == FALSE[1:]:
956            val = False
957        elif data == TRUE[1:]:
958            val = True
959        else:
960            try:
961                val = int(data)
962            except ValueError:
963                val = long(data)
964        self.append(val)
965    dispatch[INT] = load_int
966
967    def load_binint(self):
968        self.append(mloads('i' + self.read(4)))
969    dispatch[BININT] = load_binint
970
971    def load_binint1(self):
972        self.append(ord(self.read(1)))
973    dispatch[BININT1] = load_binint1
974
975    def load_binint2(self):
976        self.append(mloads('i' + self.read(2) + '\000\000'))
977    dispatch[BININT2] = load_binint2
978
979    def load_long(self):
980        self.append(long(self.readline()[:-1], 0))
981    dispatch[LONG] = load_long
982
983    def load_long1(self):
984        n = ord(self.read(1))
985        bytes = self.read(n)
986        self.append(decode_long(bytes))
987    dispatch[LONG1] = load_long1
988
989    def load_long4(self):
990        n = mloads('i' + self.read(4))
991        bytes = self.read(n)
992        self.append(decode_long(bytes))
993    dispatch[LONG4] = load_long4
994
995    def load_float(self):
996        self.append(float(self.readline()[:-1]))
997    dispatch[FLOAT] = load_float
998
999    def load_binfloat(self, unpack=struct.unpack):
1000        self.append(unpack('>d', self.read(8))[0])
1001    dispatch[BINFLOAT] = load_binfloat
1002
1003    def load_string(self):
1004        rep = self.readline()[:-1]
1005        for q in "\"'": # double or single quote
1006            if rep.startswith(q):
1007                if len(rep) < 2 or not rep.endswith(q):
1008                    raise ValueError, "insecure string pickle"
1009                rep = rep[len(q):-len(q)]
1010                break
1011        else:
1012            raise ValueError, "insecure string pickle"
1013        self.append(rep.decode("string-escape"))
1014    dispatch[STRING] = load_string
1015
1016    def load_binstring(self):
1017        len = mloads('i' + self.read(4))
1018        self.append(self.read(len))
1019    dispatch[BINSTRING] = load_binstring
1020
1021    def load_binbytes(self):
1022        len = mloads('i' + self.read(4))
1023        self.append(BinaryType(self.read(len)))
1024    dispatch[BINBYTES] = load_binbytes
1025
1026    def load_unicode(self):
1027        self.append(unicode(self.readline()[:-1],'raw-unicode-escape'))
1028    dispatch[UNICODE] = load_unicode
1029
1030    def load_binunicode(self):
1031        len = mloads('i' + self.read(4))
1032        self.append(unicode(self.read(len),'utf-8'))
1033    dispatch[BINUNICODE] = load_binunicode
1034
1035    def load_short_binstring(self):
1036        len = ord(self.read(1))
1037        self.append(self.read(len))
1038    dispatch[SHORT_BINSTRING] = load_short_binstring
1039
1040    def load_short_binbytes(self):
1041        len = ord(self.read(1))
1042        self.append(BinaryType(self.read(len)))
1043    dispatch[SHORT_BINBYTES] = load_short_binbytes
1044
1045    def load_tuple(self):
1046        k = self.marker()
1047        self.stack[k:] = [tuple(self.stack[k+1:])]
1048    dispatch[TUPLE] = load_tuple
1049
1050    def load_empty_tuple(self):
1051        self.stack.append(())
1052    dispatch[EMPTY_TUPLE] = load_empty_tuple
1053
1054    def load_tuple1(self):
1055        self.stack[-1] = (self.stack[-1],)
1056    dispatch[TUPLE1] = load_tuple1
1057
1058    def load_tuple2(self):
1059        self.stack[-2:] = [(self.stack[-2], self.stack[-1])]
1060    dispatch[TUPLE2] = load_tuple2
1061
1062    def load_tuple3(self):
1063        self.stack[-3:] = [(self.stack[-3], self.stack[-2], self.stack[-1])]
1064    dispatch[TUPLE3] = load_tuple3
1065
1066    def load_empty_list(self):
1067        self.stack.append([])
1068    dispatch[EMPTY_LIST] = load_empty_list
1069
1070    def load_empty_dictionary(self):
1071        self.stack.append({})
1072    dispatch[EMPTY_DICT] = load_empty_dictionary
1073
1074    def load_list(self):
1075        k = self.marker()
1076        self.stack[k:] = [self.stack[k+1:]]
1077    dispatch[LIST] = load_list
1078
1079    def load_dict(self):
1080        k = self.marker()
1081        d = {}
1082        items = self.stack[k+1:]
1083        for i in range(0, len(items), 2):
1084            key = items[i]
1085            value = items[i+1]
1086            d[key] = value
1087        self.stack[k:] = [d]
1088    dispatch[DICT] = load_dict
1089
1090    # INST and OBJ differ only in how they get a class object.  It's not
1091    # only sensible to do the rest in a common routine, the two routines
1092    # previously diverged and grew different bugs.
1093    # klass is the class to instantiate, and k points to the topmost mark
1094    # object, following which are the arguments for klass.__init__.
1095    def _instantiate(self, klass, k):
1096        args = tuple(self.stack[k+1:])
1097        del self.stack[k:]
1098        instantiated = 0
1099        if (not args and
1100                type(klass) is ClassType and
1101                not hasattr(klass, "__getinitargs__")):
1102            try:
1103                value = _EmptyClass()
1104                value.__class__ = klass
1105                instantiated = 1
1106            except RuntimeError:
1107                # In restricted execution, assignment to inst.__class__ is
1108                # prohibited
1109                pass
1110        if not instantiated:
1111            try:
1112                value = klass(*args)
1113            except TypeError, err:
1114                raise TypeError, "in constructor for %s: %s" % (
1115                    klass.__name__, str(err)), sys.exc_info()[2]
1116        self.append(value)
1117
1118    def load_inst(self):
1119        module = self.readline()[:-1]
1120        name = self.readline()[:-1]
1121        klass = self.find_class(module, name)
1122        self._instantiate(klass, self.marker())
1123    dispatch[INST] = load_inst
1124
1125    def load_obj(self):
1126        # Stack is ... markobject classobject arg1 arg2 ...
1127        k = self.marker()
1128        klass = self.stack.pop(k+1)
1129        self._instantiate(klass, k)
1130    dispatch[OBJ] = load_obj
1131
1132    def load_newobj(self):
1133        args = self.stack.pop()
1134        cls = self.stack[-1]
1135        obj = cls.__new__(cls, *args)
1136        self.stack[-1] = obj
1137    dispatch[NEWOBJ] = load_newobj
1138
1139    def load_global(self):
1140        module = self.readline()[:-1]
1141        name = self.readline()[:-1]
1142        klass = self.find_class(module, name)
1143        self.append(klass)
1144    dispatch[GLOBAL] = load_global
1145
1146    def load_ext1(self):
1147        code = ord(self.read(1))
1148        self.get_extension(code)
1149    dispatch[EXT1] = load_ext1
1150
1151    def load_ext2(self):
1152        code = mloads('i' + self.read(2) + '\000\000')
1153        self.get_extension(code)
1154    dispatch[EXT2] = load_ext2
1155
1156    def load_ext4(self):
1157        code = mloads('i' + self.read(4))
1158        self.get_extension(code)
1159    dispatch[EXT4] = load_ext4
1160
1161    def get_extension(self, code):
1162        nil = []
1163        obj = _extension_cache.get(code, nil)
1164        if obj is not nil:
1165            self.append(obj)
1166            return
1167        key = _inverted_registry.get(code)
1168        if not key:
1169            raise ValueError("unregistered extension code %d" % code)
1170        obj = self.find_class(*key)
1171        _extension_cache[code] = obj
1172        self.append(obj)
1173
1174    def find_class(self, module, name):
1175        # Subclasses may override this
1176        __import__(module)
1177        mod = sys.modules[module]
1178        klass = getattr(mod, name)
1179        return klass
1180
1181    def load_reduce(self):
1182        stack = self.stack
1183        args = stack.pop()
1184        func = stack[-1]
1185        value = func(*args)
1186        stack[-1] = value
1187    dispatch[REDUCE] = load_reduce
1188
1189    def load_pop(self):
1190        del self.stack[-1]
1191    dispatch[POP] = load_pop
1192
1193    def load_pop_mark(self):
1194        k = self.marker()
1195        del self.stack[k:]
1196    dispatch[POP_MARK] = load_pop_mark
1197
1198    def load_dup(self):
1199        self.append(self.stack[-1])
1200    dispatch[DUP] = load_dup
1201
1202    def load_get(self):
1203        self.append(self.memo[self.readline()[:-1]])
1204    dispatch[GET] = load_get
1205
1206    def load_binget(self):
1207        i = ord(self.read(1))
1208        self.append(self.memo[repr(i)])
1209    dispatch[BINGET] = load_binget
1210
1211    def load_long_binget(self):
1212        i = mloads('i' + self.read(4))
1213        self.append(self.memo[repr(i)])
1214    dispatch[LONG_BINGET] = load_long_binget
1215
1216    def load_put(self):
1217        self.memo[self.readline()[:-1]] = self.stack[-1]
1218    dispatch[PUT] = load_put
1219
1220    def load_binput(self):
1221        i = ord(self.read(1))
1222        self.memo[repr(i)] = self.stack[-1]
1223    dispatch[BINPUT] = load_binput
1224
1225    def load_long_binput(self):
1226        i = mloads('i' + self.read(4))
1227        self.memo[repr(i)] = self.stack[-1]
1228    dispatch[LONG_BINPUT] = load_long_binput
1229
1230    def load_append(self):
1231        stack = self.stack
1232        value = stack.pop()
1233        list = stack[-1]
1234        list.append(value)
1235    dispatch[APPEND] = load_append
1236
1237    def load_appends(self):
1238        stack = self.stack
1239        mark = self.marker()
1240        list = stack[mark - 1]
1241        list.extend(stack[mark + 1:])
1242        del stack[mark:]
1243    dispatch[APPENDS] = load_appends
1244
1245    def load_setitem(self):
1246        stack = self.stack
1247        value = stack.pop()
1248        key = stack.pop()
1249        dict = stack[-1]
1250        dict[key] = value
1251    dispatch[SETITEM] = load_setitem
1252
1253    def load_setitems(self):
1254        stack = self.stack
1255        mark = self.marker()
1256        dict = stack[mark - 1]
1257        for i in range(mark + 1, len(stack), 2):
1258            dict[stack[i]] = stack[i + 1]
1259
1260        del stack[mark:]
1261    dispatch[SETITEMS] = load_setitems
1262
1263    def load_build(self):
1264        stack = self.stack
1265        state = stack.pop()
1266        inst = stack[-1]
1267        setstate = getattr(inst, "__setstate__", None)
1268        if setstate:
1269            setstate(state)
1270            return
1271        slotstate = None
1272        if isinstance(state, tuple) and len(state) == 2:
1273            state, slotstate = state
1274        if state:
1275            try:
1276                d = inst.__dict__
1277                try:
1278                    for k, v in state.iteritems():
1279                        d[intern(k)] = v
1280                # keys in state don't have to be strings
1281                # don't blow up, but don't go out of our way
1282                except TypeError:
1283                    d.update(state)
1284
1285            except RuntimeError:
1286                # XXX In restricted execution, the instance's __dict__
1287                # is not accessible.  Use the old way of unpickling
1288                # the instance variables.  This is a semantic
1289                # difference when unpickling in restricted
1290                # vs. unrestricted modes.
1291                # Note, however, that cPickle has never tried to do the
1292                # .update() business, and always uses
1293                #     PyObject_SetItem(inst.__dict__, key, value) in a
1294                # loop over state.items().
1295                for k, v in state.items():
1296                    setattr(inst, k, v)
1297        if slotstate:
1298            for k, v in slotstate.items():
1299                setattr(inst, k, v)
1300    dispatch[BUILD] = load_build
1301
1302    def load_mark(self):
1303        self.append(self.mark)
1304    dispatch[MARK] = load_mark
1305
1306    def load_stop(self):
1307        value = self.stack.pop()
1308        raise _Stop(value)
1309    dispatch[STOP] = load_stop
1310
1311    nl_dispatch = dispatch.copy()
1312
1313    def noload_obj(self):
1314        # Stack is ... markobject classobject arg1 arg2 ...
1315        k = self.marker()
1316        klass = self.stack.pop(k+1)
1317    nl_dispatch[OBJ[0]] = noload_obj
1318
1319    def noload_inst(self):
1320        self.readline() # skip module
1321        self.readline()[:-1] # skip name
1322        k = self.marker()
1323        klass = self.stack.pop(k+1)
1324        self.append(None)
1325    nl_dispatch[INST[0]] = noload_inst
1326
1327    def noload_newobj(self):
1328        self.stack.pop() # skip args
1329        self.stack.pop() # skip cls
1330        self.stack.append(None)
1331    nl_dispatch[NEWOBJ[0]] = noload_newobj
1332
1333    def noload_global(self):
1334        self.readline() # skip module
1335        self.readline()[:-1] # skip name
1336        self.append(None)
1337    nl_dispatch[GLOBAL[0]] = noload_global
1338
1339    def noload_append(self):
1340        if self.stack[-2] is not None:
1341            self.load_append()
1342        else:
1343            self.stack.pop()
1344    nl_dispatch[APPEND[0]] = noload_append
1345
1346    def noload_appends(self):
1347        stack = self.stack
1348        mark = self.marker()
1349        list = stack[mark - 1]
1350        if list is not None:
1351            list.extend(stack[mark + 1:])
1352        del self.stack[mark:]
1353    nl_dispatch[APPENDS[0]] = noload_appends
1354
1355    def noload_setitem(self):
1356        if self.stack[-3] is not None:
1357            self.load_setitem()
1358        else:
1359            self.stack.pop() # skip value
1360            self.stack.pop() # skip key
1361    nl_dispatch[SETITEM[0]] = noload_setitem
1362
1363    def noload_setitems(self):
1364        stack = self.stack
1365        mark = self.marker()
1366        dict = stack[mark - 1]
1367        if dict is not None:
1368            for i in range(mark + 1, len(stack), 2):
1369                dict[stack[i]] = stack[i + 1]
1370
1371        del stack[mark:]
1372    nl_dispatch[SETITEMS[0]] = noload_setitems
1373
1374    def noload_reduce(self):
1375        self.stack.pop() # skip args
1376        self.stack.pop() # skip func
1377        self.stack.append(None)
1378    nl_dispatch[REDUCE[0]] = noload_reduce
1379
1380    def noload_build(self):
1381        state = self.stack.pop()
1382    nl_dispatch[BUILD[0]] = noload_build
1383
1384    def noload_ext1(self):
1385        code = ord(self.read(1))
1386        self.get_extension(code)
1387        self.stack.pop()
1388        self.stack.append(None)
1389    nl_dispatch[EXT1[0]] = noload_ext1
1390
1391    def noload_ext2(self):
1392        code = mloads(b'i' + self.read(2) + b'\000\000')
1393        self.get_extension(code)
1394        self.stack.pop()
1395        self.stack.append(None)
1396    nl_dispatch[EXT2[0]] = noload_ext2
1397
1398    def noload_ext4(self):
1399        code = mloads(b'i' + self.read(4))
1400        self.get_extension(code)
1401        self.stack.pop()
1402        self.stack.append(None)
1403    nl_dispatch[EXT4[0]] = noload_ext4
1404
1405
1406# Helper class for load_inst/load_obj
1407
1408class _EmptyClass:
1409    pass
1410
1411# Encode/decode longs in linear time.
1412
1413import binascii as _binascii
1414
1415def encode_long(x):
1416    r"""Encode a long to a two's complement little-endian binary string.
1417    Note that 0L is a special case, returning an empty string, to save a
1418    byte in the LONG1 pickling context.
1419
1420    >>> encode_long(0L)
1421    ''
1422    >>> encode_long(255L)
1423    '\xff\x00'
1424    >>> encode_long(32767L)
1425    '\xff\x7f'
1426    >>> encode_long(-256L)
1427    '\x00\xff'
1428    >>> encode_long(-32768L)
1429    '\x00\x80'
1430    >>> encode_long(-128L)
1431    '\x80'
1432    >>> encode_long(127L)
1433    '\x7f'
1434    >>>
1435    """
1436
1437    if x == 0:
1438        return ''
1439    if x > 0:
1440        ashex = hex(x)
1441        assert ashex.startswith("0x")
1442        njunkchars = 2 + ashex.endswith('L')
1443        nibbles = len(ashex) - njunkchars
1444        if nibbles & 1:
1445            # need an even # of nibbles for unhexlify
1446            ashex = "0x0" + ashex[2:]
1447        elif int(ashex[2], 16) >= 8:
1448            # "looks negative", so need a byte of sign bits
1449            ashex = "0x00" + ashex[2:]
1450    else:
1451        # Build the 256's-complement:  (1L << nbytes) + x.  The trick is
1452        # to find the number of bytes in linear time (although that should
1453        # really be a constant-time task).
1454        ashex = hex(-x)
1455        assert ashex.startswith("0x")
1456        njunkchars = 2 + ashex.endswith('L')
1457        nibbles = len(ashex) - njunkchars
1458        if nibbles & 1:
1459            # Extend to a full byte.
1460            nibbles += 1
1461        nbits = nibbles * 4
1462        x += 1L << nbits
1463        assert x > 0
1464        ashex = hex(x)
1465        njunkchars = 2 + ashex.endswith('L')
1466        newnibbles = len(ashex) - njunkchars
1467        if newnibbles < nibbles:
1468            ashex = "0x" + "0" * (nibbles - newnibbles) + ashex[2:]
1469        if int(ashex[2], 16) < 8:
1470            # "looks positive", so need a byte of sign bits
1471            ashex = "0xff" + ashex[2:]
1472
1473    if ashex.endswith('L'):
1474        ashex = ashex[2:-1]
1475    else:
1476        ashex = ashex[2:]
1477    assert len(ashex) & 1 == 0, (x, ashex)
1478    binary = _binascii.unhexlify(ashex)
1479    return binary[::-1]
1480
1481def decode_long(data):
1482    r"""Decode a long from a two's complement little-endian binary string.
1483
1484    >>> decode_long('')
1485    0L
1486    >>> decode_long("\xff\x00")
1487    255L
1488    >>> decode_long("\xff\x7f")
1489    32767L
1490    >>> decode_long("\x00\xff")
1491    -256L
1492    >>> decode_long("\x00\x80")
1493    -32768L
1494    >>> decode_long("\x80")
1495    -128L
1496    >>> decode_long("\x7f")
1497    127L
1498    """
1499
1500    nbytes = len(data)
1501    if nbytes == 0:
1502        return 0L
1503    ashex = _binascii.hexlify(data[::-1])
1504    n = long(ashex, 16) # quadratic time before Python 2.3; linear now
1505    if data[-1] >= '\x80':
1506        n -= 1L << (nbytes * 8)
1507    return n
1508
1509# Shorthands
1510
1511try:
1512    from cStringIO import StringIO
1513except ImportError:
1514    from StringIO import StringIO
1515
1516def dump(obj, file, protocol=None):
1517    Pickler(file, protocol).dump(obj)
1518
1519def dumps(obj, protocol=None):
1520    file = StringIO()
1521    Pickler(file, protocol).dump(obj)
1522    return file.getvalue()
1523
1524def load(file):
1525    return Unpickler(file).load()
1526
1527def loads(str):
1528    file = StringIO(str)
1529    return Unpickler(file).load()
1530
1531# Doctest
1532
1533def _test():
1534    import doctest
1535    return doctest.testmod()
1536
1537if __name__ == "__main__":
1538    _test()
1539