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