aboutsummaryrefslogtreecommitdiff
blob: 790e6890758163cd3fda1f0c4496ccae14411aed (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import py
from ctypes import *
from support import BaseCTypesTestChecker
import os

import ctypes

signed_int_types = (c_byte, c_short, c_int, c_long, c_longlong)
unsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong)
int_types = unsigned_int_types + signed_int_types


def setup_module(mod):
    import conftest
    _ctypes_test = str(conftest.sofile)
    func = CDLL(_ctypes_test).unpack_bitfields
    func.argtypes = POINTER(BITS), c_char
    mod.func = func


class BITS(Structure):
    _fields_ = [("A", c_int, 1),
                ("B", c_int, 2),
                ("C", c_int, 3),
                ("D", c_int, 4),
                ("E", c_int, 5),
                ("F", c_int, 6),
                ("G", c_int, 7),
                ("H", c_int, 8),
                ("I", c_int, 9),

                ("M", c_short, 1),
                ("N", c_short, 2),
                ("O", c_short, 3),
                ("P", c_short, 4),
                ("Q", c_short, 5),
                ("R", c_short, 6),
                ("S", c_short, 7)]


class TestC:
    def test_ints(self):
        for i in range(512):
            for name in "ABCDEFGHI":
                b = BITS()
                setattr(b, name, i)
                assert (name, i, getattr(b, name)) == (name, i, func(byref(b), name))

    def test_shorts(self):
        for i in range(256):
            for name in "MNOPQRS":
                b = BITS()
                setattr(b, name, i)
                assert (name, i, getattr(b, name)) == (name, i, func(byref(b), name))


class TestBitField:
    def test_longlong(self):
        class X(Structure):
            _fields_ = [("a", c_longlong, 1),
                        ("b", c_longlong, 62),
                        ("c", c_longlong, 1)]

        assert sizeof(X) == sizeof(c_longlong)
        x = X()
        x.a, x.b, x.c = -1, 7, -1
        assert (x.a, x.b, x.c) == (-1, 7, -1)

        x = X()
        x.a, x.b, x.c = -1, -7, -1
        assert (x.a, x.b, x.c) == (-1, -7, -1)

    def test_ulonglong(self):
        class X(Structure):
            _fields_ = [("a", c_ulonglong, 1),
                        ("b", c_ulonglong, 62),
                        ("c", c_ulonglong, 1)]

        assert sizeof(X) == sizeof(c_longlong)
        x = X()
        assert (x.a, x.b, x.c) == (0, 0, 0)
        x.a, x.b, x.c = 7, 2305843009213693953, 7
        assert (x.a, x.b, x.c) == (1, 2305843009213693953, 1)

    def test_signed(self):
        for c_typ in signed_int_types:
            class X(Structure):
                _fields_ = [("dummy", c_typ),
                            ("a", c_typ, 3),
                            ("b", c_typ, 3),
                            ("c", c_typ, 1)]
            assert sizeof(X) == sizeof(c_typ)*2

            x = X()
            assert (c_typ, x.a, x.b, x.c) == (c_typ, 0, 0, 0)
            x.a = -1
            assert (c_typ, x.a, x.b, x.c) == (c_typ, -1, 0, 0)
            x.a, x.b = 0, -1
            assert (c_typ, x.a, x.b, x.c) == (c_typ, 0, -1, 0)

    def test_unsigned(self):
        for c_typ in unsigned_int_types:
            class X(Structure):
                _fields_ = [("a", c_typ, 3),
                            ("b", c_typ, 3),
                            ("c", c_typ, 1)]
            assert sizeof(X) == sizeof(c_typ)

            x = X()
            assert (c_typ, x.a, x.b, x.c) == (c_typ, 0, 0, 0)
            x.a = -1
            assert (c_typ, x.a, x.b, x.c) == (c_typ, 7, 0, 0)
            x.a, x.b = 0, -1
            assert (c_typ, x.a, x.b, x.c) == (c_typ, 0, 7, 0)

    def fail_fields(self, *fields):
        return self.get_except(type(Structure), "X", (),
                               {"_fields_": fields})

    def test_nonint_types(self):
        # bit fields are not allowed on non-integer types.
        result = self.fail_fields(("a", c_char_p, 1))
        assert result == (TypeError, 'bit fields not allowed for type c_char_p')

        result = self.fail_fields(("a", c_void_p, 1))
        assert result == (TypeError, 'bit fields not allowed for type c_void_p')

        if c_int != c_long:
            result = self.fail_fields(("a", POINTER(c_int), 1))
            assert result == (TypeError, 'bit fields not allowed for type LP_c_int')

        result = self.fail_fields(("a", c_char, 1))
        assert result == (TypeError, 'bit fields not allowed for type c_char')

        try:
            c_wchar
        except NameError:
            pass
        else:
            result = self.fail_fields(("a", c_wchar, 1))
            assert result == (TypeError, 'bit fields not allowed for type c_wchar')

        class Dummy(Structure):
            _fields_ = []

        result = self.fail_fields(("a", Dummy, 1))
        assert result == (TypeError, 'bit fields not allowed for type Dummy')

    def test_single_bitfield_size(self):
        for c_typ in int_types:
            result = self.fail_fields(("a", c_typ, -1))
            assert result == (ValueError, 'number of bits invalid for bit field')

            result = self.fail_fields(("a", c_typ, 0))
            assert result == (ValueError, 'number of bits invalid for bit field')

            class X(Structure):
                _fields_ = [("a", c_typ, 1)]
            assert sizeof(X) == sizeof(c_typ)

            class X(Structure):
                _fields_ = [("a", c_typ, sizeof(c_typ)*8)]
            assert sizeof(X) == sizeof(c_typ)

            result = self.fail_fields(("a", c_typ, sizeof(c_typ)*8 + 1))
            assert result == (ValueError, 'number of bits invalid for bit field')

    def test_multi_bitfields_size(self):
        class X(Structure):
            _fields_ = [("a", c_short, 1),
                        ("b", c_short, 14),
                        ("c", c_short, 1)]
        assert sizeof(X) == sizeof(c_short)

        class X(Structure):
            _fields_ = [("a", c_short, 1),
                        ("a1", c_short),
                        ("b", c_short, 14),
                        ("c", c_short, 1)]
        assert sizeof(X) == sizeof(c_short)*3
        assert X.a.offset == 0
        assert X.a1.offset == sizeof(c_short)
        assert X.b.offset == sizeof(c_short)*2
        assert X.c.offset == sizeof(c_short)*2

        class X(Structure):
            _fields_ = [("a", c_short, 3),
                        ("b", c_short, 14),
                        ("c", c_short, 14)]
        assert sizeof(X) == sizeof(c_short)*3
        assert X.a.offset == sizeof(c_short)*0
        assert X.b.offset == sizeof(c_short)*1
        assert X.c.offset == sizeof(c_short)*2

    def get_except(self, func, *args, **kw):
        try:
            func(*args, **kw)
        except Exception as detail:
            import traceback
            traceback.print_exc()
            return detail.__class__, str(detail)

    def test_mixed_1(self):
        class X(Structure):
            _fields_ = [("a", c_byte, 4),
                        ("b", c_int, 4)]
        if os.name in ("nt", "ce"):
            assert sizeof(X) == sizeof(c_int)*2
        else:
            assert sizeof(X) == sizeof(c_int)

    def test_mixed_2(self):
        class X(Structure):
            _fields_ = [("a", c_byte, 4),
                        ("b", c_int, 32)]
        assert sizeof(X) == sizeof(c_int)*2

    def test_mixed_3(self):
        class X(Structure):
            _fields_ = [("a", c_byte, 4),
                        ("b", c_ubyte, 4)]
        assert sizeof(X) == sizeof(c_byte)

    def test_anon_bitfields(self):
        # anonymous bit-fields gave a strange error message
        class X(Structure):
            _fields_ = [("a", c_byte, 4),
                        ("b", c_ubyte, 4)]
        class Y(Structure):
            _anonymous_ = ["_"]
            _fields_ = [("_", X)]

    def test_set_fields_attr(self):
        class A(Structure):
            pass
        A._fields_ = [("a", c_byte),
                      ("b", c_ubyte)]

    def test_set_fields_attr_bitfields(self):
        class A(Structure):
            pass
        A._fields_ = [("a", POINTER(A)),
                      ("b", c_ubyte, 4)]

    def test_set_fields_cycle_fails(self):
        class A(Structure):
            pass
        import pytest
        pytest.raises(AttributeError, """
            A._fields_ = [("a", A)]
                      """)