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

pitrou webhook-mailer at python.org
Fri Apr 23 17:27:32 EDT 2021


https://github.com/python/cpython/commit/1e9f0933095403b215c2c4a0be7915d034ff7026
commit: 1e9f0933095403b215c2c4a0be7915d034ff7026
branch: master
author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
committer: pitrou <pitrou at free.fr>
date: 2021-04-23T23:27:14+02:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2021-04-23-20-57-20.bpo-43907.3RJEjv.rst
M Lib/pickle.py
M Lib/test/pickletester.py

diff --git a/Lib/pickle.py b/Lib/pickle.py
index e63a8b6e4dbb7..5ab312f2acaee 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -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:
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index fd05e7af94a1a..8e01d311360e3 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2021-04-23-20-57-20.bpo-43907.3RJEjv.rst b/Misc/NEWS.d/next/Library/2021-04-23-20-57-20.bpo-43907.3RJEjv.rst
new file mode 100644
index 0000000000000..7da3a1c3f7612
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-04-23-20-57-20.bpo-43907.3RJEjv.rst
@@ -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.



More information about the Python-checkins mailing list