|
1 | 1 | /* |
2 | | - * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
46 | 46 | import static org.junit.Assert.assertTrue; |
47 | 47 |
|
48 | 48 | import java.math.BigInteger; |
| 49 | +import java.nio.ByteBuffer; |
49 | 50 | import java.nio.ByteOrder; |
50 | 51 | import java.time.LocalDate; |
51 | 52 | import java.time.LocalTime; |
52 | 53 | import java.time.ZoneId; |
53 | 54 |
|
54 | 55 | import org.graalvm.polyglot.Context; |
55 | 56 | import org.graalvm.polyglot.Value; |
| 57 | +import org.graalvm.polyglot.io.ByteSequence; |
56 | 58 | import org.junit.After; |
57 | 59 | import org.junit.Before; |
58 | 60 | import org.junit.Test; |
@@ -713,4 +715,79 @@ public void testByteBuffer() { |
713 | 715 | t.writeBufferDouble(ByteOrder.LITTLE_ENDIAN, 0, 12345.6789123); |
714 | 716 | assertEquals(12345.6789123, t.readBufferDouble(ByteOrder.LITTLE_ENDIAN, 0), 0.0); |
715 | 717 | } |
| 718 | + |
| 719 | + @Test |
| 720 | + public void testHostByteBufferAsPythonBuffer() { |
| 721 | + byte[] writable = new byte[]{1, 2, 3, 4}; |
| 722 | + context.getBindings("python").putMember("writable_bb", ByteBuffer.wrap(writable)); |
| 723 | + context.getBindings("python").putMember("readonly_bb", ByteBuffer.wrap(new byte[]{-1, 5, 6, 7, 8}).asReadOnlyBuffer()); |
| 724 | + |
| 725 | + context.eval("python", """ |
| 726 | + import binascii |
| 727 | + import io |
| 728 | +
|
| 729 | + mv = memoryview(writable_bb) |
| 730 | + assert not mv.readonly |
| 731 | + assert mv.tobytes() == b"\\x01\\x02\\x03\\x04" |
| 732 | + assert bytes(writable_bb) == b"\\x01\\x02\\x03\\x04" |
| 733 | + assert bytearray(writable_bb) == bytearray(b"\\x01\\x02\\x03\\x04") |
| 734 | + assert binascii.hexlify(writable_bb) == b"01020304" |
| 735 | + bio = io.BytesIO() |
| 736 | + assert bio.write(writable_bb) == 4 |
| 737 | + assert bio.getvalue() == b"\\x01\\x02\\x03\\x04" |
| 738 | + mv[1] = 9 |
| 739 | + assert io.BytesIO(b"abcd").readinto(writable_bb) == 4 |
| 740 | + assert bytes(writable_bb) == b"abcd" |
| 741 | +
|
| 742 | + ro = memoryview(readonly_bb) |
| 743 | + assert ro.readonly |
| 744 | + assert ro.tobytes() == b"\\xff\\x05\\x06\\x07\\x08" |
| 745 | + assert bytes(readonly_bb) == b"\\xff\\x05\\x06\\x07\\x08" |
| 746 | + assert bytearray(readonly_bb) == bytearray(b"\\xff\\x05\\x06\\x07\\x08") |
| 747 | + assert io.BytesIO().write(readonly_bb) == 5 |
| 748 | + try: |
| 749 | + ro[0] = 1 |
| 750 | + raise AssertionError("expected memoryview write to fail") |
| 751 | + except TypeError: |
| 752 | + pass |
| 753 | + try: |
| 754 | + io.BytesIO(b"wxyz").readinto(readonly_bb) |
| 755 | + raise AssertionError("expected readinto to fail") |
| 756 | + except TypeError: |
| 757 | + pass |
| 758 | + """); |
| 759 | + |
| 760 | + assertArrayEquals(new byte[]{'a', 'b', 'c', 'd'}, writable); |
| 761 | + } |
| 762 | + |
| 763 | + @Test |
| 764 | + public void testHostByteSequenceAsPythonBuffer() { |
| 765 | + byte[] bytes = new byte[]{10, 20, 30, 40}; |
| 766 | + context.getBindings("python").putMember("seq", ByteSequence.create(bytes)); |
| 767 | + |
| 768 | + context.eval("python", """ |
| 769 | + import binascii |
| 770 | + import io |
| 771 | +
|
| 772 | + mv = memoryview(seq) |
| 773 | + assert mv.readonly |
| 774 | + assert mv.tobytes() == b"\\x0a\\x14\\x1e\\x28" |
| 775 | + assert bytes(seq) == b"\\x0a\\x14\\x1e\\x28" |
| 776 | + assert bytearray(seq) == bytearray(b"\\x0a\\x14\\x1e\\x28") |
| 777 | + assert binascii.hexlify(seq) == b"0a141e28" |
| 778 | + bio = io.BytesIO() |
| 779 | + assert bio.write(seq) == 4 |
| 780 | + assert bio.getvalue() == b"\\x0a\\x14\\x1e\\x28" |
| 781 | + try: |
| 782 | + mv[0] = 1 |
| 783 | + raise AssertionError("expected memoryview write to fail") |
| 784 | + except TypeError: |
| 785 | + pass |
| 786 | + try: |
| 787 | + io.BytesIO(b"abcd").readinto(seq) |
| 788 | + raise AssertionError("expected readinto to fail") |
| 789 | + except TypeError: |
| 790 | + pass |
| 791 | + """); |
| 792 | + } |
716 | 793 | } |
0 commit comments