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