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 | |
Victor Stinner | 7fa767e | 2014-03-20 09:16:38 +0100 | [diff] [blame] | 26 | from types import FunctionType |
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 |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 29 | from itertools import islice |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 30 | from functools import partial |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 31 | import sys |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 32 | from sys import maxsize |
| 33 | from struct import pack, unpack |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 34 | import re |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 35 | import io |
Walter Dörwald | 42748a8 | 2007-06-12 16:40:17 +0000 | [diff] [blame] | 36 | import codecs |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 37 | import _compat_pickle |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 38 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 39 | __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", |
| 40 | "Unpickler", "dump", "dumps", "load", "loads"] |
| 41 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 42 | # Shortcut for use in isinstance testing |
Alexandre Vassalotti | 8cb02b6 | 2008-05-03 01:42:49 +0000 | [diff] [blame] | 43 | bytes_types = (bytes, bytearray) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 44 | |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 45 | # These are purely informational; no code uses these. |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 46 | format_version = "4.0" # File format version we write |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 47 | compatible_formats = ["1.0", # Original protocol 0 |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 48 | "1.1", # Protocol 0 with INST added |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 49 | "1.2", # Original protocol 1 |
| 50 | "1.3", # Protocol 1 with BINFLOAT added |
| 51 | "2.0", # Protocol 2 |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 52 | "3.0", # Protocol 3 |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 53 | "4.0", # Protocol 4 |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 54 | ] # Old format versions we can read |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 56 | # This is the highest protocol number we know how to read. |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 57 | HIGHEST_PROTOCOL = 4 |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 59 | # The protocol we write by default. May be less than HIGHEST_PROTOCOL. |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 60 | # Only bump this if the oldest still supported version of Python already |
| 61 | # includes it. |
| 62 | DEFAULT_PROTOCOL = 4 |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 63 | |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 64 | class PickleError(Exception): |
Neal Norwitz | efbb67b | 2002-05-30 12:12:04 +0000 | [diff] [blame] | 65 | """A common base class for the other pickling exceptions.""" |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 66 | pass |
| 67 | |
| 68 | class PicklingError(PickleError): |
| 69 | """This exception is raised when an unpicklable object is passed to the |
| 70 | dump() method. |
| 71 | |
| 72 | """ |
| 73 | pass |
| 74 | |
| 75 | class UnpicklingError(PickleError): |
| 76 | """This exception is raised when there is a problem unpickling an object, |
| 77 | such as a security violation. |
| 78 | |
| 79 | Note that other exceptions may also be raised during unpickling, including |
| 80 | (but not necessarily limited to) AttributeError, EOFError, ImportError, |
| 81 | and IndexError. |
| 82 | |
| 83 | """ |
| 84 | pass |
Guido van Rossum | 7849da8 | 1995-03-09 14:08:35 +0000 | [diff] [blame] | 85 | |
Tim Peters | c0c12b5 | 2003-01-29 00:56:17 +0000 | [diff] [blame] | 86 | # An instance of _Stop is raised by Unpickler.load_stop() in response to |
| 87 | # 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] | 88 | class _Stop(Exception): |
| 89 | def __init__(self, value): |
| 90 | self.value = value |
| 91 | |
Guido van Rossum | 533dbcf | 2003-01-28 17:55:05 +0000 | [diff] [blame] | 92 | # Jython has PyStringMap; it's a dict subclass with string keys |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 93 | try: |
| 94 | from org.python.core import PyStringMap |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 95 | except ImportError: |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 96 | PyStringMap = None |
| 97 | |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame] | 98 | # Pickle opcodes. See pickletools.py for extensive docs. The listing |
| 99 | # here is in kind-of alphabetical order of 1-character pickle code. |
| 100 | # pickletools groups them by purpose. |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 102 | MARK = b'(' # push special markobject on stack |
| 103 | STOP = b'.' # every pickle ends with STOP |
| 104 | POP = b'0' # discard topmost stack item |
| 105 | POP_MARK = b'1' # discard stack top through topmost markobject |
| 106 | DUP = b'2' # duplicate top stack item |
| 107 | FLOAT = b'F' # push float object; decimal string argument |
| 108 | INT = b'I' # push integer or bool; decimal string argument |
| 109 | BININT = b'J' # push four-byte signed int |
| 110 | BININT1 = b'K' # push 1-byte unsigned int |
| 111 | LONG = b'L' # push long; decimal string argument |
| 112 | BININT2 = b'M' # push 2-byte unsigned int |
| 113 | NONE = b'N' # push None |
| 114 | PERSID = b'P' # push persistent object; id is taken from string arg |
| 115 | BINPERSID = b'Q' # " " " ; " " " " stack |
| 116 | REDUCE = b'R' # apply callable to argtuple, both on stack |
| 117 | STRING = b'S' # push string; NL-terminated string argument |
| 118 | BINSTRING = b'T' # push string; counted binary string argument |
| 119 | SHORT_BINSTRING= b'U' # " " ; " " " " < 256 bytes |
| 120 | UNICODE = b'V' # push Unicode string; raw-unicode-escaped'd argument |
| 121 | BINUNICODE = b'X' # " " " ; counted UTF-8 string argument |
| 122 | APPEND = b'a' # append stack top to list below it |
| 123 | BUILD = b'b' # call __setstate__ or __dict__.update() |
| 124 | GLOBAL = b'c' # push self.find_class(modname, name); 2 string args |
| 125 | DICT = b'd' # build a dict from stack items |
| 126 | EMPTY_DICT = b'}' # push empty dict |
| 127 | APPENDS = b'e' # extend list on stack by topmost stack slice |
| 128 | GET = b'g' # push item from memo on stack; index is string arg |
| 129 | BINGET = b'h' # " " " " " " ; " " 1-byte arg |
| 130 | INST = b'i' # build & push class instance |
| 131 | LONG_BINGET = b'j' # push item from memo on stack; index is 4-byte arg |
| 132 | LIST = b'l' # build list from topmost stack items |
| 133 | EMPTY_LIST = b']' # push empty list |
| 134 | OBJ = b'o' # build & push class instance |
| 135 | PUT = b'p' # store stack top in memo; index is string arg |
| 136 | BINPUT = b'q' # " " " " " ; " " 1-byte arg |
| 137 | LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg |
| 138 | SETITEM = b's' # add key+value pair to dict |
| 139 | TUPLE = b't' # build tuple from topmost stack items |
| 140 | EMPTY_TUPLE = b')' # push empty tuple |
| 141 | SETITEMS = b'u' # modify dict by adding topmost key+value pairs |
| 142 | BINFLOAT = b'G' # push float; arg is 8-byte float encoding |
Tim Peters | 22a449a | 2003-01-27 20:16:36 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 144 | TRUE = b'I01\n' # not an opcode; see INT docs in pickletools.py |
| 145 | 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] | 146 | |
Guido van Rossum | 586c9e8 | 2003-01-29 06:16:12 +0000 | [diff] [blame] | 147 | # Protocol 2 |
Guido van Rossum | 5a2d8f5 | 2003-01-27 21:44:25 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 149 | PROTO = b'\x80' # identify pickle protocol |
| 150 | NEWOBJ = b'\x81' # build object by applying cls.__new__ to argtuple |
| 151 | EXT1 = b'\x82' # push object from extension registry; 1-byte index |
| 152 | EXT2 = b'\x83' # ditto, but 2-byte index |
| 153 | EXT4 = b'\x84' # ditto, but 4-byte index |
| 154 | TUPLE1 = b'\x85' # build 1-tuple from stack top |
| 155 | TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items |
| 156 | TUPLE3 = b'\x87' # build 3-tuple from three topmost stack items |
| 157 | NEWTRUE = b'\x88' # push True |
| 158 | NEWFALSE = b'\x89' # push False |
| 159 | LONG1 = b'\x8a' # push long from < 256 bytes |
| 160 | LONG4 = b'\x8b' # push really big long |
Guido van Rossum | 5a2d8f5 | 2003-01-27 21:44:25 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 162 | _tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
| 163 | |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 164 | # Protocol 3 (Python 3.x) |
| 165 | |
| 166 | BINBYTES = b'B' # push bytes; counted binary string argument |
| 167 | SHORT_BINBYTES = b'C' # " " ; " " " " < 256 bytes |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 169 | # Protocol 4 |
| 170 | SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes |
| 171 | BINUNICODE8 = b'\x8d' # push very long string |
| 172 | BINBYTES8 = b'\x8e' # push very long bytes string |
| 173 | EMPTY_SET = b'\x8f' # push empty set on the stack |
| 174 | ADDITEMS = b'\x90' # modify set by adding topmost stack items |
| 175 | FROZENSET = b'\x91' # build frozenset from topmost stack items |
| 176 | NEWOBJ_EX = b'\x92' # like NEWOBJ but work with keyword only arguments |
| 177 | STACK_GLOBAL = b'\x93' # same as GLOBAL but using names on the stacks |
| 178 | MEMOIZE = b'\x94' # store top of the stack in memo |
| 179 | FRAME = b'\x95' # indicate the beginning of a new frame |
| 180 | |
| 181 | __all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$", x)]) |
| 182 | |
| 183 | |
| 184 | class _Framer: |
| 185 | |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 186 | _FRAME_SIZE_MIN = 4 |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 187 | _FRAME_SIZE_TARGET = 64 * 1024 |
| 188 | |
| 189 | def __init__(self, file_write): |
| 190 | self.file_write = file_write |
| 191 | self.current_frame = None |
| 192 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 193 | def start_framing(self): |
| 194 | self.current_frame = io.BytesIO() |
| 195 | |
| 196 | def end_framing(self): |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 197 | if self.current_frame and self.current_frame.tell() > 0: |
| 198 | self.commit_frame(force=True) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 199 | self.current_frame = None |
| 200 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 201 | def commit_frame(self, force=False): |
| 202 | if self.current_frame: |
| 203 | f = self.current_frame |
| 204 | if f.tell() >= self._FRAME_SIZE_TARGET or force: |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 205 | data = f.getbuffer() |
| 206 | write = self.file_write |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 207 | if len(data) >= self._FRAME_SIZE_MIN: |
| 208 | # Issue a single call to the write method of the underlying |
| 209 | # file object for the frame opcode with the size of the |
| 210 | # frame. The concatenation is expected to be less expensive |
| 211 | # than issuing an additional call to write. |
| 212 | write(FRAME + pack("<Q", len(data))) |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 213 | |
| 214 | # Issue a separate call to write to append the frame |
| 215 | # contents without concatenation to the above to avoid a |
| 216 | # memory copy. |
| 217 | write(data) |
| 218 | |
| 219 | # Start the new frame with a new io.BytesIO instance so that |
| 220 | # the file object can have delayed access to the previous frame |
| 221 | # contents via an unreleased memoryview of the previous |
| 222 | # io.BytesIO instance. |
| 223 | self.current_frame = io.BytesIO() |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 224 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 225 | def write(self, data): |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 226 | if self.current_frame: |
| 227 | return self.current_frame.write(data) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 228 | else: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 229 | return self.file_write(data) |
| 230 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 231 | def write_large_bytes(self, header, payload): |
| 232 | write = self.file_write |
| 233 | if self.current_frame: |
| 234 | # Terminate the current frame and flush it to the file. |
| 235 | self.commit_frame(force=True) |
| 236 | |
| 237 | # Perform direct write of the header and payload of the large binary |
| 238 | # object. Be careful not to concatenate the header and the payload |
| 239 | # prior to calling 'write' as we do not want to allocate a large |
| 240 | # temporary bytes object. |
| 241 | # We intentionally do not insert a protocol 4 frame opcode to make |
| 242 | # it possible to optimize file.read calls in the loader. |
| 243 | write(header) |
| 244 | write(payload) |
| 245 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 246 | |
| 247 | class _Unframer: |
| 248 | |
| 249 | def __init__(self, file_read, file_readline, file_tell=None): |
| 250 | self.file_read = file_read |
| 251 | self.file_readline = file_readline |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 252 | self.current_frame = None |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 253 | |
| 254 | def read(self, n): |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 255 | if self.current_frame: |
| 256 | data = self.current_frame.read(n) |
| 257 | if not data and n != 0: |
| 258 | self.current_frame = None |
| 259 | return self.file_read(n) |
| 260 | if len(data) < n: |
| 261 | raise UnpicklingError( |
| 262 | "pickle exhausted before end of frame") |
| 263 | return data |
| 264 | else: |
| 265 | return self.file_read(n) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 266 | |
| 267 | def readline(self): |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 268 | if self.current_frame: |
| 269 | data = self.current_frame.readline() |
| 270 | if not data: |
| 271 | self.current_frame = None |
| 272 | return self.file_readline() |
Serhiy Storchaka | 21d7533 | 2015-01-26 10:37:01 +0200 | [diff] [blame] | 273 | if data[-1] != b'\n'[0]: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 274 | raise UnpicklingError( |
| 275 | "pickle exhausted before end of frame") |
| 276 | return data |
| 277 | else: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 278 | return self.file_readline() |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 279 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 280 | def load_frame(self, frame_size): |
| 281 | if self.current_frame and self.current_frame.read() != b'': |
| 282 | raise UnpicklingError( |
| 283 | "beginning of a new frame before end of current frame") |
| 284 | self.current_frame = io.BytesIO(self.file_read(frame_size)) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 285 | |
| 286 | |
| 287 | # Tools used for pickling. |
| 288 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 289 | def _getattribute(obj, name): |
| 290 | for subpath in name.split('.'): |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 291 | if subpath == '<locals>': |
| 292 | raise AttributeError("Can't get local attribute {!r} on {!r}" |
| 293 | .format(name, obj)) |
| 294 | try: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 295 | parent = obj |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 296 | obj = getattr(obj, subpath) |
| 297 | except AttributeError: |
| 298 | raise AttributeError("Can't get attribute {!r} on {!r}" |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 299 | .format(name, obj)) from None |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 300 | return obj, parent |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 301 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 302 | def whichmodule(obj, name): |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 303 | """Find the module an object belong to.""" |
| 304 | module_name = getattr(obj, '__module__', None) |
| 305 | if module_name is not None: |
| 306 | return module_name |
Antoine Pitrou | e161849 | 2014-10-04 22:15:27 +0200 | [diff] [blame] | 307 | # Protect the iteration by using a list copy of sys.modules against dynamic |
| 308 | # modules that trigger imports of other modules upon calls to getattr. |
| 309 | for module_name, module in list(sys.modules.items()): |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 310 | if module_name == '__main__' or module is None: |
| 311 | continue |
| 312 | try: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 313 | if _getattribute(module, name)[0] is obj: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 314 | return module_name |
| 315 | except AttributeError: |
| 316 | pass |
| 317 | return '__main__' |
| 318 | |
| 319 | def encode_long(x): |
| 320 | r"""Encode a long to a two's complement little-endian binary string. |
| 321 | Note that 0 is a special case, returning an empty string, to save a |
| 322 | byte in the LONG1 pickling context. |
| 323 | |
| 324 | >>> encode_long(0) |
| 325 | b'' |
| 326 | >>> encode_long(255) |
| 327 | b'\xff\x00' |
| 328 | >>> encode_long(32767) |
| 329 | b'\xff\x7f' |
| 330 | >>> encode_long(-256) |
| 331 | b'\x00\xff' |
| 332 | >>> encode_long(-32768) |
| 333 | b'\x00\x80' |
| 334 | >>> encode_long(-128) |
| 335 | b'\x80' |
| 336 | >>> encode_long(127) |
| 337 | b'\x7f' |
| 338 | >>> |
| 339 | """ |
| 340 | if x == 0: |
| 341 | return b'' |
| 342 | nbytes = (x.bit_length() >> 3) + 1 |
| 343 | result = x.to_bytes(nbytes, byteorder='little', signed=True) |
| 344 | if x < 0 and nbytes > 1: |
| 345 | if result[-1] == 0xff and (result[-2] & 0x80) != 0: |
| 346 | result = result[:-1] |
| 347 | return result |
| 348 | |
| 349 | def decode_long(data): |
| 350 | r"""Decode a long from a two's complement little-endian binary string. |
| 351 | |
| 352 | >>> decode_long(b'') |
| 353 | 0 |
| 354 | >>> decode_long(b"\xff\x00") |
| 355 | 255 |
| 356 | >>> decode_long(b"\xff\x7f") |
| 357 | 32767 |
| 358 | >>> decode_long(b"\x00\xff") |
| 359 | -256 |
| 360 | >>> decode_long(b"\x00\x80") |
| 361 | -32768 |
| 362 | >>> decode_long(b"\x80") |
| 363 | -128 |
| 364 | >>> decode_long(b"\x7f") |
| 365 | 127 |
| 366 | """ |
| 367 | return int.from_bytes(data, byteorder='little', signed=True) |
| 368 | |
Skip Montanaro | 23bafc6 | 2001-02-18 03:10:09 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 1be3175 | 2003-01-28 15:19:53 +0000 | [diff] [blame] | 370 | # Pickling machinery |
| 371 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 372 | class _Pickler: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 374 | def __init__(self, file, protocol=None, *, fix_imports=True): |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 375 | """This takes a binary file for writing a pickle data stream. |
| 376 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 377 | The optional *protocol* argument tells the pickler to use the |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 378 | given protocol; supported protocols are 0, 1, 2, 3 and 4. The |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 379 | default protocol is 4. It was introduced in Python 3.4, it is |
| 380 | incompatible with previous versions. |
Guido van Rossum | f29d3d6 | 2003-01-27 22:47:53 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 7eff63a | 2003-01-31 19:42:31 +0000 | [diff] [blame] | 382 | Specifying a negative protocol version selects the highest |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 383 | protocol version supported. The higher the protocol used, the |
| 384 | more recent the version of Python needed to read the pickle |
| 385 | produced. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 386 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 387 | The *file* argument must have a write() method that accepts a |
| 388 | single bytes argument. It can thus be a file object opened for |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 389 | binary writing, an io.BytesIO instance, or any other custom |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 390 | object that meets this interface. |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 391 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 392 | If *fix_imports* is True and *protocol* is less than 3, pickle |
| 393 | will try to map the new Python 3 names to the old module names |
| 394 | used in Python 2, so that the pickle data stream is readable |
| 395 | with Python 2. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 396 | """ |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 397 | if protocol is None: |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 398 | protocol = DEFAULT_PROTOCOL |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 399 | if protocol < 0: |
Tim Peters | 8587b3c | 2003-02-13 15:44:41 +0000 | [diff] [blame] | 400 | protocol = HIGHEST_PROTOCOL |
| 401 | elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
| 402 | raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 403 | try: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 404 | self._file_write = file.write |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 405 | except AttributeError: |
| 406 | raise TypeError("file must have a 'write' attribute") |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 407 | self.framer = _Framer(self._file_write) |
| 408 | self.write = self.framer.write |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 409 | self._write_large_bytes = self.framer.write_large_bytes |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 410 | self.memo = {} |
Guido van Rossum | cf117b0 | 2003-02-09 17:19:41 +0000 | [diff] [blame] | 411 | self.proto = int(protocol) |
| 412 | self.bin = protocol >= 1 |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 413 | self.fast = 0 |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 414 | self.fix_imports = fix_imports and protocol < 3 |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 415 | |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 416 | def clear_memo(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 417 | """Clears the pickler's "memo". |
| 418 | |
| 419 | The memo is the data structure that remembers which objects the |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 420 | pickler has already seen, so that shared or recursive objects |
| 421 | are pickled by reference and not by value. This method is |
| 422 | useful when re-using picklers. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 423 | """ |
Fred Drake | 7f781c9 | 2002-05-01 20:33:53 +0000 | [diff] [blame] | 424 | self.memo.clear() |
| 425 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 426 | def dump(self, obj): |
Tim Peters | 5bd2a79 | 2003-02-01 16:45:06 +0000 | [diff] [blame] | 427 | """Write a pickled representation of obj to the open file.""" |
Alexandre Vassalotti | 3cfcab9 | 2008-12-27 09:30:39 +0000 | [diff] [blame] | 428 | # Check whether Pickler was initialized correctly. This is |
| 429 | # only needed to mimic the behavior of _pickle.Pickler.dump(). |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 430 | if not hasattr(self, "_file_write"): |
Alexandre Vassalotti | 3cfcab9 | 2008-12-27 09:30:39 +0000 | [diff] [blame] | 431 | raise PicklingError("Pickler.__init__() was not called by " |
| 432 | "%s.__init__()" % (self.__class__.__name__,)) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 433 | if self.proto >= 2: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 434 | self.write(PROTO + pack("<B", self.proto)) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 435 | if self.proto >= 4: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 436 | self.framer.start_framing() |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 437 | self.save(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 438 | self.write(STOP) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 439 | self.framer.end_framing() |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 440 | |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 441 | def memoize(self, obj): |
| 442 | """Store an object in the memo.""" |
| 443 | |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 444 | # The Pickler memo is a dictionary mapping object ids to 2-tuples |
| 445 | # that contain the Unpickler memo key and the object being memoized. |
| 446 | # The memo key is written to the pickle and will become |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 447 | # 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] | 448 | # Pickler memo so that transient objects are kept alive during |
| 449 | # pickling. |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 450 | |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 451 | # The use of the Unpickler memo length as the memo key is just a |
| 452 | # convention. The only requirement is that the memo values be unique. |
| 453 | # But there appears no advantage to any other scheme, and this |
Tim Peters | cbd0a32 | 2003-01-28 00:24:43 +0000 | [diff] [blame] | 454 | # scheme allows the Unpickler memo to be implemented as a plain (but |
Tim Peters | e46b73f | 2003-01-27 21:22:10 +0000 | [diff] [blame] | 455 | # growable) array, indexed by memo key. |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 456 | if self.fast: |
| 457 | return |
Guido van Rossum | 9b40e80 | 2003-01-30 06:37:41 +0000 | [diff] [blame] | 458 | assert id(obj) not in self.memo |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 459 | idx = len(self.memo) |
| 460 | self.write(self.put(idx)) |
| 461 | self.memo[id(obj)] = idx, obj |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 462 | |
Tim Peters | bb38e30 | 2003-01-27 21:25:41 +0000 | [diff] [blame] | 463 | # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 464 | def put(self, idx): |
| 465 | if self.proto >= 4: |
| 466 | return MEMOIZE |
| 467 | elif self.bin: |
| 468 | if idx < 256: |
| 469 | return BINPUT + pack("<B", idx) |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 470 | else: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 471 | return LONG_BINPUT + pack("<I", idx) |
| 472 | else: |
| 473 | return PUT + repr(idx).encode("ascii") + b'\n' |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 474 | |
Tim Peters | bb38e30 | 2003-01-27 21:25:41 +0000 | [diff] [blame] | 475 | # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 476 | def get(self, i): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 477 | if self.bin: |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 478 | if i < 256: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 479 | return BINGET + pack("<B", i) |
Guido van Rossum | 5c938d0 | 2003-01-28 03:03:08 +0000 | [diff] [blame] | 480 | else: |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 481 | return LONG_BINGET + pack("<I", i) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 482 | |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 483 | return GET + repr(i).encode("ascii") + b'\n' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 484 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 485 | def save(self, obj, save_persistent_id=True): |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 486 | self.framer.commit_frame() |
| 487 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 488 | # Check for persistent id (defined by a subclass) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 489 | pid = self.persistent_id(obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 490 | if pid is not None and save_persistent_id: |
Jeremy Hylton | 5e0f4e7 | 2002-11-13 22:01:27 +0000 | [diff] [blame] | 491 | self.save_pers(pid) |
| 492 | return |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 494 | # Check the memo |
| 495 | x = self.memo.get(id(obj)) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 496 | if x is not None: |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 497 | self.write(self.get(x[0])) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 498 | return |
| 499 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 500 | # Check the type dispatch table |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 501 | t = type(obj) |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 502 | f = self.dispatch.get(t) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 503 | if f is not None: |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 504 | f(self, obj) # Call unbound method with explicit self |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 505 | return |
| 506 | |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 507 | # Check private dispatch table if any, or else copyreg.dispatch_table |
| 508 | reduce = getattr(self, 'dispatch_table', dispatch_table).get(t) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 509 | if reduce is not None: |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 510 | rv = reduce(obj) |
| 511 | else: |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 512 | # Check for a class with a custom metaclass; treat as regular class |
Berker Peksag | 2708578 | 2018-07-09 23:14:54 +0300 | [diff] [blame] | 513 | if issubclass(t, type): |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 514 | self.save_global(obj) |
| 515 | return |
| 516 | |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 517 | # Check for a __reduce_ex__ method, fall back to __reduce__ |
| 518 | reduce = getattr(obj, "__reduce_ex__", None) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 519 | if reduce is not None: |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 520 | rv = reduce(self.proto) |
| 521 | else: |
| 522 | reduce = getattr(obj, "__reduce__", None) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 523 | if reduce is not None: |
Guido van Rossum | c53f009 | 2003-02-18 22:05:12 +0000 | [diff] [blame] | 524 | rv = reduce() |
| 525 | else: |
| 526 | raise PicklingError("Can't pickle %r object: %r" % |
| 527 | (t.__name__, obj)) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 529 | # Check for string returned by reduce(), meaning "save as global" |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 530 | if isinstance(rv, str): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 531 | self.save_global(obj, rv) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 532 | return |
| 533 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 534 | # Assert that reduce() returned a tuple |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 535 | if not isinstance(rv, tuple): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 536 | raise PicklingError("%s must return string or tuple" % reduce) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 538 | # Assert that it returned an appropriately sized tuple |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 539 | l = len(rv) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 540 | if not (2 <= l <= 5): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 541 | raise PicklingError("Tuple returned by %s must have " |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 542 | "two to five elements" % reduce) |
Tim Peters | b32a831 | 2003-01-28 00:48:09 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 544 | # Save the reduce() output and finally memoize the object |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 545 | self.save_reduce(obj=obj, *rv) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 547 | def persistent_id(self, obj): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 548 | # This exists so a subclass can override it |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 549 | return None |
| 550 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 551 | def save_pers(self, pid): |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 552 | # Save a persistent id reference |
Tim Peters | bd1cdb9 | 2003-01-28 01:03:10 +0000 | [diff] [blame] | 553 | if self.bin: |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 554 | self.save(pid, save_persistent_id=False) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 555 | self.write(BINPERSID) |
Tim Peters | bd1cdb9 | 2003-01-28 01:03:10 +0000 | [diff] [blame] | 556 | else: |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 557 | try: |
| 558 | self.write(PERSID + str(pid).encode("ascii") + b'\n') |
| 559 | except UnicodeEncodeError: |
| 560 | raise PicklingError( |
| 561 | "persistent IDs in protocol 0 must be ASCII strings") |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 563 | def save_reduce(self, func, args, state=None, listitems=None, |
| 564 | dictitems=None, obj=None): |
Jeremy Hylton | e3a565e | 2003-06-29 16:59:59 +0000 | [diff] [blame] | 565 | # This API is called by some subclasses |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 567 | if not isinstance(args, tuple): |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 568 | raise PicklingError("args from save_reduce() must be a tuple") |
Florent Xicluna | 5d1155c | 2011-10-28 14:45:05 +0200 | [diff] [blame] | 569 | if not callable(func): |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 570 | raise PicklingError("func from save_reduce() must be callable") |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 571 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 572 | save = self.save |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 573 | write = self.write |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 575 | func_name = getattr(func, "__name__", "") |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 576 | if self.proto >= 2 and func_name == "__newobj_ex__": |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 577 | cls, args, kwargs = args |
| 578 | if not hasattr(cls, "__new__"): |
| 579 | raise PicklingError("args[0] from {} args has no __new__" |
| 580 | .format(func_name)) |
| 581 | if obj is not None and cls is not obj.__class__: |
| 582 | raise PicklingError("args[0] from {} args has the wrong class" |
| 583 | .format(func_name)) |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 584 | if self.proto >= 4: |
| 585 | save(cls) |
| 586 | save(args) |
| 587 | save(kwargs) |
| 588 | write(NEWOBJ_EX) |
| 589 | else: |
| 590 | func = partial(cls.__new__, cls, *args, **kwargs) |
| 591 | save(func) |
| 592 | save(()) |
| 593 | write(REDUCE) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 594 | elif self.proto >= 2 and func_name == "__newobj__": |
| 595 | # A __reduce__ implementation can direct protocol 2 or newer to |
Guido van Rossum | d053b4b | 2003-01-31 16:51:45 +0000 | [diff] [blame] | 596 | # use the more efficient NEWOBJ opcode, while still |
| 597 | # allowing protocol 0 and 1 to work normally. For this to |
| 598 | # work, the function returned by __reduce__ should be |
| 599 | # called __newobj__, and its first argument should be a |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 600 | # class. The implementation for __newobj__ |
Guido van Rossum | d053b4b | 2003-01-31 16:51:45 +0000 | [diff] [blame] | 601 | # should be as follows, although pickle has no way to |
| 602 | # verify this: |
| 603 | # |
| 604 | # def __newobj__(cls, *args): |
| 605 | # return cls.__new__(cls, *args) |
| 606 | # |
| 607 | # Protocols 0 and 1 will pickle a reference to __newobj__, |
| 608 | # while protocol 2 (and above) will pickle a reference to |
| 609 | # cls, the remaining args tuple, and the NEWOBJ code, |
| 610 | # which calls cls.__new__(cls, *args) at unpickling time |
| 611 | # (see load_newobj below). If __reduce__ returns a |
| 612 | # three-tuple, the state from the third tuple item will be |
| 613 | # pickled regardless of the protocol, calling __setstate__ |
| 614 | # at unpickling time (see load_build below). |
| 615 | # |
| 616 | # Note that no standard __newobj__ implementation exists; |
| 617 | # you have to provide your own. This is to enforce |
| 618 | # compatibility with Python 2.2 (pickles written using |
| 619 | # protocol 0 or 1 in Python 2.3 should be unpicklable by |
| 620 | # Python 2.2). |
| 621 | cls = args[0] |
| 622 | if not hasattr(cls, "__new__"): |
| 623 | raise PicklingError( |
| 624 | "args[0] from __newobj__ args has no __new__") |
Guido van Rossum | f7f4517 | 2003-01-31 17:17:49 +0000 | [diff] [blame] | 625 | if obj is not None and cls is not obj.__class__: |
| 626 | raise PicklingError( |
| 627 | "args[0] from __newobj__ args has the wrong class") |
Guido van Rossum | d053b4b | 2003-01-31 16:51:45 +0000 | [diff] [blame] | 628 | args = args[1:] |
| 629 | save(cls) |
| 630 | save(args) |
| 631 | write(NEWOBJ) |
| 632 | else: |
| 633 | save(func) |
| 634 | save(args) |
| 635 | write(REDUCE) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | f7f4517 | 2003-01-31 17:17:49 +0000 | [diff] [blame] | 637 | if obj is not None: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 638 | # If the object is already in the memo, this means it is |
| 639 | # recursive. In this case, throw away everything we put on the |
| 640 | # stack, and fetch the object back from the memo. |
| 641 | if id(obj) in self.memo: |
| 642 | write(POP + self.get(self.memo[id(obj)][0])) |
| 643 | else: |
| 644 | self.memoize(obj) |
Guido van Rossum | f7f4517 | 2003-01-31 17:17:49 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 646 | # More new special cases (that work with older protocols as |
| 647 | # well): when __reduce__ returns a tuple with 4 or 5 items, |
| 648 | # the 4th and 5th item should be iterators that provide list |
| 649 | # items and dict items (as (key, value) tuples), or None. |
| 650 | |
| 651 | if listitems is not None: |
| 652 | self._batch_appends(listitems) |
| 653 | |
| 654 | if dictitems is not None: |
| 655 | self._batch_setitems(dictitems) |
| 656 | |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 657 | if state is not None: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 658 | save(state) |
| 659 | write(BUILD) |
| 660 | |
Guido van Rossum | bc64e22 | 2003-01-28 16:34:19 +0000 | [diff] [blame] | 661 | # Methods below this point are dispatched through the dispatch table |
| 662 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 663 | dispatch = {} |
| 664 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 665 | def save_none(self, obj): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 666 | self.write(NONE) |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 667 | dispatch[type(None)] = save_none |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 668 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 669 | def save_bool(self, obj): |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 670 | if self.proto >= 2: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 671 | self.write(NEWTRUE if obj else NEWFALSE) |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 672 | else: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 673 | self.write(TRUE if obj else FALSE) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 674 | dispatch[bool] = save_bool |
| 675 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 676 | def save_long(self, obj): |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 677 | if self.bin: |
| 678 | # If the int is small enough to fit in a signed 4-byte 2's-comp |
| 679 | # format, we can store it more efficiently than the general |
| 680 | # case. |
| 681 | # First one- and two-byte unsigned ints: |
| 682 | if obj >= 0: |
| 683 | if obj <= 0xff: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 684 | self.write(BININT1 + pack("<B", obj)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 685 | return |
| 686 | if obj <= 0xffff: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 687 | self.write(BININT2 + pack("<H", obj)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 688 | return |
| 689 | # Next check for 4-byte signed ints: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 690 | if -0x80000000 <= obj <= 0x7fffffff: |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 691 | self.write(BININT + pack("<i", obj)) |
| 692 | return |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 693 | if self.proto >= 2: |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 694 | encoded = encode_long(obj) |
| 695 | n = len(encoded) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 696 | if n < 256: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 697 | self.write(LONG1 + pack("<B", n) + encoded) |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 698 | else: |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 699 | self.write(LONG4 + pack("<i", n) + encoded) |
Tim Peters | ee1a53c | 2003-02-02 02:57:53 +0000 | [diff] [blame] | 700 | return |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 701 | if -0x80000000 <= obj <= 0x7fffffff: |
| 702 | self.write(INT + repr(obj).encode("ascii") + b'\n') |
| 703 | else: |
| 704 | self.write(LONG + repr(obj).encode("ascii") + b'L\n') |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 705 | dispatch[int] = save_long |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 706 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 707 | def save_float(self, obj): |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 708 | if self.bin: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 709 | self.write(BINFLOAT + pack('>d', obj)) |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 710 | else: |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 711 | self.write(FLOAT + repr(obj).encode("ascii") + b'\n') |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 712 | dispatch[float] = save_float |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 713 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 714 | def save_bytes(self, obj): |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 715 | if self.proto < 3: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 716 | if not obj: # bytes object is empty |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 717 | self.save_reduce(bytes, (), obj=obj) |
| 718 | else: |
| 719 | self.save_reduce(codecs.encode, |
| 720 | (str(obj, 'latin1'), 'latin1'), obj=obj) |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 721 | return |
| 722 | n = len(obj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 723 | if n <= 0xff: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 724 | self.write(SHORT_BINBYTES + pack("<B", n) + obj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 725 | elif n > 0xffffffff and self.proto >= 4: |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 726 | self._write_large_bytes(BINBYTES8 + pack("<Q", n), obj) |
| 727 | elif n >= self.framer._FRAME_SIZE_TARGET: |
| 728 | self._write_large_bytes(BINBYTES + pack("<I", n), obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 729 | else: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 730 | self.write(BINBYTES + pack("<I", n) + obj) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 731 | self.memoize(obj) |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 732 | dispatch[bytes] = save_bytes |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 733 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 734 | def save_str(self, obj): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 735 | if self.bin: |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 736 | encoded = obj.encode('utf-8', 'surrogatepass') |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 737 | n = len(encoded) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 738 | if n <= 0xff and self.proto >= 4: |
| 739 | self.write(SHORT_BINUNICODE + pack("<B", n) + encoded) |
| 740 | elif n > 0xffffffff and self.proto >= 4: |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 741 | self._write_large_bytes(BINUNICODE8 + pack("<Q", n), encoded) |
| 742 | elif n >= self.framer._FRAME_SIZE_TARGET: |
| 743 | self._write_large_bytes(BINUNICODE + pack("<I", n), encoded) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 744 | else: |
| 745 | self.write(BINUNICODE + pack("<I", n) + encoded) |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 746 | else: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 747 | obj = obj.replace("\\", "\\u005c") |
| 748 | obj = obj.replace("\n", "\\u000a") |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 749 | self.write(UNICODE + obj.encode('raw-unicode-escape') + |
| 750 | b'\n') |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 751 | self.memoize(obj) |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 752 | dispatch[str] = save_str |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 754 | def save_tuple(self, obj): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 755 | if not obj: # tuple is empty |
| 756 | if self.bin: |
| 757 | self.write(EMPTY_TUPLE) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 758 | else: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 759 | self.write(MARK + TUPLE) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 760 | return |
| 761 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 762 | n = len(obj) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 763 | save = self.save |
| 764 | memo = self.memo |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 765 | if n <= 3 and self.proto >= 2: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 766 | for element in obj: |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 767 | save(element) |
| 768 | # Subtle. Same as in the big comment below. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 769 | if id(obj) in memo: |
| 770 | get = self.get(memo[id(obj)][0]) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 771 | self.write(POP * n + get) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 772 | else: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 773 | self.write(_tuplesize2code[n]) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 774 | self.memoize(obj) |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 775 | return |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 776 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 777 | # 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] | 778 | # has more than 3 elements. |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 779 | write = self.write |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 780 | write(MARK) |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 781 | for element in obj: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 782 | save(element) |
| 783 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 784 | if id(obj) in memo: |
Tim Peters | f558da0 | 2003-01-28 02:09:55 +0000 | [diff] [blame] | 785 | # Subtle. d was not in memo when we entered save_tuple(), so |
| 786 | # the process of saving the tuple's elements must have saved |
| 787 | # the tuple itself: the tuple is recursive. The proper action |
| 788 | # now is to throw away everything we put on the stack, and |
| 789 | # simply GET the tuple (it's already constructed). This check |
| 790 | # could have been done in the "for element" loop instead, but |
| 791 | # recursive tuples are a rare thing. |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 792 | get = self.get(memo[id(obj)][0]) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 793 | if self.bin: |
Tim Peters | f558da0 | 2003-01-28 02:09:55 +0000 | [diff] [blame] | 794 | write(POP_MARK + get) |
| 795 | else: # proto 0 -- POP_MARK not available |
Tim Peters | d97da80 | 2003-01-28 05:48:29 +0000 | [diff] [blame] | 796 | write(POP * (n+1) + get) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 797 | return |
| 798 | |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 799 | # No recursion. |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 800 | write(TUPLE) |
Tim Peters | 1d63c9f | 2003-02-02 20:29:39 +0000 | [diff] [blame] | 801 | self.memoize(obj) |
Jeremy Hylton | 3422c99 | 2003-01-24 19:29:52 +0000 | [diff] [blame] | 802 | |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 803 | dispatch[tuple] = save_tuple |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 804 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 805 | def save_list(self, obj): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 806 | if self.bin: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 807 | self.write(EMPTY_LIST) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 808 | else: # proto 0 -- can't use EMPTY_LIST |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 809 | self.write(MARK + LIST) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 810 | |
| 811 | self.memoize(obj) |
Alexandre Vassalotti | c7db1d6 | 2008-05-14 21:57:18 +0000 | [diff] [blame] | 812 | self._batch_appends(obj) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 814 | dispatch[list] = save_list |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 816 | _BATCHSIZE = 1000 |
| 817 | |
| 818 | def _batch_appends(self, items): |
| 819 | # Helper to batch up APPENDS sequences |
| 820 | save = self.save |
| 821 | write = self.write |
| 822 | |
| 823 | if not self.bin: |
| 824 | for x in items: |
| 825 | save(x) |
| 826 | write(APPEND) |
| 827 | return |
| 828 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 829 | it = iter(items) |
| 830 | while True: |
| 831 | tmp = list(islice(it, self._BATCHSIZE)) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 832 | n = len(tmp) |
| 833 | if n > 1: |
| 834 | write(MARK) |
| 835 | for x in tmp: |
| 836 | save(x) |
| 837 | write(APPENDS) |
| 838 | elif n: |
| 839 | save(tmp[0]) |
| 840 | write(APPEND) |
| 841 | # else tmp is empty, and we're done |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 842 | if n < self._BATCHSIZE: |
| 843 | return |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 844 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 845 | def save_dict(self, obj): |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 846 | if self.bin: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 847 | self.write(EMPTY_DICT) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 848 | else: # proto 0 -- can't use EMPTY_DICT |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 849 | self.write(MARK + DICT) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 850 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 851 | self.memoize(obj) |
Alexandre Vassalotti | c7db1d6 | 2008-05-14 21:57:18 +0000 | [diff] [blame] | 852 | self._batch_setitems(obj.items()) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 853 | |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 854 | dispatch[dict] = save_dict |
| 855 | if PyStringMap is not None: |
Jeremy Hylton | 2b9d029 | 1998-05-27 22:38:22 +0000 | [diff] [blame] | 856 | dispatch[PyStringMap] = save_dict |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 858 | def _batch_setitems(self, items): |
| 859 | # Helper to batch up SETITEMS sequences; proto >= 1 only |
| 860 | save = self.save |
| 861 | write = self.write |
| 862 | |
| 863 | if not self.bin: |
| 864 | for k, v in items: |
| 865 | save(k) |
| 866 | save(v) |
| 867 | write(SETITEM) |
| 868 | return |
| 869 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 870 | it = iter(items) |
| 871 | while True: |
| 872 | tmp = list(islice(it, self._BATCHSIZE)) |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 873 | n = len(tmp) |
| 874 | if n > 1: |
| 875 | write(MARK) |
| 876 | for k, v in tmp: |
| 877 | save(k) |
| 878 | save(v) |
| 879 | write(SETITEMS) |
| 880 | elif n: |
| 881 | k, v = tmp[0] |
| 882 | save(k) |
| 883 | save(v) |
| 884 | write(SETITEM) |
| 885 | # else tmp is empty, and we're done |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 886 | if n < self._BATCHSIZE: |
| 887 | return |
Guido van Rossum | 25cb7df | 2003-01-31 18:53:21 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 889 | def save_set(self, obj): |
| 890 | save = self.save |
| 891 | write = self.write |
| 892 | |
| 893 | if self.proto < 4: |
| 894 | self.save_reduce(set, (list(obj),), obj=obj) |
| 895 | return |
| 896 | |
| 897 | write(EMPTY_SET) |
| 898 | self.memoize(obj) |
| 899 | |
| 900 | it = iter(obj) |
| 901 | while True: |
| 902 | batch = list(islice(it, self._BATCHSIZE)) |
| 903 | n = len(batch) |
| 904 | if n > 0: |
| 905 | write(MARK) |
| 906 | for item in batch: |
| 907 | save(item) |
| 908 | write(ADDITEMS) |
| 909 | if n < self._BATCHSIZE: |
| 910 | return |
| 911 | dispatch[set] = save_set |
| 912 | |
| 913 | def save_frozenset(self, obj): |
| 914 | save = self.save |
| 915 | write = self.write |
| 916 | |
| 917 | if self.proto < 4: |
| 918 | self.save_reduce(frozenset, (list(obj),), obj=obj) |
| 919 | return |
| 920 | |
| 921 | write(MARK) |
| 922 | for item in obj: |
| 923 | save(item) |
| 924 | |
| 925 | if id(obj) in self.memo: |
| 926 | # If the object is already in the memo, this means it is |
| 927 | # recursive. In this case, throw away everything we put on the |
| 928 | # stack, and fetch the object back from the memo. |
| 929 | write(POP_MARK + self.get(self.memo[id(obj)][0])) |
| 930 | return |
| 931 | |
| 932 | write(FROZENSET) |
| 933 | self.memoize(obj) |
| 934 | dispatch[frozenset] = save_frozenset |
| 935 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 936 | def save_global(self, obj, name=None): |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 937 | write = self.write |
| 938 | memo = self.memo |
| 939 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 940 | if name is None: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 941 | name = getattr(obj, '__qualname__', None) |
Tim Peters | c32d824 | 2001-04-10 02:48:53 +0000 | [diff] [blame] | 942 | if name is None: |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 943 | name = obj.__name__ |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 944 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 945 | module_name = whichmodule(obj, name) |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 946 | try: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 947 | __import__(module_name, level=0) |
| 948 | module = sys.modules[module_name] |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 949 | obj2, parent = _getattribute(module, name) |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 950 | except (ImportError, KeyError, AttributeError): |
| 951 | raise PicklingError( |
| 952 | "Can't pickle %r: it's not found as %s.%s" % |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 953 | (obj, module_name, name)) from None |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 954 | else: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 955 | if obj2 is not obj: |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 956 | raise PicklingError( |
| 957 | "Can't pickle %r: it's not the same object as %s.%s" % |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 958 | (obj, module_name, name)) |
Guido van Rossum | b0a98e9 | 2001-08-17 18:49:52 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 960 | if self.proto >= 2: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 961 | code = _extension_registry.get((module_name, name)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 962 | if code: |
| 963 | assert code > 0 |
| 964 | if code <= 0xff: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 965 | write(EXT1 + pack("<B", code)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 966 | elif code <= 0xffff: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 967 | write(EXT2 + pack("<H", code)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 968 | else: |
| 969 | write(EXT4 + pack("<i", code)) |
| 970 | return |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 971 | lastname = name.rpartition('.')[2] |
| 972 | if parent is module: |
| 973 | name = lastname |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 974 | # Non-ASCII identifiers are supported only with protocols >= 3. |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 975 | if self.proto >= 4: |
| 976 | self.save(module_name) |
| 977 | self.save(name) |
| 978 | write(STACK_GLOBAL) |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 979 | elif parent is not module: |
| 980 | self.save_reduce(getattr, (parent, lastname)) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 981 | elif self.proto >= 3: |
| 982 | write(GLOBAL + bytes(module_name, "utf-8") + b'\n' + |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 983 | bytes(name, "utf-8") + b'\n') |
| 984 | else: |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 985 | if self.fix_imports: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 986 | r_name_mapping = _compat_pickle.REVERSE_NAME_MAPPING |
| 987 | r_import_mapping = _compat_pickle.REVERSE_IMPORT_MAPPING |
| 988 | if (module_name, name) in r_name_mapping: |
| 989 | module_name, name = r_name_mapping[(module_name, name)] |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 990 | elif module_name in r_import_mapping: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 991 | module_name = r_import_mapping[module_name] |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 992 | try: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 993 | write(GLOBAL + bytes(module_name, "ascii") + b'\n' + |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 994 | bytes(name, "ascii") + b'\n') |
| 995 | except UnicodeEncodeError: |
| 996 | raise PicklingError( |
| 997 | "can't pickle global identifier '%s.%s' using " |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 998 | "pickle protocol %i" % (module, name, self.proto)) from None |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 999 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1000 | self.memoize(obj) |
Tim Peters | 3b76983 | 2003-01-28 03:51:36 +0000 | [diff] [blame] | 1001 | |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 1002 | def save_type(self, obj): |
| 1003 | if obj is type(None): |
| 1004 | return self.save_reduce(type, (None,), obj=obj) |
| 1005 | elif obj is type(NotImplemented): |
| 1006 | return self.save_reduce(type, (NotImplemented,), obj=obj) |
| 1007 | elif obj is type(...): |
| 1008 | return self.save_reduce(type, (...,), obj=obj) |
| 1009 | return self.save_global(obj) |
| 1010 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1011 | dispatch[FunctionType] = save_global |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 1012 | dispatch[type] = save_type |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1013 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1014 | |
Guido van Rossum | 1be3175 | 2003-01-28 15:19:53 +0000 | [diff] [blame] | 1015 | # Unpickling machinery |
| 1016 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1017 | class _Unpickler: |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1019 | def __init__(self, file, *, fix_imports=True, |
| 1020 | encoding="ASCII", errors="strict"): |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1021 | """This takes a binary file for reading a pickle data stream. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 1022 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1023 | The protocol version of the pickle is detected automatically, so |
| 1024 | no proto argument is needed. |
| 1025 | |
| 1026 | The argument *file* must have two methods, a read() method that |
| 1027 | takes an integer argument, and a readline() method that requires |
| 1028 | no arguments. Both methods should return bytes. Thus *file* |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 1029 | can be a binary file object opened for reading, an io.BytesIO |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1030 | object, or any other custom object that meets this interface. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 1031 | |
Guido van Rossum | feea078 | 2007-10-10 18:00:50 +0000 | [diff] [blame] | 1032 | The file-like object must have two methods, a read() method |
| 1033 | that takes an integer argument, and a readline() method that |
| 1034 | requires no arguments. Both methods should return bytes. |
| 1035 | Thus file-like object can be a binary file object opened for |
| 1036 | reading, a BytesIO object, or any other custom object that |
| 1037 | meets this interface. |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1038 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1039 | Optional keyword arguments are *fix_imports*, *encoding* and |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1040 | *errors*, which are used to control compatibility support for |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1041 | pickle stream generated by Python 2. If *fix_imports* is True, |
| 1042 | pickle will try to map the old Python 2 names to the new names |
| 1043 | used in Python 3. The *encoding* and *errors* tell pickle how |
| 1044 | to decode 8-bit string instances pickled by Python 2; these |
| 1045 | default to 'ASCII' and 'strict', respectively. *encoding* can be |
| 1046 | 'bytes' to read theses 8-bit string instances as bytes objects. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 1047 | """ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1048 | self._file_readline = file.readline |
| 1049 | self._file_read = file.read |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1050 | self.memo = {} |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1051 | self.encoding = encoding |
| 1052 | self.errors = errors |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1053 | self.proto = 0 |
| 1054 | self.fix_imports = fix_imports |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1056 | def load(self): |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1057 | """Read a pickled object representation from the open file. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1059 | Return the reconstituted object hierarchy specified in the file. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 1060 | """ |
Alexandre Vassalotti | 3cfcab9 | 2008-12-27 09:30:39 +0000 | [diff] [blame] | 1061 | # Check whether Unpickler was initialized correctly. This is |
| 1062 | # only needed to mimic the behavior of _pickle.Unpickler.dump(). |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1063 | if not hasattr(self, "_file_read"): |
Alexandre Vassalotti | 3cfcab9 | 2008-12-27 09:30:39 +0000 | [diff] [blame] | 1064 | raise UnpicklingError("Unpickler.__init__() was not called by " |
| 1065 | "%s.__init__()" % (self.__class__.__name__,)) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1066 | self._unframer = _Unframer(self._file_read, self._file_readline) |
| 1067 | self.read = self._unframer.read |
| 1068 | self.readline = self._unframer.readline |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1069 | self.metastack = [] |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1070 | self.stack = [] |
| 1071 | self.append = self.stack.append |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1072 | self.proto = 0 |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1073 | read = self.read |
| 1074 | dispatch = self.dispatch |
| 1075 | try: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1076 | while True: |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1077 | key = read(1) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1078 | if not key: |
| 1079 | raise EOFError |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1080 | assert isinstance(key, bytes_types) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1081 | dispatch[key[0]](self) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1082 | except _Stop as stopinst: |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 1083 | return stopinst.value |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1084 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1085 | # Return a list of items pushed in the stack after last MARK instruction. |
| 1086 | def pop_mark(self): |
| 1087 | items = self.stack |
| 1088 | self.stack = self.metastack.pop() |
| 1089 | self.append = self.stack.append |
| 1090 | return items |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1091 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1092 | def persistent_load(self, pid): |
Benjamin Peterson | 49956b2 | 2009-01-10 17:05:44 +0000 | [diff] [blame] | 1093 | raise UnpicklingError("unsupported persistent id encountered") |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1094 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1095 | dispatch = {} |
| 1096 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1097 | def load_proto(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1098 | proto = self.read(1)[0] |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1099 | if not 0 <= proto <= HIGHEST_PROTOCOL: |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 1100 | raise ValueError("unsupported pickle protocol: %d" % proto) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1101 | self.proto = proto |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1102 | dispatch[PROTO[0]] = load_proto |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1103 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1104 | def load_frame(self): |
| 1105 | frame_size, = unpack('<Q', self.read(8)) |
| 1106 | if frame_size > sys.maxsize: |
| 1107 | raise ValueError("frame size > sys.maxsize: %d" % frame_size) |
| 1108 | self._unframer.load_frame(frame_size) |
| 1109 | dispatch[FRAME[0]] = load_frame |
| 1110 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1111 | def load_persid(self): |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 1112 | try: |
| 1113 | pid = self.readline()[:-1].decode("ascii") |
| 1114 | except UnicodeDecodeError: |
| 1115 | raise UnpicklingError( |
| 1116 | "persistent IDs in protocol 0 must be ASCII strings") |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1117 | self.append(self.persistent_load(pid)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1118 | dispatch[PERSID[0]] = load_persid |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1119 | |
| 1120 | def load_binpersid(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1121 | pid = self.stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1122 | self.append(self.persistent_load(pid)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1123 | dispatch[BINPERSID[0]] = load_binpersid |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1124 | |
| 1125 | def load_none(self): |
| 1126 | self.append(None) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1127 | dispatch[NONE[0]] = load_none |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1128 | |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 1129 | def load_false(self): |
| 1130 | self.append(False) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1131 | dispatch[NEWFALSE[0]] = load_false |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 1132 | |
| 1133 | def load_true(self): |
| 1134 | self.append(True) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1135 | dispatch[NEWTRUE[0]] = load_true |
Guido van Rossum | 7d97d31 | 2003-01-28 04:25:27 +0000 | [diff] [blame] | 1136 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1137 | def load_int(self): |
Tim Peters | 19ef62d | 2001-08-28 22:21:18 +0000 | [diff] [blame] | 1138 | data = self.readline() |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 1139 | if data == FALSE[1:]: |
| 1140 | val = False |
| 1141 | elif data == TRUE[1:]: |
| 1142 | val = True |
| 1143 | else: |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1144 | val = int(data, 0) |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 1145 | self.append(val) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1146 | dispatch[INT[0]] = load_int |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1147 | |
| 1148 | def load_binint(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1149 | self.append(unpack('<i', self.read(4))[0]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1150 | dispatch[BININT[0]] = load_binint |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1151 | |
| 1152 | def load_binint1(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1153 | self.append(self.read(1)[0]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1154 | dispatch[BININT1[0]] = load_binint1 |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1155 | |
| 1156 | def load_binint2(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1157 | self.append(unpack('<H', self.read(2))[0]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1158 | dispatch[BININT2[0]] = load_binint2 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1159 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1160 | def load_long(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1161 | val = self.readline()[:-1] |
| 1162 | if val and val[-1] == b'L'[0]: |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1163 | val = val[:-1] |
Guido van Rossum | feea078 | 2007-10-10 18:00:50 +0000 | [diff] [blame] | 1164 | self.append(int(val, 0)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1165 | dispatch[LONG[0]] = load_long |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1166 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1167 | def load_long1(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1168 | n = self.read(1)[0] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1169 | data = self.read(n) |
| 1170 | self.append(decode_long(data)) |
| 1171 | dispatch[LONG1[0]] = load_long1 |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1172 | |
| 1173 | def load_long4(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1174 | n, = unpack('<i', self.read(4)) |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1175 | if n < 0: |
| 1176 | # Corrupt or hostile pickle -- we never write one like this |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 1177 | raise UnpicklingError("LONG pickle has negative byte count") |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1178 | data = self.read(n) |
| 1179 | self.append(decode_long(data)) |
| 1180 | dispatch[LONG4[0]] = load_long4 |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1181 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1182 | def load_float(self): |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 1183 | self.append(float(self.readline()[:-1])) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1184 | dispatch[FLOAT[0]] = load_float |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1185 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1186 | def load_binfloat(self): |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 1187 | self.append(unpack('>d', self.read(8))[0]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1188 | dispatch[BINFLOAT[0]] = load_binfloat |
Guido van Rossum | d370379 | 1998-10-22 20:15:36 +0000 | [diff] [blame] | 1189 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1190 | def _decode_string(self, value): |
| 1191 | # Used to allow strings from Python 2 to be decoded either as |
| 1192 | # bytes or Unicode strings. This should be used only with the |
| 1193 | # STRING, BINSTRING and SHORT_BINSTRING opcodes. |
| 1194 | if self.encoding == "bytes": |
| 1195 | return value |
| 1196 | else: |
| 1197 | return value.decode(self.encoding, self.errors) |
| 1198 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1199 | def load_string(self): |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 1200 | data = self.readline()[:-1] |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1201 | # Strip outermost quotes |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 1202 | if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'': |
| 1203 | data = data[1:-1] |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1204 | else: |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 1205 | raise UnpicklingError("the STRING opcode argument must be quoted") |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1206 | self.append(self._decode_string(codecs.escape_decode(data)[0])) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1207 | dispatch[STRING[0]] = load_string |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1208 | |
| 1209 | def load_binstring(self): |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1210 | # Deprecated BINSTRING uses signed 32-bit length |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1211 | len, = unpack('<i', self.read(4)) |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1212 | if len < 0: |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 1213 | raise UnpicklingError("BINSTRING pickle has negative byte count") |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1214 | data = self.read(len) |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1215 | self.append(self._decode_string(data)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1216 | dispatch[BINSTRING[0]] = load_binstring |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1217 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1218 | def load_binbytes(self): |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1219 | len, = unpack('<I', self.read(4)) |
| 1220 | if len > maxsize: |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 1221 | raise UnpicklingError("BINBYTES exceeds system's maximum size " |
| 1222 | "of %d bytes" % maxsize) |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1223 | self.append(self.read(len)) |
| 1224 | dispatch[BINBYTES[0]] = load_binbytes |
| 1225 | |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 1226 | def load_unicode(self): |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1227 | self.append(str(self.readline()[:-1], 'raw-unicode-escape')) |
| 1228 | dispatch[UNICODE[0]] = load_unicode |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 1229 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1230 | def load_binunicode(self): |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1231 | len, = unpack('<I', self.read(4)) |
| 1232 | if len > maxsize: |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 1233 | raise UnpicklingError("BINUNICODE exceeds system's maximum size " |
| 1234 | "of %d bytes" % maxsize) |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 1235 | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1236 | dispatch[BINUNICODE[0]] = load_binunicode |
Guido van Rossum | b5f2f1b | 2000-03-10 23:20:09 +0000 | [diff] [blame] | 1237 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1238 | def load_binunicode8(self): |
| 1239 | len, = unpack('<Q', self.read(8)) |
| 1240 | if len > maxsize: |
| 1241 | raise UnpicklingError("BINUNICODE8 exceeds system's maximum size " |
| 1242 | "of %d bytes" % maxsize) |
| 1243 | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
| 1244 | dispatch[BINUNICODE8[0]] = load_binunicode8 |
| 1245 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 1246 | def load_binbytes8(self): |
| 1247 | len, = unpack('<Q', self.read(8)) |
| 1248 | if len > maxsize: |
| 1249 | raise UnpicklingError("BINBYTES8 exceeds system's maximum size " |
| 1250 | "of %d bytes" % maxsize) |
| 1251 | self.append(self.read(len)) |
| 1252 | dispatch[BINBYTES8[0]] = load_binbytes8 |
| 1253 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1254 | def load_short_binstring(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1255 | len = self.read(1)[0] |
| 1256 | data = self.read(len) |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 1257 | self.append(self._decode_string(data)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1258 | dispatch[SHORT_BINSTRING[0]] = load_short_binstring |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1259 | |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1260 | def load_short_binbytes(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1261 | len = self.read(1)[0] |
| 1262 | self.append(self.read(len)) |
Guido van Rossum | f416981 | 2008-03-17 22:56:06 +0000 | [diff] [blame] | 1263 | dispatch[SHORT_BINBYTES[0]] = load_short_binbytes |
| 1264 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1265 | def load_short_binunicode(self): |
| 1266 | len = self.read(1)[0] |
| 1267 | self.append(str(self.read(len), 'utf-8', 'surrogatepass')) |
| 1268 | dispatch[SHORT_BINUNICODE[0]] = load_short_binunicode |
| 1269 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1270 | def load_tuple(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1271 | items = self.pop_mark() |
| 1272 | self.append(tuple(items)) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1273 | dispatch[TUPLE[0]] = load_tuple |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1274 | |
| 1275 | def load_empty_tuple(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1276 | self.append(()) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1277 | dispatch[EMPTY_TUPLE[0]] = load_empty_tuple |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1278 | |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1279 | def load_tuple1(self): |
| 1280 | self.stack[-1] = (self.stack[-1],) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1281 | dispatch[TUPLE1[0]] = load_tuple1 |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1282 | |
| 1283 | def load_tuple2(self): |
| 1284 | self.stack[-2:] = [(self.stack[-2], self.stack[-1])] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1285 | dispatch[TUPLE2[0]] = load_tuple2 |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1286 | |
| 1287 | def load_tuple3(self): |
| 1288 | 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] | 1289 | dispatch[TUPLE3[0]] = load_tuple3 |
Guido van Rossum | 44f0ea5 | 2003-01-28 04:14:51 +0000 | [diff] [blame] | 1290 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1291 | def load_empty_list(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1292 | self.append([]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1293 | dispatch[EMPTY_LIST[0]] = load_empty_list |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1294 | |
| 1295 | def load_empty_dictionary(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1296 | self.append({}) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1297 | dispatch[EMPTY_DICT[0]] = load_empty_dictionary |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1299 | def load_empty_set(self): |
| 1300 | self.append(set()) |
| 1301 | dispatch[EMPTY_SET[0]] = load_empty_set |
| 1302 | |
| 1303 | def load_frozenset(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1304 | items = self.pop_mark() |
| 1305 | self.append(frozenset(items)) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1306 | dispatch[FROZENSET[0]] = load_frozenset |
| 1307 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1308 | def load_list(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1309 | items = self.pop_mark() |
| 1310 | self.append(items) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1311 | dispatch[LIST[0]] = load_list |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1312 | |
| 1313 | def load_dict(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1314 | items = self.pop_mark() |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1315 | d = {items[i]: items[i+1] |
| 1316 | for i in range(0, len(items), 2)} |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1317 | self.append(d) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1318 | dispatch[DICT[0]] = load_dict |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1319 | |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1320 | # INST and OBJ differ only in how they get a class object. It's not |
| 1321 | # only sensible to do the rest in a common routine, the two routines |
| 1322 | # previously diverged and grew different bugs. |
| 1323 | # klass is the class to instantiate, and k points to the topmost mark |
| 1324 | # object, following which are the arguments for klass.__init__. |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1325 | def _instantiate(self, klass, args): |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 1326 | if (args or not isinstance(klass, type) or |
| 1327 | hasattr(klass, "__getinitargs__")): |
Guido van Rossum | 743d17e | 1998-09-15 20:25:57 +0000 | [diff] [blame] | 1328 | try: |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1329 | value = klass(*args) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1330 | except TypeError as err: |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 1331 | raise TypeError("in constructor for %s: %s" % |
| 1332 | (klass.__name__, str(err)), sys.exc_info()[2]) |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 1333 | else: |
| 1334 | value = klass.__new__(klass) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1335 | self.append(value) |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1336 | |
| 1337 | def load_inst(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1338 | module = self.readline()[:-1].decode("ascii") |
| 1339 | name = self.readline()[:-1].decode("ascii") |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1340 | klass = self.find_class(module, name) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1341 | self._instantiate(klass, self.pop_mark()) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1342 | dispatch[INST[0]] = load_inst |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1343 | |
| 1344 | def load_obj(self): |
Tim Peters | d01c1e9 | 2003-01-30 15:41:46 +0000 | [diff] [blame] | 1345 | # Stack is ... markobject classobject arg1 arg2 ... |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1346 | args = self.pop_mark() |
| 1347 | cls = args.pop(0) |
| 1348 | self._instantiate(cls, args) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1349 | dispatch[OBJ[0]] = load_obj |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1350 | |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1351 | def load_newobj(self): |
| 1352 | args = self.stack.pop() |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1353 | cls = self.stack.pop() |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1354 | obj = cls.__new__(cls, *args) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1355 | self.append(obj) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1356 | dispatch[NEWOBJ[0]] = load_newobj |
Guido van Rossum | 3a41c61 | 2003-01-28 15:10:22 +0000 | [diff] [blame] | 1357 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1358 | def load_newobj_ex(self): |
| 1359 | kwargs = self.stack.pop() |
| 1360 | args = self.stack.pop() |
| 1361 | cls = self.stack.pop() |
| 1362 | obj = cls.__new__(cls, *args, **kwargs) |
| 1363 | self.append(obj) |
| 1364 | dispatch[NEWOBJ_EX[0]] = load_newobj_ex |
| 1365 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1366 | def load_global(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1367 | module = self.readline()[:-1].decode("utf-8") |
| 1368 | name = self.readline()[:-1].decode("utf-8") |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1369 | klass = self.find_class(module, name) |
| 1370 | self.append(klass) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1371 | dispatch[GLOBAL[0]] = load_global |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1373 | def load_stack_global(self): |
| 1374 | name = self.stack.pop() |
| 1375 | module = self.stack.pop() |
| 1376 | if type(name) is not str or type(module) is not str: |
| 1377 | raise UnpicklingError("STACK_GLOBAL requires str") |
| 1378 | self.append(self.find_class(module, name)) |
| 1379 | dispatch[STACK_GLOBAL[0]] = load_stack_global |
| 1380 | |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1381 | def load_ext1(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1382 | code = self.read(1)[0] |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1383 | self.get_extension(code) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1384 | dispatch[EXT1[0]] = load_ext1 |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1385 | |
| 1386 | def load_ext2(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1387 | code, = unpack('<H', self.read(2)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1388 | self.get_extension(code) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1389 | dispatch[EXT2[0]] = load_ext2 |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1390 | |
| 1391 | def load_ext4(self): |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1392 | code, = unpack('<i', self.read(4)) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1393 | self.get_extension(code) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1394 | dispatch[EXT4[0]] = load_ext4 |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1395 | |
| 1396 | def get_extension(self, code): |
| 1397 | nil = [] |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1398 | obj = _extension_cache.get(code, nil) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1399 | if obj is not nil: |
| 1400 | self.append(obj) |
| 1401 | return |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1402 | key = _inverted_registry.get(code) |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1403 | if not key: |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1404 | if code <= 0: # note that 0 is forbidden |
| 1405 | # Corrupt or hostile pickle. |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 1406 | raise UnpicklingError("EXT specifies code <= 0") |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1407 | raise ValueError("unregistered extension code %d" % code) |
| 1408 | obj = self.find_class(*key) |
Guido van Rossum | d4b920c | 2003-02-04 01:54:49 +0000 | [diff] [blame] | 1409 | _extension_cache[code] = obj |
Guido van Rossum | 255f3ee | 2003-01-29 06:14:11 +0000 | [diff] [blame] | 1410 | self.append(obj) |
| 1411 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1412 | def find_class(self, module, name): |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1413 | # Subclasses may override this. |
| 1414 | if self.proto < 3 and self.fix_imports: |
| 1415 | if (module, name) in _compat_pickle.NAME_MAPPING: |
| 1416 | module, name = _compat_pickle.NAME_MAPPING[(module, name)] |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 1417 | elif module in _compat_pickle.IMPORT_MAPPING: |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1418 | module = _compat_pickle.IMPORT_MAPPING[module] |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1419 | __import__(module, level=0) |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1420 | if self.proto >= 4: |
| 1421 | return _getattribute(sys.modules[module], name)[0] |
| 1422 | else: |
| 1423 | return getattr(sys.modules[module], name) |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1424 | |
| 1425 | def load_reduce(self): |
| 1426 | stack = self.stack |
Guido van Rossum | b26a97a | 2003-01-28 22:29:13 +0000 | [diff] [blame] | 1427 | args = stack.pop() |
| 1428 | func = stack[-1] |
Serhiy Storchaka | a8d83f5 | 2015-12-01 00:39:25 +0200 | [diff] [blame] | 1429 | stack[-1] = func(*args) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1430 | dispatch[REDUCE[0]] = load_reduce |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1431 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1432 | def load_pop(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1433 | if self.stack: |
| 1434 | del self.stack[-1] |
| 1435 | else: |
| 1436 | self.pop_mark() |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1437 | dispatch[POP[0]] = load_pop |
Guido van Rossum | 7b5430f | 1995-03-04 22:25:21 +0000 | [diff] [blame] | 1438 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1439 | def load_pop_mark(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1440 | self.pop_mark() |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1441 | dispatch[POP_MARK[0]] = load_pop_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1442 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1443 | def load_dup(self): |
Guido van Rossum | b1062fc | 1998-03-31 17:00:46 +0000 | [diff] [blame] | 1444 | self.append(self.stack[-1]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1445 | dispatch[DUP[0]] = load_dup |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1446 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1447 | def load_get(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1448 | i = int(self.readline()[:-1]) |
| 1449 | self.append(self.memo[i]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1450 | dispatch[GET[0]] = load_get |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1451 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1452 | def load_binget(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1453 | i = self.read(1)[0] |
| 1454 | self.append(self.memo[i]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1455 | dispatch[BINGET[0]] = load_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1456 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1457 | def load_long_binget(self): |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1458 | i, = unpack('<I', self.read(4)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1459 | self.append(self.memo[i]) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1460 | dispatch[LONG_BINGET[0]] = load_long_binget |
Guido van Rossum | 7853647 | 1996-04-12 13:36:27 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1462 | def load_put(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1463 | i = int(self.readline()[:-1]) |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 1464 | if i < 0: |
| 1465 | raise ValueError("negative PUT argument") |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1466 | self.memo[i] = self.stack[-1] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1467 | dispatch[PUT[0]] = load_put |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1468 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1469 | def load_binput(self): |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1470 | i = self.read(1)[0] |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 1471 | if i < 0: |
| 1472 | raise ValueError("negative BINPUT argument") |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1473 | self.memo[i] = self.stack[-1] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1474 | dispatch[BINPUT[0]] = load_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1475 | |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1476 | def load_long_binput(self): |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1477 | i, = unpack('<I', self.read(4)) |
| 1478 | if i > maxsize: |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 1479 | raise ValueError("negative LONG_BINPUT argument") |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1480 | self.memo[i] = self.stack[-1] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1481 | dispatch[LONG_BINPUT[0]] = load_long_binput |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1483 | def load_memoize(self): |
| 1484 | memo = self.memo |
| 1485 | memo[len(memo)] = self.stack[-1] |
| 1486 | dispatch[MEMOIZE[0]] = load_memoize |
| 1487 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1488 | def load_append(self): |
| 1489 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1490 | value = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1491 | list = stack[-1] |
| 1492 | list.append(value) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1493 | dispatch[APPEND[0]] = load_append |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1494 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1495 | def load_appends(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1496 | items = self.pop_mark() |
| 1497 | list_obj = self.stack[-1] |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 1498 | try: |
| 1499 | extend = list_obj.extend |
| 1500 | except AttributeError: |
| 1501 | pass |
Alexandre Vassalotti | 1f7492c | 2013-04-20 13:19:46 -0700 | [diff] [blame] | 1502 | else: |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 1503 | extend(items) |
| 1504 | return |
| 1505 | # Even if the PEP 307 requires extend() and append() methods, |
| 1506 | # fall back on append() if the object has no extend() method |
| 1507 | # for backward compatibility. |
| 1508 | append = list_obj.append |
| 1509 | for item in items: |
| 1510 | append(item) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1511 | dispatch[APPENDS[0]] = load_appends |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1512 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1513 | def load_setitem(self): |
| 1514 | stack = self.stack |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1515 | value = stack.pop() |
| 1516 | key = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1517 | dict = stack[-1] |
| 1518 | dict[key] = value |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1519 | dispatch[SETITEM[0]] = load_setitem |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1520 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1521 | def load_setitems(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1522 | items = self.pop_mark() |
| 1523 | dict = self.stack[-1] |
| 1524 | for i in range(0, len(items), 2): |
| 1525 | dict[items[i]] = items[i + 1] |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1526 | dispatch[SETITEMS[0]] = load_setitems |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1527 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1528 | def load_additems(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1529 | items = self.pop_mark() |
| 1530 | set_obj = self.stack[-1] |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1531 | if isinstance(set_obj, set): |
| 1532 | set_obj.update(items) |
| 1533 | else: |
| 1534 | add = set_obj.add |
| 1535 | for item in items: |
| 1536 | add(item) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1537 | dispatch[ADDITEMS[0]] = load_additems |
| 1538 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1539 | def load_build(self): |
| 1540 | stack = self.stack |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1541 | state = stack.pop() |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1542 | inst = stack[-1] |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1543 | setstate = getattr(inst, "__setstate__", None) |
Serhiy Storchaka | a3e32c9 | 2013-04-14 13:37:02 +0300 | [diff] [blame] | 1544 | if setstate is not None: |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1545 | setstate(state) |
| 1546 | return |
| 1547 | slotstate = None |
| 1548 | if isinstance(state, tuple) and len(state) == 2: |
| 1549 | state, slotstate = state |
| 1550 | if state: |
Alexandre Vassalotti | ebfecfd | 2009-05-25 18:50:33 +0000 | [diff] [blame] | 1551 | inst_dict = inst.__dict__ |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 1552 | intern = sys.intern |
Alexandre Vassalotti | ebfecfd | 2009-05-25 18:50:33 +0000 | [diff] [blame] | 1553 | for k, v in state.items(): |
| 1554 | if type(k) is str: |
| 1555 | inst_dict[intern(k)] = v |
| 1556 | else: |
| 1557 | inst_dict[k] = v |
Guido van Rossum | ac5b5d2 | 2003-01-28 22:01:16 +0000 | [diff] [blame] | 1558 | if slotstate: |
| 1559 | for k, v in slotstate.items(): |
| 1560 | setattr(inst, k, v) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1561 | dispatch[BUILD[0]] = load_build |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1562 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1563 | def load_mark(self): |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 1564 | self.metastack.append(self.stack) |
| 1565 | self.stack = [] |
| 1566 | self.append = self.stack.append |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1567 | dispatch[MARK[0]] = load_mark |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | b72cf2d | 1997-04-09 17:32:51 +0000 | [diff] [blame] | 1569 | def load_stop(self): |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 1570 | value = self.stack.pop() |
Guido van Rossum | ff87174 | 2000-12-13 18:11:56 +0000 | [diff] [blame] | 1571 | raise _Stop(value) |
Guido van Rossum | 2e6a4b3 | 2007-05-04 19:56:22 +0000 | [diff] [blame] | 1572 | dispatch[STOP[0]] = load_stop |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 1573 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1575 | # Shorthands |
| 1576 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1577 | def _dump(obj, file, protocol=None, *, fix_imports=True): |
| 1578 | _Pickler(file, protocol, fix_imports=fix_imports).dump(obj) |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1580 | def _dumps(obj, protocol=None, *, fix_imports=True): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 1581 | f = io.BytesIO() |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1582 | _Pickler(f, protocol, fix_imports=fix_imports).dump(obj) |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 1583 | res = f.getvalue() |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1584 | assert isinstance(res, bytes_types) |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 1585 | return res |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1587 | def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict"): |
| 1588 | return _Unpickler(file, fix_imports=fix_imports, |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 1589 | encoding=encoding, errors=errors).load() |
Guido van Rossum | 0c891ce | 1995-03-14 15:09:05 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1591 | def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict"): |
Guido van Rossum | cfe5f20 | 2007-05-08 21:26:54 +0000 | [diff] [blame] | 1592 | if isinstance(s, str): |
| 1593 | raise TypeError("Can't load pickle from unicode string") |
| 1594 | file = io.BytesIO(s) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1595 | return _Unpickler(file, fix_imports=fix_imports, |
| 1596 | encoding=encoding, errors=errors).load() |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1597 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1598 | # Use the faster _pickle if possible |
| 1599 | try: |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1600 | from _pickle import ( |
| 1601 | PickleError, |
| 1602 | PicklingError, |
| 1603 | UnpicklingError, |
| 1604 | Pickler, |
| 1605 | Unpickler, |
| 1606 | dump, |
| 1607 | dumps, |
| 1608 | load, |
| 1609 | loads |
| 1610 | ) |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 1611 | except ImportError: |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1612 | Pickler, Unpickler = _Pickler, _Unpickler |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1613 | dump, dumps, load, loads = _dump, _dumps, _load, _loads |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1615 | # Doctest |
Guido van Rossum | d6c9e63 | 2003-01-28 03:49:52 +0000 | [diff] [blame] | 1616 | def _test(): |
| 1617 | import doctest |
| 1618 | return doctest.testmod() |
| 1619 | |
| 1620 | if __name__ == "__main__": |
Florent Xicluna | 54540ec | 2011-11-04 08:29:17 +0100 | [diff] [blame] | 1621 | import argparse |
Alexander Belopolsky | 455f7bd | 2010-07-27 23:02:38 +0000 | [diff] [blame] | 1622 | parser = argparse.ArgumentParser( |
| 1623 | description='display contents of the pickle files') |
| 1624 | parser.add_argument( |
| 1625 | 'pickle_file', type=argparse.FileType('br'), |
| 1626 | nargs='*', help='the pickle file') |
| 1627 | parser.add_argument( |
| 1628 | '-t', '--test', action='store_true', |
| 1629 | help='run self-test suite') |
| 1630 | parser.add_argument( |
| 1631 | '-v', action='store_true', |
| 1632 | help='run verbosely; only affects self-test run') |
| 1633 | args = parser.parse_args() |
| 1634 | if args.test: |
| 1635 | _test() |
| 1636 | else: |
| 1637 | if not args.pickle_file: |
| 1638 | parser.print_help() |
| 1639 | else: |
| 1640 | import pprint |
| 1641 | for f in args.pickle_file: |
| 1642 | obj = load(f) |
| 1643 | pprint.pprint(obj) |