Skip to content

Commit a6a5713

Browse files
Aaron WieczorekAaron Wieczorek
authored andcommitted
Refactor and migrate tests from test_pickle to pickletester
1 parent 4129734 commit a6a5713

2 files changed

Lines changed: 30 additions & 33 deletions

File tree

Lib/test/pickletester.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2658,7 +2658,6 @@ def test_misc(self):
26582658
self.assert_is_copy(x, y)
26592659

26602660
# XXX test __reduce__ protocol?
2661-
26622661
def test_roundtrip_equality(self):
26632662
expected = self._testdata
26642663
for proto in protocols:
@@ -4261,6 +4260,36 @@ def check_array(arr):
42614260
# 2-D, non-contiguous
42624261
check_array(arr[::2])
42634262

4263+
def do_test_concurrent_mutation_in_buffer_callback(self, factory):
4264+
class R:
4265+
def __bool__(self):
4266+
buf.release()
4267+
return True
4268+
4269+
max_proto = getattr(self, "proto", pickle.HIGHEST_PROTOCOL)
4270+
for proto in range(5, max_proto + 1):
4271+
obj, sub = factory()
4272+
buf = pickle.PickleBuffer(obj)
4273+
buffer_callback = lambda _: R()
4274+
4275+
with self.subTest(proto=proto, obj=obj, sub=sub):
4276+
res = self.dumps(buf, proto, buffer_callback=buffer_callback)
4277+
self.assertIn(sub, res)
4278+
4279+
def test_concurrent_mutation_in_buffer_with_bytearray(self):
4280+
def factory():
4281+
s = b"a" * 16
4282+
return bytearray(s), s
4283+
self.do_test_concurrent_mutation_in_buffer_callback(factory)
4284+
4285+
def test_concurrent_mutation_in_buffer_with_memoryview(self):
4286+
def factory():
4287+
c, n = b"a", 64
4288+
sub = c * (n // 2)
4289+
obj = memoryview(bytearray(c * n))[n // 4 : 3 * n // 4]
4290+
return obj, sub
4291+
self.do_test_concurrent_mutation_in_buffer_callback(factory)
4292+
42644293
def test_evil_class_mutating_dict(self):
42654294
# https://github.com/python/cpython/issues/92930
42664295
from random import getrandbits

Lib/test/test_pickle.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -388,38 +388,6 @@ class CPicklingErrorTests(PyPicklingErrorTests):
388388
class CPicklerTests(PyPicklerTests):
389389
pickler = _pickle.Pickler
390390
unpickler = _pickle.Unpickler
391-
def test_release_in_callback_keepalive(self):
392-
base = bytearray(b'A' * 16)
393-
pb = pickle.PickleBuffer(base)
394-
395-
class Evil:
396-
def __bool__(self):
397-
pb.release()
398-
return True
399-
400-
def callback(p):
401-
return Evil()
402-
403-
for proto in range(5, pickle.HIGHEST_PROTOCOL + 1):
404-
result = self.dumps(pb, proto, buffer_callback=callback)
405-
self.assertIn(b'A' * 16, result)
406-
407-
def test_release_in_callback_complex_keepalive(self):
408-
base = bytearray(b'A' * 32)
409-
view = memoryview(base)[10:26]
410-
pb = pickle.PickleBuffer(view)
411-
412-
class Evil:
413-
def __bool__(self):
414-
pb.release()
415-
return True
416-
417-
def callback(p):
418-
return Evil()
419-
420-
for proto in range(5, pickle.HIGHEST_PROTOCOL + 1):
421-
result = self.dumps(pb, proto, buffer_callback=callback)
422-
self.assertIn(b'A' * 16, result)
423391

424392
class CPersPicklerTests(PyPersPicklerTests):
425393
pickler = _pickle.Pickler

0 commit comments

Comments
 (0)