-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_memfunctions.py
More file actions
137 lines (114 loc) · 5.2 KB
/
test_memfunctions.py
File metadata and controls
137 lines (114 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import sys
import unittest
from test import support
from ctypes import (POINTER, sizeof, cast,
create_string_buffer, string_at,
create_unicode_buffer, wstring_at,
memmove, memset,
memoryview_at, c_void_p,
c_char_p, c_byte, c_ubyte, c_wchar,
addressof, byref)
class MemFunctionsTest(unittest.TestCase):
def test_overflow(self):
# string_at and wstring_at must use the Python calling
# convention (which acquires the GIL and checks the Python
# error flag). Provoke an error and catch it; see also issue
# gh-47804.
self.assertRaises((OverflowError, MemoryError, SystemError),
lambda: wstring_at(u"foo", sys.maxsize - 1))
self.assertRaises((OverflowError, MemoryError, SystemError),
lambda: string_at("foo", sys.maxsize - 1))
def test_memmove(self):
# large buffers apparently increase the chance that the memory
# is allocated in high address space.
a = create_string_buffer(1000000)
p = b"Hello, World"
result = memmove(a, p, len(p))
self.assertEqual(a.value, b"Hello, World")
self.assertEqual(string_at(result), b"Hello, World")
self.assertEqual(string_at(result, 5), b"Hello")
self.assertEqual(string_at(result, 16), b"Hello, World\0\0\0\0")
self.assertEqual(string_at(result, 0), b"")
def test_memset(self):
a = create_string_buffer(1000000)
result = memset(a, ord('x'), 16)
self.assertEqual(a.value, b"xxxxxxxxxxxxxxxx")
self.assertEqual(string_at(result), b"xxxxxxxxxxxxxxxx")
self.assertEqual(string_at(a), b"xxxxxxxxxxxxxxxx")
self.assertEqual(string_at(a, 20), b"xxxxxxxxxxxxxxxx\0\0\0\0")
def test_cast(self):
a = (c_ubyte * 32)(*map(ord, "abcdef"))
self.assertEqual(cast(a, c_char_p).value, b"abcdef")
self.assertEqual(cast(a, POINTER(c_byte))[:7],
[97, 98, 99, 100, 101, 102, 0])
self.assertEqual(cast(a, POINTER(c_byte))[:7:],
[97, 98, 99, 100, 101, 102, 0])
self.assertEqual(cast(a, POINTER(c_byte))[6:-1:-1],
[0, 102, 101, 100, 99, 98, 97])
self.assertEqual(cast(a, POINTER(c_byte))[:7:2],
[97, 99, 101, 0])
self.assertEqual(cast(a, POINTER(c_byte))[:7:7],
[97])
@support.refcount_test
def test_string_at(self):
s = string_at(b"foo bar")
self.assertTrue(s, "foo bar")
self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
self.assertEqual(string_at(b"foo bar", 3), b"foo")
def test_wstring_at(self):
p = create_unicode_buffer("Hello, World")
a = create_unicode_buffer(1000000)
result = memmove(a, p, len(p) * sizeof(c_wchar))
self.assertEqual(a.value, "Hello, World")
self.assertEqual(wstring_at(a), "Hello, World")
self.assertEqual(wstring_at(a, 5), "Hello")
self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
self.assertEqual(wstring_at(a, 0), "")
def test_memoryview_at(self):
b = (c_byte * 10)()
size = len(b)
for foreign_ptr in (
b,
cast(b, c_void_p),
byref(b),
addressof(b),
):
with self.subTest(foreign_ptr=type(foreign_ptr).__name__):
b[:] = b"initialval"
v = memoryview_at(foreign_ptr, size)
self.assertIsInstance(v, memoryview)
self.assertEqual(bytes(v), b"initialval")
# test that writes to source buffer get reflected in memoryview
b[:] = b"0123456789"
self.assertEqual(bytes(v), b"0123456789")
# test that writes to memoryview get reflected in source buffer
v[:] = b"9876543210"
self.assertEqual(bytes(b), b"9876543210")
with self.assertRaises(ValueError):
memoryview_at(foreign_ptr, -1)
with self.assertRaises(ValueError):
memoryview_at(foreign_ptr, sys.maxsize + 1)
v0 = memoryview_at(foreign_ptr, 0)
self.assertEqual(bytes(v0), b'')
def test_memoryview_at_readonly(self):
b = (c_byte * 10)()
size = len(b)
for foreign_ptr in (
b,
cast(b, c_void_p),
byref(b),
addressof(b),
):
with self.subTest(foreign_ptr=type(foreign_ptr).__name__):
b[:] = b"initialval"
v = memoryview_at(foreign_ptr, size, readonly=True)
self.assertIsInstance(v, memoryview)
self.assertEqual(bytes(v), b"initialval")
# test that writes to source buffer get reflected in memoryview
b[:] = b"0123456789"
self.assertEqual(bytes(v), b"0123456789")
# test that writes to the memoryview are blocked
with self.assertRaises(TypeError):
v[:] = b"9876543210"
if __name__ == "__main__":
unittest.main()