self.assertIsNot(b2a, b2b)
self.assert_is_copy(b2a, b2b)
+ def test_picklebuffer_memoization(self):
+ if self.py_version < (3, 8):
+ self.skipTest('not supported in Python < 3.8')
+ array_types = [bytes, bytearray]
+ for proto in range(5, pickle.HIGHEST_PROTOCOL + 1):
+ for array_type in array_types:
+ for s in b'', b'xyz', b'xyz'*100:
+ with self.subTest(proto=proto, array_type=array_type, s=s, independent=False):
+ b = pickle.PickleBuffer(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):
+ b = array_type(s)
+ b1a = pickle.PickleBuffer(b)
+ b2a = pickle.PickleBuffer(b)
+ p = self.dumps((b1a, b2a), proto)
+ b1b, b2b = self.loads(p)
+ if array_type is not bytes:
+ self.assertIsNot(b1b, b2b)
+ self.assert_is_copy(b1b, b)
+ self.assert_is_copy(b2b, b)
+
+ def test_empty_picklebuffer_memoization(self):
+ # gh-148914: Empty writable PickleBuffer memoized an empty bytearray
+ # with the id of b'' (a singleton in CPython).
+ if self.py_version < (3, 8):
+ self.skipTest('not supported in Python < 3.8')
+ for proto in range(5, pickle.HIGHEST_PROTOCOL + 1):
+ for readonly in False, True:
+ with self.subTest(proto=proto, readonly=readonly):
+ b = b''
+ ba = bytearray()
+ buf = pickle.PickleBuffer(b if readonly else ba)
+ p = self.dumps((buf, b, ba), proto)
+ buf, b, ba = self.loads(p)
+ array_type = bytes if readonly else bytearray
+ self.assertIsInstance(buf, array_type)
+ self.assertIsInstance(b, bytes)
+ self.assertIsInstance(ba, bytearray)
+ self.assertEqual(buf, b'')
+ self.assertEqual(b, b'')
+ self.assertEqual(ba, b'')
+
def test_ints(self):
for proto in protocols:
n = sys.maxsize