| 1135 | # Unpickling machinery |
| 1136 | |
| 1137 | class _Unpickler: |
| 1138 | |
| 1139 | def __init__(self, file, *, fix_imports=True, |
| 1140 | encoding="ASCII", errors="strict", buffers=None): |
| 1141 | """This takes a binary file for reading a pickle data stream. |
| 1142 | |
| 1143 | The protocol version of the pickle is detected automatically, so |
| 1144 | no proto argument is needed. |
| 1145 | |
| 1146 | The argument *file* must have two methods, a read() method that |
| 1147 | takes an integer argument, and a readline() method that requires |
| 1148 | no arguments. Both methods should return bytes. Thus *file* |
| 1149 | can be a binary file object opened for reading, an io.BytesIO |
| 1150 | object, or any other custom object that meets this interface. |
| 1151 | |
| 1152 | The file-like object must have two methods, a read() method |
| 1153 | that takes an integer argument, and a readline() method that |
| 1154 | requires no arguments. Both methods should return bytes. |
| 1155 | Thus file-like object can be a binary file object opened for |
| 1156 | reading, a BytesIO object, or any other custom object that |
| 1157 | meets this interface. |
| 1158 | |
| 1159 | If *buffers* is not None, it should be an iterable of buffer-enabled |
| 1160 | objects that is consumed each time the pickle stream references |
| 1161 | an out-of-band buffer view. Such buffers have been given in order |
| 1162 | to the *buffer_callback* of a Pickler object. |
| 1163 | |
| 1164 | If *buffers* is None (the default), then the buffers are taken |
| 1165 | from the pickle stream, assuming they are serialized there. |
| 1166 | It is an error for *buffers* to be None if the pickle stream |
| 1167 | was produced with a non-None *buffer_callback*. |
| 1168 | |
| 1169 | Other optional arguments are *fix_imports*, *encoding* and |
| 1170 | *errors*, which are used to control compatibility support for |
| 1171 | pickle stream generated by Python 2. If *fix_imports* is True, |
| 1172 | pickle will try to map the old Python 2 names to the new names |
| 1173 | used in Python 3. The *encoding* and *errors* tell pickle how |
| 1174 | to decode 8-bit string instances pickled by Python 2; these |
| 1175 | default to 'ASCII' and 'strict', respectively. *encoding* can be |
| 1176 | 'bytes' to read these 8-bit string instances as bytes objects. |
| 1177 | """ |
| 1178 | self._buffers = iter(buffers) if buffers is not None else None |
| 1179 | self._file_readline = file.readline |
| 1180 | self._file_read = file.read |
| 1181 | self.memo = {} |
| 1182 | self.encoding = encoding |
| 1183 | self.errors = errors |
| 1184 | self.proto = 0 |
| 1185 | self.fix_imports = fix_imports |
| 1186 | |
| 1187 | def load(self): |
| 1188 | """Read a pickled object representation from the open file. |
| 1189 | |
| 1190 | Return the reconstituted object hierarchy specified in the file. |
| 1191 | """ |
| 1192 | # Check whether Unpickler was initialized correctly. This is |
| 1193 | # only needed to mimic the behavior of _pickle.Unpickler.dump(). |
| 1194 | if not hasattr(self, "_file_read"): |