home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / pickle.py < prev    next >
Encoding:
Python Source  |  2003-04-22  |  27.2 KB  |  982 lines

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