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