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