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