(in_band_file_path, out_of_band_file_path)
| 661 | |
| 662 | @cross_language_test |
| 663 | def test_oob_buffer(in_band_file_path, out_of_band_file_path): |
| 664 | with open(in_band_file_path, "rb") as f: |
| 665 | in_band_bytes = f.read() |
| 666 | with open(out_of_band_file_path, "rb") as f: |
| 667 | out_of_band_buffer = pyfory.Buffer(f.read()) |
| 668 | fory = pyfory.Fory(xlang=True, compatible=False, ref=True) |
| 669 | n_buffers = out_of_band_buffer.read_int32() |
| 670 | buffers = [] |
| 671 | for i in range(n_buffers): |
| 672 | length = out_of_band_buffer.read_int32() |
| 673 | reader_index = out_of_band_buffer.get_reader_index() |
| 674 | buffers.append(out_of_band_buffer.slice(reader_index, length)) |
| 675 | out_of_band_buffer.set_reader_index(out_of_band_buffer.get_reader_index() + length) |
| 676 | new_obj = fory.deserialize(in_band_bytes, buffers) |
| 677 | obj = [bytes(bytearray([0, 1])) for _ in range(10)] |
| 678 | assert new_obj == obj, (obj, new_obj) |
| 679 | |
| 680 | buffer_objects = [] |
| 681 | counter = 0 |
| 682 | |
| 683 | def buffer_callback(binary_object): |
| 684 | nonlocal counter |
| 685 | counter += 1 |
| 686 | if counter % 2 == 0: |
| 687 | buffer_objects.append(binary_object) |
| 688 | return False |
| 689 | else: |
| 690 | return True |
| 691 | |
| 692 | serialized = fory.serialize(obj, buffer_callback=buffer_callback) |
| 693 | # in_band_bytes size may be different because it may contain language-specific meta. |
| 694 | debug_print(f"{len(serialized), len(in_band_bytes)}") |
| 695 | debug_print(f"deserialized from other language {new_obj}") |
| 696 | debug_print(f"deserialized from python {fory.deserialize(serialized, [o.getbuffer() for o in buffer_objects])}") |
| 697 | fory.deserialize(serialized, [o.getbuffer() for o in buffer_objects]) |
| 698 | with open(in_band_file_path, "wb+") as f: |
| 699 | f.write(serialized) |
| 700 | out_of_band_buffer.write_int32(len(buffer_objects)) |
| 701 | for buffer_object in buffer_objects: |
| 702 | out_of_band_buffer.write_int32(buffer_object.total_bytes()) |
| 703 | buffer_object.write_to(out_of_band_buffer) |
| 704 | with open(out_of_band_file_path, "wb+") as f: |
| 705 | f.write(out_of_band_buffer.to_bytes(0, out_of_band_buffer.get_writer_index())) |
| 706 | |
| 707 | |
| 708 | @cross_language_test |
nothing calls this directly
no test coverage detected