Debian Codegraph @

Lib/test/test_ctypes/test_pep3118.py

raw
1 import re
2 import sys
3 import unittest
4 from ctypes import (CFUNCTYPE, POINTER, sizeof, Union,
5 Structure, LittleEndianStructure, BigEndianStructure,
6 c_char, c_byte, c_ubyte,
7 c_short, c_ushort, c_int, c_uint,
8 c_long, c_ulong, c_longlong, c_ulonglong, c_uint64,
9 c_bool, c_float, c_double, c_longdouble, py_object)
10
11
12 if sys.byteorder == "little":
13 THIS_ENDIAN = "<"
14 OTHER_ENDIAN = ">"
15 else:
16 THIS_ENDIAN = ">"
17 OTHER_ENDIAN = "<"
18
19
20 def normalize(format):
21 # Remove current endian specifier and white space from a format
22 # string
23 if format is None:
24 return ""
25 format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
26 return re.sub(r"\s", "", format)
27
28
29 class Test(unittest.TestCase):
30 def test_native_types(self):
31 for tp, fmt, shape, itemtp in native_types:
32 ob = tp()
33 v = memoryview(ob)
34 self.assertEqual(normalize(v.format), normalize(fmt))
35 if shape:
36 self.assertEqual(len(v), shape[0])
37 else:
38 self.assertRaises(TypeError, len, v)
39 self.assertEqual(v.itemsize, sizeof(itemtp))
40 self.assertEqual(v.shape, shape)
41 # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides
42 # if requested. memoryview currently reconstructs missing
43 # stride information, so this assert will fail.
44 # self.assertEqual(v.strides, ())
45
46 # they are always read/write
47 self.assertFalse(v.readonly)
48
49 n = 1
50 for dim in v.shape:
51 n = n * dim
52 self.assertEqual(n * v.itemsize, len(v.tobytes()))
53
54 def test_endian_types(self):
55 for tp, fmt, shape, itemtp in endian_types:
56 ob = tp()
57 v = memoryview(ob)
58 self.assertEqual(v.format, fmt)
59 if shape:
60 self.assertEqual(len(v), shape[0])
61 else:
62 self.assertRaises(TypeError, len, v)
63 self.assertEqual(v.itemsize, sizeof(itemtp))
64 self.assertEqual(v.shape, shape)
65 # XXX Issue #12851
66 # self.assertEqual(v.strides, ())
67
68 # they are always read/write
69 self.assertFalse(v.readonly)
70
71 n = 1
72 for dim in v.shape:
73 n = n * dim
74 self.assertEqual(n * v.itemsize, len(v.tobytes()))
75
76
77 # define some structure classes
78
79 class Point(Structure):
80 _fields_ = [("x", c_long), ("y", c_long)]
81
82 class PackedPoint(Structure):
83 _pack_ = 2
84 _fields_ = [("x", c_long), ("y", c_long)]
85
86 class PointMidPad(Structure):
87 _fields_ = [("x", c_byte), ("y", c_uint)]
88
89 class PackedPointMidPad(Structure):
90 _pack_ = 2
91 _fields_ = [("x", c_byte), ("y", c_uint64)]
92
93 class PointEndPad(Structure):
94 _fields_ = [("x", c_uint), ("y", c_byte)]
95
96 class PackedPointEndPad(Structure):
97 _pack_ = 2
98 _fields_ = [("x", c_uint64), ("y", c_byte)]
99
100 class Point2(Structure):
101 pass
102 Point2._fields_ = [("x", c_long), ("y", c_long)]
103
104 class EmptyStruct(Structure):
105 _fields_ = []
106
107 class aUnion(Union):
108 _fields_ = [("a", c_int)]
109
110 class StructWithArrays(Structure):
111 _fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)]
112
113 class Incomplete(Structure):
114 pass
115
116 class Complete(Structure):
117 pass
118 PComplete = POINTER(Complete)
119 Complete._fields_ = [("a", c_long)]
120
121
122 ################################################################
123 #
124 # This table contains format strings as they look on little endian
125 # machines. The test replaces '<' with '>' on big endian machines.
126 #
127
128 # Platform-specific type codes
129 s_bool = {1: '?', 2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_bool)]
130 s_short = {2: 'h', 4: 'l', 8: 'q'}[sizeof(c_short)]
131 s_ushort = {2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_ushort)]
132 s_int = {2: 'h', 4: 'i', 8: 'q'}[sizeof(c_int)]
133 s_uint = {2: 'H', 4: 'I', 8: 'Q'}[sizeof(c_uint)]
134 s_long = {4: 'l', 8: 'q'}[sizeof(c_long)]
135 s_ulong = {4: 'L', 8: 'Q'}[sizeof(c_ulong)]
136 s_longlong = "q"
137 s_ulonglong = "Q"
138 s_float = "f"
139 s_double = "d"
140 s_longdouble = "g"
141
142 # Alias definitions in ctypes/__init__.py
143 if c_int is c_long:
144 s_int = s_long
145 if c_uint is c_ulong:
146 s_uint = s_ulong
147 if c_longlong is c_long:
148 s_longlong = s_long
149 if c_ulonglong is c_ulong:
150 s_ulonglong = s_ulong
151 if c_longdouble is c_double:
152 s_longdouble = s_double
153
154
155 native_types = [
156 # type format shape calc itemsize
157
158 ## simple types
159
160 (c_char, "<c", (), c_char),
161 (c_byte, "<b", (), c_byte),
162 (c_ubyte, "<B", (), c_ubyte),
163 (c_short, "<" + s_short, (), c_short),
164 (c_ushort, "<" + s_ushort, (), c_ushort),
165
166 (c_int, "<" + s_int, (), c_int),
167 (c_uint, "<" + s_uint, (), c_uint),
168
169 (c_long, "<" + s_long, (), c_long),
170 (c_ulong, "<" + s_ulong, (), c_ulong),
171
172 (c_longlong, "<" + s_longlong, (), c_longlong),
173 (c_ulonglong, "<" + s_ulonglong, (), c_ulonglong),
174
175 (c_float, "<f", (), c_float),
176 (c_double, "<d", (), c_double),
177
178 (c_longdouble, "<" + s_longdouble, (), c_longdouble),
179
180 (c_bool, "<" + s_bool, (), c_bool),
181 (py_object, "<O", (), py_object),
182
183 ## pointers
184
185 (POINTER(c_byte), "&<b", (), POINTER(c_byte)),
186 (POINTER(POINTER(c_long)), "&&<" + s_long, (), POINTER(POINTER(c_long))),
187
188 ## arrays and pointers
189
190 (c_double * 4, "<d", (4,), c_double),
191 (c_double * 0, "<d", (0,), c_double),
192 (c_float * 4 * 3 * 2, "<f", (2,3,4), c_float),
193 (c_float * 4 * 0 * 2, "<f", (2,0,4), c_float),
194 (POINTER(c_short) * 2, "&<" + s_short, (2,), POINTER(c_short)),
195 (POINTER(c_short) * 2 * 3, "&<" + s_short, (3,2,), POINTER(c_short)),
196 (POINTER(c_short * 2), "&(2)<" + s_short, (), POINTER(c_short)),
197
198 ## structures and unions
199
200 (Point2, "T{<l:x:<l:y:}".replace('l', s_long), (), Point2),
201 (Point, "T{<l:x:<l:y:}".replace('l', s_long), (), Point),
202 (PackedPoint, "T{<l:x:<l:y:}".replace('l', s_long), (), PackedPoint),
203 (PointMidPad, "T{<b:x:3x<I:y:}".replace('I', s_uint), (), PointMidPad),
204 (PackedPointMidPad, "T{<b:x:x<Q:y:}", (), PackedPointMidPad),
205 (PointEndPad, "T{<I:x:<b:y:3x}".replace('I', s_uint), (), PointEndPad),
206 (PackedPointEndPad, "T{<Q:x:<b:y:x}", (), PackedPointEndPad),
207 (EmptyStruct, "T{}", (), EmptyStruct),
208 # the pep doesn't support unions
209 (aUnion, "B", (), aUnion),
210 # structure with sub-arrays
211 (StructWithArrays, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}".replace('l', s_long), (), StructWithArrays),
212 (StructWithArrays * 3, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}".replace('l', s_long), (3,), StructWithArrays),
213
214 ## pointer to incomplete structure
215 (Incomplete, "B", (), Incomplete),
216 (POINTER(Incomplete), "&B", (), POINTER(Incomplete)),
217
218 # 'Complete' is a structure that starts incomplete, but is completed after the
219 # pointer type to it has been created.
220 (Complete, "T{<l:a:}".replace('l', s_long), (), Complete),
221 # Unfortunately the pointer format string is not fixed...
222 (POINTER(Complete), "&B", (), POINTER(Complete)),
223
224 ## other
225
226 # function signatures are not implemented
227 (CFUNCTYPE(None), "X{}", (), CFUNCTYPE(None)),
228
229 ]
230
231
232 class BEPoint(BigEndianStructure):
233 _fields_ = [("x", c_long), ("y", c_long)]
234
235 class LEPoint(LittleEndianStructure):
236 _fields_ = [("x", c_long), ("y", c_long)]
237
238
239 # This table contains format strings as they really look, on both big
240 # and little endian machines.
241 endian_types = [
242 (BEPoint, "T{>l:x:>l:y:}".replace('l', s_long), (), BEPoint),
243 (LEPoint * 1, "T{<l:x:<l:y:}".replace('l', s_long), (1,), LEPoint),
244 (POINTER(BEPoint), "&T{>l:x:>l:y:}".replace('l', s_long), (), POINTER(BEPoint)),
245 (POINTER(LEPoint), "&T{<l:x:<l:y:}".replace('l', s_long), (), POINTER(LEPoint)),
246 ]
247
248
249 if __name__ == "__main__":
250 unittest.main()