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