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