| 1248 | # Unpickling machinery |
| 1249 | |
| 1250 | class _Unpickler: |
| 1251 | |
| 1252 | def __init__(self, file, *, fix_imports=True, |
| 1253 | encoding="ASCII", errors="strict", buffers=None): |
| 1254 | """This takes a binary file for reading a pickle data stream. |
| 1255 | |
| 1256 | The protocol version of the pickle is detected automatically, so |
| 1257 | no proto argument is needed. |
| 1258 | |
| 1259 | The argument *file* must have two methods, a read() method that |
| 1260 | takes an integer argument, and a readline() method that requires |
| 1261 | no arguments. Both methods should return bytes. Thus *file* |
| 1262 | can be a binary file object opened for reading, an io.BytesIO |
| 1263 | object, or any other custom object that meets this interface. |
| 1264 | |
| 1265 | The file-like object must have two methods, a read() method |
| 1266 | that takes an integer argument, and a readline() method that |
| 1267 | requires no arguments. Both methods should return bytes. |
| 1268 | Thus file-like object can be a binary file object opened for |
| 1269 | reading, a BytesIO object, or any other custom object that |
| 1270 | meets this interface. |
| 1271 | |
| 1272 | If *buffers* is not None, it should be an iterable of buffer-enabled |
| 1273 | objects that is consumed each time the pickle stream references |
| 1274 | an out-of-band buffer view. Such buffers have been given in order |
| 1275 | to the *buffer_callback* of a Pickler object. |
| 1276 | |
| 1277 | If *buffers* is None (the default), then the buffers are taken |
| 1278 | from the pickle stream, assuming they are serialized there. |
| 1279 | It is an error for *buffers* to be None if the pickle stream |
| 1280 | was produced with a non-None *buffer_callback*. |
| 1281 | |
| 1282 | Other optional arguments are *fix_imports*, *encoding* and |
| 1283 | *errors*, which are used to control compatibility support for |
| 1284 | pickle stream generated by Python 2. If *fix_imports* is True, |
| 1285 | pickle will try to map the old Python 2 names to the new names |
| 1286 | used in Python 3. The *encoding* and *errors* tell pickle how |
| 1287 | to decode 8-bit string instances pickled by Python 2; these |
| 1288 | default to 'ASCII' and 'strict', respectively. *encoding* can be |
| 1289 | 'bytes' to read these 8-bit string instances as bytes objects. |
| 1290 | """ |
| 1291 | self._buffers = iter(buffers) if buffers is not None else None |
| 1292 | self._file_readline = file.readline |
| 1293 | self._file_read = file.read |
| 1294 | self.memo = {} |
| 1295 | self.encoding = encoding |
| 1296 | self.errors = errors |
| 1297 | self.proto = 0 |
| 1298 | self.fix_imports = fix_imports |
| 1299 | |
| 1300 | def load(self): |
| 1301 | """Read a pickled object representation from the open file. |
| 1302 | |
| 1303 | Return the reconstituted object hierarchy specified in the file. |
| 1304 | """ |
| 1305 | # Check whether Unpickler was initialized correctly. This is |
| 1306 | # only needed to mimic the behavior of _pickle.Unpickler.dump(). |
| 1307 | if not hasattr(self, "_file_read"): |