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