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