|
13 | 13 | dump(object, file) |
14 | 14 | dumps(object) -> string |
15 | 15 | load(file) -> object |
16 | | - loads(string) -> object |
| 16 | + loads(bytes) -> object |
17 | 17 |
|
18 | 18 | Misc variables: |
19 | 19 |
|
@@ -818,6 +818,7 @@ def save_bytearray(self, obj): |
818 | 818 | self._write_large_bytes(BYTEARRAY8 + pack("<Q", n), obj) |
819 | 819 | else: |
820 | 820 | self.write(BYTEARRAY8 + pack("<Q", n) + obj) |
| 821 | + self.memoize(obj) |
821 | 822 | dispatch[bytearray] = save_bytearray |
822 | 823 |
|
823 | 824 | if _HAVE_PICKLE_BUFFER: |
@@ -1172,7 +1173,7 @@ def __init__(self, file, *, fix_imports=True, |
1172 | 1173 | used in Python 3. The *encoding* and *errors* tell pickle how |
1173 | 1174 | to decode 8-bit string instances pickled by Python 2; these |
1174 | 1175 | default to 'ASCII' and 'strict', respectively. *encoding* can be |
1175 | | - 'bytes' to read theses 8-bit string instances as bytes objects. |
| 1176 | + 'bytes' to read these 8-bit string instances as bytes objects. |
1176 | 1177 | """ |
1177 | 1178 | self._buffers = iter(buffers) if buffers is not None else None |
1178 | 1179 | self._file_readline = file.readline |
@@ -1606,17 +1607,29 @@ def load_dup(self): |
1606 | 1607 |
|
1607 | 1608 | def load_get(self): |
1608 | 1609 | i = int(self.readline()[:-1]) |
1609 | | - self.append(self.memo[i]) |
| 1610 | + try: |
| 1611 | + self.append(self.memo[i]) |
| 1612 | + except KeyError: |
| 1613 | + msg = f'Memo value not found at index {i}' |
| 1614 | + raise UnpicklingError(msg) from None |
1610 | 1615 | dispatch[GET[0]] = load_get |
1611 | 1616 |
|
1612 | 1617 | def load_binget(self): |
1613 | 1618 | i = self.read(1)[0] |
1614 | | - self.append(self.memo[i]) |
| 1619 | + try: |
| 1620 | + self.append(self.memo[i]) |
| 1621 | + except KeyError as exc: |
| 1622 | + msg = f'Memo value not found at index {i}' |
| 1623 | + raise UnpicklingError(msg) from None |
1615 | 1624 | dispatch[BINGET[0]] = load_binget |
1616 | 1625 |
|
1617 | 1626 | def load_long_binget(self): |
1618 | 1627 | i, = unpack('<I', self.read(4)) |
1619 | | - self.append(self.memo[i]) |
| 1628 | + try: |
| 1629 | + self.append(self.memo[i]) |
| 1630 | + except KeyError as exc: |
| 1631 | + msg = f'Memo value not found at index {i}' |
| 1632 | + raise UnpicklingError(msg) from None |
1620 | 1633 | dispatch[LONG_BINGET[0]] = load_long_binget |
1621 | 1634 |
|
1622 | 1635 | def load_put(self): |
@@ -1751,7 +1764,7 @@ def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict", |
1751 | 1764 | return _Unpickler(file, fix_imports=fix_imports, buffers=buffers, |
1752 | 1765 | encoding=encoding, errors=errors).load() |
1753 | 1766 |
|
1754 | | -def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict", |
| 1767 | +def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict", |
1755 | 1768 | buffers=None): |
1756 | 1769 | if isinstance(s, str): |
1757 | 1770 | raise TypeError("Can't load pickle from unicode string") |
|
0 commit comments