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