Skip to content

Commit 4c4dec1

Browse files
authored
Merge pull request RustPython#3541 from fanninpm/test-pickle
Add test_pickle from CPython 3.10
2 parents 99a3dae + 671989c commit 4c4dec1

File tree

3 files changed

+755
-8
lines changed

3 files changed

+755
-8
lines changed

Lib/pickle.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
dump(object, file)
1414
dumps(object) -> string
1515
load(file) -> object
16-
loads(string) -> object
16+
loads(bytes) -> object
1717
1818
Misc variables:
1919
@@ -818,6 +818,7 @@ def save_bytearray(self, obj):
818818
self._write_large_bytes(BYTEARRAY8 + pack("<Q", n), obj)
819819
else:
820820
self.write(BYTEARRAY8 + pack("<Q", n) + obj)
821+
self.memoize(obj)
821822
dispatch[bytearray] = save_bytearray
822823

823824
if _HAVE_PICKLE_BUFFER:
@@ -1172,7 +1173,7 @@ def __init__(self, file, *, fix_imports=True,
11721173
used in Python 3. The *encoding* and *errors* tell pickle how
11731174
to decode 8-bit string instances pickled by Python 2; these
11741175
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.
11761177
"""
11771178
self._buffers = iter(buffers) if buffers is not None else None
11781179
self._file_readline = file.readline
@@ -1606,17 +1607,29 @@ def load_dup(self):
16061607

16071608
def load_get(self):
16081609
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
16101615
dispatch[GET[0]] = load_get
16111616

16121617
def load_binget(self):
16131618
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
16151624
dispatch[BINGET[0]] = load_binget
16161625

16171626
def load_long_binget(self):
16181627
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
16201633
dispatch[LONG_BINGET[0]] = load_long_binget
16211634

16221635
def load_put(self):
@@ -1751,7 +1764,7 @@ def _load(file, *, fix_imports=True, encoding="ASCII", errors="strict",
17511764
return _Unpickler(file, fix_imports=fix_imports, buffers=buffers,
17521765
encoding=encoding, errors=errors).load()
17531766

1754-
def _loads(s, *, fix_imports=True, encoding="ASCII", errors="strict",
1767+
def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict",
17551768
buffers=None):
17561769
if isinstance(s, str):
17571770
raise TypeError("Can't load pickle from unicode string")

Lib/test/test_bytes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,6 @@ def test_obsolete_write_lock(self):
16371637
from _testcapi import getbuffer_with_null_view
16381638
self.assertRaises(BufferError, getbuffer_with_null_view, bytearray())
16391639

1640-
# TODO: RUSTPYTHON
1641-
@unittest.expectedFailure
16421640
def test_iterator_pickling2(self):
16431641
orig = bytearray(b'abc')
16441642
data = list(b'qwerty')

0 commit comments

Comments
 (0)