Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 097a41c

Browse files
committed
Add test for ctypes.memoryview_at
1 parent d7e2f25 commit 097a41c

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_ctypes/test_memfunctions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
create_string_buffer, string_at,
66
create_unicode_buffer, wstring_at,
77
memmove, memset,
8+
memoryview_at, c_void_p,
89
c_char_p, c_byte, c_ubyte, c_wchar)
910

1011

@@ -77,6 +78,25 @@ def test_wstring_at(self):
7778
self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
7879
self.assertEqual(wstring_at(a, 0), "")
7980

81+
def test_memoryview_at(self):
82+
b = (c_byte * 10)()
83+
84+
foreign_ptr = cast(b, c_void_p)
85+
foreign_ptr_size = len(b)
86+
87+
# memoryview_at() is normally used with pointers given to us
88+
# by C APIs. It's an efficient way to get a buffer
89+
# representing a dynamically-sized memory region without having
90+
# to create an array type first.
91+
v = memoryview_at(foreign_ptr, foreign_ptr_size)
92+
93+
# test that writes to source buffer get reflected in memoryview
94+
b[:] = b"0123456789"
95+
self.assertEqual(bytes(v), b"0123456789")
96+
97+
# test that writes to memoryview get reflected in source buffer
98+
v[:] = b"9876543210"
99+
self.assertEqual(bytes(b), b"9876543210")
80100

81101
if __name__ == "__main__":
82102
unittest.main()

0 commit comments

Comments
 (0)