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