Skip to content

bpo-43907: add missing memoize call in pure python pickling of bytearray #25501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ def save_bytearray(self, obj):
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:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,14 @@ def test_bytearray(self):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.