Ejemplo n.º 1
0
    def test_structreturn(self):
        import _rawffi
        X = _rawffi.Structure([('x', 'l')])
        x = X()
        x.x = 121
        Tm = _rawffi.Structure([('tm_sec', 'i'), ('tm_min', 'i'),
                                ('tm_hour', 'i'), ("tm_mday", 'i'),
                                ("tm_mon", 'i'), ("tm_year", 'i'),
                                ("tm_wday", 'i'), ("tm_yday", 'i'),
                                ("tm_isdst", 'i')])
        libc = _rawffi.get_libc()
        try:
            gmtime = libc.ptr('gmtime', ['P'], 'P')
        except AttributeError:
            # Since msvcr80, this function is named differently
            gmtime = libc.ptr('_gmtime32', ['P'], 'P')

        arg = x.byptr()
        res = gmtime(arg)
        t = Tm.fromaddress(res[0])
        arg.free()
        assert t.tm_year == 70
        assert t.tm_sec == 1
        assert t.tm_min == 2
        x.free()
Ejemplo n.º 2
0
 def test_array_of_structure(self):
     import _rawffi
     lib = _rawffi.CDLL(self.lib_name)
     A = _rawffi.Array('P')
     X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'),
                            ('next', 'P')])
     x = X()
     x.x2 = 3
     a = A(3)
     a[1] = x
     get_array_elem_s = lib.ptr('get_array_elem_s', ['P', 'i'], 'P')
     arg1 = a.byptr()
     arg2 = _rawffi.Array('i')(1)
     res = get_array_elem_s(arg1, arg2)
     assert res[0] == 0
     res.free()
     arg2[0] = 1
     res = get_array_elem_s(arg1, arg2)
     assert X.fromaddress(res[0]).x2 == 3
     assert res[0] == x.buffer
     res.free()
     arg1.free()
     arg2.free()
     x.free()
     a.free()
Ejemplo n.º 3
0
    def test_ret_struct(self):
        import _rawffi
        S2H = _rawffi.Structure([('x', 'h'), ('y', 'h')])
        s2h = S2H()
        lib = _rawffi.CDLL(self.lib_name)
        give = lib.ptr('give', ['h', 'h'], (S2H, 1))
        a1 = _rawffi.Array('h')(1)
        a2 = _rawffi.Array('h')(1)
        a1[0] = 13
        a2[0] = 17
        res = give(a1, a2)
        assert isinstance(res, _rawffi.StructureInstanceAutoFree)
        assert res.shape is S2H
        assert res.x == 13
        assert res.y == 17
        a1.free()
        a2.free()

        s2h.x = 7
        s2h.y = 11
        perturb = lib.ptr('perturb', [(S2H, 1)], (S2H, 1))
        res = perturb(s2h)
        assert isinstance(res, _rawffi.StructureInstanceAutoFree)
        assert res.shape is S2H
        assert res.x == 14
        assert res.y == 33
        assert s2h.x == 7
        assert s2h.y == 11

        s2h.free()
Ejemplo n.º 4
0
    def test_ret_struct(self):
        if self.isx86_64:
            skip("Segfaults on x86_64 because small structures "
                 "may be passed in registers and "
                 "c_elements must not be null")

        import _rawffi
        S2H = _rawffi.Structure([('x', 'h'), ('y', 'h')])
        s2h = S2H()
        lib = _rawffi.CDLL(self.lib_name)
        give = lib.ptr('give', ['h', 'h'], (S2H, 1))
        a1 = _rawffi.Array('h')(1)
        a2 = _rawffi.Array('h')(1)
        a1[0] = 13
        a2[0] = 17
        res = give(a1, a2)
        assert isinstance(res, _rawffi.StructureInstanceAutoFree)
        assert res.shape is S2H
        assert res.x == 13
        assert res.y == 17
        a1.free()
        a2.free()

        s2h.x = 7
        s2h.y = 11
        perturb = lib.ptr('perturb', [(S2H, 1)], (S2H, 1))
        res = perturb(s2h)
        assert isinstance(res, _rawffi.StructureInstanceAutoFree)
        assert res.shape is S2H
        assert res.x == 14
        assert res.y == 33
        assert s2h.x == 7
        assert s2h.y == 11

        s2h.free()
Ejemplo n.º 5
0
    def __new__(self, name, cls, typedict):
        res = type.__new__(self, name, cls, typedict)
        if '_fields_' in typedict:
            if not hasattr(typedict.get('_anonymous_', []), '__iter__'):
                raise TypeError("Anonymous field must be iterable")
            for item in typedict.get('_anonymous_', []):
                if item not in dict(typedict['_fields_']):
                    raise AttributeError("Anonymous field not found")
            res._names, rawfields, res._fieldtypes = names_and_fields(
                typedict['_fields_'], cls[0], False,
                typedict.get('_anonymous_', None))
            res._ffistruct = _rawffi.Structure(rawfields)
            res._ffishape = res._ffistruct.gettypecode()
            res._ffiargshape = res._ffishape

        def __init__(self, *args, **kwds):
            if not hasattr(self, '_ffistruct'):
                raise TypeError(
                    "Cannot instantiate structure, has no _fields_")
            self.__dict__['_buffer'] = self._ffistruct()
            self.__dict__['_needs_free'] = True
            self.__dict__['_objects'] = {}
            if len(args) > len(self._names):
                raise TypeError("too many arguments")
            for name, arg in zip(self._names, args):
                if name in kwds:
                    raise TypeError("duplicate value for argument %r" %
                                    (name, ))
                self.__setattr__(name, arg)
            for name, arg in kwds.items():
                self.__setattr__(name, arg)

        res.__init__ = __init__

        return res
Ejemplo n.º 6
0
    def test_buffer(self):
        import _rawffi
        S = _rawffi.Structure((40, 1))
        s = S(autofree=True)
        b = buffer(s)
        assert len(b) == 40
        b[4] = b'X'
        b[:3] = b'ABC'
        assert b[:6] == b'ABC\x00X\x00'

        A = _rawffi.Array('c')
        a = A(10, autofree=True)
        a[3] = b'x'
        b = buffer(a)
        assert len(b) == 10
        assert b[3] == b'x'
        b[6] = b'y'
        assert a[6] == b'y'
        b[3:5] = b'zt'
        assert a[3] == b'z'
        assert a[4] == b't'

        b = memoryview(a)
        assert len(b) == 10
        assert b[3] == b'z'
        b[3] = b'x'
        assert b[3] == b'x'
Ejemplo n.º 7
0
 def test_structure_bitfields_uint(self):
     import _rawffi
     Y = _rawffi.Structure([('a', 'I', 1), ('b', 'I', 30), ('c', 'I', 1)])
     y = Y()
     y.a, y.b, y.c = 7, (1 << 29) | 1, 7
     assert (y.a, y.b, y.c) == (1, (1 << 29) | 1, 1)
     y.free()
Ejemplo n.º 8
0
    def test_gettimeofday(self):
        if self.iswin32:
            skip("No gettimeofday on win32")
        import _rawffi
        struct_type = _rawffi.Structure([('tv_sec', 'l'), ('tv_usec', 'l')])
        structure = struct_type()
        libc = _rawffi.get_libc()
        gettimeofday = libc.ptr('gettimeofday', ['P', 'P'], 'i')

        arg1 = structure.byptr()
        arg2 = _rawffi.Array('P')(1)
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0

        struct2 = struct_type()
        arg1[0] = struct2
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0

        assert structure.tv_usec != struct2.tv_usec
        assert (structure.tv_sec == struct2.tv_sec) or (structure.tv_sec
                                                        == struct2.tv_sec - 1)
        raises(AttributeError, "structure.xxx")
        structure.free()
        struct2.free()
        arg1.free()
        arg2.free()
Ejemplo n.º 9
0
 def test_structure(self):
     import _rawffi
     oldnum = _rawffi._num_of_allocated_objects()
     s = _rawffi.Structure([('a', 'i'), ('b', 'i')])()
     assert _rawffi._num_of_allocated_objects() - oldnum == 1
     s.free()
     assert _rawffi._num_of_allocated_objects() - oldnum == 0
Ejemplo n.º 10
0
 def test_structure_bitfields_longlong(self):
     import _rawffi
     Y = _rawffi.Structure([('a', 'q', 1), ('b', 'q', 62), ('c', 'q', 1)])
     y = Y()
     y.a, y.b, y.c = -1, -7, 1
     assert (y.a, y.b, y.c) == (-1, -7, -1)
     y.free()
Ejemplo n.º 11
0
    def test_gettimeofday(self):
        import _rawffi
        struct_type = _rawffi.Structure([('tv_sec', 'l'), ('tv_usec', 'l')])
        structure = struct_type()
        libc = _rawffi.CDLL('libc.so.6')
        gettimeofday = libc.ptr('gettimeofday', ['P', 'P'], 'i')

        arg1 = structure.byptr()
        arg2 = _rawffi.Array('P')(1)
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0
        res.free()

        struct2 = struct_type()
        arg1[0] = struct2
        res = gettimeofday(arg1, arg2)
        assert res[0] == 0
        res.free()

        assert structure.tv_usec != struct2.tv_usec
        assert (structure.tv_sec == struct2.tv_sec) or (structure.tv_sec
                                                        == struct2.tv_sec - 1)
        raises(AttributeError, "structure.xxx")
        structure.free()
        struct2.free()
        arg1.free()
        arg2.free()
Ejemplo n.º 12
0
 def test_structure_bitfields_ulonglong(self):
     import _rawffi
     Y = _rawffi.Structure([('a', 'Q', 1), ('b', 'Q', 62), ('c', 'Q', 1)])
     y = Y()
     y.a, y.b, y.c = 7, (1 << 61) | 1, 7
     assert (y.a, y.b, y.c) == (1, (1 << 61) | 1, 1)
     y.free()
Ejemplo n.º 13
0
 def test_pypy_raw_address(self):
     import _rawffi
     S = _rawffi.Structure((40, 1))
     s = S(autofree=True)
     addr = buffer(s)._pypy_raw_address()
     assert type(addr) is int
     assert buffer(s)._pypy_raw_address() == addr
     assert buffer(s, 10)._pypy_raw_address() == addr + 10
Ejemplo n.º 14
0
 def test_unaligned(self):
     import _rawffi
     for k in self.float_typemap:
         S = _rawffi.Structure([('pad', 'c'), ('value', k)], pack=1)
         s = S()
         s.value = 4
         assert s.value == 4
         s.free()
Ejemplo n.º 15
0
    def test_structure_bitfields(self):
        import _rawffi
        X = _rawffi.Structure([('A', 'I', 1), ('B', 'I', 2), ('C', 'i', 2)])
        x = X()
        x.A = 0xf
        x.B = 0xf
        x.C = 0xf
        assert x.A == 1
        assert x.B == 3
        assert x.C == -1
        x.free()

        Y = _rawffi.Structure([('a', 'i', 1), ('b', 'i', 30), ('c', 'i', 1)])
        y = Y()
        y.a, y.b, y.c = -1, -7, 0
        assert (y.a, y.b, y.c) == (-1, -7, 0)
        y.free()
Ejemplo n.º 16
0
 def test_pypy_raw_address(self):
     import _rawffi
     S = _rawffi.Structure((40, 1))
     s = S(autofree=True)
     addr = memoryview(s)._pypy_raw_address()
     assert type(addr) is int
     assert memoryview(s)._pypy_raw_address() == addr
     assert memoryview(s)[10:]._pypy_raw_address() == addr + 10
Ejemplo n.º 17
0
    def test_repr(self):
        import _rawffi, struct
        s = struct.calcsize("i")
        assert (repr(_rawffi.Array('i')) == "<_rawffi.Array 'i' (%d, %d)>" %
                (s, s))
        assert repr(_rawffi.Array((18, 2))) == "<_rawffi.Array 'V' (18, 2)>"
        assert (repr(_rawffi.Structure([
            ('x', 'i'), ('yz', 'i')
        ])) == "<_rawffi.Structure 'x' 'yz' (%d, %d)>" % (2 * s, s))

        s = _rawffi.Structure([('x', 'i'), ('yz', 'i')])()
        assert repr(s) == "<_rawffi struct %x>" % (s.buffer, )
        s.free()
        a = _rawffi.Array('i')(5)
        assert repr(a) == "<_rawffi array %x of length %d>" % (a.buffer,
                                                               len(a))
        a.free()
Ejemplo n.º 18
0
 def test_structure_autofree(self):
     import gc, _rawffi
     S = _rawffi.Structure([('x', 'i')])
     oldnum = _rawffi._num_of_allocated_objects()
     s = S(autofree=True)
     s.x = 3
     s = None
     gc.collect()
     assert oldnum == _rawffi._num_of_allocated_objects()
Ejemplo n.º 19
0
 def test_nested_structures(self):
     import _rawffi
     S1 = _rawffi.Structure([('a', 'i'), ('b', 'P'), ('c', 'c')])
     S = _rawffi.Structure([('x', 'c'), ('s1', (S1, 1))])
     assert S.size == S1.alignment + S1.size
     assert S.alignment == S1.alignment
     assert S.fieldoffset('x') == 0
     assert S.fieldoffset('s1') == S1.alignment
     s = S()
     s.x = 'G'
     raises(TypeError, 's.s1')
     assert s.fieldaddress('s1') == s.buffer + S.fieldoffset('s1')
     s1 = S1.fromaddress(s.fieldaddress('s1'))
     s1.c = 'H'
     rawbuf = _rawffi.Array('c').fromaddress(s.buffer, S.size)
     assert rawbuf[0] == 'G'
     assert rawbuf[S1.alignment + S1.fieldoffset('c')] == 'H'
     s.free()
Ejemplo n.º 20
0
    def test_inspect_structure(self):
        import _rawffi, struct

        E = _rawffi.Structure([])
        assert E.size == 0
        assert E.alignment == 1
        
        align = max(struct.calcsize("i"), struct.calcsize("P"))
        assert align & (align-1) == 0, "not a power of 2??"
        def round_up(x):
            return (x+align-1) & -align

        S = _rawffi.Structure([('a', 'i'), ('b', 'P'), ('c', 'c')])
        assert S.size == round_up(struct.calcsize("iPc"))
        assert S.alignment == align
        assert S.fieldoffset('a') == 0
        assert S.fieldoffset('b') == align
        assert S.fieldoffset('c') == round_up(struct.calcsize("iP"))
        assert S.size_alignment() == (S.size, S.alignment)
        assert S.size_alignment(1) == (S.size, S.alignment)
Ejemplo n.º 21
0
 def test_opaque_structure(self):
     import _rawffi
     # define opaque structure with size = 200 and aligment = 16
     N = _rawffi.Structure((200, 16))
     assert N.size == 200
     assert N.alignment == 16
     assert N.size_alignment() == (200, 16)
     assert N.size_alignment(1) == (200, 16)
     raises(AttributeError, N.fieldoffset, '_')
     n = N()
     n.free()
Ejemplo n.º 22
0
 def test_structure_bitfields_varied(self):
     import _rawffi
     X = _rawffi.Structure([('A', 'I', 1), ('B', 'I', 2), ('C', 'i', 2)])
     x = X()
     x.A = 0xf
     x.B = 0xf
     x.C = 0xf
     assert x.A == 1
     assert x.B == 3
     assert x.C == -1
     x.free()
Ejemplo n.º 23
0
 def test_structure_bitfields_char(self):
     import _rawffi
     X = _rawffi.Structure([('A', 'B', 1), ('B', 'B', 6), ('C', 'B', 1)])
     x = X()
     x.A = 0xf
     x.B = 0xff
     x.C = 0xf
     assert x.A == 1
     assert x.B == 63
     assert x.C == 1
     x.free()
Ejemplo n.º 24
0
 def test_struct_byvalue(self):
     import _rawffi, sys
     X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')])
     x_y = X_Y()
     lib = _rawffi.CDLL(self.lib_name)
     sum_x_y = lib.ptr('sum_x_y', [(X_Y, 1)], 'l')
     x_y.x = 200
     x_y.y = 220
     res = sum_x_y(x_y)
     assert res[0] == 420
     x_y.free()
Ejemplo n.º 25
0
 def test_structure_bitfields_single_unsigned(self):
     import _rawffi
     for s in [('I', 32), ('Q', 64)]:
         Y = _rawffi.Structure([('a', ) + s])
         y = Y()
         y.a = 10
         assert y.a == 10
         val = (1 << (s[1] - 1)) | 1
         y.a = val
         assert y.a == val
         y.free()
Ejemplo n.º 26
0
def _set_shape(tp):
    size = tp._sizeofinstances()
    alignment = tp._alignmentofinstances()
    tp._ffiopaque = _rawffi.Structure((size, alignment))  # opaque
    tp._ffiargshape = tp._ffishape = (tp._ffiopaque, 1)
    tp._fficompositesize = tp._ffiopaque.size
    # we need to create an array of size one for each
    # of our elements
    tp._ffiarrays = {}
    for name, field in tp._fieldtypes.iteritems():
        tp._ffiarrays[name] = _rawffi.Array(field.ctype._ffishape)
Ejemplo n.º 27
0
 def test_union(self):
     import _rawffi
     longsize = _rawffi.sizeof('l')
     S = _rawffi.Structure([('x', 'h'), ('y', 'l')], union=True)
     s = S(autofree=False)
     s.x = 12345
     lib = _rawffi.CDLL(self.lib_name)
     f = lib.ptr('ret_un_func', [(S, 1)], (S, 1))
     ret = f(s)
     assert ret.y == 1234500, "ret.y == %d" % (ret.y, )
     s.free()
Ejemplo n.º 28
0
 def test_shape(self):
     import _rawffi
     A = _rawffi.Array('i')
     a = A(1)
     assert a.shape is A
     a.free()
     S = _rawffi.Structure([('v1', 'i')])
     s = S()
     s.v1 = 3
     assert s.shape is S
     s.free()
Ejemplo n.º 29
0
    def test_structreturn(self):
        import _rawffi
        X = _rawffi.Structure([('x', 'l')])
        x = X()
        x.x = 121
        Tm = _rawffi.Structure([('tm_sec', 'i'), ('tm_min', 'i'),
                                ('tm_hour', 'i'), ("tm_mday", 'i'),
                                ("tm_mon", 'i'), ("tm_year", 'i'),
                                ("tm_wday", 'i'), ("tm_yday", 'i'),
                                ("tm_isdst", 'i')])
        libc = _rawffi.get_libc()
        gmtime = libc.ptr('gmtime', ['P'], 'P')

        arg = x.byptr()
        res = gmtime(arg)
        t = Tm.fromaddress(res[0])
        arg.free()
        assert t.tm_year == 70
        assert t.tm_sec == 1
        assert t.tm_min == 2
        x.free()
Ejemplo n.º 30
0
 def test_setattr_struct(self):
     import _rawffi
     X = _rawffi.Structure([('value1', 'i'), ('value2', 'i')])
     x = X()
     x.value1 = 1
     x.value2 = 2
     assert x.value1 == 1
     assert x.value2 == 2
     x.value1 = 3
     assert x.value1 == 3
     raises(AttributeError, "x.foo")
     raises(AttributeError, "x.foo = 1")
     x.free()