1 | n/a | from ctypes import * |
---|
2 | n/a | from ctypes.test import need_symbol |
---|
3 | n/a | import unittest |
---|
4 | n/a | import os |
---|
5 | n/a | |
---|
6 | n/a | import _ctypes_test |
---|
7 | n/a | |
---|
8 | n/a | class BITS(Structure): |
---|
9 | n/a | _fields_ = [("A", c_int, 1), |
---|
10 | n/a | ("B", c_int, 2), |
---|
11 | n/a | ("C", c_int, 3), |
---|
12 | n/a | ("D", c_int, 4), |
---|
13 | n/a | ("E", c_int, 5), |
---|
14 | n/a | ("F", c_int, 6), |
---|
15 | n/a | ("G", c_int, 7), |
---|
16 | n/a | ("H", c_int, 8), |
---|
17 | n/a | ("I", c_int, 9), |
---|
18 | n/a | |
---|
19 | n/a | ("M", c_short, 1), |
---|
20 | n/a | ("N", c_short, 2), |
---|
21 | n/a | ("O", c_short, 3), |
---|
22 | n/a | ("P", c_short, 4), |
---|
23 | n/a | ("Q", c_short, 5), |
---|
24 | n/a | ("R", c_short, 6), |
---|
25 | n/a | ("S", c_short, 7)] |
---|
26 | n/a | |
---|
27 | n/a | func = CDLL(_ctypes_test.__file__).unpack_bitfields |
---|
28 | n/a | func.argtypes = POINTER(BITS), c_char |
---|
29 | n/a | |
---|
30 | n/a | ##for n in "ABCDEFGHIMNOPQRS": |
---|
31 | n/a | ## print n, hex(getattr(BITS, n).size), getattr(BITS, n).offset |
---|
32 | n/a | |
---|
33 | n/a | class C_Test(unittest.TestCase): |
---|
34 | n/a | |
---|
35 | n/a | def test_ints(self): |
---|
36 | n/a | for i in range(512): |
---|
37 | n/a | for name in "ABCDEFGHI": |
---|
38 | n/a | b = BITS() |
---|
39 | n/a | setattr(b, name, i) |
---|
40 | n/a | self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii'))) |
---|
41 | n/a | |
---|
42 | n/a | def test_shorts(self): |
---|
43 | n/a | for i in range(256): |
---|
44 | n/a | for name in "MNOPQRS": |
---|
45 | n/a | b = BITS() |
---|
46 | n/a | setattr(b, name, i) |
---|
47 | n/a | self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii'))) |
---|
48 | n/a | |
---|
49 | n/a | signed_int_types = (c_byte, c_short, c_int, c_long, c_longlong) |
---|
50 | n/a | unsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong) |
---|
51 | n/a | int_types = unsigned_int_types + signed_int_types |
---|
52 | n/a | |
---|
53 | n/a | class BitFieldTest(unittest.TestCase): |
---|
54 | n/a | |
---|
55 | n/a | def test_longlong(self): |
---|
56 | n/a | class X(Structure): |
---|
57 | n/a | _fields_ = [("a", c_longlong, 1), |
---|
58 | n/a | ("b", c_longlong, 62), |
---|
59 | n/a | ("c", c_longlong, 1)] |
---|
60 | n/a | |
---|
61 | n/a | self.assertEqual(sizeof(X), sizeof(c_longlong)) |
---|
62 | n/a | x = X() |
---|
63 | n/a | x.a, x.b, x.c = -1, 7, -1 |
---|
64 | n/a | self.assertEqual((x.a, x.b, x.c), (-1, 7, -1)) |
---|
65 | n/a | |
---|
66 | n/a | def test_ulonglong(self): |
---|
67 | n/a | class X(Structure): |
---|
68 | n/a | _fields_ = [("a", c_ulonglong, 1), |
---|
69 | n/a | ("b", c_ulonglong, 62), |
---|
70 | n/a | ("c", c_ulonglong, 1)] |
---|
71 | n/a | |
---|
72 | n/a | self.assertEqual(sizeof(X), sizeof(c_longlong)) |
---|
73 | n/a | x = X() |
---|
74 | n/a | self.assertEqual((x.a, x.b, x.c), (0, 0, 0)) |
---|
75 | n/a | x.a, x.b, x.c = 7, 7, 7 |
---|
76 | n/a | self.assertEqual((x.a, x.b, x.c), (1, 7, 1)) |
---|
77 | n/a | |
---|
78 | n/a | def test_signed(self): |
---|
79 | n/a | for c_typ in signed_int_types: |
---|
80 | n/a | class X(Structure): |
---|
81 | n/a | _fields_ = [("dummy", c_typ), |
---|
82 | n/a | ("a", c_typ, 3), |
---|
83 | n/a | ("b", c_typ, 3), |
---|
84 | n/a | ("c", c_typ, 1)] |
---|
85 | n/a | self.assertEqual(sizeof(X), sizeof(c_typ)*2) |
---|
86 | n/a | |
---|
87 | n/a | x = X() |
---|
88 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0)) |
---|
89 | n/a | x.a = -1 |
---|
90 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, -1, 0, 0)) |
---|
91 | n/a | x.a, x.b = 0, -1 |
---|
92 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, -1, 0)) |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | def test_unsigned(self): |
---|
96 | n/a | for c_typ in unsigned_int_types: |
---|
97 | n/a | class X(Structure): |
---|
98 | n/a | _fields_ = [("a", c_typ, 3), |
---|
99 | n/a | ("b", c_typ, 3), |
---|
100 | n/a | ("c", c_typ, 1)] |
---|
101 | n/a | self.assertEqual(sizeof(X), sizeof(c_typ)) |
---|
102 | n/a | |
---|
103 | n/a | x = X() |
---|
104 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0)) |
---|
105 | n/a | x.a = -1 |
---|
106 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 7, 0, 0)) |
---|
107 | n/a | x.a, x.b = 0, -1 |
---|
108 | n/a | self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 7, 0)) |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | def fail_fields(self, *fields): |
---|
112 | n/a | return self.get_except(type(Structure), "X", (), |
---|
113 | n/a | {"_fields_": fields}) |
---|
114 | n/a | |
---|
115 | n/a | def test_nonint_types(self): |
---|
116 | n/a | # bit fields are not allowed on non-integer types. |
---|
117 | n/a | result = self.fail_fields(("a", c_char_p, 1)) |
---|
118 | n/a | self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char_p')) |
---|
119 | n/a | |
---|
120 | n/a | result = self.fail_fields(("a", c_void_p, 1)) |
---|
121 | n/a | self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_void_p')) |
---|
122 | n/a | |
---|
123 | n/a | if c_int != c_long: |
---|
124 | n/a | result = self.fail_fields(("a", POINTER(c_int), 1)) |
---|
125 | n/a | self.assertEqual(result, (TypeError, 'bit fields not allowed for type LP_c_int')) |
---|
126 | n/a | |
---|
127 | n/a | result = self.fail_fields(("a", c_char, 1)) |
---|
128 | n/a | self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char')) |
---|
129 | n/a | |
---|
130 | n/a | class Dummy(Structure): |
---|
131 | n/a | _fields_ = [] |
---|
132 | n/a | |
---|
133 | n/a | result = self.fail_fields(("a", Dummy, 1)) |
---|
134 | n/a | self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy')) |
---|
135 | n/a | |
---|
136 | n/a | @need_symbol('c_wchar') |
---|
137 | n/a | def test_c_wchar(self): |
---|
138 | n/a | result = self.fail_fields(("a", c_wchar, 1)) |
---|
139 | n/a | self.assertEqual(result, |
---|
140 | n/a | (TypeError, 'bit fields not allowed for type c_wchar')) |
---|
141 | n/a | |
---|
142 | n/a | def test_single_bitfield_size(self): |
---|
143 | n/a | for c_typ in int_types: |
---|
144 | n/a | result = self.fail_fields(("a", c_typ, -1)) |
---|
145 | n/a | self.assertEqual(result, (ValueError, 'number of bits invalid for bit field')) |
---|
146 | n/a | |
---|
147 | n/a | result = self.fail_fields(("a", c_typ, 0)) |
---|
148 | n/a | self.assertEqual(result, (ValueError, 'number of bits invalid for bit field')) |
---|
149 | n/a | |
---|
150 | n/a | class X(Structure): |
---|
151 | n/a | _fields_ = [("a", c_typ, 1)] |
---|
152 | n/a | self.assertEqual(sizeof(X), sizeof(c_typ)) |
---|
153 | n/a | |
---|
154 | n/a | class X(Structure): |
---|
155 | n/a | _fields_ = [("a", c_typ, sizeof(c_typ)*8)] |
---|
156 | n/a | self.assertEqual(sizeof(X), sizeof(c_typ)) |
---|
157 | n/a | |
---|
158 | n/a | result = self.fail_fields(("a", c_typ, sizeof(c_typ)*8 + 1)) |
---|
159 | n/a | self.assertEqual(result, (ValueError, 'number of bits invalid for bit field')) |
---|
160 | n/a | |
---|
161 | n/a | def test_multi_bitfields_size(self): |
---|
162 | n/a | class X(Structure): |
---|
163 | n/a | _fields_ = [("a", c_short, 1), |
---|
164 | n/a | ("b", c_short, 14), |
---|
165 | n/a | ("c", c_short, 1)] |
---|
166 | n/a | self.assertEqual(sizeof(X), sizeof(c_short)) |
---|
167 | n/a | |
---|
168 | n/a | class X(Structure): |
---|
169 | n/a | _fields_ = [("a", c_short, 1), |
---|
170 | n/a | ("a1", c_short), |
---|
171 | n/a | ("b", c_short, 14), |
---|
172 | n/a | ("c", c_short, 1)] |
---|
173 | n/a | self.assertEqual(sizeof(X), sizeof(c_short)*3) |
---|
174 | n/a | self.assertEqual(X.a.offset, 0) |
---|
175 | n/a | self.assertEqual(X.a1.offset, sizeof(c_short)) |
---|
176 | n/a | self.assertEqual(X.b.offset, sizeof(c_short)*2) |
---|
177 | n/a | self.assertEqual(X.c.offset, sizeof(c_short)*2) |
---|
178 | n/a | |
---|
179 | n/a | class X(Structure): |
---|
180 | n/a | _fields_ = [("a", c_short, 3), |
---|
181 | n/a | ("b", c_short, 14), |
---|
182 | n/a | ("c", c_short, 14)] |
---|
183 | n/a | self.assertEqual(sizeof(X), sizeof(c_short)*3) |
---|
184 | n/a | self.assertEqual(X.a.offset, sizeof(c_short)*0) |
---|
185 | n/a | self.assertEqual(X.b.offset, sizeof(c_short)*1) |
---|
186 | n/a | self.assertEqual(X.c.offset, sizeof(c_short)*2) |
---|
187 | n/a | |
---|
188 | n/a | |
---|
189 | n/a | def get_except(self, func, *args, **kw): |
---|
190 | n/a | try: |
---|
191 | n/a | func(*args, **kw) |
---|
192 | n/a | except Exception as detail: |
---|
193 | n/a | return detail.__class__, str(detail) |
---|
194 | n/a | |
---|
195 | n/a | def test_mixed_1(self): |
---|
196 | n/a | class X(Structure): |
---|
197 | n/a | _fields_ = [("a", c_byte, 4), |
---|
198 | n/a | ("b", c_int, 4)] |
---|
199 | n/a | if os.name == "nt": |
---|
200 | n/a | self.assertEqual(sizeof(X), sizeof(c_int)*2) |
---|
201 | n/a | else: |
---|
202 | n/a | self.assertEqual(sizeof(X), sizeof(c_int)) |
---|
203 | n/a | |
---|
204 | n/a | def test_mixed_2(self): |
---|
205 | n/a | class X(Structure): |
---|
206 | n/a | _fields_ = [("a", c_byte, 4), |
---|
207 | n/a | ("b", c_int, 32)] |
---|
208 | n/a | self.assertEqual(sizeof(X), alignment(c_int)+sizeof(c_int)) |
---|
209 | n/a | |
---|
210 | n/a | def test_mixed_3(self): |
---|
211 | n/a | class X(Structure): |
---|
212 | n/a | _fields_ = [("a", c_byte, 4), |
---|
213 | n/a | ("b", c_ubyte, 4)] |
---|
214 | n/a | self.assertEqual(sizeof(X), sizeof(c_byte)) |
---|
215 | n/a | |
---|
216 | n/a | def test_mixed_4(self): |
---|
217 | n/a | class X(Structure): |
---|
218 | n/a | _fields_ = [("a", c_short, 4), |
---|
219 | n/a | ("b", c_short, 4), |
---|
220 | n/a | ("c", c_int, 24), |
---|
221 | n/a | ("d", c_short, 4), |
---|
222 | n/a | ("e", c_short, 4), |
---|
223 | n/a | ("f", c_int, 24)] |
---|
224 | n/a | # MSVC does NOT combine c_short and c_int into one field, GCC |
---|
225 | n/a | # does (unless GCC is run with '-mms-bitfields' which |
---|
226 | n/a | # produces code compatible with MSVC). |
---|
227 | n/a | if os.name == "nt": |
---|
228 | n/a | self.assertEqual(sizeof(X), sizeof(c_int) * 4) |
---|
229 | n/a | else: |
---|
230 | n/a | self.assertEqual(sizeof(X), sizeof(c_int) * 2) |
---|
231 | n/a | |
---|
232 | n/a | def test_anon_bitfields(self): |
---|
233 | n/a | # anonymous bit-fields gave a strange error message |
---|
234 | n/a | class X(Structure): |
---|
235 | n/a | _fields_ = [("a", c_byte, 4), |
---|
236 | n/a | ("b", c_ubyte, 4)] |
---|
237 | n/a | class Y(Structure): |
---|
238 | n/a | _anonymous_ = ["_"] |
---|
239 | n/a | _fields_ = [("_", X)] |
---|
240 | n/a | |
---|
241 | n/a | @need_symbol('c_uint32') |
---|
242 | n/a | def test_uint32(self): |
---|
243 | n/a | class X(Structure): |
---|
244 | n/a | _fields_ = [("a", c_uint32, 32)] |
---|
245 | n/a | x = X() |
---|
246 | n/a | x.a = 10 |
---|
247 | n/a | self.assertEqual(x.a, 10) |
---|
248 | n/a | x.a = 0xFDCBA987 |
---|
249 | n/a | self.assertEqual(x.a, 0xFDCBA987) |
---|
250 | n/a | |
---|
251 | n/a | @need_symbol('c_uint64') |
---|
252 | n/a | def test_uint64(self): |
---|
253 | n/a | class X(Structure): |
---|
254 | n/a | _fields_ = [("a", c_uint64, 64)] |
---|
255 | n/a | x = X() |
---|
256 | n/a | x.a = 10 |
---|
257 | n/a | self.assertEqual(x.a, 10) |
---|
258 | n/a | x.a = 0xFEDCBA9876543211 |
---|
259 | n/a | self.assertEqual(x.a, 0xFEDCBA9876543211) |
---|
260 | n/a | |
---|
261 | n/a | @need_symbol('c_uint32') |
---|
262 | n/a | def test_uint32_swap_little_endian(self): |
---|
263 | n/a | # Issue #23319 |
---|
264 | n/a | class Little(LittleEndianStructure): |
---|
265 | n/a | _fields_ = [("a", c_uint32, 24), |
---|
266 | n/a | ("b", c_uint32, 4), |
---|
267 | n/a | ("c", c_uint32, 4)] |
---|
268 | n/a | b = bytearray(4) |
---|
269 | n/a | x = Little.from_buffer(b) |
---|
270 | n/a | x.a = 0xabcdef |
---|
271 | n/a | x.b = 1 |
---|
272 | n/a | x.c = 2 |
---|
273 | n/a | self.assertEqual(b, b'\xef\xcd\xab\x21') |
---|
274 | n/a | |
---|
275 | n/a | @need_symbol('c_uint32') |
---|
276 | n/a | def test_uint32_swap_big_endian(self): |
---|
277 | n/a | # Issue #23319 |
---|
278 | n/a | class Big(BigEndianStructure): |
---|
279 | n/a | _fields_ = [("a", c_uint32, 24), |
---|
280 | n/a | ("b", c_uint32, 4), |
---|
281 | n/a | ("c", c_uint32, 4)] |
---|
282 | n/a | b = bytearray(4) |
---|
283 | n/a | x = Big.from_buffer(b) |
---|
284 | n/a | x.a = 0xabcdef |
---|
285 | n/a | x.b = 1 |
---|
286 | n/a | x.c = 2 |
---|
287 | n/a | self.assertEqual(b, b'\xab\xcd\xef\x12') |
---|
288 | n/a | |
---|
289 | n/a | if __name__ == "__main__": |
---|
290 | n/a | unittest.main() |
---|