~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/modified-2.4.1/pickle.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

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