bpo-43907: add missing memoize call in pure python pickling of bytearray (GH-25501)

This commit is contained in:
Carl Friedrich Bolz-Tereick 2021-04-23 23:27:14 +02:00 committed by GitHub
parent f24e2e5464
commit 1e9f093309
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 0 deletions

View File

@ -818,6 +818,7 @@ class _Pickler:
self._write_large_bytes(BYTEARRAY8 + pack("<Q", n), obj)
else:
self.write(BYTEARRAY8 + pack("<Q", n) + obj)
self.memoize(obj)
dispatch[bytearray] = save_bytearray
if _HAVE_PICKLE_BUFFER:

View File

@ -1853,6 +1853,14 @@ class AbstractPickleTests(unittest.TestCase):
self.assertNotIn(b'bytearray', p)
self.assertTrue(opcode_in_pickle(pickle.BYTEARRAY8, p))
def test_bytearray_memoization_bug(self):
for proto in protocols:
for s in b'', b'xyz', b'xyz'*100:
b = bytearray(s)
p = self.dumps((b, b), proto)
b1, b2 = self.loads(p)
self.assertIs(b1, b2)
def test_ints(self):
for proto in protocols:
n = sys.maxsize

View File

@ -0,0 +1,4 @@
Fix a bug in the pure-Python pickle implementation when using protocol 5,
where bytearray instances that occur several time in the pickled object
graph would incorrectly unpickle into repeated copies of the bytearray
object.