Skip to content

Commit 1e9f093

Browse files
authored
bpo-43907: add missing memoize call in pure python pickling of bytearray (GH-25501)
1 parent f24e2e5 commit 1e9f093

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/pickle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

Lib/test/pickletester.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,14 @@ def test_bytearray(self):
18531853
self.assertNotIn(b'bytearray', p)
18541854
self.assertTrue(opcode_in_pickle(pickle.BYTEARRAY8, p))
18551855

1856+
def test_bytearray_memoization_bug(self):
1857+
for proto in protocols:
1858+
for s in b'', b'xyz', b'xyz'*100:
1859+
b = bytearray(s)
1860+
p = self.dumps((b, b), proto)
1861+
b1, b2 = self.loads(p)
1862+
self.assertIs(b1, b2)
1863+
18561864
def test_ints(self):
18571865
for proto in protocols:
18581866
n = sys.maxsize
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a bug in the pure-Python pickle implementation when using protocol 5,
2+
where bytearray instances that occur several time in the pickled object
3+
graph would incorrectly unpickle into repeated copies of the bytearray
4+
object.

0 commit comments

Comments
 (0)