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