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