MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_oob_buffers

Method test_oob_buffers

Lib/test/pickletester.py:3977–4017  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3975 # (see comment in PicklableNDArray.__reduce_ex__)
3976
3977 def test_oob_buffers(self):
3978 # Test out-of-band buffers (PEP 574)
3979 for obj in self.buffer_like_objects():
3980 for proto in range(0, 5):
3981 # Need protocol >= 5 for buffer_callback
3982 with self.assertRaises(ValueError):
3983 self.dumps(obj, proto,
3984 buffer_callback=[].append)
3985 for proto in range(5, pickle.HIGHEST_PROTOCOL + 1):
3986 buffers = []
3987 buffer_callback = lambda pb: buffers.append(pb.raw())
3988 data = self.dumps(obj, proto,
3989 buffer_callback=buffer_callback)
3990 self.assertNotIn(b"abcdefgh", data)
3991 self.assertEqual(count_opcode(pickle.SHORT_BINBYTES, data), 0)
3992 self.assertEqual(count_opcode(pickle.BYTEARRAY8, data), 0)
3993 self.assertEqual(count_opcode(pickle.NEXT_BUFFER, data), 1)
3994 self.assertEqual(count_opcode(pickle.READONLY_BUFFER, data),
3995 1 if obj.readonly else 0)
3996
3997 if obj.c_contiguous:
3998 self.assertEqual(bytes(buffers[0]), b"abcdefgh")
3999 # Need buffers argument to unpickle properly
4000 with self.assertRaises(pickle.UnpicklingError):
4001 self.loads(data)
4002
4003 new = self.loads(data, buffers=buffers)
4004 if obj.zero_copy_reconstruct:
4005 # Zero-copy achieved
4006 self.assertIs(new, obj)
4007 else:
4008 self.assertIs(type(new), type(obj))
4009 self.assertEqual(new, obj)
4010 # Non-sequence buffers accepted too
4011 new = self.loads(data, buffers=iter(buffers))
4012 if obj.zero_copy_reconstruct:
4013 # Zero-copy achieved
4014 self.assertIs(new, obj)
4015 else:
4016 self.assertIs(type(new), type(obj))
4017 self.assertEqual(new, obj)
4018
4019 def test_oob_buffers_writable_to_readonly(self):
4020 # Test reconstructing readonly object from writable buffer

Callers

nothing calls this directly

Calls 11

buffer_like_objectsMethod · 0.95
count_opcodeFunction · 0.85
iterFunction · 0.85
assertNotInMethod · 0.80
assertRaisesMethod · 0.45
dumpsMethod · 0.45
appendMethod · 0.45
rawMethod · 0.45
assertEqualMethod · 0.45
loadsMethod · 0.45
assertIsMethod · 0.45

Tested by

no test coverage detected