예제 #1
0
    def test_functionptr_simple(self):
        ffi = FFI(backend=self.Backend())
        py.test.raises(TypeError, ffi.callback, "int(*)(int)")
        py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)

        def cb(n):
            return n + 1

        p = ffi.callback("int(*)(int)", cb)
        res = p(41)  # calling an 'int(*)(int)', i.e. a function pointer
        assert res == 42 and type(res) is int
        res = p(ffi.cast("int", -41))
        assert res == -40 and type(res) is int
        assert repr(p).startswith(
            "<cdata 'int(*)(int)' calling <function cb at 0x")
        assert ffi.typeof(p) is ffi.typeof("int(*)(int)")
        q = ffi.new("int(*)(int)", p)
        assert repr(
            q) == "<cdata 'int(* *)(int)' owning %d bytes>" % (SIZE_OF_PTR)
        py.test.raises(TypeError, "q(43)")
        res = q[0](43)
        assert res == 44
        q = ffi.cast("int(*)(int)", p)
        assert repr(q) == "<cdata 'int(*)(int)'>"
        res = q(45)
        assert res == 46
예제 #2
0
파일: test_function.py 프로젝트: AN2016/ffi
 def test_function_has_a_c_type(self):
     ffi = FFI(backend=self.Backend())
     ffi.cdef("""
         int puts(const char *);
     """)
     fptr = ffi.C.puts
     assert ffi.typeof(fptr) == ffi.typeof("int(*)(const char*)")
     if self.Backend is CTypesBackend:
         assert repr(fptr) == "<cdata 'int puts(char *)'>"
예제 #3
0
파일: test_function.py 프로젝트: AN2016/ffi
 def test_function_has_a_c_type(self):
     ffi = FFI(backend=self.Backend())
     ffi.cdef("""
         int puts(const char *);
     """)
     fptr = ffi.C.puts
     assert ffi.typeof(fptr) == ffi.typeof("int(*)(const char*)")
     if self.Backend is CTypesBackend:
         assert repr(fptr) == "<cdata 'int puts(char *)'>"
예제 #4
0
파일: test_parsing.py 프로젝트: AN2016/ffi
def test_typedef_more_complex():
    ffi = FFI(backend=FakeBackend())
    ffi.cdef("""
        typedef struct { int a, b; } foo_t, *foo_p;
        int foo(foo_p[]);
        """)
    assert str(ffi.typeof("foo_t")) == '<int>a, <int>b'
    assert ffi.typeof("foo_p") == '<pointer to <int>a, <int>b>'
    assert ffi.C.foo.BType == ('<func (<pointer to <pointer to '
                               '<int>a, <int>b>>), <int>, False>')
예제 #5
0
def test_typedef_more_complex():
    ffi = FFI(backend=FakeBackend())
    ffi.cdef("""
        typedef struct { int a, b; } foo_t, *foo_p;
        int foo(foo_p[]);
        """)
    assert str(ffi.typeof("foo_t")) == '<int>a, <int>b'
    assert ffi.typeof("foo_p") == '<pointer to <int>a, <int>b>'
    assert ffi.C.foo.BType == ('<func (<pointer to <pointer to '
                               '<int>a, <int>b>>), <int>, False>')
예제 #6
0
파일: backend_tests.py 프로젝트: AN2016/ffi
 def test_char_cast(self):
     ffi = FFI(backend=self.Backend())
     p = ffi.cast("int", '\x01')
     assert ffi.typeof(p) is ffi.typeof("int")
     assert int(p) == 1
     p = ffi.cast("int", ffi.cast("char", "a"))
     assert int(p) == ord("a")
     p = ffi.cast("int", ffi.cast("char", "\x80"))
     assert int(p) == 0x80     # "char" is considered unsigned in this case
     p = ffi.cast("int", "\x81")
     assert int(p) == 0x81
예제 #7
0
 def test_char_cast(self):
     ffi = FFI(backend=self.Backend())
     p = ffi.cast("int", '\x01')
     assert ffi.typeof(p) is ffi.typeof("int")
     assert int(p) == 1
     p = ffi.cast("int", ffi.cast("char", "a"))
     assert int(p) == ord("a")
     p = ffi.cast("int", ffi.cast("char", "\x80"))
     assert int(p) == 0x80  # "char" is considered unsigned in this case
     p = ffi.cast("int", "\x81")
     assert int(p) == 0x81
예제 #8
0
파일: test_parsing.py 프로젝트: AN2016/ffi
def test_typedef():
    ffi = FFI(backend=FakeBackend())
    ffi.cdef("""
        typedef unsigned int UInt;
        typedef UInt UIntReally;
        UInt foo(void);
        """)
    assert ffi.typeof("UIntReally") == '<unsigned int>'
    assert ffi.C.foo.BType == '<func (), <unsigned int>, False>'
예제 #9
0
def test_typedef():
    ffi = FFI(backend=FakeBackend())
    ffi.cdef("""
        typedef unsigned int UInt;
        typedef UInt UIntReally;
        UInt foo(void);
        """)
    assert ffi.typeof("UIntReally") == '<unsigned int>'
    assert ffi.C.foo.BType == '<func (), <unsigned int>, False>'
예제 #10
0
파일: backend_tests.py 프로젝트: AN2016/ffi
 def test_functionptr_simple(self):
     ffi = FFI(backend=self.Backend())
     py.test.raises(TypeError, ffi.callback, "int(*)(int)")
     py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)
     def cb(n):
         return n + 1
     p = ffi.callback("int(*)(int)", cb)
     res = p(41)     # calling an 'int(*)(int)', i.e. a function pointer
     assert res == 42 and type(res) is int
     res = p(ffi.cast("int", -41))
     assert res == -40 and type(res) is int
     assert repr(p).startswith(
         "<cdata 'int(*)(int)' calling <function cb at 0x")
     assert ffi.typeof(p) is ffi.typeof("int(*)(int)")
     q = ffi.new("int(*)(int)", p)
     assert repr(q) == "<cdata 'int(* *)(int)' owning %d bytes>" % (
         SIZE_OF_PTR)
     py.test.raises(TypeError, "q(43)")
     res = q[0](43)
     assert res == 44
     q = ffi.cast("int(*)(int)", p)
     assert repr(q) == "<cdata 'int(*)(int)'>"
     res = q(45)
     assert res == 46
예제 #11
0
파일: test_cdata.py 프로젝트: AN2016/ffi
def test_typeof():
    ffi = FFI(backend=FakeBackend())
    clong = ffi.typeof("signed long int")
    assert isinstance(clong, FakePrimitiveType)
    assert clong.cdecl == 'long'
예제 #12
0
파일: backend_tests.py 프로젝트: AN2016/ffi
 def test_functionptr_advanced(self):
     ffi = FFI(backend=self.Backend())
     t = ffi.typeof("int(*(*)(int))(int)")
     assert repr(t) == self.TypeRepr % "int(*(*)(int))(int)"
예제 #13
0
파일: backend_tests.py 프로젝트: AN2016/ffi
 def test_repr(self):
     typerepr = self.TypeRepr
     ffi = FFI(backend=self.Backend())
     ffi.cdef("struct foo { short a, b, c; };")
     p = ffi.cast("unsigned short int", 0)
     assert repr(p) == "<cdata 'unsigned short'>"
     assert repr(ffi.typeof(p)) == typerepr % "unsigned short"
     p = ffi.cast("int*", 0)
     assert repr(p) == "<cdata 'int *'>"
     assert repr(ffi.typeof(p)) == typerepr % "int *"
     #
     p = ffi.new("int")
     assert repr(p) == "<cdata 'int *' owning %d bytes>" % SIZE_OF_INT
     assert repr(ffi.typeof(p)) == typerepr % "int *"
     p = ffi.new("int*")
     assert repr(p) == "<cdata 'int * *' owning %d bytes>" % SIZE_OF_PTR
     assert repr(ffi.typeof(p)) == typerepr % "int * *"
     p = ffi.new("int [2]")
     assert repr(p) == "<cdata 'int[2]' owning %d bytes>" % (2*SIZE_OF_INT)
     assert repr(ffi.typeof(p)) == typerepr % "int[2]"
     p = ffi.new("int*[2][3]")
     assert repr(p) == "<cdata 'int *[2][3]' owning %d bytes>" % (
         6*SIZE_OF_PTR)
     assert repr(ffi.typeof(p)) == typerepr % "int *[2][3]"
     p = ffi.new("struct foo")
     assert repr(p) == "<cdata 'struct foo *' owning %d bytes>" % (
         3*SIZE_OF_SHORT)
     assert repr(ffi.typeof(p)) == typerepr % "struct foo *"
     #
     q = ffi.cast("short", -123)
     assert repr(q) == "<cdata 'short'>"
     assert repr(ffi.typeof(q)) == typerepr % "short"
     p = ffi.new("int")
     q = ffi.cast("short*", p)
     assert repr(q) == "<cdata 'short *'>"
     assert repr(ffi.typeof(q)) == typerepr % "short *"
     p = ffi.new("int [2]")
     q = ffi.cast("int*", p)
     assert repr(q) == "<cdata 'int *'>"
     assert repr(ffi.typeof(q)) == typerepr % "int *"
     p = ffi.new("struct foo")
     q = ffi.cast("struct foo *", p)
     assert repr(q) == "<cdata 'struct foo *'>"
     assert repr(ffi.typeof(q)) == typerepr % "struct foo *"
     q = q[0]
     assert repr(q) == "<cdata 'struct foo'>"
     assert repr(ffi.typeof(q)) == typerepr % "struct foo"
예제 #14
0
 def test_functionptr_advanced(self):
     ffi = FFI(backend=self.Backend())
     t = ffi.typeof("int(*(*)(int))(int)")
     assert repr(t) == self.TypeRepr % "int(*(*)(int))(int)"
예제 #15
0
 def test_repr(self):
     typerepr = self.TypeRepr
     ffi = FFI(backend=self.Backend())
     ffi.cdef("struct foo { short a, b, c; };")
     p = ffi.cast("unsigned short int", 0)
     assert repr(p) == "<cdata 'unsigned short'>"
     assert repr(ffi.typeof(p)) == typerepr % "unsigned short"
     p = ffi.cast("int*", 0)
     assert repr(p) == "<cdata 'int *'>"
     assert repr(ffi.typeof(p)) == typerepr % "int *"
     #
     p = ffi.new("int")
     assert repr(p) == "<cdata 'int *' owning %d bytes>" % SIZE_OF_INT
     assert repr(ffi.typeof(p)) == typerepr % "int *"
     p = ffi.new("int*")
     assert repr(p) == "<cdata 'int * *' owning %d bytes>" % SIZE_OF_PTR
     assert repr(ffi.typeof(p)) == typerepr % "int * *"
     p = ffi.new("int [2]")
     assert repr(p) == "<cdata 'int[2]' owning %d bytes>" % (2 *
                                                             SIZE_OF_INT)
     assert repr(ffi.typeof(p)) == typerepr % "int[2]"
     p = ffi.new("int*[2][3]")
     assert repr(
         p) == "<cdata 'int *[2][3]' owning %d bytes>" % (6 * SIZE_OF_PTR)
     assert repr(ffi.typeof(p)) == typerepr % "int *[2][3]"
     p = ffi.new("struct foo")
     assert repr(p) == "<cdata 'struct foo *' owning %d bytes>" % (
         3 * SIZE_OF_SHORT)
     assert repr(ffi.typeof(p)) == typerepr % "struct foo *"
     #
     q = ffi.cast("short", -123)
     assert repr(q) == "<cdata 'short'>"
     assert repr(ffi.typeof(q)) == typerepr % "short"
     p = ffi.new("int")
     q = ffi.cast("short*", p)
     assert repr(q) == "<cdata 'short *'>"
     assert repr(ffi.typeof(q)) == typerepr % "short *"
     p = ffi.new("int [2]")
     q = ffi.cast("int*", p)
     assert repr(q) == "<cdata 'int *'>"
     assert repr(ffi.typeof(q)) == typerepr % "int *"
     p = ffi.new("struct foo")
     q = ffi.cast("struct foo *", p)
     assert repr(q) == "<cdata 'struct foo *'>"
     assert repr(ffi.typeof(q)) == typerepr % "struct foo *"
     q = q[0]
     assert repr(q) == "<cdata 'struct foo'>"
     assert repr(ffi.typeof(q)) == typerepr % "struct foo"
예제 #16
0
파일: test_cdata.py 프로젝트: AN2016/ffi
def test_typeof():
    ffi = FFI(backend=FakeBackend())
    clong = ffi.typeof("signed long int")
    assert isinstance(clong, FakePrimitiveType)
    assert clong.cdecl == 'long'