summaryrefslogtreecommitdiffstats
path: root/Lib/test/pickletester.py
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-06-21 12:22:38 (GMT)
committerGitHub <noreply@github.com>2024-06-21 12:22:38 (GMT)
commit7595e6743ac78ac0dd19418176f66d251668fafc (patch)
tree80c90888e311b0cd403fe850132c6050c69ee31d /Lib/test/pickletester.py
parent83d3d7aace32b8536f552f78dd29610344f13160 (diff)
downloadcpython-7595e6743ac78ac0dd19418176f66d251668fafc.zip
cpython-7595e6743ac78ac0dd19418176f66d251668fafc.tar.gz
cpython-7595e6743ac78ac0dd19418176f66d251668fafc.tar.bz2
gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422)
Diffstat (limited to 'Lib/test/pickletester.py')
-rw-r--r--Lib/test/pickletester.py49
1 files changed, 43 insertions, 6 deletions
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 93e7dbb..9922591 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1845,6 +1845,25 @@ class AbstractPickleTests:
p = self.dumps(s, proto)
self.assert_is_copy(s, self.loads(p))
+ def test_bytes_memoization(self):
+ for proto in protocols:
+ for array_type in [bytes, ZeroCopyBytes]:
+ for s in b'', b'xyz', b'xyz'*100:
+ with self.subTest(proto=proto, array_type=array_type, s=s, independent=False):
+ b = array_type(s)
+ p = self.dumps((b, b), proto)
+ x, y = self.loads(p)
+ self.assertIs(x, y)
+ self.assert_is_copy((b, b), (x, y))
+
+ with self.subTest(proto=proto, array_type=array_type, s=s, independent=True):
+ b1, b2 = array_type(s), array_type(s)
+ p = self.dumps((b1, b2), proto)
+ # Note that (b1, b2) = self.loads(p) might have identical
+ # components, i.e., b1 is b2, but this is not always the
+ # case if the content is large (equality still holds).
+ self.assert_is_copy((b1, b2), self.loads(p))
+
def test_bytearray(self):
for proto in protocols:
for s in b'', b'xyz', b'xyz'*100:
@@ -1864,13 +1883,31 @@ class AbstractPickleTests:
self.assertNotIn(b'bytearray', p)
self.assertTrue(opcode_in_pickle(pickle.BYTEARRAY8, p))
- def test_bytearray_memoization_bug(self):
+ def test_bytearray_memoization(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)
+ for array_type in [bytearray, ZeroCopyBytearray]:
+ for s in b'', b'xyz', b'xyz'*100:
+ with self.subTest(proto=proto, array_type=array_type, s=s, independent=False):
+ b = array_type(s)
+ p = self.dumps((b, b), proto)
+ b1, b2 = self.loads(p)
+ self.assertIs(b1, b2)
+
+ with self.subTest(proto=proto, array_type=array_type, s=s, independent=True):
+ b1a, b2a = array_type(s), array_type(s)
+ # Unlike bytes, equal but independent bytearray objects are
+ # never identical.
+ self.assertIsNot(b1a, b2a)
+
+ p = self.dumps((b1a, b2a), proto)
+ b1b, b2b = self.loads(p)
+ self.assertIsNot(b1b, b2b)
+
+ self.assertIsNot(b1a, b1b)
+ self.assert_is_copy(b1a, b1b)
+
+ self.assertIsNot(b2a, b2b)
+ self.assert_is_copy(b2a, b2b)
def test_ints(self):
for proto in protocols: