Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Create portable serialized representations of Python objects. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 3 | See module cPickle for a (much) faster implementation. |
| 4 | See module copy_reg for a mechanism for registering custom picklers. |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame^] | 5 | See module pickletools source for extensive comments. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 7 | Classes: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 9 | Pickler |
| 10 | Unpickler |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 12 | Functions: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 14 | dump(object, file) |
| 15 | dumps(object) -> string |
| 16 | load(file) -> object |
| 17 | loads(string) -> object |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 19 | Misc variables: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 20 | |
Fred Drake | fe82acc | 1998-02-13 03:24:48 +0000 | [diff] [blame] | 21 | __version__ |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 22 | format_version |
| 23 | compatible_formats |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 25 | """ |
| 26 | |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 27 | __version__ = "$Revision$" # Code version |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 28 | |
| 29 | from types import * |
Guido van Rossum | 4fb5b28 | 1997-09-12 20:07:24 +0000 | [diff] [blame] | 30 | from copy_reg import dispatch_table, safe_constructors |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 31 | import marshal |
| 32 | import sys |
| 33 | import struct |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 34 | import re |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 35 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 36 | __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", |
| 37 | "Unpickler", "dump", "dumps", "load", "loads"] |
| 38 | |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 39 | format_version = "1.3" # File format version we write |
| 40 | compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 41 | |
| 42 | mdumps = marshal.dumps |
| 43 | mloads = marshal.loads |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 45 | class PickleError(Exception): |
Neal Norwitz | efbb67b | 2002-05-30 12:12:04 +0000 | [diff] [blame] | 46 | """A common base class for the other pickling exceptions.""" |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 47 | pass |
| 48 | |
| 49 | class PicklingError(PickleError): |
| 50 | """This exception is raised when an unpicklable object is passed to the |
| 51 | dump() method. |
| 52 | |
| 53 | """ |
| 54 | pass |
| 55 | |
| 56 | class UnpicklingError(PickleError): |
| 57 | """This exception is raised when there is a problem unpickling an object, |
| 58 | such as a security violation. |
| 59 | |
| 60 | Note that other exceptions may also be raised during unpickling, including |
| 61 | (but not necessarily limited to) AttributeError, EOFError, ImportError, |
| 62 | and IndexError. |
| 63 | |
| 64 | """ |
| 65 | pass |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 67 | class _Stop(Exception): |
| 68 | def __init__(self, value): |
| 69 | self.value = value |
| 70 | |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 71 | try: |
| 72 | from org.python.core import PyStringMap |
| 73 | except ImportError: |
| 74 | PyStringMap = None |
| 75 | |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 76 | try: |
| 77 | UnicodeType |
| 78 | except NameError: |
| 79 | UnicodeType = None |
| 80 | |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame^] | 81 | # Pickle opcodes. See pickletools.py for extensive docs. The listing |
| 82 | # here is in kind-of alphabetical order of 1-character pickle code. |
| 83 | # pickletools groups them by purpose. |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 84 | |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame^] | 85 | MARK = '(' # push special markobject on stack |
| 86 | STOP = '.' # every pickle ends with STOP |
| 87 | POP = '0' # discard topmost stack item |
| 88 | POP_MARK = '1' # discard stack top through topmost markobject |
| 89 | DUP = '2' # duplicate top stack item |
| 90 | FLOAT = 'F' # push float object; decimal string argument |
| 91 | INT = 'I' # push integer or bool; decimal string argument |
| 92 | BININT = 'J' # push four-byte signed int |
| 93 | BININT1 = 'K' # push 1-byte unsigned int |
| 94 | LONG = 'L' # push long; decimal string argument |
| 95 | BININT2 = 'M' # push 2-byte unsigned int |
| 96 | NONE = 'N' # push None |
| 97 | PERSID = 'P' # push persistent object; id is taken from string arg |
| 98 | BINPERSID = 'Q' # " " " ; " " " " stack |
| 99 | REDUCE = 'R' # apply callable to argtuple, both on stack |
| 100 | STRING = 'S' # push string; NL-terminated string argument |
| 101 | BINSTRING = 'T' # push string; counted binary string argument |
| 102 | SHORT_BINSTRING = 'U' # " " ; " " " " < 256 bytes |
| 103 | UNICODE = 'V' # push Unicode string; raw-unicode-escaped'd argument |
| 104 | BINUNICODE = 'X' # " " " ; counted UTF-8 string argument |
| 105 | APPEND = 'a' # append stack top to list below it |
| 106 | BUILD = 'b' # call __setstate__ or __dict__.update() |
| 107 | GLOBAL = 'c' # push self.find_class(modname, name); 2 string args |
| 108 | DICT = 'd' # build a dict from stack items |
| 109 | EMPTY_DICT = '}' # push empty dict |
| 110 | APPENDS = 'e' # extend list on stack by topmost stack slice |
| 111 | GET = 'g' # push item from memo on stack; index is string arg |
| 112 | BINGET = 'h' # " " " " " " ; " " 1-byte arg |
| 113 | INST = 'i' # build & push class instance |
| 114 | LONG_BINGET = 'j' # push item from memo on stack; index is 4-byte arg |
| 115 | LIST = 'l' # build list from topmost stack items |
| 116 | EMPTY_LIST = ']' # push empty list |
| 117 | OBJ = 'o' # build & push class instance |
| 118 | PUT = 'p' # store stack top in memo; index is string arg |
| 119 | BINPUT = 'q' # " " " " " ; " " 1-byte arg |
| 120 | LONG_BINPUT = 'r' # " " " " " ; " " 4-byte arg |
| 121 | SETITEM = 's' # add key+value pair to dict |
| 122 | TUPLE = 't' # build tuple from topmost stack items |
| 123 | EMPTY_TUPLE = ')' # push empty tuple |
| 124 | SETITEMS = 'u' # modify dict by adding topmost key+value pairs |
| 125 | BINFLOAT = 'G' # push float; arg is 8-byte float encoding |
| 126 | |
| 127 | TRUE = 'I01\n' # not an opcode; see INT docs in pickletools.py |
| 128 | FALSE = 'I00\n' # not an opcode; see INT docs in pickletools.py |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 130 | |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 131 | __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)]) |
Neal Norwitz | d5ba4ae | 2002-02-11 18:12:06 +0000 | [diff] [blame] | 132 | del x |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 133 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 134 | _quotes = ["'", '"'] |
| 135 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 136 | class Pickler: |
| 137 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 138 | def __init__(self, file, bin = 0): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 139 | """This takes a file-like object for writing a pickle data stream. |
| 140 | |
| 141 | The optional bin parameter if true, tells the pickler to use the more |
| 142 | efficient binary pickle format, otherwise the ASCII format is used |
| 143 | (this is the default). |
| 144 | |
| 145 | The file parameter must have a write() method that accepts a single |
| 146 | string argument. It can thus be an open file object, a StringIO |
| 147 | object, or any other custom object that meets this interface. |
| 148 | |
| 149 | """ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 150 | self.write = file.write |
| 151 | self.memo = {} |
| 152 | self.bin = bin |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 153 | |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 154 | def clear_memo(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 155 | """Clears the pickler's "memo". |
| 156 | |
| 157 | The memo is the data structure that remembers which objects the |
| 158 | pickler has already seen, so that shared or recursive objects pickled |
| 159 | by reference and not by value. This method is useful when re-using |
| 160 | picklers. |
| 161 | |
| 162 | """ |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 163 | self.memo.clear() |
| 164 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 165 | def dump(self, object): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 166 | """Write a pickled representation of object to the open file object. |
| 167 | |
| 168 | Either the binary or ASCII format will be used, depending on the |
| 169 | value of the bin flag passed to the constructor. |
| 170 | |
| 171 | """ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 172 | self.save(object) |
| 173 | self.write(STOP) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 174 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 175 | def memoize(self, obj): |
| 176 | """Store an object in the memo.""" |
| 177 | |
| 178 | # The memo is a dictionary mapping object ids to 2-tuples |
| 179 | # that contains the memo value and the object being memoized. |
| 180 | # The memo value is written to the pickle and will become |
| 181 | # the key in the Unpickler's memo. The object is stored in the |
| 182 | # memo so that transient objects are kept alive during pickling. |
| 183 | |
| 184 | # The use of the memo length as the memo value is just a convention. |
| 185 | # The only requirement is that the memo values by unique. |
| 186 | d = id(obj) |
| 187 | memo_len = len(self.memo) |
| 188 | self.write(self.put(memo_len)) |
| 189 | self.memo[d] = memo_len, obj |
| 190 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 191 | def put(self, i): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 192 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 193 | s = mdumps(i)[1:] |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 194 | if i < 256: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 195 | return BINPUT + s[0] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 197 | return LONG_BINPUT + s |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 199 | return PUT + `i` + '\n' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 201 | def get(self, i): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 202 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 203 | s = mdumps(i)[1:] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 204 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 205 | if i < 256: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 206 | return BINGET + s[0] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 208 | return LONG_BINGET + s |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 210 | return GET + `i` + '\n' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 212 | def save(self, object): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 213 | memo = self.memo |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 214 | |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 215 | pid = self.persistent_id(object) |
| 216 | if pid is not None: |
| 217 | self.save_pers(pid) |
| 218 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 220 | d = id(object) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 222 | t = type(object) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 223 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 224 | if (t is TupleType) and (len(object) == 0): |
| 225 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 226 | self.save_empty_tuple(object) |
| 227 | else: |
| 228 | self.save_tuple(object) |
| 229 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 230 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 231 | if d in memo: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 232 | self.write(self.get(memo[d][0])) |
| 233 | return |
| 234 | |
| 235 | try: |
| 236 | f = self.dispatch[t] |
| 237 | except KeyError: |
Guido van Rossum | 85ee491 | 2002-03-26 00:51:56 +0000 | [diff] [blame] | 238 | try: |
| 239 | issc = issubclass(t, TypeType) |
| 240 | except TypeError: # t is not a class |
| 241 | issc = 0 |
| 242 | if issc: |
Guido van Rossum | f048a8f | 2001-12-19 16:55:02 +0000 | [diff] [blame] | 243 | self.save_global(object) |
| 244 | return |
| 245 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 246 | try: |
| 247 | reduce = dispatch_table[t] |
| 248 | except KeyError: |
| 249 | try: |
| 250 | reduce = object.__reduce__ |
| 251 | except AttributeError: |
| 252 | raise PicklingError, \ |
Guido van Rossum | 08a92cb | 1999-10-10 21:14:25 +0000 | [diff] [blame] | 253 | "can't pickle %s object: %s" % (`t.__name__`, |
| 254 | `object`) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 255 | else: |
| 256 | tup = reduce() |
| 257 | else: |
| 258 | tup = reduce(object) |
| 259 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 260 | if type(tup) is StringType: |
| 261 | self.save_global(object, tup) |
| 262 | return |
Guido van Rossum | d1f4984 | 1997-12-10 23:40:18 +0000 | [diff] [blame] | 263 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 264 | if type(tup) is not TupleType: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 265 | raise PicklingError, "Value returned by %s must be a " \ |
| 266 | "tuple" % reduce |
| 267 | |
| 268 | l = len(tup) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 269 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 270 | if (l != 2) and (l != 3): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 271 | raise PicklingError, "tuple returned by %s must contain " \ |
| 272 | "only two or three elements" % reduce |
| 273 | |
| 274 | callable = tup[0] |
| 275 | arg_tup = tup[1] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 276 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 277 | if l > 2: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 278 | state = tup[2] |
| 279 | else: |
| 280 | state = None |
| 281 | |
Guido van Rossum | d1f4984 | 1997-12-10 23:40:18 +0000 | [diff] [blame] | 282 | if type(arg_tup) is not TupleType and arg_tup is not None: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 283 | raise PicklingError, "Second element of tuple returned " \ |
| 284 | "by %s must be a tuple" % reduce |
| 285 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 286 | self.save_reduce(callable, arg_tup, state) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 287 | memo_len = len(memo) |
| 288 | self.write(self.put(memo_len)) |
| 289 | memo[d] = (memo_len, object) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 290 | return |
| 291 | |
| 292 | f(self, object) |
| 293 | |
| 294 | def persistent_id(self, object): |
| 295 | return None |
| 296 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 297 | def save_pers(self, pid): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 298 | if not self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 299 | self.write(PERSID + str(pid) + '\n') |
| 300 | else: |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 301 | self.save(pid) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 302 | self.write(BINPERSID) |
| 303 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 304 | def save_reduce(self, acallable, arg_tup, state = None): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 305 | write = self.write |
| 306 | save = self.save |
| 307 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 308 | if not callable(acallable): |
| 309 | raise PicklingError("__reduce__() must return callable as " |
| 310 | "first argument, not %s" % `acallable`) |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame^] | 311 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 312 | save(acallable) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 313 | save(arg_tup) |
| 314 | write(REDUCE) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 315 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 316 | if state is not None: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 317 | save(state) |
| 318 | write(BUILD) |
| 319 | |
| 320 | dispatch = {} |
| 321 | |
| 322 | def save_none(self, object): |
| 323 | self.write(NONE) |
| 324 | dispatch[NoneType] = save_none |
| 325 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 326 | def save_bool(self, object): |
| 327 | if object: |
| 328 | self.write(TRUE) |
| 329 | else: |
| 330 | self.write(FALSE) |
| 331 | dispatch[bool] = save_bool |
| 332 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 333 | def save_int(self, object): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 334 | if self.bin: |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 335 | # If the int is small enough to fit in a signed 4-byte 2's-comp |
| 336 | # format, we can store it more efficiently than the general |
| 337 | # case. |
| 338 | high_bits = object >> 31 # note that Python shift sign-extends |
| 339 | if high_bits == 0 or high_bits == -1: |
| 340 | # All high bits are copies of bit 2**31, so the value |
| 341 | # fits in a 4-byte signed int. |
| 342 | i = mdumps(object)[1:] |
| 343 | assert len(i) == 4 |
| 344 | if i[-2:] == '\000\000': # fits in 2-byte unsigned int |
| 345 | if i[-3] == '\000': # fits in 1-byte unsigned int |
| 346 | self.write(BININT1 + i[0]) |
| 347 | else: |
| 348 | self.write(BININT2 + i[:2]) |
| 349 | else: |
| 350 | self.write(BININT + i) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 351 | return |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 352 | # Text pickle, or int too big to fit in signed 4-byte format. |
| 353 | self.write(INT + `object` + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 354 | dispatch[IntType] = save_int |
| 355 | |
| 356 | def save_long(self, object): |
| 357 | self.write(LONG + `object` + '\n') |
| 358 | dispatch[LongType] = save_long |
| 359 | |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 360 | def save_float(self, object, pack=struct.pack): |
| 361 | if self.bin: |
| 362 | self.write(BINFLOAT + pack('>d', object)) |
| 363 | else: |
| 364 | self.write(FLOAT + `object` + '\n') |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 365 | dispatch[FloatType] = save_float |
| 366 | |
| 367 | def save_string(self, object): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 368 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 369 | l = len(object) |
| 370 | s = mdumps(l)[1:] |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 371 | if l < 256: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 372 | self.write(SHORT_BINSTRING + s[0] + object) |
| 373 | else: |
| 374 | self.write(BINSTRING + s + object) |
| 375 | else: |
| 376 | self.write(STRING + `object` + '\n') |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 377 | self.memoize(object) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 378 | dispatch[StringType] = save_string |
| 379 | |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 380 | def save_unicode(self, object): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 381 | if self.bin: |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 382 | encoding = object.encode('utf-8') |
| 383 | l = len(encoding) |
| 384 | s = mdumps(l)[1:] |
| 385 | self.write(BINUNICODE + s + encoding) |
| 386 | else: |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 387 | object = object.replace("\\", "\\u005c") |
| 388 | object = object.replace("\n", "\\u000a") |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 389 | self.write(UNICODE + object.encode('raw-unicode-escape') + '\n') |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 390 | self.memoize(object) |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 391 | dispatch[UnicodeType] = save_unicode |
| 392 | |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 393 | if StringType == UnicodeType: |
| 394 | # This is true for Jython |
| 395 | def save_string(self, object): |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 396 | unicode = object.isunicode() |
| 397 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 398 | if self.bin: |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 399 | if unicode: |
| 400 | object = object.encode("utf-8") |
| 401 | l = len(object) |
| 402 | s = mdumps(l)[1:] |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 403 | if l < 256 and not unicode: |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 404 | self.write(SHORT_BINSTRING + s[0] + object) |
| 405 | else: |
| 406 | if unicode: |
| 407 | self.write(BINUNICODE + s + object) |
| 408 | else: |
| 409 | self.write(BINSTRING + s + object) |
| 410 | else: |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 411 | if unicode: |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 412 | object = object.replace("\\", "\\u005c") |
| 413 | object = object.replace("\n", "\\u000a") |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 414 | object = object.encode('raw-unicode-escape') |
| 415 | self.write(UNICODE + object + '\n') |
| 416 | else: |
| 417 | self.write(STRING + `object` + '\n') |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 418 | self.memoize(object) |
Guido van Rossum | 31584cb | 2001-01-22 14:53:29 +0000 | [diff] [blame] | 419 | dispatch[StringType] = save_string |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 421 | def save_tuple(self, object): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 422 | write = self.write |
| 423 | save = self.save |
| 424 | memo = self.memo |
| 425 | |
| 426 | d = id(object) |
| 427 | |
| 428 | write(MARK) |
| 429 | |
| 430 | for element in object: |
| 431 | save(element) |
| 432 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 433 | if len(object) and d in memo: |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 434 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 435 | write(POP_MARK + self.get(memo[d][0])) |
| 436 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | 599174f | 1998-03-31 16:30:28 +0000 | [diff] [blame] | 438 | write(POP * (len(object) + 1) + self.get(memo[d][0])) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 439 | return |
| 440 | |
| 441 | memo_len = len(memo) |
| 442 | self.write(TUPLE + self.put(memo_len)) |
| 443 | memo[d] = (memo_len, object) |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 445 | dispatch[TupleType] = save_tuple |
| 446 | |
| 447 | def save_empty_tuple(self, object): |
| 448 | self.write(EMPTY_TUPLE) |
| 449 | |
| 450 | def save_list(self, object): |
| 451 | d = id(object) |
| 452 | |
| 453 | write = self.write |
| 454 | save = self.save |
| 455 | memo = self.memo |
| 456 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 457 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 458 | write(EMPTY_LIST) |
| 459 | else: |
| 460 | write(MARK + LIST) |
| 461 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 462 | self.memoize(object) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 463 | |
| 464 | using_appends = (self.bin and (len(object) > 1)) |
| 465 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 466 | if using_appends: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 467 | write(MARK) |
| 468 | |
| 469 | for element in object: |
| 470 | save(element) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 471 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 472 | if not using_appends: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 473 | write(APPEND) |
| 474 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 475 | if using_appends: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 476 | write(APPENDS) |
| 477 | dispatch[ListType] = save_list |
| 478 | |
| 479 | def save_dict(self, object): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 480 | write = self.write |
| 481 | save = self.save |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 482 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 483 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 484 | write(EMPTY_DICT) |
| 485 | else: |
| 486 | write(MARK + DICT) |
| 487 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 488 | self.memoize(object) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 489 | |
| 490 | using_setitems = (self.bin and (len(object) > 1)) |
| 491 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 492 | if using_setitems: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 493 | write(MARK) |
| 494 | |
| 495 | items = object.items() |
| 496 | for key, value in items: |
| 497 | save(key) |
| 498 | save(value) |
| 499 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 500 | if not using_setitems: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 501 | write(SETITEM) |
| 502 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 503 | if using_setitems: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 504 | write(SETITEMS) |
| 505 | |
| 506 | dispatch[DictionaryType] = save_dict |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 507 | if not PyStringMap is None: |
| 508 | dispatch[PyStringMap] = save_dict |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 509 | |
| 510 | def save_inst(self, object): |
| 511 | d = id(object) |
| 512 | cls = object.__class__ |
| 513 | |
| 514 | memo = self.memo |
| 515 | write = self.write |
| 516 | save = self.save |
| 517 | |
| 518 | if hasattr(object, '__getinitargs__'): |
| 519 | args = object.__getinitargs__() |
| 520 | len(args) # XXX Assert it's a sequence |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 521 | _keep_alive(args, memo) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 522 | else: |
| 523 | args = () |
| 524 | |
| 525 | write(MARK) |
| 526 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 527 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 528 | save(cls) |
| 529 | |
| 530 | for arg in args: |
| 531 | save(arg) |
| 532 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 533 | # This method does not use memoize() so that it can handle |
| 534 | # the special case for non-binary mode. |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 535 | memo_len = len(memo) |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 536 | if self.bin: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 537 | write(OBJ + self.put(memo_len)) |
| 538 | else: |
Guido van Rossum | 4fb5b28 | 1997-09-12 20:07:24 +0000 | [diff] [blame] | 539 | write(INST + cls.__module__ + '\n' + cls.__name__ + '\n' + |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 540 | self.put(memo_len)) |
| 541 | |
| 542 | memo[d] = (memo_len, object) |
| 543 | |
| 544 | try: |
| 545 | getstate = object.__getstate__ |
| 546 | except AttributeError: |
| 547 | stuff = object.__dict__ |
| 548 | else: |
| 549 | stuff = getstate() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 550 | _keep_alive(stuff, memo) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 551 | save(stuff) |
| 552 | write(BUILD) |
| 553 | dispatch[InstanceType] = save_inst |
| 554 | |
| 555 | def save_global(self, object, name = None): |
| 556 | write = self.write |
| 557 | memo = self.memo |
| 558 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 559 | if name is None: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 560 | name = object.__name__ |
| 561 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 562 | try: |
| 563 | module = object.__module__ |
| 564 | except AttributeError: |
| 565 | module = whichmodule(object, name) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 567 | try: |
| 568 | __import__(module) |
| 569 | mod = sys.modules[module] |
| 570 | klass = getattr(mod, name) |
| 571 | except (ImportError, KeyError, AttributeError): |
| 572 | raise PicklingError( |
| 573 | "Can't pickle %r: it's not found as %s.%s" % |
| 574 | (object, module, name)) |
| 575 | else: |
| 576 | if klass is not object: |
| 577 | raise PicklingError( |
| 578 | "Can't pickle %r: it's not the same object as %s.%s" % |
| 579 | (object, module, name)) |
| 580 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 581 | memo_len = len(memo) |
| 582 | write(GLOBAL + module + '\n' + name + '\n' + |
| 583 | self.put(memo_len)) |
| 584 | memo[id(object)] = (memo_len, object) |
| 585 | dispatch[ClassType] = save_global |
| 586 | dispatch[FunctionType] = save_global |
| 587 | dispatch[BuiltinFunctionType] = save_global |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 588 | dispatch[TypeType] = save_global |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 591 | def _keep_alive(x, memo): |
| 592 | """Keeps a reference to the object x in the memo. |
| 593 | |
| 594 | Because we remember objects by their id, we have |
| 595 | to assure that possibly temporary objects are kept |
| 596 | alive by referencing them. |
| 597 | We store a reference at the id of the memo, which should |
| 598 | normally not be used unless someone tries to deepcopy |
| 599 | the memo itself... |
| 600 | """ |
| 601 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 602 | memo[id(memo)].append(x) |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 603 | except KeyError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 604 | # aha, this is the first one :-) |
| 605 | memo[id(memo)]=[x] |
Guido van Rossum | 5ed5c4c | 1997-09-03 00:23:54 +0000 | [diff] [blame] | 606 | |
| 607 | |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 608 | classmap = {} # called classmap for backwards compatibility |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 609 | |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 610 | def whichmodule(func, funcname): |
| 611 | """Figure out the module in which a function occurs. |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 613 | Search sys.modules for the module. |
| 614 | Cache in classmap. |
| 615 | Return a module name. |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 616 | If the function cannot be found, return __main__. |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 617 | """ |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 618 | if func in classmap: |
| 619 | return classmap[func] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 620 | |
| 621 | for name, module in sys.modules.items(): |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 622 | if module is None: |
Jeremy Hylton | 065a5ab | 2002-09-19 22:57:26 +0000 | [diff] [blame] | 623 | continue # skip dummy package entries |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 624 | if name != '__main__' and \ |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 625 | hasattr(module, funcname) and \ |
| 626 | getattr(module, funcname) is func: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 627 | break |
| 628 | else: |
| 629 | name = '__main__' |
Jeremy Hylton | f0cfdf7 | 2002-09-19 23:00:12 +0000 | [diff] [blame] | 630 | classmap[func] = name |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 631 | return name |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 632 | |
| 633 | |
| 634 | class Unpickler: |
| 635 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 636 | def __init__(self, file): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 637 | """This takes a file-like object for reading a pickle data stream. |
| 638 | |
| 639 | This class automatically determines whether the data stream was |
| 640 | written in binary mode or not, so it does not need a flag as in |
| 641 | the Pickler class factory. |
| 642 | |
| 643 | The file-like object must have two methods, a read() method that |
| 644 | takes an integer argument, and a readline() method that requires no |
| 645 | arguments. Both methods should return a string. Thus file-like |
| 646 | object can be a file object opened for reading, a StringIO object, |
| 647 | or any other custom object that meets this interface. |
| 648 | |
| 649 | """ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 650 | self.readline = file.readline |
| 651 | self.read = file.read |
| 652 | self.memo = {} |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 654 | def load(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 655 | """Read a pickled object representation from the open file object. |
| 656 | |
| 657 | Return the reconstituted object hierarchy specified in the file |
| 658 | object. |
| 659 | |
| 660 | """ |
Jeremy Hylton | 20747fa | 2001-11-09 16:15:04 +0000 | [diff] [blame] | 661 | self.mark = object() # any new unique object |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 662 | self.stack = [] |
| 663 | self.append = self.stack.append |
| 664 | read = self.read |
| 665 | dispatch = self.dispatch |
| 666 | try: |
| 667 | while 1: |
| 668 | key = read(1) |
| 669 | dispatch[key](self) |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 670 | except _Stop, stopinst: |
| 671 | return stopinst.value |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 672 | |
| 673 | def marker(self): |
| 674 | stack = self.stack |
| 675 | mark = self.mark |
| 676 | k = len(stack)-1 |
| 677 | while stack[k] is not mark: k = k-1 |
| 678 | return k |
| 679 | |
| 680 | dispatch = {} |
| 681 | |
| 682 | def load_eof(self): |
| 683 | raise EOFError |
| 684 | dispatch[''] = load_eof |
| 685 | |
| 686 | def load_persid(self): |
| 687 | pid = self.readline()[:-1] |
| 688 | self.append(self.persistent_load(pid)) |
| 689 | dispatch[PERSID] = load_persid |
| 690 | |
| 691 | def load_binpersid(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 692 | pid = self.stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 693 | self.append(self.persistent_load(pid)) |
| 694 | dispatch[BINPERSID] = load_binpersid |
| 695 | |
| 696 | def load_none(self): |
| 697 | self.append(None) |
| 698 | dispatch[NONE] = load_none |
| 699 | |
| 700 | def load_int(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 701 | data = self.readline() |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 702 | if data == FALSE[1:]: |
| 703 | val = False |
| 704 | elif data == TRUE[1:]: |
| 705 | val = True |
| 706 | else: |
| 707 | try: |
| 708 | val = int(data) |
| 709 | except ValueError: |
| 710 | val = long(data) |
| 711 | self.append(val) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 712 | dispatch[INT] = load_int |
| 713 | |
| 714 | def load_binint(self): |
| 715 | self.append(mloads('i' + self.read(4))) |
| 716 | dispatch[BININT] = load_binint |
| 717 | |
| 718 | def load_binint1(self): |
| 719 | self.append(mloads('i' + self.read(1) + '\000\000\000')) |
| 720 | dispatch[BININT1] = load_binint1 |
| 721 | |
| 722 | def load_binint2(self): |
| 723 | self.append(mloads('i' + self.read(2) + '\000\000')) |
| 724 | dispatch[BININT2] = load_binint2 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 725 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 726 | def load_long(self): |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 727 | self.append(long(self.readline()[:-1], 0)) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 728 | dispatch[LONG] = load_long |
| 729 | |
| 730 | def load_float(self): |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 731 | self.append(float(self.readline()[:-1])) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 732 | dispatch[FLOAT] = load_float |
| 733 | |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 734 | def load_binfloat(self, unpack=struct.unpack): |
| 735 | self.append(unpack('>d', self.read(8))[0]) |
| 736 | dispatch[BINFLOAT] = load_binfloat |
| 737 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 738 | def load_string(self): |
Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 739 | rep = self.readline()[:-1] |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 740 | for q in _quotes: |
| 741 | if rep.startswith(q): |
| 742 | if not rep.endswith(q): |
| 743 | raise ValueError, "insecure string pickle" |
| 744 | rep = rep[len(q):-len(q)] |
| 745 | break |
| 746 | else: |
Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 747 | raise ValueError, "insecure string pickle" |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 748 | self.append(rep.decode("string-escape")) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 749 | dispatch[STRING] = load_string |
| 750 | |
Jeremy Hylton | be467e5 | 2000-09-15 15:14:51 +0000 | [diff] [blame] | 751 | def _is_string_secure(self, s): |
| 752 | """Return true if s contains a string that is safe to eval |
| 753 | |
| 754 | The definition of secure string is based on the implementation |
| 755 | in cPickle. s is secure as long as it only contains a quoted |
| 756 | string and optional trailing whitespace. |
| 757 | """ |
| 758 | q = s[0] |
| 759 | if q not in ("'", '"'): |
| 760 | return 0 |
| 761 | # find the closing quote |
| 762 | offset = 1 |
| 763 | i = None |
| 764 | while 1: |
| 765 | try: |
| 766 | i = s.index(q, offset) |
| 767 | except ValueError: |
| 768 | # if there is an error the first time, there is no |
| 769 | # close quote |
| 770 | if offset == 1: |
| 771 | return 0 |
| 772 | if s[i-1] != '\\': |
| 773 | break |
| 774 | # check to see if this one is escaped |
| 775 | nslash = 0 |
| 776 | j = i - 1 |
| 777 | while j >= offset and s[j] == '\\': |
| 778 | j = j - 1 |
| 779 | nslash = nslash + 1 |
| 780 | if nslash % 2 == 0: |
| 781 | break |
| 782 | offset = i + 1 |
| 783 | for c in s[i+1:]: |
| 784 | if ord(c) > 32: |
| 785 | return 0 |
| 786 | return 1 |
| 787 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 788 | def load_binstring(self): |
| 789 | len = mloads('i' + self.read(4)) |
| 790 | self.append(self.read(len)) |
| 791 | dispatch[BINSTRING] = load_binstring |
| 792 | |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 793 | def load_unicode(self): |
| 794 | self.append(unicode(self.readline()[:-1],'raw-unicode-escape')) |
| 795 | dispatch[UNICODE] = load_unicode |
| 796 | |
| 797 | def load_binunicode(self): |
| 798 | len = mloads('i' + self.read(4)) |
| 799 | self.append(unicode(self.read(len),'utf-8')) |
| 800 | dispatch[BINUNICODE] = load_binunicode |
| 801 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 802 | def load_short_binstring(self): |
| 803 | len = mloads('i' + self.read(1) + '\000\000\000') |
| 804 | self.append(self.read(len)) |
| 805 | dispatch[SHORT_BINSTRING] = load_short_binstring |
| 806 | |
| 807 | def load_tuple(self): |
| 808 | k = self.marker() |
| 809 | self.stack[k:] = [tuple(self.stack[k+1:])] |
| 810 | dispatch[TUPLE] = load_tuple |
| 811 | |
| 812 | def load_empty_tuple(self): |
| 813 | self.stack.append(()) |
| 814 | dispatch[EMPTY_TUPLE] = load_empty_tuple |
| 815 | |
| 816 | def load_empty_list(self): |
| 817 | self.stack.append([]) |
| 818 | dispatch[EMPTY_LIST] = load_empty_list |
| 819 | |
| 820 | def load_empty_dictionary(self): |
| 821 | self.stack.append({}) |
| 822 | dispatch[EMPTY_DICT] = load_empty_dictionary |
| 823 | |
| 824 | def load_list(self): |
| 825 | k = self.marker() |
| 826 | self.stack[k:] = [self.stack[k+1:]] |
| 827 | dispatch[LIST] = load_list |
| 828 | |
| 829 | def load_dict(self): |
| 830 | k = self.marker() |
| 831 | d = {} |
| 832 | items = self.stack[k+1:] |
| 833 | for i in range(0, len(items), 2): |
| 834 | key = items[i] |
| 835 | value = items[i+1] |
| 836 | d[key] = value |
| 837 | self.stack[k:] = [d] |
| 838 | dispatch[DICT] = load_dict |
| 839 | |
| 840 | def load_inst(self): |
| 841 | k = self.marker() |
| 842 | args = tuple(self.stack[k+1:]) |
| 843 | del self.stack[k:] |
| 844 | module = self.readline()[:-1] |
| 845 | name = self.readline()[:-1] |
| 846 | klass = self.find_class(module, name) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 847 | instantiated = 0 |
| 848 | if (not args and type(klass) is ClassType and |
| 849 | not hasattr(klass, "__getinitargs__")): |
| 850 | try: |
| 851 | value = _EmptyClass() |
| 852 | value.__class__ = klass |
Guido van Rossum | b19e2a3 | 1998-04-13 18:08:45 +0000 | [diff] [blame] | 853 | instantiated = 1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 854 | except RuntimeError: |
| 855 | # In restricted execution, assignment to inst.__class__ is |
| 856 | # prohibited |
| 857 | pass |
| 858 | if not instantiated: |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 859 | try: |
Barry Warsaw | bf4d959 | 2001-11-15 23:42:58 +0000 | [diff] [blame] | 860 | if not hasattr(klass, '__safe_for_unpickling__'): |
| 861 | raise UnpicklingError('%s is not safe for unpickling' % |
| 862 | klass) |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 863 | value = apply(klass, args) |
| 864 | except TypeError, err: |
| 865 | raise TypeError, "in constructor for %s: %s" % ( |
| 866 | klass.__name__, str(err)), sys.exc_info()[2] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 867 | self.append(value) |
| 868 | dispatch[INST] = load_inst |
| 869 | |
| 870 | def load_obj(self): |
| 871 | stack = self.stack |
| 872 | k = self.marker() |
| 873 | klass = stack[k + 1] |
| 874 | del stack[k + 1] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 875 | args = tuple(stack[k + 1:]) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 876 | del stack[k:] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 877 | instantiated = 0 |
| 878 | if (not args and type(klass) is ClassType and |
| 879 | not hasattr(klass, "__getinitargs__")): |
| 880 | try: |
| 881 | value = _EmptyClass() |
| 882 | value.__class__ = klass |
| 883 | instantiated = 1 |
| 884 | except RuntimeError: |
| 885 | # In restricted execution, assignment to inst.__class__ is |
| 886 | # prohibited |
| 887 | pass |
| 888 | if not instantiated: |
| 889 | value = apply(klass, args) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 890 | self.append(value) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 891 | dispatch[OBJ] = load_obj |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 892 | |
| 893 | def load_global(self): |
| 894 | module = self.readline()[:-1] |
| 895 | name = self.readline()[:-1] |
| 896 | klass = self.find_class(module, name) |
| 897 | self.append(klass) |
| 898 | dispatch[GLOBAL] = load_global |
| 899 | |
| 900 | def find_class(self, module, name): |
Barry Warsaw | bf4d959 | 2001-11-15 23:42:58 +0000 | [diff] [blame] | 901 | __import__(module) |
| 902 | mod = sys.modules[module] |
| 903 | klass = getattr(mod, name) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 904 | return klass |
| 905 | |
| 906 | def load_reduce(self): |
| 907 | stack = self.stack |
| 908 | |
| 909 | callable = stack[-2] |
| 910 | arg_tup = stack[-1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 911 | del stack[-2:] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 912 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 913 | if type(callable) is not ClassType: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 914 | if not callable in safe_constructors: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 915 | try: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 916 | safe = callable.__safe_for_unpickling__ |
| 917 | except AttributeError: |
| 918 | safe = None |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 919 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 920 | if not safe: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 921 | raise UnpicklingError, "%s is not safe for " \ |
| 922 | "unpickling" % callable |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 923 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 924 | if arg_tup is None: |
Raymond Hettinger | 97394bc | 2002-05-21 17:22:02 +0000 | [diff] [blame] | 925 | import warnings |
| 926 | warnings.warn("The None return argument form of __reduce__ is " |
| 927 | "deprecated. Return a tuple of arguments instead.", |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 928 | DeprecationWarning) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 929 | value = callable.__basicnew__() |
| 930 | else: |
| 931 | value = apply(callable, arg_tup) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 932 | self.append(value) |
| 933 | dispatch[REDUCE] = load_reduce |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 934 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 935 | def load_pop(self): |
| 936 | del self.stack[-1] |
| 937 | dispatch[POP] = load_pop |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 938 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 939 | def load_pop_mark(self): |
| 940 | k = self.marker() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 941 | del self.stack[k:] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 942 | dispatch[POP_MARK] = load_pop_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 944 | def load_dup(self): |
Guido van Rossum | b1062fc | 1998-03-31 17:00:46 +0000 | [diff] [blame] | 945 | self.append(self.stack[-1]) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 946 | dispatch[DUP] = load_dup |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 948 | def load_get(self): |
| 949 | self.append(self.memo[self.readline()[:-1]]) |
| 950 | dispatch[GET] = load_get |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 952 | def load_binget(self): |
| 953 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 954 | self.append(self.memo[`i`]) |
| 955 | dispatch[BINGET] = load_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 957 | def load_long_binget(self): |
| 958 | i = mloads('i' + self.read(4)) |
| 959 | self.append(self.memo[`i`]) |
| 960 | dispatch[LONG_BINGET] = load_long_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 961 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 962 | def load_put(self): |
| 963 | self.memo[self.readline()[:-1]] = self.stack[-1] |
| 964 | dispatch[PUT] = load_put |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 965 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 966 | def load_binput(self): |
| 967 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 968 | self.memo[`i`] = self.stack[-1] |
| 969 | dispatch[BINPUT] = load_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 970 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 971 | def load_long_binput(self): |
| 972 | i = mloads('i' + self.read(4)) |
| 973 | self.memo[`i`] = self.stack[-1] |
| 974 | dispatch[LONG_BINPUT] = load_long_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 975 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 976 | def load_append(self): |
| 977 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 978 | value = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 979 | list = stack[-1] |
| 980 | list.append(value) |
| 981 | dispatch[APPEND] = load_append |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 983 | def load_appends(self): |
| 984 | stack = self.stack |
| 985 | mark = self.marker() |
| 986 | list = stack[mark - 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 987 | for i in range(mark + 1, len(stack)): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 988 | list.append(stack[i]) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 989 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 990 | del stack[mark:] |
| 991 | dispatch[APPENDS] = load_appends |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 992 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 993 | def load_setitem(self): |
| 994 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 995 | value = stack.pop() |
| 996 | key = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 997 | dict = stack[-1] |
| 998 | dict[key] = value |
| 999 | dispatch[SETITEM] = load_setitem |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1000 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1001 | def load_setitems(self): |
| 1002 | stack = self.stack |
| 1003 | mark = self.marker() |
| 1004 | dict = stack[mark - 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1005 | for i in range(mark + 1, len(stack), 2): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1006 | dict[stack[i]] = stack[i + 1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1007 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1008 | del stack[mark:] |
| 1009 | dispatch[SETITEMS] = load_setitems |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1010 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1011 | def load_build(self): |
| 1012 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1013 | value = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1014 | inst = stack[-1] |
| 1015 | try: |
| 1016 | setstate = inst.__setstate__ |
| 1017 | except AttributeError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 1018 | try: |
| 1019 | inst.__dict__.update(value) |
| 1020 | except RuntimeError: |
| 1021 | # XXX In restricted execution, the instance's __dict__ is not |
| 1022 | # accessible. Use the old way of unpickling the instance |
| 1023 | # variables. This is a semantic different when unpickling in |
| 1024 | # restricted vs. unrestricted modes. |
| 1025 | for k, v in value.items(): |
| 1026 | setattr(inst, k, v) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1027 | else: |
| 1028 | setstate(value) |
| 1029 | dispatch[BUILD] = load_build |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1030 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1031 | def load_mark(self): |
| 1032 | self.append(self.mark) |
| 1033 | dispatch[MARK] = load_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1034 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1035 | def load_stop(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1036 | value = self.stack.pop() |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 1037 | raise _Stop(value) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1038 | dispatch[STOP] = load_stop |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 1040 | # Helper class for load_inst/load_obj |
| 1041 | |
| 1042 | class _EmptyClass: |
| 1043 | pass |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1044 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1045 | # Shorthands |
| 1046 | |
Jeremy Hylton | abe2c62 | 2001-10-15 21:29:28 +0000 | [diff] [blame] | 1047 | try: |
| 1048 | from cStringIO import StringIO |
| 1049 | except ImportError: |
| 1050 | from StringIO import StringIO |
Guido van Rossum | c7c5e69 | 1996-07-22 22:26:07 +0000 | [diff] [blame] | 1051 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1052 | def dump(object, file, bin = 0): |
| 1053 | Pickler(file, bin).dump(object) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1054 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1055 | def dumps(object, bin = 0): |
| 1056 | file = StringIO() |
| 1057 | Pickler(file, bin).dump(object) |
| 1058 | return file.getvalue() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1059 | |
| 1060 | def load(file): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1061 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1062 | |
| 1063 | def loads(str): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1064 | file = StringIO(str) |
| 1065 | return Unpickler(file).load() |