(self)
| 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 |
nothing calls this directly
no test coverage detected