File: test_struct.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (332 lines) | stat: -rw-r--r-- 13,044 bytes parent folder | download
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.module._rawffi.alt.interp_ffitype import app_types, W_FFIType
from pypy.module._rawffi.alt.interp_struct import compute_size_and_alignement, W_Field
from pypy.module._rawffi.alt.test.test_funcptr import BaseAppTestFFI


class TestStruct(object):

    class FakeSpace(object):
        def interp_w(self, cls, obj):
            return obj

    def compute(self, ffitypes_w):
        fields_w = [W_Field('<dummy>', w_ffitype) for
                    w_ffitype in ffitypes_w]
        return compute_size_and_alignement(self.FakeSpace(), fields_w)

    def sizeof(self, ffitypes_w):
        size, aligned, fields_w = self.compute(ffitypes_w)
        return size

    def test_compute_size(self):
        T = app_types
        byte_size = app_types.sbyte.sizeof()
        long_size = app_types.slong.sizeof()
        llong_size = app_types.slonglong.sizeof()
        llong_align = app_types.slonglong.get_alignment()
        #
        assert llong_align >= 4
        assert self.sizeof([T.sbyte, T.slong]) == 2*long_size
        assert self.sizeof([T.sbyte, T.slonglong]) == llong_align + llong_size
        assert self.sizeof([T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size
        assert self.sizeof([T.sbyte, T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size
        assert self.sizeof([T.sbyte, T.sbyte, T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size
        assert self.sizeof([T.slonglong, T.sbyte]) == llong_size + llong_align
        assert self.sizeof([T.slonglong, T.sbyte, T.sbyte]) == llong_size + llong_align
        assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align
        assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align


class AppTestStruct(BaseAppTestFFI):

    @classmethod
    def read_raw_mem(cls, addr, typename, length):
        import ctypes
        addr = ctypes.cast(addr, ctypes.c_void_p)
        c_type = getattr(ctypes, typename)
        array_type = ctypes.POINTER(c_type * length)
        ptr_array = ctypes.cast(addr, array_type)
        array = ptr_array[0]
        lst = [array[i] for i in range(length)]
        return lst

    def setup_class(cls):
        BaseAppTestFFI.setup_class.im_func(cls)

        from rpython.rlib import clibffi
        from rpython.rlib.rarithmetic import r_uint
        from rpython.rtyper.lltypesystem import lltype, rffi

        if cls.runappdirect:
            cls.w_read_raw_mem = cls.read_raw_mem
        else:
            @unwrap_spec(addr=r_uint, typename='text', length=int)
            def read_raw_mem_w(space, addr, typename, length):
                return space.wrap(cls.read_raw_mem(addr, typename, length))
            cls.w_read_raw_mem = cls.space.wrap(interp2app(read_raw_mem_w))
        #
        dummy_type = lltype.malloc(clibffi.FFI_TYPE_P.TO, flavor='raw')
        dummy_type.c_size = r_uint(123)
        dummy_type.c_alignment = rffi.cast(rffi.USHORT, 0)
        dummy_type.c_type = rffi.cast(rffi.USHORT, 0)
        cls.w_dummy_type = W_FFIType('dummy', dummy_type)
        cls.w_runappdirect = cls.space.wrap(cls.runappdirect)
        
    def test__StructDescr(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo', fields)
        assert descr.ffitype.sizeof() == longsize*2
        assert descr.ffitype.name == 'struct foo'

    def test_alignment(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.sbyte),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo', fields)
        assert descr.ffitype.sizeof() == longsize*2
        assert fields[0].offset == 0
        assert fields[1].offset == longsize # aligned to WORD

    def test_missing_field(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        raises(AttributeError, "struct.getfield('missing')")
        raises(AttributeError, "struct.setfield('missing', 42)")

    def test_unknown_type(self):
        if self.runappdirect:
            skip('cannot use self.dummy_type with -A')
        from _rawffi.alt import _StructDescr, Field
        fields = [
            Field('x', self.dummy_type),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        raises(TypeError, "struct.getfield('x')")
        raises(TypeError, "struct.setfield('x', 42)")

    def test_getfield_setfield(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('x', 42)
        struct.setfield('y', 43)
        assert struct.getfield('x') == 42
        assert struct.getfield('y') == 43
        mem = self.read_raw_mem(struct.getaddr(), 'c_long', 2)
        assert mem == [42, 43]

    def test_getfield_setfield_signed_types(self):
        import sys
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('sbyte', types.sbyte),
            Field('sshort', types.sshort),
            Field('sint', types.sint),
            Field('slong', types.slong),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('sbyte', 128)
        assert struct.getfield('sbyte') == -128
        struct.setfield('sshort', 32768)
        assert struct.getfield('sshort') == -32768
        struct.setfield('sint', 43)
        assert struct.getfield('sint') == 43
        struct.setfield('slong', sys.maxsize+1)
        assert struct.getfield('slong') == -sys.maxsize-1
        struct.setfield('slong', sys.maxsize*3)
        assert struct.getfield('slong') == sys.maxsize-2

    def test_getfield_setfield_unsigned_types(self):
        import sys
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('ubyte', types.ubyte),
            Field('ushort', types.ushort),
            Field('uint', types.uint),
            Field('ulong', types.ulong),
            Field('char', types.char),
            Field('unichar', types.unichar),
            Field('ptr', types.void_p),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('ubyte', -1)
        assert struct.getfield('ubyte') == 255
        struct.setfield('ushort', -1)
        assert struct.getfield('ushort') == 65535
        struct.setfield('uint', 43)
        assert struct.getfield('uint') == 43
        struct.setfield('ulong', -1)
        assert struct.getfield('ulong') == sys.maxsize*2 + 1
        struct.setfield('ulong', sys.maxsize*2 + 2)
        assert struct.getfield('ulong') == 0
        struct.setfield('char', b'a')
        assert struct.getfield('char') == b'a'
        struct.setfield('unichar', '\u1234')
        assert struct.getfield('unichar') == '\u1234'
        struct.setfield('ptr', -1)
        assert struct.getfield('ptr') == sys.maxsize*2 + 1
    
    def test_getfield_setfield_longlong(self):
        import sys
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('slonglong', types.slonglong),
            Field('ulonglong', types.ulonglong),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('slonglong', 9223372036854775808)
        assert struct.getfield('slonglong') == -9223372036854775808
        struct.setfield('ulonglong', -1)
        assert struct.getfield('ulonglong') == 18446744073709551615        
        mem = self.read_raw_mem(struct.getaddr(), 'c_longlong', 2)
        assert mem == [-9223372036854775808, -1]

    def test_getfield_setfield_float(self):
        import sys
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.double),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('x', 123.4)
        assert struct.getfield('x') == 123.4
        mem = self.read_raw_mem(struct.getaddr(), 'c_double', 1)
        assert mem == [123.4]

    def test_getfield_setfield_singlefloat(self):
        import sys
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.float),
            ]
        descr = _StructDescr('foo', fields)
        struct = descr.allocate()
        struct.setfield('x', 123.4) # this is a value which DOES loose
                                    # precision in a single float
        assert 0 < abs(struct.getfield('x') - 123.4) < 0.0001
        #
        struct.setfield('x', 123.5) # this is a value which does not loose
                                    # precision in a single float
        assert struct.getfield('x') == 123.5
        mem = self.read_raw_mem(struct.getaddr(), 'c_float', 1)
        assert mem == [123.5]

    def test_define_fields(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo')
        assert descr.ffitype.name == 'struct foo'
        assert repr(descr.ffitype) == '<ffi type struct foo (incomplete)>'
        raises(ValueError, "descr.ffitype.sizeof()")
        raises(ValueError, "descr.allocate()")
        #
        descr.define_fields(fields)
        assert repr(descr.ffitype) == '<ffi type struct foo>'
        assert descr.ffitype.sizeof() == longsize*2
        raises(ValueError, "descr.define_fields(fields)")

    def test_pointer_to_incomplete_struct(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        descr = _StructDescr('foo')
        foo_ffitype = descr.ffitype
        foo_p = types.Pointer(descr.ffitype)
        assert foo_p.deref_pointer() is foo_ffitype
        descr.define_fields(fields)
        assert descr.ffitype is foo_ffitype
        assert foo_p.deref_pointer() is foo_ffitype
        assert types.Pointer(descr.ffitype) is foo_p

    def test_nested_structure(self):
        from _rawffi.alt import _StructDescr, Field, types
        longsize = types.slong.sizeof()
        foo_fields = [
            Field('x', types.slong),
            Field('y', types.slong),
            ]
        foo_descr = _StructDescr('foo', foo_fields)
        #
        bar_fields = [
            Field('x', types.slong),
            Field('foo', foo_descr.ffitype),
            ]
        bar_descr = _StructDescr('bar', bar_fields)
        assert bar_descr.ffitype.sizeof() == longsize*3
        #
        struct = bar_descr.allocate()
        struct.setfield('x', 40)
        # reading a nested structure yields a reference to it
        struct_foo = struct.getfield('foo')
        struct_foo.setfield('x', 41)
        struct_foo.setfield('y', 42)
        mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3)
        assert mem == [40, 41, 42]
        #
        struct_foo2 = foo_descr.allocate()
        struct_foo2.setfield('x', 141)
        struct_foo2.setfield('y', 142)
        # writing a nested structure copies its memory into the target
        struct.setfield('foo', struct_foo2)
        struct_foo2.setfield('x', 241)
        struct_foo2.setfield('y', 242)
        mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3)
        assert mem == [40, 141, 142]
        mem = self.read_raw_mem(struct_foo2.getaddr(), 'c_long', 2)
        assert mem == [241, 242]



    def test_compute_shape(self):
        from _rawffi.alt import Structure, Field, types
        class Point(Structure):
            _fields_ = [
                Field('x', types.slong),
                Field('y', types.slong),
                ]

        longsize = types.slong.sizeof()
        assert isinstance(Point.x, Field)
        assert isinstance(Point.y, Field)
        assert Point.x.offset == 0
        assert Point.y.offset == longsize
        assert Point._struct_.ffitype.sizeof() == longsize*2
        assert Point._struct_.ffitype.name == 'struct Point'