mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
[3.12] gh-120380: fix Python implementation of pickle.Pickler
for bytes
and bytearray
objects in protocol version 5. (GH-120422) (GH-120833)
gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422)
(cherry picked from commit 7595e6743a
)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
41e1ac6a23
commit
6de373d750
3 changed files with 81 additions and 21 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue