(self)
| 4380 | # (see comment in PicklableNDArray.__reduce_ex__) |
| 4381 | |
| 4382 | def test_oob_buffers(self): |
| 4383 | # Test out-of-band buffers (PEP 574) |
| 4384 | for obj in self.buffer_like_objects(): |
| 4385 | for proto in range(0, 5): |
| 4386 | # Need protocol >= 5 for buffer_callback |
| 4387 | with self.assertRaises(ValueError): |
| 4388 | self.dumps(obj, proto, |
| 4389 | buffer_callback=[].append) |
| 4390 | for proto in range(5, pickle.HIGHEST_PROTOCOL + 1): |
| 4391 | buffers = [] |
| 4392 | buffer_callback = lambda pb: buffers.append(pb.raw()) |
| 4393 | data = self.dumps(obj, proto, |
| 4394 | buffer_callback=buffer_callback) |
| 4395 | self.assertNotIn(b"abcdefgh", data) |
| 4396 | self.assertEqual(count_opcode(pickle.SHORT_BINBYTES, data), 0) |
| 4397 | self.assertEqual(count_opcode(pickle.BYTEARRAY8, data), 0) |
| 4398 | self.assertEqual(count_opcode(pickle.NEXT_BUFFER, data), 1) |
| 4399 | self.assertEqual(count_opcode(pickle.READONLY_BUFFER, data), |
| 4400 | 1 if obj.readonly else 0) |
| 4401 | |
| 4402 | if obj.c_contiguous: |
| 4403 | self.assertEqual(bytes(buffers[0]), b"abcdefgh") |
| 4404 | # Need buffers argument to unpickle properly |
| 4405 | with self.assertRaises(pickle.UnpicklingError): |
| 4406 | self.loads(data) |
| 4407 | |
| 4408 | new = self.loads(data, buffers=buffers) |
| 4409 | if obj.zero_copy_reconstruct: |
| 4410 | # Zero-copy achieved |
| 4411 | self.assertIs(new, obj) |
| 4412 | else: |
| 4413 | self.assertIs(type(new), type(obj)) |
| 4414 | self.assertEqual(new, obj) |
| 4415 | # Non-sequence buffers accepted too |
| 4416 | new = self.loads(data, buffers=iter(buffers)) |
| 4417 | if obj.zero_copy_reconstruct: |
| 4418 | # Zero-copy achieved |
| 4419 | self.assertIs(new, obj) |
| 4420 | else: |
| 4421 | self.assertIs(type(new), type(obj)) |
| 4422 | self.assertEqual(new, obj) |
| 4423 | |
| 4424 | def test_oob_buffers_writable_to_readonly(self): |
| 4425 | # Test reconstructing readonly object from writable buffer |
nothing calls this directly
no test coverage detected