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