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): |
| 580 | self.append(eval(self.readline()[:-1], |
| 581 | {'__builtins__': {}})) # Let's be careful |
| 582 | dispatch[STRING] = load_string |
| 583 | |
| 584 | def load_binstring(self): |
| 585 | len = mloads('i' + self.read(4)) |
| 586 | self.append(self.read(len)) |
| 587 | dispatch[BINSTRING] = load_binstring |
| 588 | |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 589 | def load_unicode(self): |
| 590 | self.append(unicode(self.readline()[:-1],'raw-unicode-escape')) |
| 591 | dispatch[UNICODE] = load_unicode |
| 592 | |
| 593 | def load_binunicode(self): |
| 594 | len = mloads('i' + self.read(4)) |
| 595 | self.append(unicode(self.read(len),'utf-8')) |
| 596 | dispatch[BINUNICODE] = load_binunicode |
| 597 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 598 | def load_short_binstring(self): |
| 599 | len = mloads('i' + self.read(1) + '\000\000\000') |
| 600 | self.append(self.read(len)) |
| 601 | dispatch[SHORT_BINSTRING] = load_short_binstring |
| 602 | |
| 603 | def load_tuple(self): |
| 604 | k = self.marker() |
| 605 | self.stack[k:] = [tuple(self.stack[k+1:])] |
| 606 | dispatch[TUPLE] = load_tuple |
| 607 | |
| 608 | def load_empty_tuple(self): |
| 609 | self.stack.append(()) |
| 610 | dispatch[EMPTY_TUPLE] = load_empty_tuple |
| 611 | |
| 612 | def load_empty_list(self): |
| 613 | self.stack.append([]) |
| 614 | dispatch[EMPTY_LIST] = load_empty_list |
| 615 | |
| 616 | def load_empty_dictionary(self): |
| 617 | self.stack.append({}) |
| 618 | dispatch[EMPTY_DICT] = load_empty_dictionary |
| 619 | |
| 620 | def load_list(self): |
| 621 | k = self.marker() |
| 622 | self.stack[k:] = [self.stack[k+1:]] |
| 623 | dispatch[LIST] = load_list |
| 624 | |
| 625 | def load_dict(self): |
| 626 | k = self.marker() |
| 627 | d = {} |
| 628 | items = self.stack[k+1:] |
| 629 | for i in range(0, len(items), 2): |
| 630 | key = items[i] |
| 631 | value = items[i+1] |
| 632 | d[key] = value |
| 633 | self.stack[k:] = [d] |
| 634 | dispatch[DICT] = load_dict |
| 635 | |
| 636 | def load_inst(self): |
| 637 | k = self.marker() |
| 638 | args = tuple(self.stack[k+1:]) |
| 639 | del self.stack[k:] |
| 640 | module = self.readline()[:-1] |
| 641 | name = self.readline()[:-1] |
| 642 | klass = self.find_class(module, name) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 643 | instantiated = 0 |
| 644 | if (not args and type(klass) is ClassType and |
| 645 | not hasattr(klass, "__getinitargs__")): |
| 646 | try: |
| 647 | value = _EmptyClass() |
| 648 | value.__class__ = klass |
Guido van Rossum | b19e2a3 | 1998-04-13 18:08:45 +0000 | [diff] [blame] | 649 | instantiated = 1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 650 | except RuntimeError: |
| 651 | # In restricted execution, assignment to inst.__class__ is |
| 652 | # prohibited |
| 653 | pass |
| 654 | if not instantiated: |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 655 | try: |
| 656 | value = apply(klass, args) |
| 657 | except TypeError, err: |
| 658 | raise TypeError, "in constructor for %s: %s" % ( |
| 659 | klass.__name__, str(err)), sys.exc_info()[2] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 660 | self.append(value) |
| 661 | dispatch[INST] = load_inst |
| 662 | |
| 663 | def load_obj(self): |
| 664 | stack = self.stack |
| 665 | k = self.marker() |
| 666 | klass = stack[k + 1] |
| 667 | del stack[k + 1] |
| 668 | args = tuple(stack[k + 1:]) |
| 669 | del stack[k:] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 670 | instantiated = 0 |
| 671 | if (not args and type(klass) is ClassType and |
| 672 | not hasattr(klass, "__getinitargs__")): |
| 673 | try: |
| 674 | value = _EmptyClass() |
| 675 | value.__class__ = klass |
| 676 | instantiated = 1 |
| 677 | except RuntimeError: |
| 678 | # In restricted execution, assignment to inst.__class__ is |
| 679 | # prohibited |
| 680 | pass |
| 681 | if not instantiated: |
| 682 | value = apply(klass, args) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 683 | self.append(value) |
| 684 | dispatch[OBJ] = load_obj |
| 685 | |
| 686 | def load_global(self): |
| 687 | module = self.readline()[:-1] |
| 688 | name = self.readline()[:-1] |
| 689 | klass = self.find_class(module, name) |
| 690 | self.append(klass) |
| 691 | dispatch[GLOBAL] = load_global |
| 692 | |
| 693 | def find_class(self, module, name): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 694 | try: |
Guido van Rossum | 397032a | 1999-03-25 21:58:59 +0000 | [diff] [blame] | 695 | __import__(module) |
| 696 | mod = sys.modules[module] |
| 697 | klass = getattr(mod, name) |
| 698 | except (ImportError, KeyError, AttributeError): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 699 | raise SystemError, \ |
| 700 | "Failed to import class %s from module %s" % \ |
| 701 | (name, module) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 702 | return klass |
| 703 | |
| 704 | def load_reduce(self): |
| 705 | stack = self.stack |
| 706 | |
| 707 | callable = stack[-2] |
| 708 | arg_tup = stack[-1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 709 | del stack[-2:] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 710 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 711 | if type(callable) is not ClassType: |
| 712 | if not safe_constructors.has_key(callable): |
| 713 | try: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 714 | safe = callable.__safe_for_unpickling__ |
| 715 | except AttributeError: |
| 716 | safe = None |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 717 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 718 | if (not safe): |
| 719 | raise UnpicklingError, "%s is not safe for " \ |
| 720 | "unpickling" % callable |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 721 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 722 | if arg_tup is None: |
| 723 | value = callable.__basicnew__() |
| 724 | else: |
| 725 | value = apply(callable, arg_tup) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 726 | self.append(value) |
| 727 | dispatch[REDUCE] = load_reduce |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 729 | def load_pop(self): |
| 730 | del self.stack[-1] |
| 731 | dispatch[POP] = load_pop |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 732 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 733 | def load_pop_mark(self): |
| 734 | k = self.marker() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 735 | del self.stack[k:] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 736 | dispatch[POP_MARK] = load_pop_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 737 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 738 | def load_dup(self): |
Guido van Rossum | b1062fc | 1998-03-31 17:00:46 +0000 | [diff] [blame] | 739 | self.append(self.stack[-1]) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 740 | dispatch[DUP] = load_dup |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 742 | def load_get(self): |
| 743 | self.append(self.memo[self.readline()[:-1]]) |
| 744 | dispatch[GET] = load_get |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 746 | def load_binget(self): |
| 747 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 748 | self.append(self.memo[`i`]) |
| 749 | dispatch[BINGET] = load_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 750 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 751 | def load_long_binget(self): |
| 752 | i = mloads('i' + self.read(4)) |
| 753 | self.append(self.memo[`i`]) |
| 754 | dispatch[LONG_BINGET] = load_long_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 756 | def load_put(self): |
| 757 | self.memo[self.readline()[:-1]] = self.stack[-1] |
| 758 | dispatch[PUT] = load_put |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 759 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 760 | def load_binput(self): |
| 761 | i = mloads('i' + self.read(1) + '\000\000\000') |
| 762 | self.memo[`i`] = self.stack[-1] |
| 763 | dispatch[BINPUT] = load_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 765 | def load_long_binput(self): |
| 766 | i = mloads('i' + self.read(4)) |
| 767 | self.memo[`i`] = self.stack[-1] |
| 768 | dispatch[LONG_BINPUT] = load_long_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 770 | def load_append(self): |
| 771 | stack = self.stack |
| 772 | value = stack[-1] |
| 773 | del stack[-1] |
| 774 | list = stack[-1] |
| 775 | list.append(value) |
| 776 | dispatch[APPEND] = load_append |
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_appends(self): |
| 779 | stack = self.stack |
| 780 | mark = self.marker() |
| 781 | list = stack[mark - 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 782 | for i in range(mark + 1, len(stack)): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 783 | list.append(stack[i]) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 785 | del stack[mark:] |
| 786 | dispatch[APPENDS] = load_appends |
| 787 | |
| 788 | def load_setitem(self): |
| 789 | stack = self.stack |
| 790 | value = stack[-1] |
| 791 | key = stack[-2] |
| 792 | del stack[-2:] |
| 793 | dict = stack[-1] |
| 794 | dict[key] = value |
| 795 | dispatch[SETITEM] = load_setitem |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 797 | def load_setitems(self): |
| 798 | stack = self.stack |
| 799 | mark = self.marker() |
| 800 | dict = stack[mark - 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 801 | for i in range(mark + 1, len(stack), 2): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 802 | dict[stack[i]] = stack[i + 1] |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 804 | del stack[mark:] |
| 805 | dispatch[SETITEMS] = load_setitems |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 806 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 807 | def load_build(self): |
| 808 | stack = self.stack |
| 809 | value = stack[-1] |
| 810 | del stack[-1] |
| 811 | inst = stack[-1] |
| 812 | try: |
| 813 | setstate = inst.__setstate__ |
| 814 | except AttributeError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 815 | try: |
| 816 | inst.__dict__.update(value) |
| 817 | except RuntimeError: |
| 818 | # XXX In restricted execution, the instance's __dict__ is not |
| 819 | # accessible. Use the old way of unpickling the instance |
| 820 | # variables. This is a semantic different when unpickling in |
| 821 | # restricted vs. unrestricted modes. |
| 822 | for k, v in value.items(): |
| 823 | setattr(inst, k, v) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 824 | else: |
| 825 | setstate(value) |
| 826 | dispatch[BUILD] = load_build |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 828 | def load_mark(self): |
| 829 | self.append(self.mark) |
| 830 | dispatch[MARK] = load_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 831 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 832 | def load_stop(self): |
| 833 | value = self.stack[-1] |
| 834 | del self.stack[-1] |
| 835 | raise STOP, value |
| 836 | dispatch[STOP] = load_stop |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 837 | |
Guido van Rossum | e467be6 | 1997-12-05 19:42:42 +0000 | [diff] [blame] | 838 | # Helper class for load_inst/load_obj |
| 839 | |
| 840 | class _EmptyClass: |
| 841 | pass |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 842 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 843 | # Shorthands |
| 844 | |
Guido van Rossum | c7c5e69 | 1996-07-22 22:26:07 +0000 | [diff] [blame] | 845 | from StringIO import StringIO |
| 846 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 847 | def dump(object, file, bin = 0): |
| 848 | Pickler(file, bin).dump(object) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 849 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 850 | def dumps(object, bin = 0): |
| 851 | file = StringIO() |
| 852 | Pickler(file, bin).dump(object) |
| 853 | return file.getvalue() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 854 | |
| 855 | def load(file): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 856 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 857 | |
| 858 | def loads(str): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 859 | file = StringIO(str) |
| 860 | return Unpickler(file).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 861 | |
| 862 | |
| 863 | # The rest is used for testing only |
| 864 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 865 | class C: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 866 | def __cmp__(self, other): |
| 867 | return cmp(self.__dict__, other.__dict__) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 868 | |
| 869 | def test(): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 870 | fn = 'out' |
| 871 | c = C() |
| 872 | c.foo = 1 |
| 873 | c.bar = 2 |
| 874 | x = [0, 1, 2, 3] |
| 875 | y = ('abc', 'abc', c, c) |
| 876 | x.append(y) |
| 877 | x.append(y) |
| 878 | x.append(5) |
| 879 | f = open(fn, 'w') |
| 880 | F = Pickler(f) |
| 881 | F.dump(x) |
| 882 | f.close() |
| 883 | f = open(fn, 'r') |
| 884 | U = Unpickler(f) |
| 885 | x2 = U.load() |
| 886 | print x |
| 887 | print x2 |
| 888 | print x == x2 |
| 889 | print map(id, x) |
| 890 | print map(id, x2) |
| 891 | print F.memo |
| 892 | print U.memo |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 893 | |
| 894 | if __name__ == '__main__': |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 895 | test() |